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