]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8d99147c62e9a52555e8834a2382c04940c0a01a
[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.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 {
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
88     @Override
89     public String getServiceName() { 
90         throw new UnsupportedOperationException(); //FIXME: REM - http://issues.collectionspace.org/browse/CSPACE-3498   
91     }
92     
93     /* (non-Javadoc)
94      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
95      */
96     @Override
97     protected CollectionSpaceClient getClientInstance() {
98         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
99     }
100
101     /* (non-Javadoc)
102      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
103      */
104     @Override
105     protected AbstractCommonList getAbstractCommonList(
106             ClientResponse<AbstractCommonList> 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", dataProviderClass = AbstractServiceTestImpl.class)
115     public void createIntakeWithAuthRefs(String testName) throws Exception {
116
117         if (logger.isDebugEnabled()) {
118             logger.debug(testBanner(testName, CLASS_NAME));
119         }
120         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
121
122         // Submit the request to the service and store the response.
123         String identifier = createIdentifier();
124
125         // Create all the person refs and entities
126         createPersonRefs();
127
128         IntakeClient intakeClient = new IntakeClient();
129         PoxPayloadOut multipart = createIntakeInstance(
130                 "entryNumber-" + identifier,
131                 "entryDate-" + identifier,
132                 currentOwnerRefName,
133                 depositorRefName,
134                 conditionCheckerAssessorRefName,
135                 insurerRefName,
136                 valuerRefName);
137
138         ClientResponse<Response> res = intakeClient.create(multipart);
139         try {
140             int statusCode = res.getStatus();
141
142             // Check the status code of the response: does it match
143             // the expected response(s)?
144             //
145             // Specifically:
146             // Does it fall within the set of valid status codes?
147             // Does it exactly match the expected status code?
148             if (logger.isDebugEnabled()) {
149                 logger.debug(testName + ": status = " + statusCode);
150             }
151             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
154         } finally {
155             res.releaseConnection();
156         }
157
158         // Store the ID returned from the first resource created
159         // for additional tests below.
160         if (knownIntakeId == null) {
161             knownIntakeId = extractId(res);
162             if (logger.isDebugEnabled()) {
163                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
164             }
165         }
166
167         // Store the IDs from every resource created by tests,
168         // so they can be deleted after tests have been run.
169         intakeIdsCreated.add(extractId(res));
170     }
171
172     /**
173      * Creates the person refs.
174      */
175     protected void createPersonRefs() {
176         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
177         PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
178                 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
179         ClientResponse<Response> res = personAuthClient.create(multipart);
180         int statusCode = res.getStatus();
181
182         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
183                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
184         Assert.assertEquals(statusCode, STATUS_CREATED);
185         personAuthCSID = extractId(res);
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
227     protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
228         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
229         Map<String, String> personInfo = new HashMap<String, String>();
230         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
231         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
232         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
233         PoxPayloadOut multipart =
234                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
235                 authRefName, personInfo, personAuthClient.getItemCommonPartName());
236         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
237         int statusCode = res.getStatus();
238
239         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
240                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
241         Assert.assertEquals(statusCode, STATUS_CREATED);
242         return extractId(res);
243     }
244
245     // Success outcomes
246     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
247     dependsOnMethods = {"createIntakeWithAuthRefs"})
248     public void readAndCheckAuthRefDocs(String testName) throws Exception {
249
250         if (logger.isDebugEnabled()) {
251             logger.debug(testBanner(testName, CLASS_NAME));
252         }
253         // Perform setup.
254         testSetup(STATUS_OK, ServiceRequestType.READ);
255
256         // Get the auth ref docs and check them
257
258         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
259         ClientResponse<AuthorityRefDocList> refDocListResp =
260                 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
261
262         int statusCode = refDocListResp.getStatus();
263
264         if (logger.isDebugEnabled()) {
265             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
266         }
267         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
268                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
269         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
270
271         AuthorityRefDocList list = refDocListResp.getEntity();
272         List<AuthorityRefDocList.AuthorityRefDocItem> items =
273                 list.getAuthorityRefDocItem();
274         Assert.assertTrue(items != null);
275         Assert.assertTrue(items.size() > 0);
276
277         // Optionally output additional data about list members for debugging.
278         boolean iterateThroughList = true;
279         boolean fFoundIntake = false;
280         if (iterateThroughList && logger.isDebugEnabled()) {
281             int i = 0;
282             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
283             for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
284                 logger.debug(testName + ": list-item[" + i + "] "
285                         + item.getDocType() + "("
286                         + item.getDocId() + ") Name:["
287                         + item.getDocName() + "] Number:["
288                         + item.getDocNumber() + "] in field:["
289                         + item.getSourceField() + "]");
290                 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
291                     fFoundIntake = true;
292                 }
293                 i++;
294             }
295             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
296         }
297
298         personAuthClient = new PersonAuthorityClient();
299         refDocListResp =
300                 personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
301
302         statusCode = refDocListResp.getStatus();
303
304         if (logger.isDebugEnabled()) {
305             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
306         }
307         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
308                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
309         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
310
311         list = refDocListResp.getEntity();
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", dataProviderClass = AbstractServiceTestImpl.class,
344     dependsOnMethods = {"createIntakeWithAuthRefs"}, groups = {"repeatableScalar"})
345     public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
346
347         if (logger.isDebugEnabled()) {
348             logger.debug(testBanner(testName, CLASS_NAME));
349         }
350         // Perform setup.
351         testSetup(STATUS_OK, ServiceRequestType.READ);
352
353         // Get the auth ref docs and check them
354
355         // Single scalar field
356         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
357         ClientResponse<AuthorityRefDocList> refDocListResp =
358                 personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
359
360         int statusCode = refDocListResp.getStatus();
361
362         if (logger.isDebugEnabled()) {
363             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
364         }
365         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
368
369         AuthorityRefDocList list = refDocListResp.getEntity();
370         List<AuthorityRefDocList.AuthorityRefDocItem> items =
371                 list.getAuthorityRefDocItem();
372         Assert.assertTrue(items != null);
373         Assert.assertTrue(items.size() > 0);
374         Assert.assertTrue(items.get(0) != null);
375
376         // Optionally output additional data about list members for debugging.
377         boolean iterateThroughList = true;
378         boolean fFoundIntake = false;
379         if (iterateThroughList && logger.isDebugEnabled()) {
380             int i = 0;
381             logger.debug(testName + ": Docs that use: " + insurerRefName);
382             for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
383                 logger.debug(testName + ": list-item[" + i + "] "
384                         + item.getDocType() + "("
385                         + item.getDocId() + ") Name:["
386                         + item.getDocName() + "] Number:["
387                         + item.getDocNumber() + "] in field:["
388                         + item.getSourceField() + "]");
389                 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
390                     fFoundIntake = true;
391                 }
392                 i++;
393             }
394             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
395         }
396     }
397
398
399     // ---------------------------------------------------------------
400     // Cleanup of resources created during testing
401     // ---------------------------------------------------------------
402     /**
403      * Deletes all resources created by tests, after all tests have been run.
404      *
405      * This cleanup method will always be run, even if one or more tests fail.
406      * For this reason, it attempts to remove all resources created
407      * at any point during testing, even if some of those resources
408      * may be expected to be deleted by certain tests.
409      */
410     @AfterClass(alwaysRun = true)
411     public void cleanUp() {
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             ClientResponse<Response> res = intakeClient.delete(resourceId);
426             res.releaseConnection();
427         }
428         // Delete persons before PersonAuth
429         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
430         for (String resourceId : personIdsCreated) {
431             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
432             res.releaseConnection();
433         }
434         if (personAuthCSID != null) {
435             personAuthClient.delete(personAuthCSID).releaseConnection();
436         }
437     }
438
439     // ---------------------------------------------------------------
440     // Utility methods used by tests above
441     // ---------------------------------------------------------------
442     @Override
443     public String getServicePathComponent() {
444         return SERVICE_PATH_COMPONENT;
445     }
446
447     private PoxPayloadOut createIntakeInstance(String entryNumber,
448             String entryDate,
449             String currentOwner,
450             String depositor,
451             String conditionCheckerAssessor,
452             String insurer,
453             String Valuer) {
454         IntakesCommon intake = new IntakesCommon();
455         intake.setEntryNumber(entryNumber);
456         intake.setEntryDate(entryDate);
457         intake.setCurrentOwner(currentOwner);
458         intake.setDepositor(depositor);
459         intake.setValuer(Valuer);
460
461         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
462         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
463         checkersOrAssessors.add(conditionCheckerAssessor);
464         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
465
466         InsurerList insurerList = new InsurerList();
467         List<String> insurers = insurerList.getInsurer();
468         insurers.add(insurer);
469         intake.setInsurers(insurerList);
470
471         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
472         PayloadOutputPart commonPart =
473                 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
474         commonPart.setLabel(new IntakeClient().getCommonPartName());
475
476         if (logger.isDebugEnabled()) {
477             logger.debug("to be created, intake common");
478             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
479         }
480
481         return multipart;
482     }
483 }