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.IntakeClient;
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.intake.IntakesCommon;
39 import org.collectionspace.services.intake.IntakesCommonList;
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 * IntakeAuthRefsTest, carries out tests against a
55 * deployed and running Intake Service.
57 * $LastChangedRevision: 1327 $
58 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60 public class IntakeAuthRefsTest extends BaseServiceTest {
62 private final Logger logger =
63 LoggerFactory.getLogger(IntakeAuthRefsTest.class);
65 // Instance variables specific to this test.
66 private IntakeClient intakeClient = new IntakeClient();
67 private PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
68 final String SERVICE_PATH_COMPONENT = "intakes";
69 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
70 private String knownResourceId = null;
71 private List<String> intakeIdsCreated = 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 currentOwnerRefName = null;
77 private String depositorRefName = null;
78 private String conditionCheckAssesorRefName = null;
79 private String insurerRefName = null;
80 private String fieldCollectorRefName = null;
81 private String valuerRefName = null;
82 private final int NUM_AUTH_REFS_EXPECTED = 6;
84 // ---------------------------------------------------------------
85 // CRUD tests : CREATE tests
86 // ---------------------------------------------------------------
88 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
89 public void createWithAuthRefs(String testName) throws Exception {
91 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
93 // Submit the request to the service and store the response.
94 String identifier = createIdentifier();
96 // Create all the person refs and entities
99 MultipartOutput multipart = createIntakeInstance(
100 "entryNumber-" + identifier,
101 "entryDate-" + identifier,
104 conditionCheckAssesorRefName,
106 fieldCollectorRefName,
109 ClientResponse<Response> res = intakeClient.create(multipart);
111 int statusCode = res.getStatus();
113 // Check the status code of the response: does it match
114 // the expected response(s)?
117 // Does it fall within the set of valid status codes?
118 // Does it exactly match the expected status code?
119 if(logger.isDebugEnabled()){
120 logger.debug(testName + ": status = " + statusCode);
122 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
123 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
124 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
126 // Store the ID returned from the first resource created
127 // for additional tests below.
128 if (knownResourceId == null){
129 knownResourceId = extractId(res);
130 if (logger.isDebugEnabled()) {
131 logger.debug(testName + ": knownResourceId=" + knownResourceId);
135 // Store the IDs from every resource created by tests,
136 // so they can be deleted after tests have been run.
137 intakeIdsCreated.add(extractId(res));
140 protected void createPersonRefs(){
142 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
143 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
144 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
145 ClientResponse<Response> res = personAuthClient.create(multipart);
146 int statusCode = res.getStatus();
148 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
149 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
150 Assert.assertEquals(statusCode, CREATED_STATUS);
151 personAuthCSID = extractId(res);
153 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
154 authRefName, "Olivier Owner", true);
155 personIdsCreated.add(createPerson("Olivier", "Owner", currentOwnerRefName));
157 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
158 authRefName, "Debbie Depositor", true);
159 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
161 conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
162 authRefName, "Andrew Assessor", true);
163 personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
165 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
166 authRefName, "Ingrid Insurer", true);
167 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
169 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
170 authRefName, "Connie Collector", true);
171 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
173 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
174 authRefName, "Vince Valuer", true);
175 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
180 protected String createPerson(String firstName, String surName, String refName ) {
181 Map<String, String> personInfo = new HashMap<String,String>();
182 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
183 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
184 MultipartOutput multipart =
185 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
186 refName, personInfo, personAuthClient.getItemCommonPartName());
187 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
188 int statusCode = res.getStatus();
190 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
191 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
192 Assert.assertEquals(statusCode, CREATED_STATUS);
193 return extractId(res);
197 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
198 dependsOnMethods = {"createWithAuthRefs"})
199 public void readAndCheckAuthRefs(String testName) throws Exception {
202 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
204 // Submit the request to the service and store the response.
205 ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
206 int statusCode = res.getStatus();
208 // Check the status code of the response: does it match
209 // the expected response(s)?
210 if(logger.isDebugEnabled()){
211 logger.debug(testName + ".read: status = " + statusCode);
213 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
214 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
215 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
217 MultipartInput input = (MultipartInput) res.getEntity();
218 IntakesCommon intake = (IntakesCommon) extractPart(input,
219 intakeClient.getCommonPartName(), IntakesCommon.class);
220 Assert.assertNotNull(intake);
221 // Check a couple of fields
222 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
223 Assert.assertEquals(intake.getInsurer(), insurerRefName);
225 // Get the auth refs and check them
226 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
227 statusCode = res2.getStatus();
229 if(logger.isDebugEnabled()){
230 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
232 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
235 AuthorityRefList list = res2.getEntity();
237 // Optionally output additional data about list members for debugging.
238 boolean iterateThroughList = true;
239 if(iterateThroughList && logger.isDebugEnabled()){
240 List<AuthorityRefList.AuthorityRefItem> items =
241 list.getAuthorityRefItem();
243 for(AuthorityRefList.AuthorityRefItem item : items){
244 logger.debug(testName + ": list-item[" + i + "] Field:" +
245 item.getSourceField() + "= " +
246 item.getAuthDisplayName() +
247 item.getItemDisplayName());
248 logger.debug(testName + ": list-item[" + i + "] refName=" +
250 logger.debug(testName + ": list-item[" + i + "] URI=" +
254 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
259 // ---------------------------------------------------------------
260 // Cleanup of resources created during testing
261 // ---------------------------------------------------------------
264 * Deletes all resources created by tests, after all tests have been run.
266 * This cleanup method will always be run, even if one or more tests fail.
267 * For this reason, it attempts to remove all resources created
268 * at any point during testing, even if some of those resources
269 * may be expected to be deleted by certain tests.
271 @AfterClass(alwaysRun=true)
272 public void cleanUp() {
273 if (logger.isDebugEnabled()) {
274 logger.debug("Cleaning up temporary resources created for testing ...");
276 // Note: Any non-success responses are ignored and not reported.
277 for (String resourceId : intakeIdsCreated) {
278 ClientResponse<Response> res = intakeClient.delete(resourceId);
280 // Delete persons before PersonAuth
281 for (String resourceId : personIdsCreated) {
282 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
284 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
287 // ---------------------------------------------------------------
288 // Utility methods used by tests above
289 // ---------------------------------------------------------------
291 public String getServicePathComponent() {
292 return SERVICE_PATH_COMPONENT;
295 private MultipartOutput createIntakeInstance(String entryNumber,
299 String conditionCheckAssesor,
301 String fieldCollector,
303 IntakesCommon intake = new IntakesCommon();
304 intake.setEntryNumber(entryNumber);
305 intake.setEntryDate(entryDate);
306 intake.setCurrentOwner(currentOwner);
307 intake.setDepositor(depositor);
308 intake.setConditionCheckAssesor(conditionCheckAssesor);
309 intake.setInsurer(insurer);
310 intake.setFieldCollector(fieldCollector);
311 intake.setValuer(Valuer);
312 MultipartOutput multipart = new MultipartOutput();
313 OutputPart commonPart =
314 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
315 commonPart.getHeaders().add("label", intakeClient.getCommonPartName());
317 if(logger.isDebugEnabled()){
318 logger.debug("to be created, intake common");
319 logger.debug(objectAsXmlString(intake, IntakesCommon.class));