]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e0c0cd917de0091e089a839cf3262b4626870fe1
[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.AuthorityRefList;
39 import org.collectionspace.services.intake.IntakesCommon;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41
42 import org.jboss.resteasy.client.ClientResponse;
43
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
46 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
50
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * IntakeAuthRefsTest, carries out tests against a
56  * deployed and running Intake Service.
57  *
58  * $LastChangedRevision: 1327 $
59  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60  */
61 public class IntakeAuthRefsTest extends BaseServiceTest {
62
63     private final String CLASS_NAME = IntakeAuthRefsTest.class.getName();
64     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
65
66     // Instance variables specific to this test.
67     final String SERVICE_PATH_COMPONENT = "intakes";
68     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
69     private String knownResourceId = null;
70     private List<String> intakeIdsCreated = new ArrayList<String>();
71     private List<String> personIdsCreated = new ArrayList<String>();
72     private String personAuthCSID = null; 
73     private String currentOwnerRefName = null;
74     private String depositorRefName = null;
75     private String conditionCheckAssesorRefName = null;
76     private String insurerRefName = null;
77     private String fieldCollectorRefName = null;
78     private String valuerRefName = null;
79     private final int NUM_AUTH_REFS_EXPECTED = 6;
80
81     /* (non-Javadoc)
82      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
83      */
84     @Override
85     protected CollectionSpaceClient getClientInstance() {
86         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
87     }
88     
89     /* (non-Javadoc)
90      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
91      */
92     @Override
93         protected AbstractCommonList getAbstractCommonList(
94                         ClientResponse<AbstractCommonList> response) {
95         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
96     }
97
98     // ---------------------------------------------------------------
99     // CRUD tests : CREATE tests
100     // ---------------------------------------------------------------
101     // Success outcomes
102     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
103     public void createWithAuthRefs(String testName) throws Exception {
104
105         if (logger.isDebugEnabled()) {
106             logger.debug(testBanner(testName, CLASS_NAME));
107         }
108         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
109
110         // Submit the request to the service and store the response.
111         String identifier = createIdentifier();
112         
113         // Create all the person refs and entities
114         createPersonRefs();
115         
116         // Submit the request to the service and store the response.
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         ClientResponse<Response> res = intakeClient.create(multipart);
128
129         int statusCode = res.getStatus();
130
131         // Check the status code of the response: does it match
132         // the expected response(s)?
133         //
134         // Specifically:
135         // Does it fall within the set of valid status codes?
136         // Does it exactly match the expected status code?
137         if(logger.isDebugEnabled()){
138             logger.debug(testName + ": status = " + statusCode);
139         }
140         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
141                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
142         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
143
144         // Store the ID returned from the first resource created
145         // for additional tests below.
146         if (knownResourceId == null){
147             knownResourceId = extractId(res);
148             if (logger.isDebugEnabled()) {
149                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
150             }
151         }
152         
153         // Store the IDs from every resource created by tests,
154         // so they can be deleted after tests have been run.
155         intakeIdsCreated.add(extractId(res));
156     }
157     
158     protected void createPersonRefs(){
159         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
160         String authRefName = 
161                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
162         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
163                         PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
164         ClientResponse<Response> res = personAuthClient.create(multipart);
165         int statusCode = res.getStatus();
166
167         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
168                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
169         Assert.assertEquals(statusCode, STATUS_CREATED);
170         personAuthCSID = extractId(res);
171         
172         currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
173                                                                 authRefName, "Olivier Owner", true);
174         personIdsCreated.add(createPerson("Olivier", "Owner", currentOwnerRefName));
175         
176         depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
177                                                                         authRefName, "Debbie Depositor", true);
178         personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
179         
180         conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
181                                                                         authRefName, "Andrew Assessor", true);
182         personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
183         
184         insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
185                                                                         authRefName, "Ingrid Insurer", true);
186         personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
187         
188         fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
189                                                                         authRefName, "Connie Collector", true);
190         personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
191         
192         valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
193                                                                         authRefName, "Vince Valuer", true);
194         personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
195         
196
197     }
198     
199     protected String createPerson(String firstName, String surName, String refName ) {
200         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
201         Map<String, String> personInfo = new HashMap<String,String>();
202         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
203         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
204         MultipartOutput multipart = 
205                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
206                                 refName, personInfo, personAuthClient.getItemCommonPartName());
207         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
208         int statusCode = res.getStatus();
209
210         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
211                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
212         Assert.assertEquals(statusCode, STATUS_CREATED);
213         return extractId(res);
214     }
215
216     // Success outcomes
217     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
218         dependsOnMethods = {"createWithAuthRefs"})
219     public void readAndCheckAuthRefs(String testName) throws Exception {
220
221         if (logger.isDebugEnabled()) {
222             logger.debug(testBanner(testName, CLASS_NAME));
223         }
224         // Perform setup.
225         testSetup(STATUS_OK, ServiceRequestType.READ);
226
227         // Submit the request to the service and store the response.
228         IntakeClient intakeClient = new IntakeClient();
229         ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
230         int statusCode = res.getStatus();
231
232         // Check the status code of the response: does it match
233         // the expected response(s)?
234         if(logger.isDebugEnabled()){
235             logger.debug(testName + ".read: status = " + statusCode);
236         }
237         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
238                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
239         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
240
241         MultipartInput input = (MultipartInput) res.getEntity();
242         IntakesCommon intake = (IntakesCommon) extractPart(input,
243                         intakeClient.getCommonPartName(), IntakesCommon.class);
244         Assert.assertNotNull(intake);
245         // Check a couple of fields
246         Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
247         Assert.assertEquals(intake.getInsurer(), insurerRefName);
248         
249         // Get the auth refs and check them
250         ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
251         statusCode = res2.getStatus();
252
253         if(logger.isDebugEnabled()){
254             logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
255         }
256         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
257                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
258         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
259         AuthorityRefList list = res2.getEntity();
260         
261         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
262         int numAuthRefsFound = items.size();
263         if(logger.isDebugEnabled()){
264             logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
265                 " authority references, found " + numAuthRefsFound);
266         }
267         Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
268             "Did not find all expected authority references! " +
269             "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
270
271         // Optionally output additional data about list members for debugging.
272         boolean iterateThroughList = true;
273         if(iterateThroughList && logger.isDebugEnabled()){
274             int i = 0;
275             for(AuthorityRefList.AuthorityRefItem item : items){
276                 logger.debug(testName + ": list-item[" + i + "] Field:" +
277                                 item.getSourceField() + "= " +
278                         item.getAuthDisplayName() +
279                         item.getItemDisplayName());
280                 logger.debug(testName + ": list-item[" + i + "] refName=" +
281                         item.getRefName());
282                 logger.debug(testName + ": list-item[" + i + "] URI=" +
283                         item.getUri());
284                 i++;
285             }
286         }
287     }
288
289
290     // ---------------------------------------------------------------
291     // Cleanup of resources created during testing
292     // ---------------------------------------------------------------
293
294     /**
295      * Deletes all resources created by tests, after all tests have been run.
296      *
297      * This cleanup method will always be run, even if one or more tests fail.
298      * For this reason, it attempts to remove all resources created
299      * at any point during testing, even if some of those resources
300      * may be expected to be deleted by certain tests.
301      */
302     @AfterClass(alwaysRun=true)
303     public void cleanUp() {
304         String noTest = System.getProperty("noTestCleanup");
305         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
306             if (logger.isDebugEnabled()) {
307                 logger.debug("Skipping Cleanup phase ...");
308             }
309             return;
310         }
311         if (logger.isDebugEnabled()) {
312             logger.debug("Cleaning up temporary resources created for testing ...");
313         }
314         IntakeClient intakeClient = new IntakeClient();
315         // Note: Any non-success responses are ignored and not reported.
316         for (String resourceId : intakeIdsCreated) {
317             intakeClient.delete(resourceId).releaseConnection();
318         }
319         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
320         // Delete persons before PersonAuth
321         for (String resourceId : personIdsCreated) {
322             personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
323         }
324         personAuthClient.delete(personAuthCSID).releaseConnection();
325     }
326
327     // ---------------------------------------------------------------
328     // Utility methods used by tests above
329     // ---------------------------------------------------------------
330     @Override
331     public String getServicePathComponent() {
332         return SERVICE_PATH_COMPONENT;
333     }
334
335    private MultipartOutput createIntakeInstance(String entryNumber,
336                 String entryDate,
337                                 String currentOwner,
338                                 String depositor,
339                                 String conditionCheckAssesor,
340                                 String insurer,
341                                 String fieldCollector,
342                                 String Valuer ) {
343         IntakesCommon intake = new IntakesCommon();
344         intake.setEntryNumber(entryNumber);
345         intake.setEntryDate(entryDate);
346         intake.setCurrentOwner(currentOwner);
347         intake.setDepositor(depositor);
348         intake.setConditionCheckAssesor(conditionCheckAssesor);
349         intake.setInsurer(insurer);
350         intake.setFieldCollector(fieldCollector);
351         intake.setValuer(Valuer);
352         MultipartOutput multipart = new MultipartOutput();
353         OutputPart commonPart =
354             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
355         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
356
357         if(logger.isDebugEnabled()){
358             logger.debug("to be created, intake common");
359             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
360         }
361
362         return multipart;
363     }
364 }