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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
33 import org.collectionspace.services.PersonJAXBSchema;
34 import org.collectionspace.services.client.AcquisitionClient;
35 import org.collectionspace.services.client.CollectionSpaceClient;
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.jaxb.AbstractCommonList;
40 import org.collectionspace.services.acquisition.AcquisitionsCommon;
41 //import org.collectionspace.services.acquisition.AcquisitionsCommonList;
43 import org.jboss.resteasy.client.ClientResponse;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
47 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.Test;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * AcquisitionAuthRefsTest, carries out tests against a
57 * deployed and running Acquisition Service.
59 * $LastChangedRevision: 1327 $
60 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
62 public class AcquisitionAuthRefsTest extends BaseServiceTest {
64 private final String CLASS_NAME = AcquisitionAuthRefsTest.class.getName();
65 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
67 // Instance variables specific to this test.
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<String>();
72 private List<String> personIdsCreated = new ArrayList<String>();
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;
84 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
87 protected CollectionSpaceClient getClientInstance() {
88 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
92 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
95 protected AbstractCommonList getAbstractCommonList(
96 ClientResponse<AbstractCommonList> response) {
97 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
100 // ---------------------------------------------------------------
101 // CRUD tests : CREATE tests
102 // ---------------------------------------------------------------
104 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
105 public void createWithAuthRefs(String testName) throws Exception {
107 if (logger.isDebugEnabled()) {
108 logger.debug(testBanner(testName, CLASS_NAME));
112 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
114 // Submit the request to the service and store the response.
115 String identifier = createIdentifier();
117 // Create all the person refs and entities
120 MultipartOutput multipart = createAcquisitionInstance(
122 acquisitionAuthorizerRefName,
123 acquisitionFundingSourceRefName,
124 fieldCollectorRefName );
126 AcquisitionClient acquisitionClient = new AcquisitionClient();
127 ClientResponse<Response> res = acquisitionClient.create(multipart);
129 int statusCode = res.getStatus();
131 // Check the status code of the response: does it match
132 // the expected response(s)?
135 // Does it fall within the set of valid status codes?
136 // Does it exactly match the expected status code?
137 if(logger.isDebugEnabled()){
138 logger.debug(testName + ": status = " + statusCode);
140 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
141 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
142 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
144 // Store the ID returned from the first resource created
145 // for additional tests below.
146 if (knownResourceId == null){
147 knownResourceId = extractId(res);
148 if (logger.isDebugEnabled()) {
149 logger.debug(testName + ": knownResourceId=" + knownResourceId);
153 // Store the IDs from every resource created by tests,
154 // so they can be deleted after tests have been run.
155 acquisitionIdsCreated.add(extractId(res));
158 protected void createPersonRefs(){
160 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
161 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
162 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
163 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
164 ClientResponse<Response> res = personAuthClient.create(multipart);
165 int statusCode = res.getStatus();
167 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
168 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
169 Assert.assertEquals(statusCode, CREATED_STATUS);
170 personAuthCSID = extractId(res);
172 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.createPersonRefName(
173 authRefName, "Annie Authorizer", true);
174 personIdsCreated.add(createPerson("Annie", "Authorizer", acquisitionAuthorizerRefName));
176 acquisitionFundingSourceRefName = PersonAuthorityClientUtils.createPersonRefName(
177 authRefName, "Sammy Source", true);
178 personIdsCreated.add(createPerson("Sammy", "Source", acquisitionFundingSourceRefName));
181 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
182 authRefName, "Connie Collector", true);
183 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
186 protected String createPerson(String firstName, String surName, String refName ) {
187 Map<String, String> personInfo = new HashMap<String,String>();
188 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
189 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
190 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
191 MultipartOutput multipart =
192 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
193 refName, personInfo, personAuthClient.getItemCommonPartName());
194 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
195 int statusCode = res.getStatus();
197 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
198 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
199 Assert.assertEquals(statusCode, CREATED_STATUS);
200 return extractId(res);
204 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
205 dependsOnMethods = {"createWithAuthRefs"})
206 public void readAndCheckAuthRefs(String testName) throws Exception {
208 if (logger.isDebugEnabled()) {
209 logger.debug(testBanner(testName, CLASS_NAME));
213 testSetup(STATUS_OK, ServiceRequestType.READ);
215 // Submit the request to the service and store the response.
216 AcquisitionClient acquisitionClient = new AcquisitionClient();
217 ClientResponse<MultipartInput> res = acquisitionClient.read(knownResourceId);
218 int statusCode = res.getStatus();
220 // Check the status code of the response: does it match
221 // the expected response(s)?
222 if(logger.isDebugEnabled()){
223 logger.debug(testName + ".read: status = " + statusCode);
225 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
226 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
227 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
229 MultipartInput input = (MultipartInput) res.getEntity();
230 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
231 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
232 Assert.assertNotNull(acquisition);
233 // Check a couple of fields
234 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
235 Assert.assertEquals(acquisition.getFieldCollector(), fieldCollectorRefName);
237 // Get the auth refs and check them
238 ClientResponse<AuthorityRefList> res2 =
239 acquisitionClient.getAuthorityRefs(knownResourceId);
240 statusCode = res2.getStatus();
242 if(logger.isDebugEnabled()){
243 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
245 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
248 AuthorityRefList list = res2.getEntity();
250 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
251 int numAuthRefsFound = items.size();
252 if(logger.isDebugEnabled()){
253 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
254 " authority references, found " + numAuthRefsFound);
256 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
257 "Did not find all expected authority references! " +
258 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
260 // Optionally output additional data about list members for debugging.
261 boolean iterateThroughList = true;
262 if(iterateThroughList && logger.isDebugEnabled()){
264 for(AuthorityRefList.AuthorityRefItem item : items){
265 logger.debug(testName + ": list-item[" + i + "] Field:" +
266 item.getSourceField() + "= " +
267 item.getAuthDisplayName() +
268 item.getItemDisplayName());
269 logger.debug(testName + ": list-item[" + i + "] refName=" +
271 logger.debug(testName + ": list-item[" + i + "] URI=" +
279 // ---------------------------------------------------------------
280 // Cleanup of resources created during testing
281 // ---------------------------------------------------------------
284 * Deletes all resources created by tests, after all tests have been run.
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.
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 ...");
300 if (logger.isDebugEnabled()) {
301 logger.debug("Cleaning up temporary resources created for testing ...");
303 AcquisitionClient acquisitionClient = new AcquisitionClient();
304 for (String resourceId : acquisitionIdsCreated) {
305 // Note: Any non-success responses are ignored and not reported.
306 ClientResponse<Response> res = acquisitionClient.delete(resourceId);
307 res.releaseConnection();
309 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
310 // Delete persons before PersonAuth
311 for (String resourceId : personIdsCreated) {
312 // Note: Any non-success responses are ignored and not reported.
313 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
314 res.releaseConnection();
316 // Note: Any non-success response is ignored and not reported.
317 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
318 res.releaseConnection();
321 // ---------------------------------------------------------------
322 // Utility methods used by tests above
323 // ---------------------------------------------------------------
325 public String getServicePathComponent() {
326 return SERVICE_PATH_COMPONENT;
329 private MultipartOutput createAcquisitionInstance(
330 String accessionDate,
331 String acquisitionAuthorizer,
332 String acquisitionFundingSource,
333 String fieldCollector ) {
334 AcquisitionsCommon acquisition = new AcquisitionsCommon();
335 acquisition.setAccessionDate(accessionDate);
336 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
337 acquisition.setAcquisitionFundingSource(acquisitionFundingSource);
338 acquisition.setFieldCollector(fieldCollector);
339 MultipartOutput multipart = new MultipartOutput();
340 OutputPart commonPart =
341 multipart.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
342 AcquisitionClient acquisitionClient = new AcquisitionClient();
343 commonPart.getHeaders().add("label", acquisitionClient.getCommonPartName());
345 if(logger.isDebugEnabled()){
346 logger.debug("to be created, acquisition common");
347 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));