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();
158 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
159 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
160 ClientResponse<Response> res = personAuthClient.create(multipart);
161 int statusCode = res.getStatus();
163 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
164 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
165 Assert.assertEquals(statusCode, STATUS_CREATED);
166 personAuthCSID = extractId(res);
168 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
170 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
171 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
172 personIdsCreated.add(csid);
174 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
175 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
176 personIdsCreated.add(csid);
178 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
179 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
180 personIdsCreated.add(csid);
182 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
183 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
184 personIdsCreated.add(csid);
186 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
187 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
188 personIdsCreated.add(csid);
191 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
192 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
193 Map<String, String> personInfo = new HashMap<String,String>();
194 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
195 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
196 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
197 MultipartOutput multipart =
198 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
199 authRefName, personInfo, personAuthClient.getItemCommonPartName());
200 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
201 int statusCode = res.getStatus();
203 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
204 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
205 Assert.assertEquals(statusCode, STATUS_CREATED);
206 return extractId(res);
210 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
211 dependsOnMethods = {"createWithAuthRefs"})
212 public void readAndCheckAuthRefs(String testName) throws Exception {
214 if (logger.isDebugEnabled()) {
215 logger.debug(testBanner(testName, CLASS_NAME));
218 testSetup(STATUS_OK, ServiceRequestType.READ);
220 // Submit the request to the service and store the response.
221 IntakeClient intakeClient = new IntakeClient();
222 ClientResponse<MultipartInput> res = intakeClient.read(knownResourceId);
223 int statusCode = res.getStatus();
225 // Check the status code of the response: does it match
226 // the expected response(s)?
227 if(logger.isDebugEnabled()){
228 logger.debug(testName + ".read: status = " + statusCode);
230 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
231 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
232 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
234 MultipartInput input = (MultipartInput) res.getEntity();
235 IntakesCommon intake = (IntakesCommon) extractPart(input,
236 intakeClient.getCommonPartName(), IntakesCommon.class);
237 Assert.assertNotNull(intake);
238 // Check a couple of fields
239 Assert.assertEquals(intake.getCurrentOwner(), currentOwnerRefName);
240 Assert.assertEquals(intake.getInsurer(), insurerRefName);
242 // Get the auth refs and check them
243 ClientResponse<AuthorityRefList> res2 = intakeClient.getAuthorityRefs(knownResourceId);
244 statusCode = res2.getStatus();
246 if(logger.isDebugEnabled()){
247 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
249 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
250 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
251 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
252 AuthorityRefList list = res2.getEntity();
254 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
255 int numAuthRefsFound = items.size();
256 if(logger.isDebugEnabled()){
257 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
258 " authority references, found " + numAuthRefsFound);
260 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
261 "Did not find all expected authority references! " +
262 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
264 // Optionally output additional data about list members for debugging.
265 boolean iterateThroughList = true;
266 if(iterateThroughList && logger.isDebugEnabled()){
268 for(AuthorityRefList.AuthorityRefItem item : items){
269 logger.debug(testName + ": list-item[" + i + "] Field:" +
270 item.getSourceField() + "= " +
271 item.getAuthDisplayName() +
272 item.getItemDisplayName());
273 logger.debug(testName + ": list-item[" + i + "] refName=" +
275 logger.debug(testName + ": list-item[" + i + "] URI=" +
283 // ---------------------------------------------------------------
284 // Cleanup of resources created during testing
285 // ---------------------------------------------------------------
288 * Deletes all resources created by tests, after all tests have been run.
290 * This cleanup method will always be run, even if one or more tests fail.
291 * For this reason, it attempts to remove all resources created
292 * at any point during testing, even if some of those resources
293 * may be expected to be deleted by certain tests.
295 @AfterClass(alwaysRun=true)
296 public void cleanUp() {
297 String noTest = System.getProperty("noTestCleanup");
298 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
299 if (logger.isDebugEnabled()) {
300 logger.debug("Skipping Cleanup phase ...");
304 if (logger.isDebugEnabled()) {
305 logger.debug("Cleaning up temporary resources created for testing ...");
307 IntakeClient intakeClient = new IntakeClient();
308 // Note: Any non-success responses are ignored and not reported.
309 for (String resourceId : intakeIdsCreated) {
310 intakeClient.delete(resourceId).releaseConnection();
312 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
313 // Delete persons before PersonAuth
314 for (String resourceId : personIdsCreated) {
315 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
317 personAuthClient.delete(personAuthCSID).releaseConnection();
320 // ---------------------------------------------------------------
321 // Utility methods used by tests above
322 // ---------------------------------------------------------------
324 public String getServicePathComponent() {
325 return SERVICE_PATH_COMPONENT;
328 private MultipartOutput createIntakeInstance(String entryNumber,
332 String conditionCheckerAssessor,
335 IntakesCommon intake = new IntakesCommon();
336 intake.setEntryNumber(entryNumber);
337 intake.setEntryDate(entryDate);
338 intake.setCurrentOwner(currentOwner);
339 intake.setDepositor(depositor);
340 intake.setConditionCheckerAssessor(conditionCheckerAssessor);
341 intake.setInsurer(insurer);
342 intake.setValuer(Valuer);
343 MultipartOutput multipart = new MultipartOutput();
344 OutputPart commonPart =
345 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
346 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
348 if(logger.isDebugEnabled()){
349 logger.debug("to be created, intake common");
350 logger.debug(objectAsXmlString(intake, IntakesCommon.class));