]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2eebbc8d5c04bd79a816bb55233acc890613465b
[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     final String SERVICE_PATH_COMPONENT = "intakes";
67     final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
68     private String knownResourceId = null;
69     private List<String> intakeIdsCreated = new ArrayList();
70     private List<String> personIdsCreated = new ArrayList();
71     private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
72     private int OK_STATUS = Response.Status.OK.getStatusCode();
73     private String personAuthCSID = null; 
74     private String currentOwnerRefName = null;
75     private String depositorRefName = null;
76     private String conditionCheckAssesorRefName = null;
77     private String insurerRefName = null;
78     private String fieldCollectorRefName = null;
79     private String valuerRefName = null;
80     private final int NUM_AUTH_REFS_EXPECTED = 6;
81
82     // ---------------------------------------------------------------
83     // CRUD tests : CREATE tests
84     // ---------------------------------------------------------------
85     // Success outcomes
86     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
87     public void createWithAuthRefs(String testName) throws Exception {
88
89         testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
90
91         // Submit the request to the service and store the response.
92         String identifier = createIdentifier();
93         
94         // Create all the person refs and entities
95         createPersonRefs();
96         
97         // Submit the request to the service and store the response.
98         IntakeClient intakeClient = new IntakeClient();
99         MultipartOutput multipart = createIntakeInstance(
100                 "entryNumber-" + identifier,
101                 "entryDate-" + identifier,
102                 currentOwnerRefName,
103                 depositorRefName,
104                 conditionCheckAssesorRefName,
105                 insurerRefName,
106                 fieldCollectorRefName,
107                 valuerRefName );
108         ClientResponse<Response> res = intakeClient.create(multipart);
109
110         int statusCode = res.getStatus();
111
112         // Check the status code of the response: does it match
113         // the expected response(s)?
114         //
115         // Specifically:
116         // Does it fall within the set of valid status codes?
117         // Does it exactly match the expected status code?
118         if(logger.isDebugEnabled()){
119             logger.debug(testName + ": status = " + statusCode);
120         }
121         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
122                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
123         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
124
125         // Store the ID returned from the first resource created
126         // for additional tests below.
127         if (knownResourceId == null){
128             knownResourceId = extractId(res);
129             if (logger.isDebugEnabled()) {
130                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
131             }
132         }
133         
134         // Store the IDs from every resource created by tests,
135         // so they can be deleted after tests have been run.
136         intakeIdsCreated.add(extractId(res));
137     }
138     
139     protected void createPersonRefs(){
140         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
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         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
182         Map<String, String> personInfo = new HashMap<String,String>();
183         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
184         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
185         MultipartOutput multipart = 
186                 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, 
187                                 refName, personInfo, personAuthClient.getItemCommonPartName());
188         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
189         int statusCode = res.getStatus();
190
191         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
192                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
193         Assert.assertEquals(statusCode, CREATED_STATUS);
194         return extractId(res);
195     }
196
197     // Success outcomes
198     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
199         dependsOnMethods = {"createWithAuthRefs"})
200     public void readAndCheckAuthRefs(String testName) throws Exception {
201
202         // Perform setup.
203         testSetup(OK_STATUS, ServiceRequestType.READ,testName);
204
205         // Submit the request to the service and store the response.
206         IntakeClient intakeClient = new IntakeClient();
207         ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
208         int statusCode = res.getStatus();
209
210         // Check the status code of the response: does it match
211         // the expected response(s)?
212         if(logger.isDebugEnabled()){
213             logger.debug(testName + ".read: status = " + statusCode);
214         }
215         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
216                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
217         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
218
219         MultipartInput input = (MultipartInput) res.getEntity();
220         IntakesCommon intake = (IntakesCommon) extractPart(input,
221                         intakeClient.getCommonPartName(), IntakesCommon.class);
222         Assert.assertNotNull(intake);
223         // Check a couple of fields
224         Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
225         Assert.assertEquals(intake.getInsurer(), insurerRefName);
226         
227         // Get the auth refs and check them
228         ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
229         statusCode = res2.getStatus();
230
231         if(logger.isDebugEnabled()){
232             logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
233         }
234         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
235                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
236         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
237         AuthorityRefList list = res2.getEntity();
238
239         // Optionally output additional data about list members for debugging.
240         boolean iterateThroughList = true;
241         if(iterateThroughList && logger.isDebugEnabled()){
242             List<AuthorityRefList.AuthorityRefItem> items =
243                     list.getAuthorityRefItem();
244             int i = 0;
245             for(AuthorityRefList.AuthorityRefItem item : items){
246                 logger.debug(testName + ": list-item[" + i + "] Field:" +
247                                 item.getSourceField() + "= " +
248                         item.getAuthDisplayName() +
249                         item.getItemDisplayName());
250                 logger.debug(testName + ": list-item[" + i + "] refName=" +
251                         item.getRefName());
252                 logger.debug(testName + ": list-item[" + i + "] URI=" +
253                         item.getUri());
254                 i++;
255             }
256             Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
257         }
258     }
259
260
261     // ---------------------------------------------------------------
262     // Cleanup of resources created during testing
263     // ---------------------------------------------------------------
264
265     /**
266      * Deletes all resources created by tests, after all tests have been run.
267      *
268      * This cleanup method will always be run, even if one or more tests fail.
269      * For this reason, it attempts to remove all resources created
270      * at any point during testing, even if some of those resources
271      * may be expected to be deleted by certain tests.
272      */
273     @AfterClass(alwaysRun=true)
274     public void cleanUp() {
275         if (logger.isDebugEnabled()) {
276             logger.debug("Cleaning up temporary resources created for testing ...");
277         }
278         IntakeClient intakeClient = new IntakeClient();
279         // Note: Any non-success responses are ignored and not reported.
280         for (String resourceId : intakeIdsCreated) {
281             ClientResponse<Response> res = intakeClient.delete(resourceId);
282         }
283         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
284         // Delete persons before PersonAuth
285         for (String resourceId : personIdsCreated) {
286             ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
287         }
288         ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
289     }
290
291     // ---------------------------------------------------------------
292     // Utility methods used by tests above
293     // ---------------------------------------------------------------
294     @Override
295     public String getServicePathComponent() {
296         return SERVICE_PATH_COMPONENT;
297     }
298
299    private MultipartOutput createIntakeInstance(String entryNumber,
300                 String entryDate,
301                                 String currentOwner,
302                                 String depositor,
303                                 String conditionCheckAssesor,
304                                 String insurer,
305                                 String fieldCollector,
306                                 String Valuer ) {
307         IntakesCommon intake = new IntakesCommon();
308         intake.setEntryNumber(entryNumber);
309         intake.setEntryDate(entryDate);
310         intake.setCurrentOwner(currentOwner);
311         intake.setDepositor(depositor);
312         intake.setConditionCheckAssesor(conditionCheckAssesor);
313         intake.setInsurer(insurer);
314         intake.setFieldCollector(fieldCollector);
315         intake.setValuer(Valuer);
316         MultipartOutput multipart = new MultipartOutput();
317         OutputPart commonPart =
318             multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
319         commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
320
321         if(logger.isDebugEnabled()){
322             logger.debug("to be created, intake common");
323             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
324         }
325
326         return multipart;
327     }
328 }