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.Response;
32 import org.collectionspace.services.PersonJAXBSchema;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.IntakeClient;
35 import org.collectionspace.services.client.PayloadOutputPart;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
40 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
41 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
42 import org.collectionspace.services.intake.IntakesCommon;
43 import org.collectionspace.services.intake.InsurerList;
44 import org.collectionspace.services.jaxb.AbstractCommonList;
45 import org.collectionspace.services.person.PersonTermGroup;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * PersonAuthRefDocsTest, carries out tests against a
55 * deployed and running Person Service.
57 * $LastChangedRevision: 1327 $
58 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60 public class PersonAuthRefDocsTest extends BaseServiceTest<AbstractCommonList> {
62 private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
63 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
64 // Instance variables specific to this test.
65 final String SERVICE_PATH_COMPONENT = "intakes";
66 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
67 private String knownIntakeId = null;
68 private List<String> intakeIdsCreated = new ArrayList<String>();
69 private List<String> personIdsCreated = new ArrayList<String>();
70 private String personAuthCSID = null;
71 private String personShortId = PERSON_AUTHORITY_NAME;
72 private String currentOwnerPersonCSID = null;
73 private String depositorPersonCSID = null;
74 private String insurerPersonCSID = null;
75 private String currentOwnerRefName = null;
76 private String depositorRefName = null;
77 private String conditionCheckerAssessorRefName = null;
78 private String insurerRefName = null;
79 private String valuerRefName = null;
80 private String valuerShortId = null;
81 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
82 private final static String CURRENT_DATE_UTC =
83 GregorianCalendarDateTimeUtils.currentDateUTC();
86 public String getServiceName() {
87 throw new UnsupportedOperationException(); //FIXME: REM - http://issues.collectionspace.org/browse/CSPACE-3498
91 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
94 protected CollectionSpaceClient getClientInstance() {
95 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
99 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
100 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
104 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
107 protected AbstractCommonList getCommonList(Response response) {
108 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
111 // ---------------------------------------------------------------
112 // CRUD tests : CREATE tests
113 // ---------------------------------------------------------------
115 @Test(dataProvider = "testName")
116 public void createIntakeWithAuthRefs(String testName) throws Exception {
117 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
119 // Submit the request to the service and store the response.
120 String identifier = createIdentifier();
122 // Create all the person refs and entities
125 IntakeClient intakeClient = new IntakeClient();
126 PoxPayloadOut multipart = createIntakeInstance(
127 "entryNumber-" + identifier,
131 conditionCheckerAssessorRefName,
135 Response res = intakeClient.create(multipart);
137 int statusCode = res.getStatus();
139 // Check the status code of the response: does it match
140 // the expected response(s)?
143 // Does it fall within the set of valid status codes?
144 // Does it exactly match the expected status code?
145 if (logger.isDebugEnabled()) {
146 logger.debug(testName + ": status = " + statusCode);
148 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
149 invalidStatusCodeMessage(testRequestType, statusCode));
150 Assert.assertEquals(statusCode, testExpectedStatusCode);
155 // Store the ID returned from the first resource created
156 // for additional tests below.
157 if (knownIntakeId == null) {
158 knownIntakeId = extractId(res);
159 if (logger.isDebugEnabled()) {
160 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
164 // Store the IDs from every resource created by tests,
165 // so they can be deleted after tests have been run.
166 intakeIdsCreated.add(extractId(res));
170 * Creates the person refs.
172 protected void createPersonRefs() {
173 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
174 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
175 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
176 Response res = personAuthClient.create(multipart);
178 int statusCode = res.getStatus();
179 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
180 invalidStatusCodeMessage(testRequestType, statusCode));
181 Assert.assertEquals(statusCode, STATUS_CREATED);
182 personAuthCSID = extractId(res);
187 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
189 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
190 Assert.assertNotNull(csid);
191 currentOwnerPersonCSID = csid;
192 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
193 Assert.assertNotNull(currentOwnerRefName);
194 personIdsCreated.add(csid);
196 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
197 Assert.assertNotNull(csid);
198 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
199 depositorPersonCSID = csid;
200 Assert.assertNotNull(depositorRefName);
201 personIdsCreated.add(csid);
203 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
204 Assert.assertNotNull(csid);
205 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
206 Assert.assertNotNull(conditionCheckerAssessorRefName);
207 personIdsCreated.add(csid);
209 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
210 Assert.assertNotNull(csid);
211 insurerPersonCSID = csid;
212 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
213 Assert.assertNotNull(insurerRefName);
214 personIdsCreated.add(csid);
216 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
217 Assert.assertNotNull(csid);
218 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
219 if (logger.isDebugEnabled()) {
220 logger.debug("valuerShortId=" + valuerShortId);
222 Assert.assertNotNull(valuerRefName);
223 personIdsCreated.add(csid);
226 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
227 String result = null;
229 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
230 Map<String, String> personInfo = new HashMap<String, String>();
231 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
232 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
233 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
234 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
235 PersonTermGroup term = new PersonTermGroup();
236 String termName = firstName + " " + surName;
237 term.setTermDisplayName(termName);
238 term.setTermName(termName);
239 personTerms.add(term);
240 PoxPayloadOut multipart =
241 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
242 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
243 Response res = personAuthClient.createItem(personAuthCSID, multipart);
245 int statusCode = res.getStatus();
247 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
248 invalidStatusCodeMessage(testRequestType, statusCode));
249 Assert.assertEquals(statusCode, STATUS_CREATED);
250 result = extractId(res);
259 @Test(dataProvider = "testName", dependsOnMethods = {"createIntakeWithAuthRefs"})
260 public void readAndCheckAuthRefDocs(String testName) throws Exception {
262 testSetup(STATUS_OK, ServiceRequestType.READ);
264 // Get the auth ref docs and check them
266 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
267 Response res = personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
268 AuthorityRefDocList list = null;
270 assertStatusCode(res, testName);
271 list = res.readEntity(AuthorityRefDocList.class);
278 List<AuthorityRefDocList.AuthorityRefDocItem> items =
279 list.getAuthorityRefDocItem();
280 Assert.assertTrue(items != null);
281 Assert.assertTrue(items.size() > 0);
283 // Optionally output additional data about list members for debugging.
284 boolean iterateThroughList = true;
285 boolean fFoundIntake = false;
286 if (iterateThroughList && logger.isDebugEnabled()) {
288 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
289 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
290 logger.debug(testName + ": list-item[" + i + "] "
291 + item.getDocType() + "("
292 + item.getDocId() + ") Name:["
293 + item.getDocName() + "] Number:["
294 + item.getDocNumber() + "] in field:["
295 + item.getSourceField() + "]");
296 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
301 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
304 // Get the referencing objects
306 personAuthClient = new PersonAuthorityClient();
307 res = personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
309 assertStatusCode(res, testName);
310 list = res.readEntity(AuthorityRefDocList.class);
317 items = list.getAuthorityRefDocItem();
318 Assert.assertTrue(items != null);
319 Assert.assertTrue(items.size() > 0);
320 Assert.assertTrue(items.get(0) != null);
322 // Optionally output additional data about list members for debugging.
323 iterateThroughList = true;
324 fFoundIntake = false;
325 if (iterateThroughList && logger.isDebugEnabled()) {
327 logger.debug(testName + ": Docs that use: " + depositorRefName);
328 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
329 logger.debug(testName + ": list-item[" + i + "] "
330 + item.getDocType() + "("
331 + item.getDocId() + ") Name:["
332 + item.getDocName() + "] Number:["
333 + item.getDocNumber() + "] in field:["
334 + item.getSourceField() + "]");
335 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
340 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
345 * Read and check the list of referencing objects, where the authRef field
346 * is a value instance of a repeatable scalar field.
348 @Test(dataProvider = "testName", dependsOnMethods = {"createIntakeWithAuthRefs"},
349 groups = {"repeatableScalar"})
350 public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
352 testSetup(STATUS_OK, ServiceRequestType.READ);
354 // Get the auth ref docs and check them
356 // Single scalar field
357 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
358 Response res = personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
359 AuthorityRefDocList list = null;
361 assertStatusCode(res, testName);
362 list = res.readEntity(AuthorityRefDocList.class);
369 List<AuthorityRefDocList.AuthorityRefDocItem> items =
370 list.getAuthorityRefDocItem();
371 Assert.assertTrue(items != null);
372 Assert.assertTrue(items.size() > 0);
373 Assert.assertTrue(items.get(0) != null);
375 // Optionally output additional data about list members for debugging.
376 boolean iterateThroughList = true;
377 boolean fFoundIntake = false;
378 if (iterateThroughList && logger.isDebugEnabled()) {
380 logger.debug(testName + ": Docs that use: " + insurerRefName);
381 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
382 logger.debug(testName + ": list-item[" + i + "] "
383 + item.getDocType() + "("
384 + item.getDocId() + ") Name:["
385 + item.getDocName() + "] Number:["
386 + item.getDocNumber() + "] in field:["
387 + item.getSourceField() + "]");
388 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
393 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
398 // ---------------------------------------------------------------
399 // Cleanup of resources created during testing
400 // ---------------------------------------------------------------
402 * Deletes all resources created by tests, after all tests have been run.
404 * This cleanup method will always be run, even if one or more tests fail.
405 * For this reason, it attempts to remove all resources created
406 * at any point during testing, even if some of those resources
407 * may be expected to be deleted by certain tests.
409 @AfterClass(alwaysRun = true)
410 public void cleanUp() {
411 String noTest = System.getProperty("noTestCleanup");
412 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
413 if (logger.isDebugEnabled()) {
414 logger.debug("Skipping Cleanup phase ...");
418 if (logger.isDebugEnabled()) {
419 logger.debug("Cleaning up temporary resources created for testing ...");
421 IntakeClient intakeClient = new IntakeClient();
422 // Note: Any non-success responses are ignored and not reported.
423 for (String resourceId : intakeIdsCreated) {
424 intakeClient.delete(resourceId).close();
426 // Delete persons before PersonAuth
427 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
428 for (String resourceId : personIdsCreated) {
429 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
431 if (personAuthCSID != null) {
432 personAuthClient.delete(personAuthCSID).close();
436 // ---------------------------------------------------------------
437 // Utility methods used by tests above
438 // ---------------------------------------------------------------
440 public String getServicePathComponent() {
441 return SERVICE_PATH_COMPONENT;
444 private PoxPayloadOut createIntakeInstance(String entryNumber,
448 String conditionCheckerAssessor,
451 IntakesCommon intake = new IntakesCommon();
452 intake.setEntryNumber(entryNumber);
453 intake.setEntryDate(entryDate);
454 intake.setCurrentOwner(currentOwner);
455 intake.setDepositor(depositor);
456 intake.setValuer(Valuer);
458 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
459 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
460 checkersOrAssessors.add(conditionCheckerAssessor);
461 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
463 InsurerList insurerList = new InsurerList();
464 List<String> insurers = insurerList.getInsurer();
465 insurers.add(insurer);
466 intake.setInsurers(insurerList);
468 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
469 PayloadOutputPart commonPart =
470 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
472 if (logger.isDebugEnabled()) {
473 logger.debug("to be created, intake common");
474 logger.debug(objectAsXmlString(intake, IntakesCommon.class));