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.AuthorityRefDocList;
39 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
40 import org.collectionspace.services.intake.IntakesCommon;
41 import org.collectionspace.services.intake.InsurerList;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
44 import org.jboss.resteasy.client.ClientResponse;
46 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
48 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * PersonAuthRefDocsTest, carries out tests against a
58 * deployed and running Person Service.
60 * $LastChangedRevision: 1327 $
61 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
63 public class PersonAuthRefDocsTest extends BaseServiceTest {
65 private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
66 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
67 // Instance variables specific to this test.
68 final String SERVICE_PATH_COMPONENT = "intakes";
69 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
70 private String knownIntakeId = null;
71 private List<String> intakeIdsCreated = new ArrayList<String>();
72 private List<String> personIdsCreated = new ArrayList<String>();
73 private String personAuthCSID = null;
74 private String currentOwnerPersonCSID = null;
75 private String depositorPersonCSID = null;
76 private String insurerPersonCSID = null;
77 private String currentOwnerRefName = null;
78 private String depositorRefName = null;
79 private String conditionCheckerAssessorRefName = null;
80 private String insurerRefName = null;
81 private String valuerRefName = null;
82 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
85 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
88 protected CollectionSpaceClient getClientInstance() {
89 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
93 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
96 protected AbstractCommonList getAbstractCommonList(
97 ClientResponse<AbstractCommonList> response) {
98 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
101 // ---------------------------------------------------------------
102 // CRUD tests : CREATE tests
103 // ---------------------------------------------------------------
105 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
106 public void createIntakeWithAuthRefs(String testName) throws Exception {
108 if (logger.isDebugEnabled()) {
109 logger.debug(testBanner(testName, CLASS_NAME));
111 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
113 // Submit the request to the service and store the response.
114 String identifier = createIdentifier();
116 // Create all the person refs and entities
119 IntakeClient intakeClient = new IntakeClient();
120 MultipartOutput multipart = createIntakeInstance(
121 "entryNumber-" + identifier,
122 "entryDate-" + identifier,
125 conditionCheckerAssessorRefName,
129 ClientResponse<Response> res = intakeClient.create(multipart);
131 int statusCode = res.getStatus();
133 // Check the status code of the response: does it match
134 // the expected response(s)?
137 // Does it fall within the set of valid status codes?
138 // Does it exactly match the expected status code?
139 if (logger.isDebugEnabled()) {
140 logger.debug(testName + ": status = " + statusCode);
142 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
143 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
144 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
146 res.releaseConnection();
149 // Store the ID returned from the first resource created
150 // for additional tests below.
151 if (knownIntakeId == null) {
152 knownIntakeId = extractId(res);
153 if (logger.isDebugEnabled()) {
154 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
158 // Store the IDs from every resource created by tests,
159 // so they can be deleted after tests have been run.
160 intakeIdsCreated.add(extractId(res));
164 * Creates the person refs.
166 protected void createPersonRefs() {
167 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
168 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
169 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
170 ClientResponse<Response> res = personAuthClient.create(multipart);
171 int statusCode = res.getStatus();
173 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175 Assert.assertEquals(statusCode, STATUS_CREATED);
176 personAuthCSID = extractId(res);
178 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
180 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
181 Assert.assertNotNull(csid);
182 currentOwnerPersonCSID = csid;
183 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
184 Assert.assertNotNull(currentOwnerRefName);
185 personIdsCreated.add(csid);
187 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
188 Assert.assertNotNull(csid);
189 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
190 depositorPersonCSID = csid;
191 Assert.assertNotNull(depositorRefName);
192 personIdsCreated.add(csid);
194 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
195 Assert.assertNotNull(csid);
196 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
197 Assert.assertNotNull(conditionCheckerAssessorRefName);
198 personIdsCreated.add(csid);
200 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
201 Assert.assertNotNull(csid);
202 insurerPersonCSID = csid;
203 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
204 Assert.assertNotNull(insurerRefName);
205 personIdsCreated.add(csid);
207 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
208 Assert.assertNotNull(csid);
209 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
210 Assert.assertNotNull(valuerRefName);
211 personIdsCreated.add(csid);
215 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
216 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
217 Map<String, String> personInfo = new HashMap<String, String>();
218 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
219 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
220 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
221 MultipartOutput multipart =
222 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
223 authRefName, personInfo, personAuthClient.getItemCommonPartName());
224 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
225 int statusCode = res.getStatus();
227 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
228 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
229 Assert.assertEquals(statusCode, STATUS_CREATED);
230 return extractId(res);
234 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
235 dependsOnMethods = {"createIntakeWithAuthRefs"})
236 public void readAndCheckAuthRefDocs(String testName) throws Exception {
238 if (logger.isDebugEnabled()) {
239 logger.debug(testBanner(testName, CLASS_NAME));
242 testSetup(STATUS_OK, ServiceRequestType.READ);
244 // Get the auth ref docs and check them
246 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
247 ClientResponse<AuthorityRefDocList> refDocListResp =
248 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
250 int statusCode = refDocListResp.getStatus();
252 if (logger.isDebugEnabled()) {
253 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
255 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
256 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
257 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
258 AuthorityRefDocList list = refDocListResp.getEntity();
260 // Optionally output additional data about list members for debugging.
261 boolean iterateThroughList = true;
262 boolean fFoundIntake = false;
263 if (iterateThroughList && logger.isDebugEnabled()) {
264 List<AuthorityRefDocList.AuthorityRefDocItem> items =
265 list.getAuthorityRefDocItem();
267 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
268 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
269 logger.debug(testName + ": list-item[" + i + "] "
270 + item.getDocType() + "("
271 + item.getDocId() + ") Name:["
272 + item.getDocName() + "] Number:["
273 + item.getDocNumber() + "] in field:["
274 + item.getSourceField() + "]");
275 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
280 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
283 personAuthClient = new PersonAuthorityClient();
285 personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
287 statusCode = refDocListResp.getStatus();
289 if (logger.isDebugEnabled()) {
290 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
292 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
293 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
294 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
295 list = refDocListResp.getEntity();
297 // Optionally output additional data about list members for debugging.
298 iterateThroughList = true;
299 fFoundIntake = false;
300 if (iterateThroughList && logger.isDebugEnabled()) {
301 List<AuthorityRefDocList.AuthorityRefDocItem> items =
302 list.getAuthorityRefDocItem();
304 logger.debug(testName + ": Docs that use: " + depositorRefName);
305 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
306 logger.debug(testName + ": list-item[" + i + "] "
307 + item.getDocType() + "("
308 + item.getDocId() + ") Name:["
309 + item.getDocName() + "] Number:["
310 + item.getDocNumber() + "] in field:["
311 + item.getSourceField() + "]");
312 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
317 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
322 * Read and check the list of referencing objects, where the authRef field
323 * is a value instance of a repeatable scalar field.
325 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
326 dependsOnMethods = {"createIntakeWithAuthRefs"}, groups = {"repeatableScalar"})
327 public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
329 if (logger.isDebugEnabled()) {
330 logger.debug(testBanner(testName, CLASS_NAME));
333 testSetup(STATUS_OK, ServiceRequestType.READ);
335 // Get the auth ref docs and check them
337 // Single scalar field
338 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
339 ClientResponse<AuthorityRefDocList> refDocListResp =
340 personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
342 int statusCode = refDocListResp.getStatus();
344 if (logger.isDebugEnabled()) {
345 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
347 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
348 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
349 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
350 AuthorityRefDocList list = refDocListResp.getEntity();
352 // Optionally output additional data about list members for debugging.
353 boolean iterateThroughList = true;
354 boolean fFoundIntake = false;
355 if (iterateThroughList && logger.isDebugEnabled()) {
356 List<AuthorityRefDocList.AuthorityRefDocItem> items =
357 list.getAuthorityRefDocItem();
359 logger.debug(testName + ": Docs that use: " + insurerRefName);
360 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
361 logger.debug(testName + ": list-item[" + i + "] "
362 + item.getDocType() + "("
363 + item.getDocId() + ") Name:["
364 + item.getDocName() + "] Number:["
365 + item.getDocNumber() + "] in field:["
366 + item.getSourceField() + "]");
367 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
372 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
376 // ---------------------------------------------------------------
377 // Cleanup of resources created during testing
378 // ---------------------------------------------------------------
380 * Deletes all resources created by tests, after all tests have been run.
382 * This cleanup method will always be run, even if one or more tests fail.
383 * For this reason, it attempts to remove all resources created
384 * at any point during testing, even if some of those resources
385 * may be expected to be deleted by certain tests.
387 @AfterClass(alwaysRun = true)
388 public void cleanUp() {
389 String noTest = System.getProperty("noTestCleanup");
390 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
391 if (logger.isDebugEnabled()) {
392 logger.debug("Skipping Cleanup phase ...");
396 if (logger.isDebugEnabled()) {
397 logger.debug("Cleaning up temporary resources created for testing ...");
399 IntakeClient intakeClient = new IntakeClient();
400 // Note: Any non-success responses are ignored and not reported.
401 for (String resourceId : intakeIdsCreated) {
402 ClientResponse<Response> res = intakeClient.delete(resourceId);
403 res.releaseConnection();
405 // Delete persons before PersonAuth
406 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
407 for (String resourceId : personIdsCreated) {
408 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
409 res.releaseConnection();
411 if (personAuthCSID != null) {
412 personAuthClient.delete(personAuthCSID).releaseConnection();
416 // ---------------------------------------------------------------
417 // Utility methods used by tests above
418 // ---------------------------------------------------------------
420 public String getServicePathComponent() {
421 return SERVICE_PATH_COMPONENT;
424 private MultipartOutput createIntakeInstance(String entryNumber,
428 String conditionCheckerAssessor,
431 IntakesCommon intake = new IntakesCommon();
432 intake.setEntryNumber(entryNumber);
433 intake.setEntryDate(entryDate);
434 intake.setCurrentOwner(currentOwner);
435 intake.setDepositor(depositor);
436 intake.setValuer(Valuer);
438 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
439 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
440 checkersOrAssessors.add(conditionCheckerAssessor);
441 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
443 InsurerList insurerList = new InsurerList();
444 List<String> insurers = insurerList.getInsurer();
445 insurers.add(insurer);
446 intake.setInsurers(insurerList);
448 MultipartOutput multipart = new MultipartOutput();
449 OutputPart commonPart =
450 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
451 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
453 if (logger.isDebugEnabled()) {
454 logger.debug("to be created, intake common");
455 logger.debug(objectAsXmlString(intake, IntakesCommon.class));