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.PersonClient;
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;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * PersonAuthRefDocsTest, carries out tests against a
54 * deployed and running Person Service.
56 * $LastChangedRevision: 1327 $
57 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
59 public class PersonAuthRefDocsTest extends BaseServiceTest<AbstractCommonList> {
61 private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
62 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
63 // Instance variables specific to this test.
64 final String SERVICE_PATH_COMPONENT = "intakes";
65 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
66 private String knownIntakeId = null;
67 private List<String> intakeIdsCreated = new ArrayList<String>();
68 private List<String> personIdsCreated = new ArrayList<String>();
69 private String personAuthCSID = null;
70 private String personShortId = PERSON_AUTHORITY_NAME;
71 private String currentOwnerPersonCSID = null;
72 private String depositorPersonCSID = null;
73 private String insurerPersonCSID = null;
74 private String currentOwnerRefName = null;
75 private String depositorRefName = null;
76 private String conditionCheckerAssessorRefName = null;
77 private String insurerRefName = null;
78 private String valuerRefName = null;
79 private String valuerShortId = null;
80 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
81 private final static String CURRENT_DATE_UTC =
82 GregorianCalendarDateTimeUtils.currentDateUTC();
85 public String getServiceName() {
86 throw new UnsupportedOperationException(); //FIXME: REM - http://issues.collectionspace.org/browse/CSPACE-3498
90 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
93 protected CollectionSpaceClient getClientInstance() {
94 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
98 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
99 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
103 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
106 protected AbstractCommonList getCommonList(Response response) {
107 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
110 // ---------------------------------------------------------------
111 // CRUD tests : CREATE tests
112 // ---------------------------------------------------------------
114 @Test(dataProvider = "testName")
115 public void createIntakeWithAuthRefs(String testName) throws Exception {
116 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
118 // Submit the request to the service and store the response.
119 String identifier = createIdentifier();
121 // Create all the person refs and entities
124 IntakeClient intakeClient = new IntakeClient();
125 PoxPayloadOut multipart = createIntakeInstance(
126 "entryNumber-" + identifier,
130 conditionCheckerAssessorRefName,
134 Response res = intakeClient.create(multipart);
136 int statusCode = res.getStatus();
138 // Check the status code of the response: does it match
139 // the expected response(s)?
142 // Does it fall within the set of valid status codes?
143 // Does it exactly match the expected status code?
144 if (logger.isDebugEnabled()) {
145 logger.debug(testName + ": status = " + statusCode);
147 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
148 invalidStatusCodeMessage(testRequestType, statusCode));
149 Assert.assertEquals(statusCode, testExpectedStatusCode);
154 // Store the ID returned from the first resource created
155 // for additional tests below.
156 if (knownIntakeId == null) {
157 knownIntakeId = extractId(res);
158 if (logger.isDebugEnabled()) {
159 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
163 // Store the IDs from every resource created by tests,
164 // so they can be deleted after tests have been run.
165 intakeIdsCreated.add(extractId(res));
169 * Creates the person refs.
172 protected void createPersonRefs() throws Exception {
173 PersonClient personAuthClient = new PersonClient();
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) throws Exception {
227 String result = null;
229 PersonClient personAuthClient = new PersonClient();
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 PersonClient personAuthClient = new PersonClient();
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 PersonClient();
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 PersonClient personAuthClient = new PersonClient();
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.
410 @AfterClass(alwaysRun = true)
411 public void cleanUp() throws Exception {
412 String noTest = System.getProperty("noTestCleanup");
413 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
414 if (logger.isDebugEnabled()) {
415 logger.debug("Skipping Cleanup phase ...");
419 if (logger.isDebugEnabled()) {
420 logger.debug("Cleaning up temporary resources created for testing ...");
422 IntakeClient intakeClient = new IntakeClient();
423 // Note: Any non-success responses are ignored and not reported.
424 for (String resourceId : intakeIdsCreated) {
425 intakeClient.delete(resourceId).close();
427 // Delete persons before PersonAuth
428 PersonClient personAuthClient = new PersonClient();
429 for (String resourceId : personIdsCreated) {
430 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
432 if (personAuthCSID != null) {
433 personAuthClient.delete(personAuthCSID).close();
437 // ---------------------------------------------------------------
438 // Utility methods used by tests above
439 // ---------------------------------------------------------------
441 public String getServicePathComponent() {
442 return SERVICE_PATH_COMPONENT;
445 private PoxPayloadOut createIntakeInstance(String entryNumber,
449 String conditionCheckerAssessor,
451 String Valuer) throws Exception {
452 IntakesCommon intake = new IntakesCommon();
453 intake.setEntryNumber(entryNumber);
454 intake.setEntryDate(entryDate);
455 intake.setCurrentOwner(currentOwner);
456 intake.setDepositor(depositor);
457 intake.setValuer(Valuer);
459 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
460 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
461 checkersOrAssessors.add(conditionCheckerAssessor);
462 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
464 InsurerList insurerList = new InsurerList();
465 List<String> insurers = insurerList.getInsurer();
466 insurers.add(insurer);
467 intake.setInsurers(insurerList);
469 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
470 PayloadOutputPart commonPart =
471 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
473 if (logger.isDebugEnabled()) {
474 logger.debug("to be created, intake common");
475 logger.debug(objectAsXmlString(intake, IntakesCommon.class));