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