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