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