]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ae955825ec26c5a92a96f9564608937ce36908c2
[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.IntakesCommon;
40 //import org.collectionspace.services.intake.IntakesCommonList;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42
43 import org.jboss.resteasy.client.ClientResponse;
44
45 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
47 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.Test;
51
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * PersonAuthRefDocsTest, carries out tests against a
57  * deployed and running Person Service.
58  *
59  * $LastChangedRevision: 1327 $
60  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
61  */
62 public class PersonAuthRefDocsTest extends BaseServiceTest {
63
64     private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
65     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
66
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 currentOwnerRefName = null;
76     private String depositorRefName = null;
77     private String conditionCheckAssesorRefName = null;
78     private String insurerRefName = null;
79     private String fieldCollectorRefName = 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                                                                 conditionCheckAssesorRefName,
125                                                                 insurerRefName,
126                                                                 fieldCollectorRefName,
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         String authRefName = 
169                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
170         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
171                         PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
172         ClientResponse<Response> res = personAuthClient.create(multipart);
173         int statusCode = res.getStatus();
174
175         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177         Assert.assertEquals(statusCode, STATUS_CREATED);
178         personAuthCSID = extractId(res);
179         
180         currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
181                                                                 authRefName, "Olivier Owner", true);
182                                 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
183         personIdsCreated.add(currentOwnerPersonCSID);
184         
185         depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
186                                                                         authRefName, "Debbie Depositor", true);
187         personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
188         
189         conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
190                                                                         authRefName, "Andrew Assessor", true);
191         personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
192         
193         insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
194                                                                         authRefName, "Ingrid Insurer", true);
195         personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
196         
197         fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
198                                                                         authRefName, "Connie Collector", true);
199         personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
200         
201         valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
202                                                                         authRefName, "Vince Valuer", true);
203         personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
204         
205
206     }
207     
208     protected String createPerson(String firstName, String surName, String refName ) {
209         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
210         Map<String, String> personInfo = new HashMap<String,String>();
211         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
212         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
213         MultipartOutput multipart = 
214                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
215                                 refName, personInfo, personAuthClient.getItemCommonPartName());
216         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
217         int statusCode = res.getStatus();
218
219         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
220                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
221         Assert.assertEquals(statusCode, STATUS_CREATED);
222         return extractId(res);
223     }
224
225     // Success outcomes
226     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
227         dependsOnMethods = {"createIntakeWithAuthRefs"})
228     public void readAndCheckAuthRefDocs(String testName) throws Exception {
229
230         if (logger.isDebugEnabled()) {
231             logger.debug(testBanner(testName, CLASS_NAME));
232         }
233         // Perform setup.
234         testSetup(STATUS_OK, ServiceRequestType.READ);
235         
236         // Get the auth ref docs and check them
237        PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
238        ClientResponse<AuthorityRefDocList> refDocListResp =
239                 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
240
241         int statusCode = refDocListResp.getStatus();
242
243         if(logger.isDebugEnabled()){
244             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
245         }
246         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
247                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
248         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
249         AuthorityRefDocList list = refDocListResp.getEntity();
250
251         // Optionally output additional data about list members for debugging.
252         boolean iterateThroughList = true;
253         boolean fFoundIntake = false;
254         if(iterateThroughList && logger.isDebugEnabled()){
255             List<AuthorityRefDocList.AuthorityRefDocItem> items =
256                     list.getAuthorityRefDocItem();
257             int i = 0;
258             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
259             for(AuthorityRefDocList.AuthorityRefDocItem item : items){
260                 logger.debug(testName + ": list-item[" + i + "] " +
261                                 item.getDocType() + "(" +
262                                 item.getDocId() + ") Name:[" +
263                                 item.getDocName() + "] Number:[" +
264                                 item.getDocNumber() + "] in field:[" +
265                                 item.getSourceField() + "]");
266                 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
267                         fFoundIntake = true;
268                 }
269                 i++;
270             }
271             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
272         }
273     }
274
275
276     // ---------------------------------------------------------------
277     // Cleanup of resources created during testing
278     // ---------------------------------------------------------------
279
280     /**
281      * Deletes all resources created by tests, after all tests have been run.
282      *
283      * This cleanup method will always be run, even if one or more tests fail.
284      * For this reason, it attempts to remove all resources created
285      * at any point during testing, even if some of those resources
286      * may be expected to be deleted by certain tests.
287      */
288     @AfterClass(alwaysRun=true)
289     public void cleanUp() {
290         String noTest = System.getProperty("noTestCleanup");
291         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
292             if (logger.isDebugEnabled()) {
293                 logger.debug("Skipping Cleanup phase ...");
294             }
295             return;
296         }
297         if (logger.isDebugEnabled()) {
298             logger.debug("Cleaning up temporary resources created for testing ...");
299         }
300         IntakeClient intakeClient = new IntakeClient();
301         // Note: Any non-success responses are ignored and not reported.
302         for (String resourceId : intakeIdsCreated) {
303             ClientResponse<Response> res = intakeClient.delete(resourceId);
304             res.releaseConnection();
305         }
306         // Delete persons before PersonAuth
307         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
308         for (String resourceId : personIdsCreated) {
309             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
310             res.releaseConnection();
311         }
312         if (personAuthCSID != null) {
313                 personAuthClient.delete(personAuthCSID).releaseConnection();
314         }
315     }
316
317     // ---------------------------------------------------------------
318     // Utility methods used by tests above
319     // ---------------------------------------------------------------
320     @Override
321     public String getServicePathComponent() {
322         return SERVICE_PATH_COMPONENT;
323     }
324
325    private MultipartOutput createIntakeInstance(String entryNumber,
326                 String entryDate,
327                                 String currentOwner,
328                                 String depositor,
329                                 String conditionCheckAssesor,
330                                 String insurer,
331                                 String fieldCollector,
332                                 String Valuer ) {
333         IntakesCommon intake = new IntakesCommon();
334         intake.setEntryNumber(entryNumber);
335         intake.setEntryDate(entryDate);
336         intake.setCurrentOwner(currentOwner);
337         intake.setDepositor(depositor);
338         intake.setConditionCheckAssesor(conditionCheckAssesor);
339         intake.setInsurer(insurer);
340         intake.setFieldCollector(fieldCollector);
341         intake.setValuer(Valuer);
342         MultipartOutput multipart = new MultipartOutput();
343         OutputPart commonPart =
344             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
345         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
346
347         if(logger.isDebugEnabled()){
348             logger.debug("to be created, intake common");
349             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
350         }
351
352         return multipart;
353     }
354 }