]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d327824e25d60c3e8eeda27955a1952733d2366d
[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 Logger logger =
65        LoggerFactory.getLogger(PersonAuthRefDocsTest.class);
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 int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
74     private int OK_STATUS = Response.Status.OK.getStatusCode();
75     private String personAuthCSID = null; 
76     private String currentOwnerPersonCSID = null; 
77     private String currentOwnerRefName = null;
78     private String depositorRefName = null;
79     private String conditionCheckAssesorRefName = null;
80     private String insurerRefName = null;
81     private String fieldCollectorRefName = null;
82     private String valuerRefName = null;
83     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
84
85     /* (non-Javadoc)
86      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
87      */
88     @Override
89     protected CollectionSpaceClient getClientInstance() {
90         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
91     }
92     
93     /* (non-Javadoc)
94      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
95      */
96     @Override
97         protected AbstractCommonList getAbstractCommonList(
98                         ClientResponse<AbstractCommonList> response) {
99         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
100     }
101
102     // ---------------------------------------------------------------
103     // CRUD tests : CREATE tests
104     // ---------------------------------------------------------------
105     // Success outcomes
106     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
107     public void createIntakeWithAuthRefs(String testName) throws Exception {
108
109         testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
110
111         // Submit the request to the service and store the response.
112         String identifier = createIdentifier();
113         
114         // Create all the person refs and entities
115         createPersonRefs();
116
117         IntakeClient intakeClient = new IntakeClient();
118         MultipartOutput multipart = createIntakeInstance(
119                 "entryNumber-" + identifier,
120                 "entryDate-" + identifier,
121                                                                 currentOwnerRefName,
122                                                                 depositorRefName,
123                                                                 conditionCheckAssesorRefName,
124                                                                 insurerRefName,
125                                                                 fieldCollectorRefName,
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         String authRefName = 
168                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
169         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
170                         PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
171         ClientResponse<Response> res = personAuthClient.create(multipart);
172         int statusCode = res.getStatus();
173
174         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176         Assert.assertEquals(statusCode, CREATED_STATUS);
177         personAuthCSID = extractId(res);
178         
179         currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
180                                                                 authRefName, "Olivier Owner", true);
181                                 currentOwnerPersonCSID = createPerson("Olivier", "Owner", currentOwnerRefName);
182         personIdsCreated.add(currentOwnerPersonCSID);
183         
184         depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
185                                                                         authRefName, "Debbie Depositor", true);
186         personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
187         
188         conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
189                                                                         authRefName, "Andrew Assessor", true);
190         personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
191         
192         insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
193                                                                         authRefName, "Ingrid Insurer", true);
194         personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
195         
196         fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
197                                                                         authRefName, "Connie Collector", true);
198         personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
199         
200         valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
201                                                                         authRefName, "Vince Valuer", true);
202         personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
203         
204
205     }
206     
207     protected String createPerson(String firstName, String surName, String refName ) {
208         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
209         Map<String, String> personInfo = new HashMap<String,String>();
210         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
211         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
212         MultipartOutput multipart = 
213                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
214                                 refName, personInfo, personAuthClient.getItemCommonPartName());
215         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
216         int statusCode = res.getStatus();
217
218         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220         Assert.assertEquals(statusCode, CREATED_STATUS);
221         return extractId(res);
222     }
223
224     // Success outcomes
225     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
226         dependsOnMethods = {"createIntakeWithAuthRefs"})
227     public void readAndCheckAuthRefDocs(String testName) throws Exception {
228
229         // Perform setup.
230         testSetup(OK_STATUS, ServiceRequestType.READ,testName);
231         
232         // Get the auth ref docs and check them
233        PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
234        ClientResponse<AuthorityRefDocList> refDocListResp =
235                 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
236
237         int statusCode = refDocListResp.getStatus();
238
239         if(logger.isDebugEnabled()){
240             logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
241         }
242         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
243                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
244         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
245         AuthorityRefDocList list = refDocListResp.getEntity();
246
247         // Optionally output additional data about list members for debugging.
248         boolean iterateThroughList = true;
249         boolean fFoundIntake = false;
250         if(iterateThroughList && logger.isDebugEnabled()){
251             List<AuthorityRefDocList.AuthorityRefDocItem> items =
252                     list.getAuthorityRefDocItem();
253             int i = 0;
254             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
255             for(AuthorityRefDocList.AuthorityRefDocItem item : items){
256                 logger.debug(testName + ": list-item[" + i + "] " +
257                                 item.getDocType() + "(" +
258                                 item.getDocId() + ") Name:[" +
259                                 item.getDocName() + "] Number:[" +
260                                 item.getDocNumber() + "] in field:[" +
261                                 item.getSourceField() + "]");
262                 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
263                         fFoundIntake = true;
264                 }
265                 i++;
266             }
267             Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
268         }
269     }
270
271
272     // ---------------------------------------------------------------
273     // Cleanup of resources created during testing
274     // ---------------------------------------------------------------
275
276     /**
277      * Deletes all resources created by tests, after all tests have been run.
278      *
279      * This cleanup method will always be run, even if one or more tests fail.
280      * For this reason, it attempts to remove all resources created
281      * at any point during testing, even if some of those resources
282      * may be expected to be deleted by certain tests.
283      */
284     @AfterClass(alwaysRun=true)
285     public void cleanUp() {
286         String noTest = System.getProperty("noTestCleanup");
287         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
288             if (logger.isDebugEnabled()) {
289                 logger.debug("Skipping Cleanup phase ...");
290             }
291             return;
292         }
293         if (logger.isDebugEnabled()) {
294             logger.debug("Cleaning up temporary resources created for testing ...");
295         }
296         IntakeClient intakeClient = new IntakeClient();
297         // Note: Any non-success responses are ignored and not reported.
298         for (String resourceId : intakeIdsCreated) {
299             ClientResponse<Response> res = intakeClient.delete(resourceId);
300             res.releaseConnection();
301         }
302         // Delete persons before PersonAuth
303         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
304         for (String resourceId : personIdsCreated) {
305             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
306             res.releaseConnection();
307         }
308         if (personAuthCSID != null) {
309                 personAuthClient.delete(personAuthCSID).releaseConnection();
310         }
311     }
312
313     // ---------------------------------------------------------------
314     // Utility methods used by tests above
315     // ---------------------------------------------------------------
316     @Override
317     public String getServicePathComponent() {
318         return SERVICE_PATH_COMPONENT;
319     }
320
321    private MultipartOutput createIntakeInstance(String entryNumber,
322                 String entryDate,
323                                 String currentOwner,
324                                 String depositor,
325                                 String conditionCheckAssesor,
326                                 String insurer,
327                                 String fieldCollector,
328                                 String Valuer ) {
329         IntakesCommon intake = new IntakesCommon();
330         intake.setEntryNumber(entryNumber);
331         intake.setEntryDate(entryDate);
332         intake.setCurrentOwner(currentOwner);
333         intake.setDepositor(depositor);
334         intake.setConditionCheckAssesor(conditionCheckAssesor);
335         intake.setInsurer(insurer);
336         intake.setFieldCollector(fieldCollector);
337         intake.setValuer(Valuer);
338         MultipartOutput multipart = new MultipartOutput();
339         OutputPart commonPart =
340             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
341         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
342
343         if(logger.isDebugEnabled()){
344             logger.debug("to be created, intake common");
345             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
346         }
347
348         return multipart;
349     }
350 }