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.authorityref.AuthorityRefDocList;
40 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
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;
46 import org.jboss.resteasy.client.ClientResponse;
48 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
49 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
50 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
51 import org.testng.Assert;
52 import org.testng.annotations.AfterClass;
53 import org.testng.annotations.Test;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 * PersonAuthRefDocsTest, carries out tests against a
60 * deployed and running Person Service.
62 * $LastChangedRevision: 1327 $
63 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
65 public class PersonAuthRefDocsTest extends BaseServiceTest<AbstractCommonList> {
67 private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
68 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
69 // Instance variables specific to this test.
70 final String SERVICE_PATH_COMPONENT = "intakes";
71 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
72 private String knownIntakeId = null;
73 private List<String> intakeIdsCreated = new ArrayList<String>();
74 private List<String> personIdsCreated = new ArrayList<String>();
75 private String personAuthCSID = null;
76 private String personShortId = PERSON_AUTHORITY_NAME;
77 private String currentOwnerPersonCSID = null;
78 private String depositorPersonCSID = null;
79 private String insurerPersonCSID = null;
80 private String currentOwnerRefName = null;
81 private String depositorRefName = null;
82 private String conditionCheckerAssessorRefName = null;
83 private String insurerRefName = null;
84 private String valuerRefName = null;
85 private String valuerShortId = null;
86 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
87 private final static String CURRENT_DATE_UTC =
88 GregorianCalendarDateTimeUtils.currentDateUTC();
91 public String getServiceName() {
92 throw new UnsupportedOperationException(); //FIXME: REM - http://issues.collectionspace.org/browse/CSPACE-3498
96 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
99 protected CollectionSpaceClient getClientInstance() {
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(
108 ClientResponse<AbstractCommonList> response) {
109 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
112 // ---------------------------------------------------------------
113 // CRUD tests : CREATE tests
114 // ---------------------------------------------------------------
116 @Test(dataProvider = "testName")
117 public void createIntakeWithAuthRefs(String testName) throws Exception {
118 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
120 // Submit the request to the service and store the response.
121 String identifier = createIdentifier();
123 // Create all the person refs and entities
126 IntakeClient intakeClient = new IntakeClient();
127 PoxPayloadOut multipart = createIntakeInstance(
128 "entryNumber-" + identifier,
132 conditionCheckerAssessorRefName,
136 ClientResponse<Response> res = intakeClient.create(multipart);
138 int statusCode = res.getStatus();
140 // Check the status code of the response: does it match
141 // the expected response(s)?
144 // Does it fall within the set of valid status codes?
145 // Does it exactly match the expected status code?
146 if (logger.isDebugEnabled()) {
147 logger.debug(testName + ": status = " + statusCode);
149 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
150 invalidStatusCodeMessage(testRequestType, statusCode));
151 Assert.assertEquals(statusCode, testExpectedStatusCode);
153 res.releaseConnection();
156 // Store the ID returned from the first resource created
157 // for additional tests below.
158 if (knownIntakeId == null) {
159 knownIntakeId = extractId(res);
160 if (logger.isDebugEnabled()) {
161 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
165 // Store the IDs from every resource created by tests,
166 // so they can be deleted after tests have been run.
167 intakeIdsCreated.add(extractId(res));
171 * Creates the person refs.
173 protected void createPersonRefs() {
174 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
175 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
176 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
177 ClientResponse<Response> res = personAuthClient.create(multipart);
178 int statusCode = res.getStatus();
180 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
181 invalidStatusCodeMessage(testRequestType, statusCode));
182 Assert.assertEquals(statusCode, STATUS_CREATED);
183 personAuthCSID = extractId(res);
185 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
187 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
188 Assert.assertNotNull(csid);
189 currentOwnerPersonCSID = csid;
190 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
191 Assert.assertNotNull(currentOwnerRefName);
192 personIdsCreated.add(csid);
194 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
195 Assert.assertNotNull(csid);
196 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
197 depositorPersonCSID = csid;
198 Assert.assertNotNull(depositorRefName);
199 personIdsCreated.add(csid);
201 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
202 Assert.assertNotNull(csid);
203 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
204 Assert.assertNotNull(conditionCheckerAssessorRefName);
205 personIdsCreated.add(csid);
207 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
208 Assert.assertNotNull(csid);
209 insurerPersonCSID = csid;
210 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
211 Assert.assertNotNull(insurerRefName);
212 personIdsCreated.add(csid);
214 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
215 Assert.assertNotNull(csid);
216 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
217 if (logger.isDebugEnabled()) {
218 logger.debug("valuerShortId=" + valuerShortId);
220 Assert.assertNotNull(valuerRefName);
221 personIdsCreated.add(csid);
225 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
226 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
227 Map<String, String> personInfo = new HashMap<String, String>();
228 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
229 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
230 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
231 PoxPayloadOut multipart =
232 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
233 authRefName, personInfo, null, personAuthClient.getItemCommonPartName());
234 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
235 int statusCode = res.getStatus();
237 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
238 invalidStatusCodeMessage(testRequestType, statusCode));
239 Assert.assertEquals(statusCode, STATUS_CREATED);
240 return extractId(res);
244 @Test(dataProvider = "testName", dependsOnMethods = {"createIntakeWithAuthRefs"})
245 public void readAndCheckAuthRefDocs(String testName) throws Exception {
247 testSetup(STATUS_OK, ServiceRequestType.READ);
249 // Get the auth ref docs and check them
251 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
252 ClientResponse<AuthorityRefDocList> res =
253 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
254 AuthorityRefDocList list = null;
256 assertStatusCode(res, testName);
257 list = res.getEntity();
260 res.releaseConnection();
264 List<AuthorityRefDocList.AuthorityRefDocItem> items =
265 list.getAuthorityRefDocItem();
266 Assert.assertTrue(items != null);
267 Assert.assertTrue(items.size() > 0);
269 // Optionally output additional data about list members for debugging.
270 boolean iterateThroughList = true;
271 boolean fFoundIntake = false;
272 if (iterateThroughList && logger.isDebugEnabled()) {
274 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
275 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
276 logger.debug(testName + ": list-item[" + i + "] "
277 + item.getDocType() + "("
278 + item.getDocId() + ") Name:["
279 + item.getDocName() + "] Number:["
280 + item.getDocNumber() + "] in field:["
281 + item.getSourceField() + "]");
282 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
287 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
290 // Get the referencing objects
292 personAuthClient = new PersonAuthorityClient();
293 res = personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
295 assertStatusCode(res, testName);
296 list = res.getEntity();
299 res.releaseConnection();
303 items = list.getAuthorityRefDocItem();
304 Assert.assertTrue(items != null);
305 Assert.assertTrue(items.size() > 0);
306 Assert.assertTrue(items.get(0) != null);
308 // Optionally output additional data about list members for debugging.
309 iterateThroughList = true;
310 fFoundIntake = false;
311 if (iterateThroughList && logger.isDebugEnabled()) {
313 logger.debug(testName + ": Docs that use: " + depositorRefName);
314 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
315 logger.debug(testName + ": list-item[" + i + "] "
316 + item.getDocType() + "("
317 + item.getDocId() + ") Name:["
318 + item.getDocName() + "] Number:["
319 + item.getDocNumber() + "] in field:["
320 + item.getSourceField() + "]");
321 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
326 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
331 * Read and check the list of referencing objects, where the authRef field
332 * is a value instance of a repeatable scalar field.
334 @Test(dataProvider = "testName", dependsOnMethods = {"createIntakeWithAuthRefs"},
335 groups = {"repeatableScalar"})
336 public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
338 testSetup(STATUS_OK, ServiceRequestType.READ);
340 // Get the auth ref docs and check them
342 // Single scalar field
343 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
344 ClientResponse<AuthorityRefDocList> res =
345 personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
346 AuthorityRefDocList list = null;
348 assertStatusCode(res, testName);
349 list = res.getEntity();
352 res.releaseConnection();
356 List<AuthorityRefDocList.AuthorityRefDocItem> items =
357 list.getAuthorityRefDocItem();
358 Assert.assertTrue(items != null);
359 Assert.assertTrue(items.size() > 0);
360 Assert.assertTrue(items.get(0) != null);
362 // Optionally output additional data about list members for debugging.
363 boolean iterateThroughList = true;
364 boolean fFoundIntake = false;
365 if (iterateThroughList && logger.isDebugEnabled()) {
367 logger.debug(testName + ": Docs that use: " + insurerRefName);
368 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
369 logger.debug(testName + ": list-item[" + i + "] "
370 + item.getDocType() + "("
371 + item.getDocId() + ") Name:["
372 + item.getDocName() + "] Number:["
373 + item.getDocNumber() + "] in field:["
374 + item.getSourceField() + "]");
375 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
380 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
385 // ---------------------------------------------------------------
386 // Cleanup of resources created during testing
387 // ---------------------------------------------------------------
389 * Deletes all resources created by tests, after all tests have been run.
391 * This cleanup method will always be run, even if one or more tests fail.
392 * For this reason, it attempts to remove all resources created
393 * at any point during testing, even if some of those resources
394 * may be expected to be deleted by certain tests.
396 @AfterClass(alwaysRun = true)
397 public void cleanUp() {
398 String noTest = System.getProperty("noTestCleanup");
399 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
400 if (logger.isDebugEnabled()) {
401 logger.debug("Skipping Cleanup phase ...");
405 if (logger.isDebugEnabled()) {
406 logger.debug("Cleaning up temporary resources created for testing ...");
408 IntakeClient intakeClient = new IntakeClient();
409 // Note: Any non-success responses are ignored and not reported.
410 for (String resourceId : intakeIdsCreated) {
411 ClientResponse<Response> res = intakeClient.delete(resourceId);
412 res.releaseConnection();
414 // Delete persons before PersonAuth
415 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
416 for (String resourceId : personIdsCreated) {
417 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
418 res.releaseConnection();
420 if (personAuthCSID != null) {
421 personAuthClient.delete(personAuthCSID).releaseConnection();
425 // ---------------------------------------------------------------
426 // Utility methods used by tests above
427 // ---------------------------------------------------------------
429 public String getServicePathComponent() {
430 return SERVICE_PATH_COMPONENT;
433 private PoxPayloadOut createIntakeInstance(String entryNumber,
437 String conditionCheckerAssessor,
440 IntakesCommon intake = new IntakesCommon();
441 intake.setEntryNumber(entryNumber);
442 intake.setEntryDate(entryDate);
443 intake.setCurrentOwner(currentOwner);
444 intake.setDepositor(depositor);
445 intake.setValuer(Valuer);
447 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
448 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
449 checkersOrAssessors.add(conditionCheckerAssessor);
450 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
452 InsurerList insurerList = new InsurerList();
453 List<String> insurers = insurerList.getInsurer();
454 insurers.add(insurer);
455 intake.setInsurers(insurerList);
457 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
458 PayloadOutputPart commonPart =
459 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
461 if (logger.isDebugEnabled()) {
462 logger.debug("to be created, intake common");
463 logger.debug(objectAsXmlString(intake, IntakesCommon.class));