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.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 {
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;
89 public String getServiceName() {
90 throw new UnsupportedOperationException(); //FIXME: REM - http://issues.collectionspace.org/browse/CSPACE-3498
94 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
97 protected CollectionSpaceClient getClientInstance() {
98 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
102 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
105 protected AbstractCommonList getAbstractCommonList(
106 ClientResponse<AbstractCommonList> 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", dataProviderClass = AbstractServiceTestImpl.class)
115 public void createIntakeWithAuthRefs(String testName) throws Exception {
117 if (logger.isDebugEnabled()) {
118 logger.debug(testBanner(testName, CLASS_NAME));
120 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
122 // Submit the request to the service and store the response.
123 String identifier = createIdentifier();
125 // Create all the person refs and entities
128 IntakeClient intakeClient = new IntakeClient();
129 PoxPayloadOut multipart = createIntakeInstance(
130 "entryNumber-" + identifier,
131 "entryDate-" + identifier,
134 conditionCheckerAssessorRefName,
138 ClientResponse<Response> res = intakeClient.create(multipart);
140 int statusCode = res.getStatus();
142 // Check the status code of the response: does it match
143 // the expected response(s)?
146 // Does it fall within the set of valid status codes?
147 // Does it exactly match the expected status code?
148 if (logger.isDebugEnabled()) {
149 logger.debug(testName + ": status = " + statusCode);
151 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
155 res.releaseConnection();
158 // Store the ID returned from the first resource created
159 // for additional tests below.
160 if (knownIntakeId == null) {
161 knownIntakeId = extractId(res);
162 if (logger.isDebugEnabled()) {
163 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
167 // Store the IDs from every resource created by tests,
168 // so they can be deleted after tests have been run.
169 intakeIdsCreated.add(extractId(res));
173 * Creates the person refs.
175 protected void createPersonRefs() {
176 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
177 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
178 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
179 ClientResponse<Response> res = personAuthClient.create(multipart);
180 int statusCode = res.getStatus();
182 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
183 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
184 Assert.assertEquals(statusCode, STATUS_CREATED);
185 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);
227 protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
228 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
229 Map<String, String> personInfo = new HashMap<String, String>();
230 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
231 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
232 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
233 PoxPayloadOut multipart =
234 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
235 authRefName, personInfo, personAuthClient.getItemCommonPartName());
236 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
237 int statusCode = res.getStatus();
239 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
240 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
241 Assert.assertEquals(statusCode, STATUS_CREATED);
242 return extractId(res);
246 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
247 dependsOnMethods = {"createIntakeWithAuthRefs"})
248 public void readAndCheckAuthRefDocs(String testName) throws Exception {
250 if (logger.isDebugEnabled()) {
251 logger.debug(testBanner(testName, CLASS_NAME));
254 testSetup(STATUS_OK, ServiceRequestType.READ);
256 // Get the auth ref docs and check them
258 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
259 ClientResponse<AuthorityRefDocList> refDocListResp =
260 personAuthClient.getReferencingObjects(personAuthCSID, currentOwnerPersonCSID);
262 int statusCode = refDocListResp.getStatus();
264 if (logger.isDebugEnabled()) {
265 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
267 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
268 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
269 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
271 AuthorityRefDocList list = refDocListResp.getEntity();
272 List<AuthorityRefDocList.AuthorityRefDocItem> items =
273 list.getAuthorityRefDocItem();
274 Assert.assertTrue(items != null);
275 Assert.assertTrue(items.size() > 0);
277 // Optionally output additional data about list members for debugging.
278 boolean iterateThroughList = true;
279 boolean fFoundIntake = false;
280 if (iterateThroughList && logger.isDebugEnabled()) {
282 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
283 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
284 logger.debug(testName + ": list-item[" + i + "] "
285 + item.getDocType() + "("
286 + item.getDocId() + ") Name:["
287 + item.getDocName() + "] Number:["
288 + item.getDocNumber() + "] in field:["
289 + item.getSourceField() + "]");
290 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
295 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
298 personAuthClient = new PersonAuthorityClient();
300 personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
302 statusCode = refDocListResp.getStatus();
304 if (logger.isDebugEnabled()) {
305 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
307 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
308 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
309 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
311 list = refDocListResp.getEntity();
312 items = list.getAuthorityRefDocItem();
313 Assert.assertTrue(items != null);
314 Assert.assertTrue(items.size() > 0);
315 Assert.assertTrue(items.get(0) != null);
317 // Optionally output additional data about list members for debugging.
318 iterateThroughList = true;
319 fFoundIntake = false;
320 if (iterateThroughList && logger.isDebugEnabled()) {
322 logger.debug(testName + ": Docs that use: " + depositorRefName);
323 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
324 logger.debug(testName + ": list-item[" + i + "] "
325 + item.getDocType() + "("
326 + item.getDocId() + ") Name:["
327 + item.getDocName() + "] Number:["
328 + item.getDocNumber() + "] in field:["
329 + item.getSourceField() + "]");
330 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
335 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
340 * Read and check the list of referencing objects, where the authRef field
341 * is a value instance of a repeatable scalar field.
343 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
344 dependsOnMethods = {"createIntakeWithAuthRefs"}, groups = {"repeatableScalar"})
345 public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
347 if (logger.isDebugEnabled()) {
348 logger.debug(testBanner(testName, CLASS_NAME));
351 testSetup(STATUS_OK, ServiceRequestType.READ);
353 // Get the auth ref docs and check them
355 // Single scalar field
356 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
357 ClientResponse<AuthorityRefDocList> refDocListResp =
358 personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
360 int statusCode = refDocListResp.getStatus();
362 if (logger.isDebugEnabled()) {
363 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
365 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
369 AuthorityRefDocList list = refDocListResp.getEntity();
370 List<AuthorityRefDocList.AuthorityRefDocItem> items =
371 list.getAuthorityRefDocItem();
372 Assert.assertTrue(items != null);
373 Assert.assertTrue(items.size() > 0);
374 Assert.assertTrue(items.get(0) != null);
376 // Optionally output additional data about list members for debugging.
377 boolean iterateThroughList = true;
378 boolean fFoundIntake = false;
379 if (iterateThroughList && logger.isDebugEnabled()) {
381 logger.debug(testName + ": Docs that use: " + insurerRefName);
382 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
383 logger.debug(testName + ": list-item[" + i + "] "
384 + item.getDocType() + "("
385 + item.getDocId() + ") Name:["
386 + item.getDocName() + "] Number:["
387 + item.getDocNumber() + "] in field:["
388 + item.getSourceField() + "]");
389 if (!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
394 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
399 // ---------------------------------------------------------------
400 // Cleanup of resources created during testing
401 // ---------------------------------------------------------------
403 * Deletes all resources created by tests, after all tests have been run.
405 * This cleanup method will always be run, even if one or more tests fail.
406 * For this reason, it attempts to remove all resources created
407 * at any point during testing, even if some of those resources
408 * may be expected to be deleted by certain tests.
410 @AfterClass(alwaysRun = true)
411 public void cleanUp() {
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 ClientResponse<Response> res = intakeClient.delete(resourceId);
426 res.releaseConnection();
428 // Delete persons before PersonAuth
429 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
430 for (String resourceId : personIdsCreated) {
431 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
432 res.releaseConnection();
434 if (personAuthCSID != null) {
435 personAuthClient.delete(personAuthCSID).releaseConnection();
439 // ---------------------------------------------------------------
440 // Utility methods used by tests above
441 // ---------------------------------------------------------------
443 public String getServicePathComponent() {
444 return SERVICE_PATH_COMPONENT;
447 private PoxPayloadOut createIntakeInstance(String entryNumber,
451 String conditionCheckerAssessor,
454 IntakesCommon intake = new IntakesCommon();
455 intake.setEntryNumber(entryNumber);
456 intake.setEntryDate(entryDate);
457 intake.setCurrentOwner(currentOwner);
458 intake.setDepositor(depositor);
459 intake.setValuer(Valuer);
461 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
462 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
463 checkersOrAssessors.add(conditionCheckerAssessor);
464 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
466 InsurerList insurerList = new InsurerList();
467 List<String> insurers = insurerList.getInsurer();
468 insurers.add(insurer);
469 intake.setInsurers(insurerList);
471 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
472 PayloadOutputPart commonPart =
473 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
474 commonPart.setLabel(new IntakeClient().getCommonPartName());
476 if (logger.isDebugEnabled()) {
477 logger.debug("to be created, intake common");
478 logger.debug(objectAsXmlString(intake, IntakesCommon.class));