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.jboss.resteasy.client.ClientResponse;
49 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
50 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
51 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
52 import org.testng.Assert;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.Test;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * PersonAuthRefDocsTest, carries out tests against a
61 * deployed and running Person Service.
63 * $LastChangedRevision: 1327 $
64 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
66 public class PersonAuthRefDocsTest extends BaseServiceTest<AbstractCommonList> {
68 private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
69 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
70 // Instance variables specific to this test.
71 final String SERVICE_PATH_COMPONENT = "intakes";
72 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
73 private String knownIntakeId = null;
74 private List<String> intakeIdsCreated = new ArrayList<String>();
75 private List<String> personIdsCreated = new ArrayList<String>();
76 private String personAuthCSID = null;
77 private String personShortId = PERSON_AUTHORITY_NAME;
78 private String currentOwnerPersonCSID = null;
79 private String depositorPersonCSID = null;
80 private String insurerPersonCSID = null;
81 private String currentOwnerRefName = null;
82 private String depositorRefName = null;
83 private String conditionCheckerAssessorRefName = null;
84 private String insurerRefName = null;
85 private String valuerRefName = null;
86 private String valuerShortId = null;
87 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
88 private final static String CURRENT_DATE_UTC =
89 GregorianCalendarDateTimeUtils.currentDateUTC();
92 public String getServiceName() {
93 throw new UnsupportedOperationException(); //FIXME: REM - http://issues.collectionspace.org/browse/CSPACE-3498
97 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
100 protected CollectionSpaceClient getClientInstance() {
101 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
105 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
108 protected AbstractCommonList getCommonList(
109 ClientResponse<AbstractCommonList> response) {
110 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
113 // ---------------------------------------------------------------
114 // CRUD tests : CREATE tests
115 // ---------------------------------------------------------------
117 @Test(dataProvider = "testName")
118 public void createIntakeWithAuthRefs(String testName) throws Exception {
119 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
121 // Submit the request to the service and store the response.
122 String identifier = createIdentifier();
124 // Create all the person refs and entities
127 IntakeClient intakeClient = new IntakeClient();
128 PoxPayloadOut multipart = createIntakeInstance(
129 "entryNumber-" + identifier,
133 conditionCheckerAssessorRefName,
137 Response res = intakeClient.create(multipart);
139 int statusCode = res.getStatus();
141 // Check the status code of the response: does it match
142 // the expected response(s)?
145 // Does it fall within the set of valid status codes?
146 // Does it exactly match the expected status code?
147 if (logger.isDebugEnabled()) {
148 logger.debug(testName + ": status = " + statusCode);
150 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
151 invalidStatusCodeMessage(testRequestType, statusCode));
152 Assert.assertEquals(statusCode, testExpectedStatusCode);
157 // Store the ID returned from the first resource created
158 // for additional tests below.
159 if (knownIntakeId == null) {
160 knownIntakeId = extractId(res);
161 if (logger.isDebugEnabled()) {
162 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
166 // Store the IDs from every resource created by tests,
167 // so they can be deleted after tests have been run.
168 intakeIdsCreated.add(extractId(res));
172 * Creates the person refs.
174 protected void createPersonRefs() {
175 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
176 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
177 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
178 Response res = personAuthClient.create(multipart);
180 int statusCode = res.getStatus();
181 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
182 invalidStatusCodeMessage(testRequestType, statusCode));
183 Assert.assertEquals(statusCode, STATUS_CREATED);
184 personAuthCSID = extractId(res);
189 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
191 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
192 Assert.assertNotNull(csid);
193 currentOwnerPersonCSID = csid;
194 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
195 Assert.assertNotNull(currentOwnerRefName);
196 personIdsCreated.add(csid);
198 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
199 Assert.assertNotNull(csid);
200 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
201 depositorPersonCSID = csid;
202 Assert.assertNotNull(depositorRefName);
203 personIdsCreated.add(csid);
205 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
206 Assert.assertNotNull(csid);
207 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
208 Assert.assertNotNull(conditionCheckerAssessorRefName);
209 personIdsCreated.add(csid);
211 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
212 Assert.assertNotNull(csid);
213 insurerPersonCSID = csid;
214 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
215 Assert.assertNotNull(insurerRefName);
216 personIdsCreated.add(csid);
218 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
219 Assert.assertNotNull(csid);
220 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
221 if (logger.isDebugEnabled()) {
222 logger.debug("valuerShortId=" + valuerShortId);
224 Assert.assertNotNull(valuerRefName);
225 personIdsCreated.add(csid);
228 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
229 String result = null;
231 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
232 Map<String, String> personInfo = new HashMap<String, String>();
233 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
234 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
235 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
236 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
237 PersonTermGroup term = new PersonTermGroup();
238 String termName = firstName + " " + surName;
239 term.setTermDisplayName(termName);
240 term.setTermName(termName);
241 personTerms.add(term);
242 PoxPayloadOut multipart =
243 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
244 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
245 Response res = personAuthClient.createItem(personAuthCSID, multipart);
247 int statusCode = res.getStatus();
249 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
250 invalidStatusCodeMessage(testRequestType, statusCode));
251 Assert.assertEquals(statusCode, STATUS_CREATED);
252 result = extractId(res);
261 @Test(dataProvider = "testName", dependsOnMethods = {"createIntakeWithAuthRefs"})
262 public void readAndCheckAuthRefDocs(String testName) throws Exception {
264 testSetup(STATUS_OK, ServiceRequestType.READ);
266 // Get the auth ref docs and check them
268 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
269 ClientResponse<AuthorityRefDocList> res =
270 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
271 AuthorityRefDocList list = null;
273 assertStatusCode(res, testName);
274 list = res.getEntity();
281 List<AuthorityRefDocList.AuthorityRefDocItem> items =
282 list.getAuthorityRefDocItem();
283 Assert.assertTrue(items != null);
284 Assert.assertTrue(items.size() > 0);
286 // Optionally output additional data about list members for debugging.
287 boolean iterateThroughList = true;
288 boolean fFoundIntake = false;
289 if (iterateThroughList && logger.isDebugEnabled()) {
291 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
292 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
293 logger.debug(testName + ": list-item[" + i + "] "
294 + item.getDocType() + "("
295 + item.getDocId() + ") Name:["
296 + item.getDocName() + "] Number:["
297 + item.getDocNumber() + "] in field:["
298 + item.getSourceField() + "]");
299 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
304 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
307 // Get the referencing objects
309 personAuthClient = new PersonAuthorityClient();
310 res = personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
312 assertStatusCode(res, testName);
313 list = res.getEntity();
316 res.releaseConnection();
320 items = list.getAuthorityRefDocItem();
321 Assert.assertTrue(items != null);
322 Assert.assertTrue(items.size() > 0);
323 Assert.assertTrue(items.get(0) != null);
325 // Optionally output additional data about list members for debugging.
326 iterateThroughList = true;
327 fFoundIntake = false;
328 if (iterateThroughList && logger.isDebugEnabled()) {
330 logger.debug(testName + ": Docs that use: " + depositorRefName);
331 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
332 logger.debug(testName + ": list-item[" + i + "] "
333 + item.getDocType() + "("
334 + item.getDocId() + ") Name:["
335 + item.getDocName() + "] Number:["
336 + item.getDocNumber() + "] in field:["
337 + item.getSourceField() + "]");
338 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
343 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
348 * Read and check the list of referencing objects, where the authRef field
349 * is a value instance of a repeatable scalar field.
351 @Test(dataProvider = "testName", dependsOnMethods = {"createIntakeWithAuthRefs"},
352 groups = {"repeatableScalar"})
353 public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
355 testSetup(STATUS_OK, ServiceRequestType.READ);
357 // Get the auth ref docs and check them
359 // Single scalar field
360 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
361 ClientResponse<AuthorityRefDocList> res =
362 personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
363 AuthorityRefDocList list = null;
365 assertStatusCode(res, testName);
366 list = res.getEntity();
369 res.releaseConnection();
373 List<AuthorityRefDocList.AuthorityRefDocItem> items =
374 list.getAuthorityRefDocItem();
375 Assert.assertTrue(items != null);
376 Assert.assertTrue(items.size() > 0);
377 Assert.assertTrue(items.get(0) != null);
379 // Optionally output additional data about list members for debugging.
380 boolean iterateThroughList = true;
381 boolean fFoundIntake = false;
382 if (iterateThroughList && logger.isDebugEnabled()) {
384 logger.debug(testName + ": Docs that use: " + insurerRefName);
385 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
386 logger.debug(testName + ": list-item[" + i + "] "
387 + item.getDocType() + "("
388 + item.getDocId() + ") Name:["
389 + item.getDocName() + "] Number:["
390 + item.getDocNumber() + "] in field:["
391 + item.getSourceField() + "]");
392 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
397 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
402 // ---------------------------------------------------------------
403 // Cleanup of resources created during testing
404 // ---------------------------------------------------------------
406 * Deletes all resources created by tests, after all tests have been run.
408 * This cleanup method will always be run, even if one or more tests fail.
409 * For this reason, it attempts to remove all resources created
410 * at any point during testing, even if some of those resources
411 * may be expected to be deleted by certain tests.
413 @AfterClass(alwaysRun = true)
414 public void cleanUp() {
415 String noTest = System.getProperty("noTestCleanup");
416 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
417 if (logger.isDebugEnabled()) {
418 logger.debug("Skipping Cleanup phase ...");
422 if (logger.isDebugEnabled()) {
423 logger.debug("Cleaning up temporary resources created for testing ...");
425 IntakeClient intakeClient = new IntakeClient();
426 // Note: Any non-success responses are ignored and not reported.
427 for (String resourceId : intakeIdsCreated) {
428 intakeClient.delete(resourceId).close();
430 // Delete persons before PersonAuth
431 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
432 for (String resourceId : personIdsCreated) {
433 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
435 if (personAuthCSID != null) {
436 personAuthClient.delete(personAuthCSID).close();
440 // ---------------------------------------------------------------
441 // Utility methods used by tests above
442 // ---------------------------------------------------------------
444 public String getServicePathComponent() {
445 return SERVICE_PATH_COMPONENT;
448 private PoxPayloadOut createIntakeInstance(String entryNumber,
452 String conditionCheckerAssessor,
455 IntakesCommon intake = new IntakesCommon();
456 intake.setEntryNumber(entryNumber);
457 intake.setEntryDate(entryDate);
458 intake.setCurrentOwner(currentOwner);
459 intake.setDepositor(depositor);
460 intake.setValuer(Valuer);
462 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
463 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
464 checkersOrAssessors.add(conditionCheckerAssessor);
465 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
467 InsurerList insurerList = new InsurerList();
468 List<String> insurers = insurerList.getInsurer();
469 insurers.add(insurer);
470 intake.setInsurers(insurerList);
472 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
473 PayloadOutputPart commonPart =
474 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
476 if (logger.isDebugEnabled()) {
477 logger.debug("to be created, intake common");
478 logger.debug(objectAsXmlString(intake, IntakesCommon.class));