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.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
39 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
40 import org.collectionspace.services.intake.IntakesCommon;
41 import org.collectionspace.services.intake.InsurerList;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
44 import org.jboss.resteasy.client.ClientResponse;
46 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
48 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * PersonAuthRefDocsTest, carries out tests against a
58 * deployed and running Person Service.
60 * $LastChangedRevision: 1327 $
61 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
63 public class PersonAuthRefDocsTest extends BaseServiceTest {
65 private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
66 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
67 // Instance variables specific to this test.
68 final String SERVICE_PATH_COMPONENT = "intakes";
69 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
70 private String knownIntakeId = null;
71 private List<String> intakeIdsCreated = new ArrayList<String>();
72 private List<String> personIdsCreated = new ArrayList<String>();
73 private String personAuthCSID = null;
74 private String personShortId = PERSON_AUTHORITY_NAME;
75 private String currentOwnerPersonCSID = null;
76 private String depositorPersonCSID = null;
77 private String insurerPersonCSID = null;
78 private String currentOwnerRefName = null;
79 private String depositorRefName = null;
80 private String conditionCheckerAssessorRefName = null;
81 private String insurerRefName = null;
82 private String valuerRefName = null;
83 private String valuerShortId = null;
84 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
90 protected CollectionSpaceClient getClientInstance() {
91 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
95 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
98 protected AbstractCommonList getAbstractCommonList(
99 ClientResponse<AbstractCommonList> response) {
100 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
103 // ---------------------------------------------------------------
104 // CRUD tests : CREATE tests
105 // ---------------------------------------------------------------
107 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
108 public void createIntakeWithAuthRefs(String testName) throws Exception {
110 if (logger.isDebugEnabled()) {
111 logger.debug(testBanner(testName, CLASS_NAME));
113 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
115 // Submit the request to the service and store the response.
116 String identifier = createIdentifier();
118 // Create all the person refs and entities
121 IntakeClient intakeClient = new IntakeClient();
122 MultipartOutput multipart = createIntakeInstance(
123 "entryNumber-" + identifier,
124 "entryDate-" + identifier,
127 conditionCheckerAssessorRefName,
131 ClientResponse<Response> res = intakeClient.create(multipart);
133 int statusCode = res.getStatus();
135 // Check the status code of the response: does it match
136 // the expected response(s)?
139 // Does it fall within the set of valid status codes?
140 // Does it exactly match the expected status code?
141 if (logger.isDebugEnabled()) {
142 logger.debug(testName + ": status = " + statusCode);
144 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
145 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
146 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
148 res.releaseConnection();
151 // Store the ID returned from the first resource created
152 // for additional tests below.
153 if (knownIntakeId == null) {
154 knownIntakeId = extractId(res);
155 if (logger.isDebugEnabled()) {
156 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
160 // Store the IDs from every resource created by tests,
161 // so they can be deleted after tests have been run.
162 intakeIdsCreated.add(extractId(res));
166 * Creates the person refs.
168 protected void createPersonRefs() {
169 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
170 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
171 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
172 ClientResponse<Response> res = personAuthClient.create(multipart);
173 int statusCode = res.getStatus();
175 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177 Assert.assertEquals(statusCode, STATUS_CREATED);
178 personAuthCSID = extractId(res);
180 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
182 String csid = createPerson("Olivier", "Owner", "olivierOwner", authRefName);
183 Assert.assertNotNull(csid);
184 currentOwnerPersonCSID = csid;
185 currentOwnerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
186 Assert.assertNotNull(currentOwnerRefName);
187 personIdsCreated.add(csid);
189 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
190 Assert.assertNotNull(csid);
191 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
192 depositorPersonCSID = csid;
193 Assert.assertNotNull(depositorRefName);
194 personIdsCreated.add(csid);
196 csid = createPerson("Andrew", "Assessor", "andrewAssessor", authRefName);
197 Assert.assertNotNull(csid);
198 conditionCheckerAssessorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
199 Assert.assertNotNull(conditionCheckerAssessorRefName);
200 personIdsCreated.add(csid);
202 csid = createPerson("Ingrid", "Insurer", "ingridInsurer", authRefName);
203 Assert.assertNotNull(csid);
204 insurerPersonCSID = csid;
205 insurerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
206 Assert.assertNotNull(insurerRefName);
207 personIdsCreated.add(csid);
209 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
210 Assert.assertNotNull(csid);
211 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
212 if (logger.isDebugEnabled()) {
213 logger.debug("valuerShortId=" + valuerShortId);
215 Assert.assertNotNull(valuerRefName);
216 personIdsCreated.add(csid);
220 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
221 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
222 Map<String, String> personInfo = new HashMap<String, String>();
223 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
224 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
225 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
226 MultipartOutput multipart =
227 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
228 authRefName, personInfo, personAuthClient.getItemCommonPartName());
229 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
230 int statusCode = res.getStatus();
232 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234 Assert.assertEquals(statusCode, STATUS_CREATED);
235 return extractId(res);
239 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
240 dependsOnMethods = {"createIntakeWithAuthRefs"})
241 public void readAndCheckAuthRefDocs(String testName) throws Exception {
243 if (logger.isDebugEnabled()) {
244 logger.debug(testBanner(testName, CLASS_NAME));
247 testSetup(STATUS_OK, ServiceRequestType.READ);
249 // Get the auth ref docs and check them
251 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
252 ClientResponse<AuthorityRefDocList> refDocListResp =
253 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
255 int statusCode = refDocListResp.getStatus();
257 if (logger.isDebugEnabled()) {
258 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
260 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
261 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
262 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
264 AuthorityRefDocList list = refDocListResp.getEntity();
265 List<AuthorityRefDocList.AuthorityRefDocItem> items =
266 list.getAuthorityRefDocItem();
267 Assert.assertTrue(items != null);
268 Assert.assertTrue(items.size() > 0);
270 // Optionally output additional data about list members for debugging.
271 boolean iterateThroughList = true;
272 boolean fFoundIntake = false;
273 if (iterateThroughList && logger.isDebugEnabled()) {
275 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
276 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
277 logger.debug(testName + ": list-item[" + i + "] "
278 + item.getDocType() + "("
279 + item.getDocId() + ") Name:["
280 + item.getDocName() + "] Number:["
281 + item.getDocNumber() + "] in field:["
282 + item.getSourceField() + "]");
283 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
288 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
291 personAuthClient = new PersonAuthorityClient();
293 personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
295 statusCode = refDocListResp.getStatus();
297 if (logger.isDebugEnabled()) {
298 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
300 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
301 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
302 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
304 list = refDocListResp.getEntity();
305 items = list.getAuthorityRefDocItem();
306 Assert.assertTrue(items != null);
307 Assert.assertTrue(items.size() > 0);
308 Assert.assertTrue(items.get(0) != null);
310 // Optionally output additional data about list members for debugging.
311 iterateThroughList = true;
312 fFoundIntake = false;
313 if (iterateThroughList && logger.isDebugEnabled()) {
315 logger.debug(testName + ": Docs that use: " + depositorRefName);
316 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
317 logger.debug(testName + ": list-item[" + i + "] "
318 + item.getDocType() + "("
319 + item.getDocId() + ") Name:["
320 + item.getDocName() + "] Number:["
321 + item.getDocNumber() + "] in field:["
322 + item.getSourceField() + "]");
323 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
328 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
333 * Read and check the list of referencing objects, where the authRef field
334 * is a value instance of a repeatable scalar field.
336 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
337 dependsOnMethods = {"createIntakeWithAuthRefs"}, groups = {"repeatableScalar"})
338 public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
340 if (logger.isDebugEnabled()) {
341 logger.debug(testBanner(testName, CLASS_NAME));
344 testSetup(STATUS_OK, ServiceRequestType.READ);
346 // Get the auth ref docs and check them
348 // Single scalar field
349 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
350 ClientResponse<AuthorityRefDocList> refDocListResp =
351 personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
353 int statusCode = refDocListResp.getStatus();
355 if (logger.isDebugEnabled()) {
356 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
358 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
359 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
360 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
362 AuthorityRefDocList list = refDocListResp.getEntity();
363 List<AuthorityRefDocList.AuthorityRefDocItem> items =
364 list.getAuthorityRefDocItem();
365 Assert.assertTrue(items != null);
366 Assert.assertTrue(items.size() > 0);
367 Assert.assertTrue(items.get(0) != null);
369 // Optionally output additional data about list members for debugging.
370 boolean iterateThroughList = true;
371 boolean fFoundIntake = false;
372 if (iterateThroughList && logger.isDebugEnabled()) {
374 logger.debug(testName + ": Docs that use: " + insurerRefName);
375 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
376 logger.debug(testName + ": list-item[" + i + "] "
377 + item.getDocType() + "("
378 + item.getDocId() + ") Name:["
379 + item.getDocName() + "] Number:["
380 + item.getDocNumber() + "] in field:["
381 + item.getSourceField() + "]");
382 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
387 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
392 // ---------------------------------------------------------------
393 // Cleanup of resources created during testing
394 // ---------------------------------------------------------------
396 * Deletes all resources created by tests, after all tests have been run.
398 * This cleanup method will always be run, even if one or more tests fail.
399 * For this reason, it attempts to remove all resources created
400 * at any point during testing, even if some of those resources
401 * may be expected to be deleted by certain tests.
403 @AfterClass(alwaysRun = true)
404 public void cleanUp() {
405 String noTest = System.getProperty("noTestCleanup");
406 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
407 if (logger.isDebugEnabled()) {
408 logger.debug("Skipping Cleanup phase ...");
412 if (logger.isDebugEnabled()) {
413 logger.debug("Cleaning up temporary resources created for testing ...");
415 IntakeClient intakeClient = new IntakeClient();
416 // Note: Any non-success responses are ignored and not reported.
417 for (String resourceId : intakeIdsCreated) {
418 ClientResponse<Response> res = intakeClient.delete(resourceId);
419 res.releaseConnection();
421 // Delete persons before PersonAuth
422 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
423 for (String resourceId : personIdsCreated) {
424 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
425 res.releaseConnection();
427 if (personAuthCSID != null) {
428 personAuthClient.delete(personAuthCSID).releaseConnection();
432 // ---------------------------------------------------------------
433 // Utility methods used by tests above
434 // ---------------------------------------------------------------
436 public String getServicePathComponent() {
437 return SERVICE_PATH_COMPONENT;
440 private MultipartOutput createIntakeInstance(String entryNumber,
444 String conditionCheckerAssessor,
447 IntakesCommon intake = new IntakesCommon();
448 intake.setEntryNumber(entryNumber);
449 intake.setEntryDate(entryDate);
450 intake.setCurrentOwner(currentOwner);
451 intake.setDepositor(depositor);
452 intake.setValuer(Valuer);
454 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
455 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
456 checkersOrAssessors.add(conditionCheckerAssessor);
457 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
459 InsurerList insurerList = new InsurerList();
460 List<String> insurers = insurerList.getInsurer();
461 insurers.add(insurer);
462 intake.setInsurers(insurerList);
464 MultipartOutput multipart = new MultipartOutput();
465 OutputPart commonPart =
466 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
467 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
469 if (logger.isDebugEnabled()) {
470 logger.debug("to be created, intake common");
471 logger.debug(objectAsXmlString(intake, IntakesCommon.class));