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.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;
41 import org.jboss.resteasy.client.ClientResponse;
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;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * AcquisitionAuthRefsTest, carries out tests against a
55 * deployed and running Acquisition Service.
57 * $LastChangedRevision: 1327 $
58 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60 public class AcquisitionAuthRefsTest extends BaseServiceTest {
62 private final Logger logger =
63 LoggerFactory.getLogger(AcquisitionAuthRefsTest.class);
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;
83 // ---------------------------------------------------------------
84 // CRUD tests : CREATE tests
85 // ---------------------------------------------------------------
87 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
88 public void createWithAuthRefs(String testName) throws Exception {
90 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
92 // Submit the request to the service and store the response.
93 String identifier = createIdentifier();
95 // Create all the person refs and entities
98 MultipartOutput multipart = createAcquisitionInstance(
100 acquisitionAuthorizerRefName,
101 acquisitionFundingSourceRefName,
102 fieldCollectorRefName );
104 ClientResponse<Response> res = acquisitionClient.create(multipart);
106 int statusCode = res.getStatus();
108 // Check the status code of the response: does it match
109 // the expected response(s)?
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);
117 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
118 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
119 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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);
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));
135 protected void createPersonRefs(){
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();
143 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
144 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
145 Assert.assertEquals(statusCode, CREATED_STATUS);
146 personAuthCSID = extractId(res);
148 acquisitionAuthorizerRefName = PersonAuthorityClientUtils.createPersonRefName(
149 authRefName, "Annie Authorizer", true);
150 personIdsCreated.add(createPerson("Annie", "Authorizer", acquisitionAuthorizerRefName));
152 acquisitionFundingSourceRefName = PersonAuthorityClientUtils.createPersonRefName(
153 authRefName, "Sammy Source", true);
154 personIdsCreated.add(createPerson("Sammy", "Source", acquisitionFundingSourceRefName));
157 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
158 authRefName, "Connie Collector", true);
159 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
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();
172 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174 Assert.assertEquals(statusCode, CREATED_STATUS);
175 return extractId(res);
179 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
180 dependsOnMethods = {"createWithAuthRefs"})
181 public void readAndCheckAuthRefs(String testName) throws Exception {
184 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
186 // Submit the request to the service and store the response.
187 ClientResponse<MultipartInput> res = acquisitionClient.read(knownResourceId);
188 int statusCode = res.getStatus();
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);
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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);
207 // Get the auth refs and check them
208 ClientResponse<AuthorityRefList> res2 = acquisitionClient.getAuthorityRefs(knownResourceId);
209 statusCode = res2.getStatus();
211 if(logger.isDebugEnabled()){
212 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
214 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
215 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
216 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
217 AuthorityRefList list = res2.getEntity();
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();
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=" +
232 logger.debug(testName + ": list-item[" + i + "] URI=" +
236 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
241 // ---------------------------------------------------------------
242 // Cleanup of resources created during testing
243 // ---------------------------------------------------------------
246 * Deletes all resources created by tests, after all tests have been run.
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.
253 // @AfterClass(alwaysRun=true)
254 public void cleanUp() {
255 if (logger.isDebugEnabled()) {
256 logger.debug("Cleaning up temporary resources created for testing ...");
258 // Note: Any non-success responses are ignored and not reported.
259 for (String resourceId : acquisitionIdsCreated) {
260 ClientResponse<Response> res = acquisitionClient.delete(resourceId);
262 // Delete persons before PersonAuth
263 for (String resourceId : personIdsCreated) {
264 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
266 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
269 // ---------------------------------------------------------------
270 // Utility methods used by tests above
271 // ---------------------------------------------------------------
273 public String getServicePathComponent() {
274 return SERVICE_PATH_COMPONENT;
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());
292 if(logger.isDebugEnabled()){
293 logger.debug("to be created, acquisition common");
294 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));