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 String personAuthCSID = null;
74 private String acquisitionAuthorizerRefName = null;
75 private String acquisitionFundingSourceRefName = null;
76 // Not ready for multiples, yet
77 //private String acquisitionSourcesRefName = null;
78 private final int NUM_AUTH_REFS_EXPECTED = 2;
81 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
84 protected CollectionSpaceClient getClientInstance() {
85 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
89 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
92 protected AbstractCommonList getAbstractCommonList(
93 ClientResponse<AbstractCommonList> response) {
94 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
97 // ---------------------------------------------------------------
98 // CRUD tests : CREATE tests
99 // ---------------------------------------------------------------
101 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
102 public void createWithAuthRefs(String testName) throws Exception {
104 if (logger.isDebugEnabled()) {
105 logger.debug(testBanner(testName, CLASS_NAME));
109 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
111 // Submit the request to the service and store the response.
112 String identifier = createIdentifier();
114 // Create all the person refs and entities
117 MultipartOutput multipart = createAcquisitionInstance(
119 acquisitionAuthorizerRefName,
120 acquisitionFundingSourceRefName);
122 AcquisitionClient acquisitionClient = new AcquisitionClient();
123 ClientResponse<Response> res = acquisitionClient.create(multipart);
125 int statusCode = res.getStatus();
127 // Check the status code of the response: does it match
128 // the expected response(s)?
131 // Does it fall within the set of valid status codes?
132 // Does it exactly match the expected status code?
133 if(logger.isDebugEnabled()){
134 logger.debug(testName + ": status = " + statusCode);
136 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
137 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
138 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
140 // Store the ID returned from the first resource created
141 // for additional tests below.
142 if (knownResourceId == null){
143 knownResourceId = extractId(res);
144 if (logger.isDebugEnabled()) {
145 logger.debug(testName + ": knownResourceId=" + knownResourceId);
149 // Store the IDs from every resource created by tests,
150 // so they can be deleted after tests have been run.
151 acquisitionIdsCreated.add(extractId(res));
154 protected void createPersonRefs(){
156 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
157 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
158 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
159 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
160 ClientResponse<Response> res = personAuthClient.create(multipart);
161 int statusCode = res.getStatus();
163 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
164 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
165 Assert.assertEquals(statusCode, STATUS_CREATED);
166 personAuthCSID = extractId(res);
168 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.createPersonRefName(
169 authRefName, "Annie Authorizer", true);
170 personIdsCreated.add(createPerson("Annie", "Authorizer", acquisitionAuthorizerRefName));
172 acquisitionFundingSourceRefName = PersonAuthorityClientUtils.createPersonRefName(
173 authRefName, "Sammy Source", true);
174 personIdsCreated.add(createPerson("Sammy", "Source", acquisitionFundingSourceRefName));
177 protected String createPerson(String firstName, String surName, String refName ) {
178 Map<String, String> personInfo = new HashMap<String,String>();
179 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
180 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
181 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
182 MultipartOutput multipart =
183 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
184 refName, personInfo, personAuthClient.getItemCommonPartName());
185 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
186 int statusCode = res.getStatus();
188 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
189 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
190 Assert.assertEquals(statusCode, STATUS_CREATED);
191 return extractId(res);
195 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
196 dependsOnMethods = {"createWithAuthRefs"})
197 public void readAndCheckAuthRefs(String testName) throws Exception {
199 if (logger.isDebugEnabled()) {
200 logger.debug(testBanner(testName, CLASS_NAME));
204 testSetup(STATUS_OK, ServiceRequestType.READ);
206 // Submit the request to the service and store the response.
207 AcquisitionClient acquisitionClient = new AcquisitionClient();
208 ClientResponse<MultipartInput> res = acquisitionClient.read(knownResourceId);
209 int statusCode = res.getStatus();
211 // Check the status code of the response: does it match
212 // the expected response(s)?
213 if(logger.isDebugEnabled()){
214 logger.debug(testName + ".read: status = " + statusCode);
216 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
217 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
218 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
220 MultipartInput input = (MultipartInput) res.getEntity();
221 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
222 acquisitionClient.getCommonPartName(), AcquisitionsCommon.class);
223 Assert.assertNotNull(acquisition);
224 // Check a couple of fields
225 Assert.assertEquals(acquisition.getAcquisitionAuthorizer(), acquisitionAuthorizerRefName);
226 Assert.assertEquals(acquisition.getAcquisitionFundingSource(), acquisitionFundingSourceRefName);
228 // Get the auth refs and check them
229 ClientResponse<AuthorityRefList> res2 =
230 acquisitionClient.getAuthorityRefs(knownResourceId);
231 statusCode = res2.getStatus();
233 if(logger.isDebugEnabled()){
234 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
236 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
237 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
238 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
239 AuthorityRefList list = res2.getEntity();
241 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
242 int numAuthRefsFound = items.size();
243 if(logger.isDebugEnabled()){
244 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
245 " authority references, found " + numAuthRefsFound);
247 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
248 "Did not find all expected authority references! " +
249 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
251 // Optionally output additional data about list members for debugging.
252 boolean iterateThroughList = true;
253 if(iterateThroughList && logger.isDebugEnabled()){
255 for(AuthorityRefList.AuthorityRefItem item : items){
256 logger.debug(testName + ": list-item[" + i + "] Field:" +
257 item.getSourceField() + "= " +
258 item.getAuthDisplayName() +
259 item.getItemDisplayName());
260 logger.debug(testName + ": list-item[" + i + "] refName=" +
262 logger.debug(testName + ": list-item[" + i + "] URI=" +
270 // ---------------------------------------------------------------
271 // Cleanup of resources created during testing
272 // ---------------------------------------------------------------
275 * Deletes all resources created by tests, after all tests have been run.
277 * This cleanup method will always be run, even if one or more tests fail.
278 * For this reason, it attempts to remove all resources created
279 * at any point during testing, even if some of those resources
280 * may be expected to be deleted by certain tests.
282 @AfterClass(alwaysRun=true)
283 public void cleanUp() {
284 String noTest = System.getProperty("noTestCleanup");
285 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
286 if (logger.isDebugEnabled()) {
287 logger.debug("Skipping Cleanup phase ...");
291 if (logger.isDebugEnabled()) {
292 logger.debug("Cleaning up temporary resources created for testing ...");
294 AcquisitionClient acquisitionClient = new AcquisitionClient();
295 for (String resourceId : acquisitionIdsCreated) {
296 // Note: Any non-success responses are ignored and not reported.
297 ClientResponse<Response> res = acquisitionClient.delete(resourceId);
298 res.releaseConnection();
300 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
301 // Delete persons before PersonAuth
302 for (String resourceId : personIdsCreated) {
303 // Note: Any non-success responses are ignored and not reported.
304 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
305 res.releaseConnection();
307 // Note: Any non-success response is ignored and not reported.
308 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
309 res.releaseConnection();
312 // ---------------------------------------------------------------
313 // Utility methods used by tests above
314 // ---------------------------------------------------------------
316 public String getServicePathComponent() {
317 return SERVICE_PATH_COMPONENT;
320 private MultipartOutput createAcquisitionInstance(
321 String accessionDate,
322 String acquisitionAuthorizer,
323 String acquisitionFundingSource) {
325 AcquisitionsCommon acquisition = new AcquisitionsCommon();
326 acquisition.setAccessionDate(accessionDate);
327 acquisition.setAcquisitionAuthorizer(acquisitionAuthorizer);
328 acquisition.setAcquisitionFundingSource(acquisitionFundingSource);
329 MultipartOutput multipart = new MultipartOutput();
330 OutputPart commonPart =
331 multipart.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
332 AcquisitionClient acquisitionClient = new AcquisitionClient();
333 commonPart.getHeaders().add("label", acquisitionClient.getCommonPartName());
335 if(logger.isDebugEnabled()){
336 logger.debug("to be created, acquisition common");
337 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));