]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5c7b636c04e5770ff1417e8755e0e5884228a669
[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.AuthorityRefDocList;
39 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
40 import org.collectionspace.services.intake.IntakesCommon;
41 import org.collectionspace.services.intake.InsurerList;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
43
44 import org.jboss.resteasy.client.ClientResponse;
45
46 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
48 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.Test;
52
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 /**
57  * PersonAuthRefDocsTest, carries out tests against a
58  * deployed and running Person Service.
59  *
60  * $LastChangedRevision: 1327 $
61  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
62  */
63 public class PersonAuthRefDocsTest extends BaseServiceTest {
64
65     private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
66     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
67     // Instance variables specific to this test.
68     final String SERVICE_PATH_COMPONENT = "intakes";
69     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
70     private String knownIntakeId = null;
71     private List<String> intakeIdsCreated = new ArrayList<String>();
72     private List<String> personIdsCreated = new ArrayList<String>();
73     private String personAuthCSID = null;
74     private String personShortId = PERSON_AUTHORITY_NAME;
75     private String currentOwnerPersonCSID = null;
76     private String depositorPersonCSID = null;
77     private String insurerPersonCSID = null;
78     private String currentOwnerRefName = null;
79     private String depositorRefName = null;
80     private String conditionCheckerAssessorRefName = null;
81     private String insurerRefName = null;
82     private String valuerRefName = null;
83     private String valuerShortId = null;
84     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
85
86     /* (non-Javadoc)
87      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
88      */
89     @Override
90     protected CollectionSpaceClient getClientInstance() {
91         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
92     }
93
94     /* (non-Javadoc)
95      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
96      */
97     @Override
98     protected AbstractCommonList getAbstractCommonList(
99             ClientResponse<AbstractCommonList> response) {
100         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
101     }
102
103     // ---------------------------------------------------------------
104     // CRUD tests : CREATE tests
105     // ---------------------------------------------------------------
106     // Success outcomes
107     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
108     public void createIntakeWithAuthRefs(String testName) throws Exception {
109
110         if (logger.isDebugEnabled()) {
111             logger.debug(testBanner(testName, CLASS_NAME));
112         }
113         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
114
115         // Submit the request to the service and store the response.
116         String identifier = createIdentifier();
117
118         // Create all the person refs and entities
119         createPersonRefs();
120
121         IntakeClient intakeClient = new IntakeClient();
122         MultipartOutput multipart = createIntakeInstance(
123                 "entryNumber-" + identifier,
124                 "entryDate-" + identifier,
125                 currentOwnerRefName,
126                 depositorRefName,
127                 conditionCheckerAssessorRefName,
128                 insurerRefName,
129                 valuerRefName);
130
131         ClientResponse<Response> res = intakeClient.create(multipart);
132         try {
133             int statusCode = res.getStatus();
134
135             // Check the status code of the response: does it match
136             // the expected response(s)?
137             //
138             // Specifically:
139             // Does it fall within the set of valid status codes?
140             // Does it exactly match the expected status code?
141             if (logger.isDebugEnabled()) {
142                 logger.debug(testName + ": status = " + statusCode);
143             }
144             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
145                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
146             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
147         } finally {
148             res.releaseConnection();
149         }
150
151         // Store the ID returned from the first resource created
152         // for additional tests below.
153         if (knownIntakeId == null) {
154             knownIntakeId = extractId(res);
155             if (logger.isDebugEnabled()) {
156                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
157             }
158         }
159
160         // Store the IDs from every resource created by tests,
161         // so they can be deleted after tests have been run.
162         intakeIdsCreated.add(extractId(res));
163     }
164
165     /**
166      * Creates the person refs.
167      */
168     protected void createPersonRefs() {
169         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
170         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
171                 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
172         ClientResponse<Response> res = personAuthClient.create(multipart);
173         int statusCode = res.getStatus();
174
175         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177         Assert.assertEquals(statusCode, STATUS_CREATED);
178         personAuthCSID = extractId(res);
179
180         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
181
182         String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
183         Assert.assertNotNull(csid);
184         currentOwnerPersonCSID = csid;
185         currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
186         Assert.assertNotNull(currentOwnerRefName);
187         personIdsCreated.add(csid);
188
189         csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
190         Assert.assertNotNull(csid);
191         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
192         depositorPersonCSID = csid;
193         Assert.assertNotNull(depositorRefName);
194         personIdsCreated.add(csid);
195
196         csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
197         Assert.assertNotNull(csid);
198         conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
199         Assert.assertNotNull(conditionCheckerAssessorRefName);
200         personIdsCreated.add(csid);
201
202         csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
203         Assert.assertNotNull(csid);
204         insurerPersonCSID = csid;
205         insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
206         Assert.assertNotNull(insurerRefName);
207         personIdsCreated.add(csid);
208
209         csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
210         Assert.assertNotNull(csid);
211         valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
212         if (logger.isDebugEnabled()) {
213             logger.debug("valuerShortId=" + valuerShortId);
214         }
215         Assert.assertNotNull(valuerRefName);
216         personIdsCreated.add(csid);
217
218     }
219
220     protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
221         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
222         Map<String, String> personInfo = new HashMap<String, String>();
223         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
224         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
225         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
226         MultipartOutput multipart =
227                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
228                 authRefName, personInfo, personAuthClient.getItemCommonPartName());
229         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
230         int statusCode = res.getStatus();
231
232         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234         Assert.assertEquals(statusCode, STATUS_CREATED);
235         return extractId(res);
236     }
237
238     // Success outcomes
239     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
240     dependsOnMethods = {"createIntakeWithAuthRefs"})
241     public void readAndCheckAuthRefDocs(String testName) throws Exception {
242
243         if (logger.isDebugEnabled()) {
244             logger.debug(testBanner(testName, CLASS_NAME));
245         }
246         // Perform setup.
247         testSetup(STATUS_OK, ServiceRequestType.READ);
248
249         // Get the auth ref docs and check them
250
251         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
252         ClientResponse<AuthorityRefDocList> refDocListResp =
253                 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
254
255         int statusCode = refDocListResp.getStatus();
256
257         if (logger.isDebugEnabled()) {
258             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
259         }
260         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
261                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
262         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
263
264         AuthorityRefDocList list = refDocListResp.getEntity();
265         List<AuthorityRefDocList.AuthorityRefDocItem> items =
266                 list.getAuthorityRefDocItem();
267         Assert.assertTrue(items != null);
268         Assert.assertTrue(items.size() > 0);
269
270         // Optionally output additional data about list members for debugging.
271         boolean iterateThroughList = true;
272         boolean fFoundIntake = false;
273         if (iterateThroughList && logger.isDebugEnabled()) {
274             int i = 0;
275             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
276             for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
277                 logger.debug(testName + ": list-item[" + i + "] "
278                         + item.getDocType() + "("
279                         + item.getDocId() + ") Name:["
280                         + item.getDocName() + "] Number:["
281                         + item.getDocNumber() + "] in field:["
282                         + item.getSourceField() + "]");
283                 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
284                     fFoundIntake = true;
285                 }
286                 i++;
287             }
288             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
289         }
290
291         personAuthClient = new PersonAuthorityClient();
292         refDocListResp =
293                 personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
294
295         statusCode = refDocListResp.getStatus();
296
297         if (logger.isDebugEnabled()) {
298             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
299         }
300         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
301                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
302         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
303
304         list = refDocListResp.getEntity();
305         items = list.getAuthorityRefDocItem();
306         Assert.assertTrue(items != null);
307         Assert.assertTrue(items.size() > 0);
308         Assert.assertTrue(items.get(0) != null);
309         
310         // Optionally output additional data about list members for debugging.
311         iterateThroughList = true;
312         fFoundIntake = false;
313         if (iterateThroughList && logger.isDebugEnabled()) {
314             int i = 0;
315             logger.debug(testName + ": Docs that use: " + depositorRefName);
316             for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
317                 logger.debug(testName + ": list-item[" + i + "] "
318                         + item.getDocType() + "("
319                         + item.getDocId() + ") Name:["
320                         + item.getDocName() + "] Number:["
321                         + item.getDocNumber() + "] in field:["
322                         + item.getSourceField() + "]");
323                 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
324                     fFoundIntake = true;
325                 }
326                 i++;
327             }
328             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
329         }
330     }
331
332     /*
333      * Read and check the list of referencing objects, where the authRef field
334      * is a value instance of a repeatable scalar field.
335      */
336     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
337     dependsOnMethods = {"createIntakeWithAuthRefs"}, groups = {"repeatableScalar"})
338     public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
339
340         if (logger.isDebugEnabled()) {
341             logger.debug(testBanner(testName, CLASS_NAME));
342         }
343         // Perform setup.
344         testSetup(STATUS_OK, ServiceRequestType.READ);
345
346         // Get the auth ref docs and check them
347
348         // Single scalar field
349         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
350         ClientResponse<AuthorityRefDocList> refDocListResp =
351                 personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
352
353         int statusCode = refDocListResp.getStatus();
354
355         if (logger.isDebugEnabled()) {
356             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
357         }
358         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
359                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
360         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
361
362         AuthorityRefDocList list = refDocListResp.getEntity();
363         List<AuthorityRefDocList.AuthorityRefDocItem> items =
364                 list.getAuthorityRefDocItem();
365         Assert.assertTrue(items != null);
366         Assert.assertTrue(items.size() > 0);
367         Assert.assertTrue(items.get(0) != null);
368
369         // Optionally output additional data about list members for debugging.
370         boolean iterateThroughList = true;
371         boolean fFoundIntake = false;
372         if (iterateThroughList && logger.isDebugEnabled()) {
373             int i = 0;
374             logger.debug(testName + ": Docs that use: " + insurerRefName);
375             for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
376                 logger.debug(testName + ": list-item[" + i + "] "
377                         + item.getDocType() + "("
378                         + item.getDocId() + ") Name:["
379                         + item.getDocName() + "] Number:["
380                         + item.getDocNumber() + "] in field:["
381                         + item.getSourceField() + "]");
382                 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
383                     fFoundIntake = true;
384                 }
385                 i++;
386             }
387             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
388         }
389     }
390
391
392     // ---------------------------------------------------------------
393     // Cleanup of resources created during testing
394     // ---------------------------------------------------------------
395     /**
396      * Deletes all resources created by tests, after all tests have been run.
397      *
398      * This cleanup method will always be run, even if one or more tests fail.
399      * For this reason, it attempts to remove all resources created
400      * at any point during testing, even if some of those resources
401      * may be expected to be deleted by certain tests.
402      */
403     @AfterClass(alwaysRun = true)
404     public void cleanUp() {
405         String noTest = System.getProperty("noTestCleanup");
406         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
407             if (logger.isDebugEnabled()) {
408                 logger.debug("Skipping Cleanup phase ...");
409             }
410             return;
411         }
412         if (logger.isDebugEnabled()) {
413             logger.debug("Cleaning up temporary resources created for testing ...");
414         }
415         IntakeClient intakeClient = new IntakeClient();
416         // Note: Any non-success responses are ignored and not reported.
417         for (String resourceId : intakeIdsCreated) {
418             ClientResponse<Response> res = intakeClient.delete(resourceId);
419             res.releaseConnection();
420         }
421         // Delete persons before PersonAuth
422         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
423         for (String resourceId : personIdsCreated) {
424             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
425             res.releaseConnection();
426         }
427         if (personAuthCSID != null) {
428             personAuthClient.delete(personAuthCSID).releaseConnection();
429         }
430     }
431
432     // ---------------------------------------------------------------
433     // Utility methods used by tests above
434     // ---------------------------------------------------------------
435     @Override
436     public String getServicePathComponent() {
437         return SERVICE_PATH_COMPONENT;
438     }
439
440     private MultipartOutput createIntakeInstance(String entryNumber,
441             String entryDate,
442             String currentOwner,
443             String depositor,
444             String conditionCheckerAssessor,
445             String insurer,
446             String Valuer) {
447         IntakesCommon intake = new IntakesCommon();
448         intake.setEntryNumber(entryNumber);
449         intake.setEntryDate(entryDate);
450         intake.setCurrentOwner(currentOwner);
451         intake.setDepositor(depositor);
452         intake.setValuer(Valuer);
453
454         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
455         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
456         checkersOrAssessors.add(conditionCheckerAssessor);
457         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
458
459         InsurerList insurerList = new InsurerList();
460         List<String> insurers = insurerList.getInsurer();
461         insurers.add(insurer);
462         intake.setInsurers(insurerList);
463
464         MultipartOutput multipart = new MultipartOutput();
465         OutputPart commonPart =
466                 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
467         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
468
469         if (logger.isDebugEnabled()) {
470             logger.debug("to be created, intake common");
471             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
472         }
473
474         return multipart;
475     }
476 }