]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
44bfb8314914635a694f7fd8bce26c38ec89d674
[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         // Optionally output additional data about list members for debugging.
262         boolean iterateThroughList = true;
263         if(iterateThroughList && logger.isDebugEnabled()){
264             List<AuthorityRefList.AuthorityRefItem> items =
265                     list.getAuthorityRefItem();
266             int i = 0;
267             for(AuthorityRefList.AuthorityRefItem item : items){
268                 logger.debug(testName + ": list-item[" + i + "] Field:" +
269                                 item.getSourceField() + "= " +
270                         item.getAuthDisplayName() +
271                         item.getItemDisplayName());
272                 logger.debug(testName + ": list-item[" + i + "] refName=" +
273                         item.getRefName());
274                 logger.debug(testName + ": list-item[" + i + "] URI=" +
275                         item.getUri());
276                 i++;
277             }
278             Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
279         }
280     }
281
282
283     // ---------------------------------------------------------------
284     // Cleanup of resources created during testing
285     // ---------------------------------------------------------------
286
287     /**
288      * Deletes all resources created by tests, after all tests have been run.
289      *
290      * This cleanup method will always be run, even if one or more tests fail.
291      * For this reason, it attempts to remove all resources created
292      * at any point during testing, even if some of those resources
293      * may be expected to be deleted by certain tests.
294      */
295     @AfterClass(alwaysRun=true)
296     public void cleanUp() {
297         String noTest = System.getProperty("noTestCleanup");
298         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
299             if (logger.isDebugEnabled()) {
300                 logger.debug("Skipping Cleanup phase ...");
301             }
302             return;
303         }
304         if (logger.isDebugEnabled()) {
305             logger.debug("Cleaning up temporary resources created for testing ...");
306         }
307         IntakeClient intakeClient = new IntakeClient();
308         // Note: Any non-success responses are ignored and not reported.
309         for (String resourceId : intakeIdsCreated) {
310             intakeClient.delete(resourceId).releaseConnection();
311         }
312         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
313         // Delete persons before PersonAuth
314         for (String resourceId : personIdsCreated) {
315             personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
316         }
317         personAuthClient.delete(personAuthCSID).releaseConnection();
318     }
319
320     // ---------------------------------------------------------------
321     // Utility methods used by tests above
322     // ---------------------------------------------------------------
323     @Override
324     public String getServicePathComponent() {
325         return SERVICE_PATH_COMPONENT;
326     }
327
328    private MultipartOutput createIntakeInstance(String entryNumber,
329                 String entryDate,
330                                 String currentOwner,
331                                 String depositor,
332                                 String conditionCheckAssesor,
333                                 String insurer,
334                                 String fieldCollector,
335                                 String Valuer ) {
336         IntakesCommon intake = new IntakesCommon();
337         intake.setEntryNumber(entryNumber);
338         intake.setEntryDate(entryDate);
339         intake.setCurrentOwner(currentOwner);
340         intake.setDepositor(depositor);
341         intake.setConditionCheckAssesor(conditionCheckAssesor);
342         intake.setInsurer(insurer);
343         intake.setFieldCollector(fieldCollector);
344         intake.setValuer(Valuer);
345         MultipartOutput multipart = new MultipartOutput();
346         OutputPart commonPart =
347             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
348         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
349
350         if(logger.isDebugEnabled()){
351             logger.debug("to be created, intake common");
352             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
353         }
354
355         return multipart;
356     }
357 }