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