]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a653700561aad74b84dd6b6cc0795f3f953cc75a
[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 Logger logger =
64        LoggerFactory.getLogger(IntakeAuthRefsTest.class);
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 int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
73     private int OK_STATUS = Response.Status.OK.getStatusCode();
74     private String personAuthCSID = 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_REFS_EXPECTED = 6;
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 createWithAuthRefs(String testName) throws Exception {
106
107         testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
108
109         // Submit the request to the service and store the response.
110         String identifier = createIdentifier();
111         
112         // Create all the person refs and entities
113         createPersonRefs();
114         
115         // Submit the request to the service and store the response.
116         IntakeClient intakeClient = new IntakeClient();
117         MultipartOutput multipart = createIntakeInstance(
118                 "entryNumber-" + identifier,
119                 "entryDate-" + identifier,
120                 currentOwnerRefName,
121                 depositorRefName,
122                 conditionCheckAssesorRefName,
123                 insurerRefName,
124                 fieldCollectorRefName,
125                 valuerRefName );
126         ClientResponse<Response> res = intakeClient.create(multipart);
127
128         int statusCode = res.getStatus();
129
130         // Check the status code of the response: does it match
131         // the expected response(s)?
132         //
133         // Specifically:
134         // Does it fall within the set of valid status codes?
135         // Does it exactly match the expected status code?
136         if(logger.isDebugEnabled()){
137             logger.debug(testName + ": status = " + statusCode);
138         }
139         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
140                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
141         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
142
143         // Store the ID returned from the first resource created
144         // for additional tests below.
145         if (knownResourceId == null){
146             knownResourceId = extractId(res);
147             if (logger.isDebugEnabled()) {
148                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
149             }
150         }
151         
152         // Store the IDs from every resource created by tests,
153         // so they can be deleted after tests have been run.
154         intakeIdsCreated.add(extractId(res));
155     }
156     
157     protected void createPersonRefs(){
158         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
159         String authRefName = 
160                 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
161         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
162                         PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
163         ClientResponse<Response> res = personAuthClient.create(multipart);
164         int statusCode = res.getStatus();
165
166         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
167                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
168         Assert.assertEquals(statusCode, CREATED_STATUS);
169         personAuthCSID = extractId(res);
170         
171         currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
172                                                                 authRefName, "Olivier Owner", true);
173         personIdsCreated.add(createPerson("Olivier", "Owner", currentOwnerRefName));
174         
175         depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
176                                                                         authRefName, "Debbie Depositor", true);
177         personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
178         
179         conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
180                                                                         authRefName, "Andrew Assessor", true);
181         personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
182         
183         insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
184                                                                         authRefName, "Ingrid Insurer", true);
185         personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
186         
187         fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
188                                                                         authRefName, "Connie Collector", true);
189         personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
190         
191         valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
192                                                                         authRefName, "Vince Valuer", true);
193         personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
194         
195
196     }
197     
198     protected String createPerson(String firstName, String surName, String refName ) {
199         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
200         Map<String, String> personInfo = new HashMap<String,String>();
201         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
202         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
203         MultipartOutput multipart = 
204                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
205                                 refName, personInfo, personAuthClient.getItemCommonPartName());
206         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
207         int statusCode = res.getStatus();
208
209         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
210                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
211         Assert.assertEquals(statusCode, CREATED_STATUS);
212         return extractId(res);
213     }
214
215     // Success outcomes
216     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
217         dependsOnMethods = {"createWithAuthRefs"})
218     public void readAndCheckAuthRefs(String testName) throws Exception {
219
220         // Perform setup.
221         testSetup(OK_STATUS, ServiceRequestType.READ,testName);
222
223         // Submit the request to the service and store the response.
224         IntakeClient intakeClient = new IntakeClient();
225         ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
226         int statusCode = res.getStatus();
227
228         // Check the status code of the response: does it match
229         // the expected response(s)?
230         if(logger.isDebugEnabled()){
231             logger.debug(testName + ".read: status = " + statusCode);
232         }
233         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
234                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
235         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
236
237         MultipartInput input = (MultipartInput) res.getEntity();
238         IntakesCommon intake = (IntakesCommon) extractPart(input,
239                         intakeClient.getCommonPartName(), IntakesCommon.class);
240         Assert.assertNotNull(intake);
241         // Check a couple of fields
242         Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
243         Assert.assertEquals(intake.getInsurer(), insurerRefName);
244         
245         // Get the auth refs and check them
246         ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
247         statusCode = res2.getStatus();
248
249         if(logger.isDebugEnabled()){
250             logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
251         }
252         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
253                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
254         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
255         AuthorityRefList list = res2.getEntity();
256
257         // Optionally output additional data about list members for debugging.
258         boolean iterateThroughList = true;
259         if(iterateThroughList && logger.isDebugEnabled()){
260             List<AuthorityRefList.AuthorityRefItem> items =
261                     list.getAuthorityRefItem();
262             int i = 0;
263             for(AuthorityRefList.AuthorityRefItem item : items){
264                 logger.debug(testName + ": list-item[" + i + "] Field:" +
265                                 item.getSourceField() + "= " +
266                         item.getAuthDisplayName() +
267                         item.getItemDisplayName());
268                 logger.debug(testName + ": list-item[" + i + "] refName=" +
269                         item.getRefName());
270                 logger.debug(testName + ": list-item[" + i + "] URI=" +
271                         item.getUri());
272                 i++;
273             }
274             Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
275         }
276     }
277
278
279     // ---------------------------------------------------------------
280     // Cleanup of resources created during testing
281     // ---------------------------------------------------------------
282
283     /**
284      * Deletes all resources created by tests, after all tests have been run.
285      *
286      * This cleanup method will always be run, even if one or more tests fail.
287      * For this reason, it attempts to remove all resources created
288      * at any point during testing, even if some of those resources
289      * may be expected to be deleted by certain tests.
290      */
291     @AfterClass(alwaysRun=true)
292     public void cleanUp() {
293         String noTest = System.getProperty("noTestCleanup");
294         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
295             if (logger.isDebugEnabled()) {
296                 logger.debug("Skipping Cleanup phase ...");
297             }
298             return;
299         }
300         if (logger.isDebugEnabled()) {
301             logger.debug("Cleaning up temporary resources created for testing ...");
302         }
303         IntakeClient intakeClient = new IntakeClient();
304         // Note: Any non-success responses are ignored and not reported.
305         for (String resourceId : intakeIdsCreated) {
306             intakeClient.delete(resourceId).releaseConnection();
307         }
308         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
309         // Delete persons before PersonAuth
310         for (String resourceId : personIdsCreated) {
311             personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
312         }
313         personAuthClient.delete(personAuthCSID).releaseConnection();
314     }
315
316     // ---------------------------------------------------------------
317     // Utility methods used by tests above
318     // ---------------------------------------------------------------
319     @Override
320     public String getServicePathComponent() {
321         return SERVICE_PATH_COMPONENT;
322     }
323
324    private MultipartOutput createIntakeInstance(String entryNumber,
325                 String entryDate,
326                                 String currentOwner,
327                                 String depositor,
328                                 String conditionCheckAssesor,
329                                 String insurer,
330                                 String fieldCollector,
331                                 String Valuer ) {
332         IntakesCommon intake = new IntakesCommon();
333         intake.setEntryNumber(entryNumber);
334         intake.setEntryDate(entryDate);
335         intake.setCurrentOwner(currentOwner);
336         intake.setDepositor(depositor);
337         intake.setConditionCheckAssesor(conditionCheckAssesor);
338         intake.setInsurer(insurer);
339         intake.setFieldCollector(fieldCollector);
340         intake.setValuer(Valuer);
341         MultipartOutput multipart = new MultipartOutput();
342         OutputPart commonPart =
343             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
344         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
345
346         if(logger.isDebugEnabled()){
347             logger.debug("to be created, intake common");
348             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
349         }
350
351         return multipart;
352     }
353 }