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 conditionCheckerAssessorRefName = null;
76 private String insurerRefName = null;
77 private String valuerRefName = null;
78 private final int NUM_AUTH_REFS_EXPECTED = 5;
81 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
84 protected CollectionSpaceClient getClientInstance() {
85 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
89 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
92 protected AbstractCommonList getAbstractCommonList(
93 ClientResponse<AbstractCommonList> response) {
94 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
97 // ---------------------------------------------------------------
98 // CRUD tests : CREATE tests
99 // ---------------------------------------------------------------
101 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
102 public void createWithAuthRefs(String testName) throws Exception {
104 if (logger.isDebugEnabled()) {
105 logger.debug(testBanner(testName, CLASS_NAME));
107 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
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 conditionCheckerAssessorRefName,
125 ClientResponse<Response> res = intakeClient.create(multipart);
127 int statusCode = res.getStatus();
129 // Check the status code of the response: does it match
130 // the expected response(s)?
133 // Does it fall within the set of valid status codes?
134 // Does it exactly match the expected status code?
135 if(logger.isDebugEnabled()){
136 logger.debug(testName + ": status = " + statusCode);
138 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
139 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
140 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
142 // Store the ID returned from the first resource created
143 // for additional tests below.
144 if (knownResourceId == null){
145 knownResourceId = extractId(res);
146 if (logger.isDebugEnabled()) {
147 logger.debug(testName + ": knownResourceId=" + knownResourceId);
151 // Store the IDs from every resource created by tests,
152 // so they can be deleted after tests have been run.
153 intakeIdsCreated.add(extractId(res));
156 protected void createPersonRefs(){
157 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
159 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
160 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
161 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
162 ClientResponse<Response> res = personAuthClient.create(multipart);
163 int statusCode = res.getStatus();
165 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
166 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
167 Assert.assertEquals(statusCode, STATUS_CREATED);
168 personAuthCSID = extractId(res);
170 currentOwnerRefName = PersonAuthorityClientUtils.createPersonRefName(
171 authRefName, "Olivier Owner", true);
172 personIdsCreated.add(createPerson("Olivier", "Owner", currentOwnerRefName));
174 depositorRefName = PersonAuthorityClientUtils.createPersonRefName(
175 authRefName, "Debbie Depositor", true);
176 personIdsCreated.add(createPerson("Debbie", "Depositor", depositorRefName));
178 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.createPersonRefName(
179 authRefName, "Andrew Checker-Assessor", true);
180 personIdsCreated.add(createPerson("Andrew", "Checker-Assessor", conditionCheckerAssessorRefName));
182 insurerRefName = PersonAuthorityClientUtils.createPersonRefName(
183 authRefName, "Ingrid Insurer", true);
184 personIdsCreated.add(createPerson("Ingrid", "Insurer", insurerRefName));
186 valuerRefName = PersonAuthorityClientUtils.createPersonRefName(
187 authRefName, "Vince Valuer", true);
188 personIdsCreated.add(createPerson("Vince", "Valuer", valuerRefName));
193 protected String createPerson(String firstName, String surName, String refName ) {
194 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
195 Map<String, String> personInfo = new HashMap<String,String>();
196 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
197 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
198 MultipartOutput multipart =
199 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
200 refName, personInfo, personAuthClient.getItemCommonPartName());
201 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
202 int statusCode = res.getStatus();
204 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
205 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
206 Assert.assertEquals(statusCode, STATUS_CREATED);
207 return extractId(res);
211 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
212 dependsOnMethods = {"createWithAuthRefs"})
213 public void readAndCheckAuthRefs(String testName) throws Exception {
215 if (logger.isDebugEnabled()) {
216 logger.debug(testBanner(testName, CLASS_NAME));
219 testSetup(STATUS_OK, ServiceRequestType.READ);
221 // Submit the request to the service and store the response.
222 IntakeClient intakeClient = new IntakeClient();
223 ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
224 int statusCode = res.getStatus();
226 // Check the status code of the response: does it match
227 // the expected response(s)?
228 if(logger.isDebugEnabled()){
229 logger.debug(testName + ".read: status = " + statusCode);
231 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
232 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
233 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
235 MultipartInput input = (MultipartInput) res.getEntity();
236 IntakesCommon intake = (IntakesCommon) extractPart(input,
237 intakeClient.getCommonPartName(), IntakesCommon.class);
238 Assert.assertNotNull(intake);
239 // Check a couple of fields
240 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
241 Assert.assertEquals(intake.getInsurer(), insurerRefName);
243 // Get the auth refs and check them
244 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
245 statusCode = res2.getStatus();
247 if(logger.isDebugEnabled()){
248 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
250 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
251 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
252 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
253 AuthorityRefList list = res2.getEntity();
255 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
256 int numAuthRefsFound = items.size();
257 if(logger.isDebugEnabled()){
258 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
259 " authority references, found " + numAuthRefsFound);
261 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
262 "Did not find all expected authority references! " +
263 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
265 // Optionally output additional data about list members for debugging.
266 boolean iterateThroughList = true;
267 if(iterateThroughList && logger.isDebugEnabled()){
269 for(AuthorityRefList.AuthorityRefItem item : items){
270 logger.debug(testName + ": list-item[" + i + "] Field:" +
271 item.getSourceField() + "= " +
272 item.getAuthDisplayName() +
273 item.getItemDisplayName());
274 logger.debug(testName + ": list-item[" + i + "] refName=" +
276 logger.debug(testName + ": list-item[" + i + "] URI=" +
284 // ---------------------------------------------------------------
285 // Cleanup of resources created during testing
286 // ---------------------------------------------------------------
289 * Deletes all resources created by tests, after all tests have been run.
291 * This cleanup method will always be run, even if one or more tests fail.
292 * For this reason, it attempts to remove all resources created
293 * at any point during testing, even if some of those resources
294 * may be expected to be deleted by certain tests.
296 @AfterClass(alwaysRun=true)
297 public void cleanUp() {
298 String noTest = System.getProperty("noTestCleanup");
299 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
300 if (logger.isDebugEnabled()) {
301 logger.debug("Skipping Cleanup phase ...");
305 if (logger.isDebugEnabled()) {
306 logger.debug("Cleaning up temporary resources created for testing ...");
308 IntakeClient intakeClient = new IntakeClient();
309 // Note: Any non-success responses are ignored and not reported.
310 for (String resourceId : intakeIdsCreated) {
311 intakeClient.delete(resourceId).releaseConnection();
313 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
314 // Delete persons before PersonAuth
315 for (String resourceId : personIdsCreated) {
316 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
318 personAuthClient.delete(personAuthCSID).releaseConnection();
321 // ---------------------------------------------------------------
322 // Utility methods used by tests above
323 // ---------------------------------------------------------------
325 public String getServicePathComponent() {
326 return SERVICE_PATH_COMPONENT;
329 private MultipartOutput createIntakeInstance(String entryNumber,
333 String conditionCheckerAssessor,
337 IntakesCommon intake = new IntakesCommon();
338 intake.setEntryNumber(entryNumber);
339 intake.setEntryDate(entryDate);
340 intake.setCurrentOwner(currentOwner);
341 intake.setDepositor(depositor);
342 intake.setConditionCheckerAssessor(conditionCheckerAssessor);
343 intake.setInsurer(insurer);
344 intake.setValuer(Valuer);
345 MultipartOutput multipart = new MultipartOutput();
346 OutputPart commonPart =
347 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
348 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
350 if(logger.isDebugEnabled()){
351 logger.debug("to be created, intake common");
352 logger.debug(objectAsXmlString(intake, IntakesCommon.class));