]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8dcaf353b556d2349d17c2c4f99cefb1b3a51382
[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
68     // Instance variables specific to this test.
69     final String SERVICE_PATH_COMPONENT = "intakes";
70     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
71     private String knownIntakeId = null;
72     private List<String> intakeIdsCreated = new ArrayList<String>();
73     private List<String> personIdsCreated = new ArrayList<String>();
74     private String personAuthCSID = null; 
75     private String currentOwnerPersonCSID = null; 
76     private String currentOwnerRefName = null;
77     private String depositorRefName = null;
78     private String conditionCheckerAssessorRefName = null;
79     private String insurerRefName = null;
80     private String valuerRefName = null;
81     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
82
83     /* (non-Javadoc)
84      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
85      */
86     @Override
87     protected CollectionSpaceClient getClientInstance() {
88         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
89     }
90     
91     /* (non-Javadoc)
92      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
93      */
94     @Override
95         protected AbstractCommonList getAbstractCommonList(
96                         ClientResponse<AbstractCommonList> response) {
97         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
98     }
99
100     // ---------------------------------------------------------------
101     // CRUD tests : CREATE tests
102     // ---------------------------------------------------------------
103     // Success outcomes
104     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
105     public void createIntakeWithAuthRefs(String testName) throws Exception {
106
107         if (logger.isDebugEnabled()) {
108             logger.debug(testBanner(testName, CLASS_NAME));
109         }
110         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
111
112         // Submit the request to the service and store the response.
113         String identifier = createIdentifier();
114         
115         // Create all the person refs and entities
116         createPersonRefs();
117
118         IntakeClient intakeClient = new IntakeClient();
119         MultipartOutput multipart = createIntakeInstance(
120                 "entryNumber-" + identifier,
121                 "entryDate-" + identifier,
122                                                                 currentOwnerRefName,
123                                                                 depositorRefName,
124                                                                 conditionCheckerAssessorRefName,
125                                                                 insurerRefName,
126                                                                 valuerRefName );
127
128         ClientResponse<Response> res = intakeClient.create(multipart);
129         try {
130                 int statusCode = res.getStatus();
131         
132                 // Check the status code of the response: does it match
133                 // the expected response(s)?
134                 //
135                 // Specifically:
136                 // Does it fall within the set of valid status codes?
137                 // Does it exactly match the expected status code?
138                 if(logger.isDebugEnabled()){
139                     logger.debug(testName + ": status = " + statusCode);
140                 }
141                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
142                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
143                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
144         } finally {
145                 res.releaseConnection();
146         }
147
148         // Store the ID returned from the first resource created
149         // for additional tests below.
150         if (knownIntakeId == null){
151             knownIntakeId = extractId(res);
152             if (logger.isDebugEnabled()) {
153                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
154             }
155         }
156         
157         // Store the IDs from every resource created by tests,
158         // so they can be deleted after tests have been run.
159         intakeIdsCreated.add(extractId(res));
160     }
161     
162     /**
163      * Creates the person refs.
164      */
165     protected void createPersonRefs(){
166         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
167         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
168                         PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
169         ClientResponse<Response> res = personAuthClient.create(multipart);
170         int statusCode = res.getStatus();
171
172         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174         Assert.assertEquals(statusCode, STATUS_CREATED);
175         personAuthCSID = extractId(res);
176         
177         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
178         
179         String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
180         Assert.assertNotNull(csid);
181         currentOwnerPersonCSID = csid;
182         currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
183         Assert.assertNotNull(currentOwnerRefName);
184         personIdsCreated.add(csid);
185         
186         csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
187         Assert.assertNotNull(csid);
188         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
189         Assert.assertNotNull(depositorRefName);
190         personIdsCreated.add(csid);
191         
192         csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
193         Assert.assertNotNull(csid);
194         conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
195         Assert.assertNotNull(conditionCheckerAssessorRefName);
196         personIdsCreated.add(csid);
197         
198         csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
199         Assert.assertNotNull(csid);
200         insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
201         Assert.assertNotNull(insurerRefName);
202         personIdsCreated.add(csid);
203         
204         csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
205         Assert.assertNotNull(csid);
206         valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
207         Assert.assertNotNull(valuerRefName);
208         personIdsCreated.add(csid);
209         
210     }
211     
212     protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
213         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
214         Map<String, String> personInfo = new HashMap<String,String>();
215         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
216         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
217         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
218         MultipartOutput multipart = 
219                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
220                                 authRefName, personInfo, personAuthClient.getItemCommonPartName());
221         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
222         int statusCode = res.getStatus();
223
224         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
225                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
226         Assert.assertEquals(statusCode, STATUS_CREATED);
227         return extractId(res);
228     }
229
230     // Success outcomes
231     
232     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
233         dependsOnMethods = {"createIntakeWithAuthRefs"})
234     public void readAndCheckAuthRefDocs(String testName) throws Exception {
235
236         if (logger.isDebugEnabled()) {
237             logger.debug(testBanner(testName, CLASS_NAME));
238         }
239         // Perform setup.
240         testSetup(STATUS_OK, ServiceRequestType.READ);
241         
242         // Get the auth ref docs and check them
243
244         // Single scalar field
245        PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
246        ClientResponse<AuthorityRefDocList> refDocListResp =
247                 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
248
249         int statusCode = refDocListResp.getStatus();
250
251         if(logger.isDebugEnabled()){
252             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
253         }
254         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
255                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
256         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
257         AuthorityRefDocList list = refDocListResp.getEntity();
258
259         // Repeatable scalar field
260
261         // FIXME: Add an appropriate test here, or preferably a new test case.
262
263         // Optionally output additional data about list members for debugging.
264         boolean iterateThroughList = true;
265         boolean fFoundIntake = false;
266         if(iterateThroughList && logger.isDebugEnabled()){
267             List<AuthorityRefDocList.AuthorityRefDocItem> items =
268                     list.getAuthorityRefDocItem();
269             int i = 0;
270             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
271             for(AuthorityRefDocList.AuthorityRefDocItem item : items){
272                 logger.debug(testName + ": list-item[" + i + "] " +
273                                 item.getDocType() + "(" +
274                                 item.getDocId() + ") Name:[" +
275                                 item.getDocName() + "] Number:[" +
276                                 item.getDocNumber() + "] in field:[" +
277                                 item.getSourceField() + "]");
278                 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
279                         fFoundIntake = true;
280                 }
281                 i++;
282             }
283             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
284         }
285     }
286
287
288     // ---------------------------------------------------------------
289     // Cleanup of resources created during testing
290     // ---------------------------------------------------------------
291
292     /**
293      * Deletes all resources created by tests, after all tests have been run.
294      *
295      * This cleanup method will always be run, even if one or more tests fail.
296      * For this reason, it attempts to remove all resources created
297      * at any point during testing, even if some of those resources
298      * may be expected to be deleted by certain tests.
299      */
300     @AfterClass(alwaysRun=true)
301     public void cleanUp() {
302         String noTest = System.getProperty("noTestCleanup");
303         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
304             if (logger.isDebugEnabled()) {
305                 logger.debug("Skipping Cleanup phase ...");
306             }
307             return;
308         }
309         if (logger.isDebugEnabled()) {
310             logger.debug("Cleaning up temporary resources created for testing ...");
311         }
312         IntakeClient intakeClient = new IntakeClient();
313         // Note: Any non-success responses are ignored and not reported.
314         for (String resourceId : intakeIdsCreated) {
315             ClientResponse<Response> res = intakeClient.delete(resourceId);
316             res.releaseConnection();
317         }
318         // Delete persons before PersonAuth
319         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
320         for (String resourceId : personIdsCreated) {
321             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
322             res.releaseConnection();
323         }
324         if (personAuthCSID != null) {
325                 personAuthClient.delete(personAuthCSID).releaseConnection();
326         }
327     }
328
329     // ---------------------------------------------------------------
330     // Utility methods used by tests above
331     // ---------------------------------------------------------------
332     @Override
333     public String getServicePathComponent() {
334         return SERVICE_PATH_COMPONENT;
335     }
336
337    private MultipartOutput createIntakeInstance(String entryNumber,
338                 String entryDate,
339                                 String currentOwner,
340                                 String depositor,
341                                 String conditionCheckerAssessor,
342                                 String insurer,
343                                 String Valuer ) {
344         IntakesCommon intake = new IntakesCommon();
345         intake.setEntryNumber(entryNumber);
346         intake.setEntryDate(entryDate);
347         intake.setCurrentOwner(currentOwner);
348         intake.setDepositor(depositor);
349         intake.setValuer(Valuer);
350
351         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
352         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
353         checkersOrAssessors.add(conditionCheckerAssessor);
354         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
355
356         InsurerList insurerList = new InsurerList();
357         List<String> insurers = insurerList.getInsurer();
358         insurers.add(insurer);
359         intake.setInsurers(insurerList);
360
361         MultipartOutput multipart = new MultipartOutput();
362         OutputPart commonPart =
363             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
364         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
365
366         if(logger.isDebugEnabled()){
367             logger.debug("to be created, intake common");
368             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
369         }
370
371         return multipart;
372     }
373 }