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.PayloadOutputPart;
37 import org.collectionspace.services.client.PersonAuthorityClient;
38 import org.collectionspace.services.client.PersonAuthorityClientUtils;
39 import org.collectionspace.services.client.PoxPayloadOut;
40 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
41 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
42 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
43 import org.collectionspace.services.intake.IntakesCommon;
44 import org.collectionspace.services.intake.InsurerList;
45 import org.collectionspace.services.jaxb.AbstractCommonList;
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 {
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 getAbstractCommonList(
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", dataProviderClass = AbstractServiceTestImpl.class)
118 public void createIntakeWithAuthRefs(String testName) throws Exception {
120 if (logger.isDebugEnabled()) {
121 logger.debug(testBanner(testName, CLASS_NAME));
123 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
125 // Submit the request to the service and store the response.
126 String identifier = createIdentifier();
128 // Create all the person refs and entities
131 IntakeClient intakeClient = new IntakeClient();
132 PoxPayloadOut multipart = createIntakeInstance(
133 "entryNumber-" + identifier,
137 conditionCheckerAssessorRefName,
141 ClientResponse<Response> res = intakeClient.create(multipart);
143 int statusCode = res.getStatus();
145 // Check the status code of the response: does it match
146 // the expected response(s)?
149 // Does it fall within the set of valid status codes?
150 // Does it exactly match the expected status code?
151 if (logger.isDebugEnabled()) {
152 logger.debug(testName + ": status = " + statusCode);
154 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
155 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
156 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
158 res.releaseConnection();
161 // Store the ID returned from the first resource created
162 // for additional tests below.
163 if (knownIntakeId == null) {
164 knownIntakeId = extractId(res);
165 if (logger.isDebugEnabled()) {
166 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
170 // Store the IDs from every resource created by tests,
171 // so they can be deleted after tests have been run.
172 intakeIdsCreated.add(extractId(res));
176 * Creates the person refs.
178 protected void createPersonRefs() {
179 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
180 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
181 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
182 ClientResponse<Response> res = personAuthClient.create(multipart);
183 int statusCode = res.getStatus();
185 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
186 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
187 Assert.assertEquals(statusCode, STATUS_CREATED);
188 personAuthCSID = extractId(res);
190 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
192 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
193 Assert.assertNotNull(csid);
194 currentOwnerPersonCSID = csid;
195 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
196 Assert.assertNotNull(currentOwnerRefName);
197 personIdsCreated.add(csid);
199 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
200 Assert.assertNotNull(csid);
201 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
202 depositorPersonCSID = csid;
203 Assert.assertNotNull(depositorRefName);
204 personIdsCreated.add(csid);
206 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
207 Assert.assertNotNull(csid);
208 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
209 Assert.assertNotNull(conditionCheckerAssessorRefName);
210 personIdsCreated.add(csid);
212 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
213 Assert.assertNotNull(csid);
214 insurerPersonCSID = csid;
215 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
216 Assert.assertNotNull(insurerRefName);
217 personIdsCreated.add(csid);
219 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
220 Assert.assertNotNull(csid);
221 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
222 if (logger.isDebugEnabled()) {
223 logger.debug("valuerShortId=" + valuerShortId);
225 Assert.assertNotNull(valuerRefName);
226 personIdsCreated.add(csid);
230 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
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 PoxPayloadOut multipart =
237 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
238 authRefName, personInfo, personAuthClient.getItemCommonPartName());
239 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
240 int statusCode = res.getStatus();
242 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
243 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
244 Assert.assertEquals(statusCode, STATUS_CREATED);
245 return extractId(res);
249 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
250 dependsOnMethods = {"createIntakeWithAuthRefs"})
251 public void readAndCheckAuthRefDocs(String testName) throws Exception {
253 if (logger.isDebugEnabled()) {
254 logger.debug(testBanner(testName, CLASS_NAME));
257 testSetup(STATUS_OK, ServiceRequestType.READ);
259 // Get the auth ref docs and check them
261 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
262 ClientResponse<AuthorityRefDocList> refDocListResp =
263 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
265 int statusCode = refDocListResp.getStatus();
267 if (logger.isDebugEnabled()) {
268 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
270 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
271 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
272 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
274 AuthorityRefDocList list = refDocListResp.getEntity();
275 List<AuthorityRefDocList.AuthorityRefDocItem> items =
276 list.getAuthorityRefDocItem();
277 Assert.assertTrue(items != null);
278 Assert.assertTrue(items.size() > 0);
280 // Optionally output additional data about list members for debugging.
281 boolean iterateThroughList = true;
282 boolean fFoundIntake = false;
283 if (iterateThroughList && logger.isDebugEnabled()) {
285 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
286 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
287 logger.debug(testName + ": list-item[" + i + "] "
288 + item.getDocType() + "("
289 + item.getDocId() + ") Name:["
290 + item.getDocName() + "] Number:["
291 + item.getDocNumber() + "] in field:["
292 + item.getSourceField() + "]");
293 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
298 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
301 personAuthClient = new PersonAuthorityClient();
303 personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
305 statusCode = refDocListResp.getStatus();
307 if (logger.isDebugEnabled()) {
308 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
310 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
311 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
312 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
314 list = refDocListResp.getEntity();
315 items = list.getAuthorityRefDocItem();
316 Assert.assertTrue(items != null);
317 Assert.assertTrue(items.size() > 0);
318 Assert.assertTrue(items.get(0) != null);
320 // Optionally output additional data about list members for debugging.
321 iterateThroughList = true;
322 fFoundIntake = false;
323 if (iterateThroughList && logger.isDebugEnabled()) {
325 logger.debug(testName + ": Docs that use: " + depositorRefName);
326 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
327 logger.debug(testName + ": list-item[" + i + "] "
328 + item.getDocType() + "("
329 + item.getDocId() + ") Name:["
330 + item.getDocName() + "] Number:["
331 + item.getDocNumber() + "] in field:["
332 + item.getSourceField() + "]");
333 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
338 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
343 * Read and check the list of referencing objects, where the authRef field
344 * is a value instance of a repeatable scalar field.
346 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
347 dependsOnMethods = {"createIntakeWithAuthRefs"}, groups = {"repeatableScalar"})
348 public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
350 if (logger.isDebugEnabled()) {
351 logger.debug(testBanner(testName, CLASS_NAME));
354 testSetup(STATUS_OK, ServiceRequestType.READ);
356 // Get the auth ref docs and check them
358 // Single scalar field
359 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
360 ClientResponse<AuthorityRefDocList> refDocListResp =
361 personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
363 int statusCode = refDocListResp.getStatus();
365 if (logger.isDebugEnabled()) {
366 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
368 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
372 AuthorityRefDocList list = refDocListResp.getEntity();
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 ClientResponse<Response> res = intakeClient.delete(resourceId);
429 res.releaseConnection();
431 // Delete persons before PersonAuth
432 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
433 for (String resourceId : personIdsCreated) {
434 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
435 res.releaseConnection();
437 if (personAuthCSID != null) {
438 personAuthClient.delete(personAuthCSID).releaseConnection();
442 // ---------------------------------------------------------------
443 // Utility methods used by tests above
444 // ---------------------------------------------------------------
446 public String getServicePathComponent() {
447 return SERVICE_PATH_COMPONENT;
450 private PoxPayloadOut createIntakeInstance(String entryNumber,
454 String conditionCheckerAssessor,
457 IntakesCommon intake = new IntakesCommon();
458 intake.setEntryNumber(entryNumber);
459 intake.setEntryDate(entryDate);
460 intake.setCurrentOwner(currentOwner);
461 intake.setDepositor(depositor);
462 intake.setValuer(Valuer);
464 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
465 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
466 checkersOrAssessors.add(conditionCheckerAssessor);
467 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
469 InsurerList insurerList = new InsurerList();
470 List<String> insurers = insurerList.getInsurer();
471 insurers.add(insurer);
472 intake.setInsurers(insurerList);
474 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
475 PayloadOutputPart commonPart =
476 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
477 commonPart.setLabel(new IntakeClient().getCommonPartName());
479 if (logger.isDebugEnabled()) {
480 logger.debug("to be created, intake common");
481 logger.debug(objectAsXmlString(intake, IntakesCommon.class));