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