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