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.CollectionSpaceClient;
35 import org.collectionspace.services.client.IntakeClient;
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.intake.IntakesCommon;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.jboss.resteasy.client.ClientResponse;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
46 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * IntakeAuthRefsTest, carries out tests against a
56 * deployed and running Intake Service.
58 * $LastChangedRevision: 1327 $
59 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
61 public class IntakeAuthRefsTest extends BaseServiceTest {
63 private final Logger logger =
64 LoggerFactory.getLogger(IntakeAuthRefsTest.class);
66 // Instance variables specific to this test.
67 final String SERVICE_PATH_COMPONENT = "intakes";
68 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
69 private String knownResourceId = null;
70 private List<String> intakeIdsCreated = new ArrayList();
71 private List<String> personIdsCreated = new ArrayList();
72 private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
73 private int OK_STATUS = Response.Status.OK.getStatusCode();
74 private String personAuthCSID = null;
75 private String currentOwnerRefName = null;
76 private String depositorRefName = null;
77 private String conditionCheckAssesorRefName = null;
78 private String insurerRefName = null;
79 private String fieldCollectorRefName = null;
80 private String valuerRefName = null;
81 private final int NUM_AUTH_REFS_EXPECTED = 6;
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 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
109 // Submit the request to the service and store the response.
110 String identifier = createIdentifier();
112 // Create all the person refs and entities
115 // Submit the request to the service and store the response.
116 IntakeClient intakeClient = new IntakeClient();
117 MultipartOutput multipart = createIntakeInstance(
118 "entryNumber-" + identifier,
119 "entryDate-" + identifier,
122 conditionCheckAssesorRefName,
124 fieldCollectorRefName,
126 ClientResponse<Response> res = intakeClient.create(multipart);
128 int statusCode = res.getStatus();
130 // Check the status code of the response: does it match
131 // the expected response(s)?
134 // Does it fall within the set of valid status codes?
135 // Does it exactly match the expected status code?
136 if(logger.isDebugEnabled()){
137 logger.debug(testName + ": status = " + statusCode);
139 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
140 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
141 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
143 // Store the ID returned from the first resource created
144 // for additional tests below.
145 if (knownResourceId == null){
146 knownResourceId = extractId(res);
147 if (logger.isDebugEnabled()) {
148 logger.debug(testName + ": knownResourceId=" + knownResourceId);
152 // Store the IDs from every resource created by tests,
153 // so they can be deleted after tests have been run.
154 intakeIdsCreated.add(extractId(res));
157 protected void createPersonRefs(){
158 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
160 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
161 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
162 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
163 ClientResponse<Response> res = personAuthClient.create(multipart);
164 int statusCode = res.getStatus();
166 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
167 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
168 Assert.assertEquals(statusCode, CREATED_STATUS);
169 personAuthCSID = extractId(res);
171 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
172 authRefName, "Olivier Owner", true);
173 personIdsCreated.add(createPerson("Olivier", "Owner", currentOwnerRefName));
175 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
176 authRefName, "Debbie Depositor", true);
177 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
179 conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
180 authRefName, "Andrew Assessor", true);
181 personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
183 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
184 authRefName, "Ingrid Insurer", true);
185 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
187 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
188 authRefName, "Connie Collector", true);
189 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
191 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
192 authRefName, "Vince Valuer", true);
193 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
198 protected String createPerson(String firstName, String surName, String refName ) {
199 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
200 Map<String, String> personInfo = new HashMap<String,String>();
201 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
202 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
203 MultipartOutput multipart =
204 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
205 refName, personInfo, personAuthClient.getItemCommonPartName());
206 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
207 int statusCode = res.getStatus();
209 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
210 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
211 Assert.assertEquals(statusCode, CREATED_STATUS);
212 return extractId(res);
216 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
217 dependsOnMethods = {"createWithAuthRefs"})
218 public void readAndCheckAuthRefs(String testName) throws Exception {
221 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
223 // Submit the request to the service and store the response.
224 IntakeClient intakeClient = new IntakeClient();
225 ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
226 int statusCode = res.getStatus();
228 // Check the status code of the response: does it match
229 // the expected response(s)?
230 if(logger.isDebugEnabled()){
231 logger.debug(testName + ".read: status = " + statusCode);
233 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
234 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
235 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
237 MultipartInput input = (MultipartInput) res.getEntity();
238 IntakesCommon intake = (IntakesCommon) extractPart(input,
239 intakeClient.getCommonPartName(), IntakesCommon.class);
240 Assert.assertNotNull(intake);
241 // Check a couple of fields
242 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
243 Assert.assertEquals(intake.getInsurer(), insurerRefName);
245 // Get the auth refs and check them
246 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
247 statusCode = res2.getStatus();
249 if(logger.isDebugEnabled()){
250 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
252 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
253 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
254 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
255 AuthorityRefList list = res2.getEntity();
257 // Optionally output additional data about list members for debugging.
258 boolean iterateThroughList = true;
259 if(iterateThroughList && logger.isDebugEnabled()){
260 List<AuthorityRefList.AuthorityRefItem> items =
261 list.getAuthorityRefItem();
263 for(AuthorityRefList.AuthorityRefItem item : items){
264 logger.debug(testName + ": list-item[" + i + "] Field:" +
265 item.getSourceField() + "= " +
266 item.getAuthDisplayName() +
267 item.getItemDisplayName());
268 logger.debug(testName + ": list-item[" + i + "] refName=" +
270 logger.debug(testName + ": list-item[" + i + "] URI=" +
274 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
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 IntakeClient intakeClient = new IntakeClient();
304 // Note: Any non-success responses are ignored and not reported.
305 for (String resourceId : intakeIdsCreated) {
306 ClientResponse<Response> res = intakeClient.delete(resourceId);
308 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
309 // Delete persons before PersonAuth
310 for (String resourceId : personIdsCreated) {
311 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
313 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
316 // ---------------------------------------------------------------
317 // Utility methods used by tests above
318 // ---------------------------------------------------------------
320 public String getServicePathComponent() {
321 return SERVICE_PATH_COMPONENT;
324 private MultipartOutput createIntakeInstance(String entryNumber,
328 String conditionCheckAssesor,
330 String fieldCollector,
332 IntakesCommon intake = new IntakesCommon();
333 intake.setEntryNumber(entryNumber);
334 intake.setEntryDate(entryDate);
335 intake.setCurrentOwner(currentOwner);
336 intake.setDepositor(depositor);
337 intake.setConditionCheckAssesor(conditionCheckAssesor);
338 intake.setInsurer(insurer);
339 intake.setFieldCollector(fieldCollector);
340 intake.setValuer(Valuer);
341 MultipartOutput multipart = new MultipartOutput();
342 OutputPart commonPart =
343 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
344 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
346 if(logger.isDebugEnabled()){
347 logger.debug("to be created, intake common");
348 logger.debug(objectAsXmlString(intake, IntakesCommon.class));