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