]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2137d338dd8668a07cff5b347c4f399cf2e48998
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32
33 import org.collectionspace.services.PersonJAXBSchema;
34 import org.collectionspace.services.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.IntakeClient;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList;
39 import org.collectionspace.services.intake.IntakesCommon;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41
42 import org.jboss.resteasy.client.ClientResponse;
43
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
46 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
50
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * IntakeAuthRefsTest, carries out tests against a
56  * deployed and running Intake Service.
57  *
58  * $LastChangedRevision: 1327 $
59  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60  */
61 public class IntakeAuthRefsTest extends BaseServiceTest {
62
63     private final String CLASS_NAME = IntakeAuthRefsTest.class.getName();
64     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
65
66     // Instance variables specific to this test.
67     final String SERVICE_PATH_COMPONENT = "intakes";
68     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
69     private String knownResourceId = null;
70     private List<String> intakeIdsCreated = new ArrayList<String>();
71     private List<String> personIdsCreated = new ArrayList<String>();
72     private String personAuthCSID = null; 
73     private String currentOwnerRefName = null;
74     private String depositorRefName = null;
75     private String conditionCheckerAssessorRefName = null;
76     private String insurerRefName = null;
77     private String valuerRefName = null;
78     private final int NUM_AUTH_REFS_EXPECTED = 5;
79
80     /* (non-Javadoc)
81      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
82      */
83     @Override
84     protected CollectionSpaceClient getClientInstance() {
85         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
86     }
87     
88     /* (non-Javadoc)
89      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
90      */
91     @Override
92         protected AbstractCommonList getAbstractCommonList(
93                         ClientResponse<AbstractCommonList> response) {
94         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
95     }
96
97     // ---------------------------------------------------------------
98     // CRUD tests : CREATE tests
99     // ---------------------------------------------------------------
100     // Success outcomes
101     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
102     public void createWithAuthRefs(String testName) throws Exception {
103
104         if (logger.isDebugEnabled()) {
105             logger.debug(testBanner(testName, CLASS_NAME));
106         }
107         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
108
109         // Submit the request to the service and store the response.
110         String identifier = createIdentifier();
111         
112         // Create all the person refs and entities
113         createPersonRefs();
114         
115         // Submit the request to the service and store the response.
116         IntakeClient intakeClient = new IntakeClient();
117         MultipartOutput multipart = createIntakeInstance(
118                 "entryNumber-" + identifier,
119                 "entryDate-" + identifier,
120                 currentOwnerRefName,
121                 depositorRefName,
122                 conditionCheckerAssessorRefName,
123                 insurerRefName,
124                 valuerRefName );
125         ClientResponse<Response> res = intakeClient.create(multipart);
126
127         int statusCode = res.getStatus();
128
129         // Check the status code of the response: does it match
130         // the expected response(s)?
131         //
132         // Specifically:
133         // Does it fall within the set of valid status codes?
134         // Does it exactly match the expected status code?
135         if(logger.isDebugEnabled()){
136             logger.debug(testName + ": status = " + statusCode);
137         }
138         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
139                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
140         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
141
142         // Store the ID returned from the first resource created
143         // for additional tests below.
144         if (knownResourceId == null){
145             knownResourceId = extractId(res);
146             if (logger.isDebugEnabled()) {
147                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
148             }
149         }
150         
151         // Store the IDs from every resource created by tests,
152         // so they can be deleted after tests have been run.
153         intakeIdsCreated.add(extractId(res));
154     }
155     
156     protected void createPersonRefs(){
157         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
158         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
159                         PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
160         ClientResponse<Response> res = personAuthClient.create(multipart);
161         int statusCode = res.getStatus();
162
163         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
164                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
165         Assert.assertEquals(statusCode, STATUS_CREATED);
166         personAuthCSID = extractId(res);
167         
168         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
169         
170         String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
171         currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
172         personIdsCreated.add(csid);
173         
174         csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
175         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
176         personIdsCreated.add(csid);
177         
178         csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
179         conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
180         personIdsCreated.add(csid);
181         
182         csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
183         insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
184         personIdsCreated.add(csid);
185         
186         csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
187         valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
188         personIdsCreated.add(csid);
189     }
190     
191     protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
192         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
193         Map<String, String> personInfo = new HashMap<String,String>();
194         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
195         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
196         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
197         MultipartOutput multipart = 
198                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
199                                 authRefName, personInfo, personAuthClient.getItemCommonPartName());
200         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
201         int statusCode = res.getStatus();
202
203         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
204                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
205         Assert.assertEquals(statusCode, STATUS_CREATED);
206         return extractId(res);
207     }
208
209     // Success outcomes
210     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
211         dependsOnMethods = {"createWithAuthRefs"})
212     public void readAndCheckAuthRefs(String testName) throws Exception {
213
214         if (logger.isDebugEnabled()) {
215             logger.debug(testBanner(testName, CLASS_NAME));
216         }
217         // Perform setup.
218         testSetup(STATUS_OK, ServiceRequestType.READ);
219
220         // Submit the request to the service and store the response.
221         IntakeClient intakeClient = new IntakeClient();
222         ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
223         int statusCode = res.getStatus();
224
225         // Check the status code of the response: does it match
226         // the expected response(s)?
227         if(logger.isDebugEnabled()){
228             logger.debug(testName + ".read: status = " + statusCode);
229         }
230         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
231                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
232         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
233
234         MultipartInput input = (MultipartInput) res.getEntity();
235         IntakesCommon intake = (IntakesCommon) extractPart(input,
236                         intakeClient.getCommonPartName(), IntakesCommon.class);
237         Assert.assertNotNull(intake);
238         // Check a couple of fields
239         Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
240         Assert.assertEquals(intake.getInsurer(), insurerRefName);
241         
242         // Get the auth refs and check them
243         ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
244         statusCode = res2.getStatus();
245
246         if(logger.isDebugEnabled()){
247             logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
248         }
249         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
250                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
251         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
252         AuthorityRefList list = res2.getEntity();
253         
254         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
255         int numAuthRefsFound = items.size();
256         if(logger.isDebugEnabled()){
257             logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
258                 " authority references, found " + numAuthRefsFound);
259         }
260         Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
261             "Did not find all expected authority references! " +
262             "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
263
264         // Optionally output additional data about list members for debugging.
265         boolean iterateThroughList = true;
266         if(iterateThroughList && logger.isDebugEnabled()){
267             int i = 0;
268             for(AuthorityRefList.AuthorityRefItem item : items){
269                 logger.debug(testName + ": list-item[" + i + "] Field:" +
270                                 item.getSourceField() + "= " +
271                         item.getAuthDisplayName() +
272                         item.getItemDisplayName());
273                 logger.debug(testName + ": list-item[" + i + "] refName=" +
274                         item.getRefName());
275                 logger.debug(testName + ": list-item[" + i + "] URI=" +
276                         item.getUri());
277                 i++;
278             }
279         }
280     }
281
282
283     // ---------------------------------------------------------------
284     // Cleanup of resources created during testing
285     // ---------------------------------------------------------------
286
287     /**
288      * Deletes all resources created by tests, after all tests have been run.
289      *
290      * This cleanup method will always be run, even if one or more tests fail.
291      * For this reason, it attempts to remove all resources created
292      * at any point during testing, even if some of those resources
293      * may be expected to be deleted by certain tests.
294      */
295     @AfterClass(alwaysRun=true)
296     public void cleanUp() {
297         String noTest = System.getProperty("noTestCleanup");
298         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
299             if (logger.isDebugEnabled()) {
300                 logger.debug("Skipping Cleanup phase ...");
301             }
302             return;
303         }
304         if (logger.isDebugEnabled()) {
305             logger.debug("Cleaning up temporary resources created for testing ...");
306         }
307         IntakeClient intakeClient = new IntakeClient();
308         // Note: Any non-success responses are ignored and not reported.
309         for (String resourceId : intakeIdsCreated) {
310             intakeClient.delete(resourceId).releaseConnection();
311         }
312         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
313         // Delete persons before PersonAuth
314         for (String resourceId : personIdsCreated) {
315             personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
316         }
317         personAuthClient.delete(personAuthCSID).releaseConnection();
318     }
319
320     // ---------------------------------------------------------------
321     // Utility methods used by tests above
322     // ---------------------------------------------------------------
323     @Override
324     public String getServicePathComponent() {
325         return SERVICE_PATH_COMPONENT;
326     }
327
328    private MultipartOutput createIntakeInstance(String entryNumber,
329                 String entryDate,
330                                 String currentOwner,
331                                 String depositor,
332                                 String conditionCheckerAssessor,
333                                 String insurer,
334                                 String Valuer ) {
335         IntakesCommon intake = new IntakesCommon();
336         intake.setEntryNumber(entryNumber);
337         intake.setEntryDate(entryDate);
338         intake.setCurrentOwner(currentOwner);
339         intake.setDepositor(depositor);
340         intake.setConditionCheckerAssessor(conditionCheckerAssessor);
341         intake.setInsurer(insurer);
342         intake.setValuer(Valuer);
343         MultipartOutput multipart = new MultipartOutput();
344         OutputPart commonPart =
345             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
346         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
347
348         if(logger.isDebugEnabled()){
349             logger.debug("to be created, intake common");
350             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
351         }
352
353         return multipart;
354     }
355 }