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 String CLASS_NAME = IntakeAuthRefsTest.class.getName();
64 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
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<String>();
71 private List<String> personIdsCreated = new ArrayList<String>();
72 private String personAuthCSID = null;
73 private String currentOwnerRefName = null;
74 private String depositorRefName = null;
75 private String conditionCheckAssesorRefName = null;
76 private String insurerRefName = null;
77 private String fieldCollectorRefName = null;
78 private String valuerRefName = null;
79 private final int NUM_AUTH_REFS_EXPECTED = 6;
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
85 protected CollectionSpaceClient getClientInstance() {
86 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
90 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
93 protected AbstractCommonList getAbstractCommonList(
94 ClientResponse<AbstractCommonList> response) {
95 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
98 // ---------------------------------------------------------------
99 // CRUD tests : CREATE tests
100 // ---------------------------------------------------------------
102 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
103 public void createWithAuthRefs(String testName) throws Exception {
105 if (logger.isDebugEnabled()) {
106 logger.debug(testBanner(testName, CLASS_NAME));
108 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
110 // Submit the request to the service and store the response.
111 String identifier = createIdentifier();
113 // Create all the person refs and entities
116 // Submit the request to the service and store the response.
117 IntakeClient intakeClient = new IntakeClient();
118 MultipartOutput multipart = createIntakeInstance(
119 "entryNumber-" + identifier,
120 "entryDate-" + identifier,
123 conditionCheckAssesorRefName,
125 fieldCollectorRefName,
127 ClientResponse<Response> res = intakeClient.create(multipart);
129 int statusCode = res.getStatus();
131 // Check the status code of the response: does it match
132 // the expected response(s)?
135 // Does it fall within the set of valid status codes?
136 // Does it exactly match the expected status code?
137 if(logger.isDebugEnabled()){
138 logger.debug(testName + ": status = " + statusCode);
140 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
141 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
142 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
144 // Store the ID returned from the first resource created
145 // for additional tests below.
146 if (knownResourceId == null){
147 knownResourceId = extractId(res);
148 if (logger.isDebugEnabled()) {
149 logger.debug(testName + ": knownResourceId=" + knownResourceId);
153 // Store the IDs from every resource created by tests,
154 // so they can be deleted after tests have been run.
155 intakeIdsCreated.add(extractId(res));
158 protected void createPersonRefs(){
159 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
161 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
162 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
163 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
164 ClientResponse<Response> res = personAuthClient.create(multipart);
165 int statusCode = res.getStatus();
167 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
168 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
169 Assert.assertEquals(statusCode, STATUS_CREATED);
170 personAuthCSID = extractId(res);
172 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
173 authRefName, "Olivier Owner", true);
174 personIdsCreated.add(createPerson("Olivier", "Owner", currentOwnerRefName));
176 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
177 authRefName, "Debbie Depositor", true);
178 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
180 conditionCheckAssesorRefName = PersonAuthorityClientUtils.createPersonRefName(
181 authRefName, "Andrew Assessor", true);
182 personIdsCreated.add(createPerson("Andrew", "Assessor", conditionCheckAssesorRefName));
184 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
185 authRefName, "Ingrid Insurer", true);
186 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
188 fieldCollectorRefName = PersonAuthorityClientUtils.createPersonRefName(
189 authRefName, "Connie Collector", true);
190 personIdsCreated.add(createPerson("Connie", "Collector", fieldCollectorRefName));
192 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
193 authRefName, "Vince Valuer", true);
194 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
199 protected String createPerson(String firstName, String surName, String refName ) {
200 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
201 Map<String, String> personInfo = new HashMap<String,String>();
202 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
203 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
204 MultipartOutput multipart =
205 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
206 refName, personInfo, personAuthClient.getItemCommonPartName());
207 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
208 int statusCode = res.getStatus();
210 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
211 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
212 Assert.assertEquals(statusCode, STATUS_CREATED);
213 return extractId(res);
217 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
218 dependsOnMethods = {"createWithAuthRefs"})
219 public void readAndCheckAuthRefs(String testName) throws Exception {
221 if (logger.isDebugEnabled()) {
222 logger.debug(testBanner(testName, CLASS_NAME));
225 testSetup(STATUS_OK, ServiceRequestType.READ);
227 // Submit the request to the service and store the response.
228 IntakeClient intakeClient = new IntakeClient();
229 ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
230 int statusCode = res.getStatus();
232 // Check the status code of the response: does it match
233 // the expected response(s)?
234 if(logger.isDebugEnabled()){
235 logger.debug(testName + ".read: status = " + statusCode);
237 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
238 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
239 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
241 MultipartInput input = (MultipartInput) res.getEntity();
242 IntakesCommon intake = (IntakesCommon) extractPart(input,
243 intakeClient.getCommonPartName(), IntakesCommon.class);
244 Assert.assertNotNull(intake);
245 // Check a couple of fields
246 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
247 Assert.assertEquals(intake.getInsurer(), insurerRefName);
249 // Get the auth refs and check them
250 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
251 statusCode = res2.getStatus();
253 if(logger.isDebugEnabled()){
254 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
256 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
257 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
258 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
259 AuthorityRefList list = res2.getEntity();
261 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
262 int numAuthRefsFound = items.size();
263 if(logger.isDebugEnabled()){
264 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
265 " authority references, found " + numAuthRefsFound);
267 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
268 "Did not find all expected authority references! " +
269 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
271 // Optionally output additional data about list members for debugging.
272 boolean iterateThroughList = true;
273 if(iterateThroughList && logger.isDebugEnabled()){
275 for(AuthorityRefList.AuthorityRefItem item : items){
276 logger.debug(testName + ": list-item[" + i + "] Field:" +
277 item.getSourceField() + "= " +
278 item.getAuthDisplayName() +
279 item.getItemDisplayName());
280 logger.debug(testName + ": list-item[" + i + "] refName=" +
282 logger.debug(testName + ": list-item[" + i + "] URI=" +
290 // ---------------------------------------------------------------
291 // Cleanup of resources created during testing
292 // ---------------------------------------------------------------
295 * Deletes all resources created by tests, after all tests have been run.
297 * This cleanup method will always be run, even if one or more tests fail.
298 * For this reason, it attempts to remove all resources created
299 * at any point during testing, even if some of those resources
300 * may be expected to be deleted by certain tests.
302 @AfterClass(alwaysRun=true)
303 public void cleanUp() {
304 String noTest = System.getProperty("noTestCleanup");
305 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
306 if (logger.isDebugEnabled()) {
307 logger.debug("Skipping Cleanup phase ...");
311 if (logger.isDebugEnabled()) {
312 logger.debug("Cleaning up temporary resources created for testing ...");
314 IntakeClient intakeClient = new IntakeClient();
315 // Note: Any non-success responses are ignored and not reported.
316 for (String resourceId : intakeIdsCreated) {
317 intakeClient.delete(resourceId).releaseConnection();
319 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
320 // Delete persons before PersonAuth
321 for (String resourceId : personIdsCreated) {
322 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
324 personAuthClient.delete(personAuthCSID).releaseConnection();
327 // ---------------------------------------------------------------
328 // Utility methods used by tests above
329 // ---------------------------------------------------------------
331 public String getServicePathComponent() {
332 return SERVICE_PATH_COMPONENT;
335 private MultipartOutput createIntakeInstance(String entryNumber,
339 String conditionCheckAssesor,
341 String fieldCollector,
343 IntakesCommon intake = new IntakesCommon();
344 intake.setEntryNumber(entryNumber);
345 intake.setEntryDate(entryDate);
346 intake.setCurrentOwner(currentOwner);
347 intake.setDepositor(depositor);
348 intake.setConditionCheckAssesor(conditionCheckAssesor);
349 intake.setInsurer(insurer);
350 intake.setFieldCollector(fieldCollector);
351 intake.setValuer(Valuer);
352 MultipartOutput multipart = new MultipartOutput();
353 OutputPart commonPart =
354 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
355 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
357 if(logger.isDebugEnabled()){
358 logger.debug("to be created, intake common");
359 logger.debug(objectAsXmlString(intake, IntakesCommon.class));