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 final String SERVICE_PATH_COMPONENT = "intakes";
67 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
68 private String knownResourceId = null;
69 private List<String> intakeIdsCreated = new ArrayList();
70 private List<String> personIdsCreated = new ArrayList();
71 private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
72 private int OK_STATUS = Response.Status.OK.getStatusCode();
73 private String personAuthCSID = null;
74 private String currentOwnerRefName = null;
75 private String depositorRefName = null;
76 private String conditionCheckAssesorRefName = null;
77 private String insurerRefName = null;
78 private String fieldCollectorRefName = null;
79 private String valuerRefName = null;
80 private final int NUM_AUTH_REFS_EXPECTED = 6;
82 // ---------------------------------------------------------------
83 // CRUD tests : CREATE tests
84 // ---------------------------------------------------------------
86 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
87 public void createWithAuthRefs(String testName) throws Exception {
89 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
91 // Submit the request to the service and store the response.
92 String identifier = createIdentifier();
94 // Create all the person refs and entities
97 // Submit the request to the service and store the response.
98 IntakeClient intakeClient = new IntakeClient();
99 MultipartOutput multipart = createIntakeInstance(
100 "entryNumber-" + identifier,
101 "entryDate-" + identifier,
104 conditionCheckAssesorRefName,
106 fieldCollectorRefName,
108 ClientResponse<Response> res = intakeClient.create(multipart);
110 int statusCode = res.getStatus();
112 // Check the status code of the response: does it match
113 // the expected response(s)?
116 // Does it fall within the set of valid status codes?
117 // Does it exactly match the expected status code?
118 if(logger.isDebugEnabled()){
119 logger.debug(testName + ": status = " + statusCode);
121 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
122 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
123 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
125 // Store the ID returned from the first resource created
126 // for additional tests below.
127 if (knownResourceId == null){
128 knownResourceId = extractId(res);
129 if (logger.isDebugEnabled()) {
130 logger.debug(testName + ": knownResourceId=" + knownResourceId);
134 // Store the IDs from every resource created by tests,
135 // so they can be deleted after tests have been run.
136 intakeIdsCreated.add(extractId(res));
139 protected void createPersonRefs(){
140 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
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 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
182 Map<String, String> personInfo = new HashMap<String,String>();
183 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
184 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
185 MultipartOutput multipart =
186 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
187 refName, personInfo, personAuthClient.getItemCommonPartName());
188 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
189 int statusCode = res.getStatus();
191 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
192 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
193 Assert.assertEquals(statusCode, CREATED_STATUS);
194 return extractId(res);
198 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
199 dependsOnMethods = {"createWithAuthRefs"})
200 public void readAndCheckAuthRefs(String testName) throws Exception {
203 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
205 // Submit the request to the service and store the response.
206 IntakeClient intakeClient = new IntakeClient();
207 ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
208 int statusCode = res.getStatus();
210 // Check the status code of the response: does it match
211 // the expected response(s)?
212 if(logger.isDebugEnabled()){
213 logger.debug(testName + ".read: status = " + statusCode);
215 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
216 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
217 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
219 MultipartInput input = (MultipartInput) res.getEntity();
220 IntakesCommon intake = (IntakesCommon) extractPart(input,
221 intakeClient.getCommonPartName(), IntakesCommon.class);
222 Assert.assertNotNull(intake);
223 // Check a couple of fields
224 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
225 Assert.assertEquals(intake.getInsurer(), insurerRefName);
227 // Get the auth refs and check them
228 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
229 statusCode = res2.getStatus();
231 if(logger.isDebugEnabled()){
232 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
234 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
235 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
236 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
237 AuthorityRefList list = res2.getEntity();
239 // Optionally output additional data about list members for debugging.
240 boolean iterateThroughList = true;
241 if(iterateThroughList && logger.isDebugEnabled()){
242 List<AuthorityRefList.AuthorityRefItem> items =
243 list.getAuthorityRefItem();
245 for(AuthorityRefList.AuthorityRefItem item : items){
246 logger.debug(testName + ": list-item[" + i + "] Field:" +
247 item.getSourceField() + "= " +
248 item.getAuthDisplayName() +
249 item.getItemDisplayName());
250 logger.debug(testName + ": list-item[" + i + "] refName=" +
252 logger.debug(testName + ": list-item[" + i + "] URI=" +
256 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
261 // ---------------------------------------------------------------
262 // Cleanup of resources created during testing
263 // ---------------------------------------------------------------
266 * Deletes all resources created by tests, after all tests have been run.
268 * This cleanup method will always be run, even if one or more tests fail.
269 * For this reason, it attempts to remove all resources created
270 * at any point during testing, even if some of those resources
271 * may be expected to be deleted by certain tests.
273 @AfterClass(alwaysRun=true)
274 public void cleanUp() {
275 if (logger.isDebugEnabled()) {
276 logger.debug("Cleaning up temporary resources created for testing ...");
278 IntakeClient intakeClient = new IntakeClient();
279 // Note: Any non-success responses are ignored and not reported.
280 for (String resourceId : intakeIdsCreated) {
281 ClientResponse<Response> res = intakeClient.delete(resourceId);
283 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
284 // Delete persons before PersonAuth
285 for (String resourceId : personIdsCreated) {
286 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
288 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
291 // ---------------------------------------------------------------
292 // Utility methods used by tests above
293 // ---------------------------------------------------------------
295 public String getServicePathComponent() {
296 return SERVICE_PATH_COMPONENT;
299 private MultipartOutput createIntakeInstance(String entryNumber,
303 String conditionCheckAssesor,
305 String fieldCollector,
307 IntakesCommon intake = new IntakesCommon();
308 intake.setEntryNumber(entryNumber);
309 intake.setEntryDate(entryDate);
310 intake.setCurrentOwner(currentOwner);
311 intake.setDepositor(depositor);
312 intake.setConditionCheckAssesor(conditionCheckAssesor);
313 intake.setInsurer(insurer);
314 intake.setFieldCollector(fieldCollector);
315 intake.setValuer(Valuer);
316 MultipartOutput multipart = new MultipartOutput();
317 OutputPart commonPart =
318 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
319 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
321 if(logger.isDebugEnabled()){
322 logger.debug("to be created, intake common");
323 logger.debug(objectAsXmlString(intake, IntakesCommon.class));