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;
29 import javax.ws.rs.core.Response;
31 import org.collectionspace.services.PersonJAXBSchema;
32 import org.collectionspace.services.client.CollectionSpaceClient;
33 import org.collectionspace.services.client.HeldintrustClient;
34 import org.collectionspace.services.client.PersonAuthorityClientUtils;
35 import org.collectionspace.services.client.PersonClient;
36 import org.collectionspace.services.client.PoxPayloadOut;
37 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
38 import org.collectionspace.services.heldintrust.HeldintrustsCommon;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.collectionspace.services.person.PersonTermGroup;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.testng.Assert;
44 import org.testng.annotations.AfterClass;
45 import org.testng.annotations.Test;
48 * PersonAuthRefDocsTest, carries out tests against a
49 * deployed and running Person Service.
51 * $LastChangedRevision: 1327 $
52 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
54 public class PersonAuthRefDocsTest extends BaseServiceTest<AbstractCommonList> {
56 // Instance variables specific to this test.
57 final String SERVICE_PATH_COMPONENT = "hits";
58 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
59 private final String CLASS_NAME = PersonAuthRefDocsTest.class.getName();
60 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
61 private String knownHitId = null;
62 private final List<String> hitIdsCreated = new ArrayList<String>();
63 private final List<String> personIdsCreated = new ArrayList<String>();
64 private String personAuthCSID = null;
65 private String currentContactPersonCSID = null;
66 private String depositorPersonCSID = null;
67 private String insurerPersonCSID = null;
68 private String depositorContactRefName = null;
69 private String depositorRefName = null;
70 private String externalApprovalIndividualRefName = null;
71 private String correspondenceSenderRefName = null;
72 private String valuerRefName = null;
73 private final String valuerShortId = null;
76 public String getServiceName() {
77 throw new UnsupportedOperationException(); //FIXME: REM - http://issues.collectionspace.org/browse/CSPACE-3498
81 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
84 protected CollectionSpaceClient getClientInstance() {
85 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
89 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
90 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
94 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client
98 protected AbstractCommonList getCommonList(Response response) {
99 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
102 // ---------------------------------------------------------------
103 // CRUD tests : CREATE tests
104 // ---------------------------------------------------------------
106 @Test(dataProvider = "testName")
107 public void createHitWithAuthRefs(String testName) throws Exception {
108 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
110 // Submit the request to the service and store the response.
111 String identifier = createIdentifier();
113 // Create all the person refs and entities
116 HeldintrustClient heldInTrustClient = new HeldintrustClient();
117 PoxPayloadOut multipart = createHitInstance(
118 "entryNumber-" + identifier,
119 depositorContactRefName,
121 externalApprovalIndividualRefName,
122 correspondenceSenderRefName);
124 Response res = heldInTrustClient.create(multipart);
126 int statusCode = res.getStatus();
128 // Check the status code of the response: does it match
129 // the expected response(s)?
132 // Does it fall within the set of valid status codes?
133 // Does it exactly match the expected status code?
134 logger.debug("{}: status = {}", testName, statusCode);
135 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
136 invalidStatusCodeMessage(testRequestType, statusCode));
137 Assert.assertEquals(statusCode, testExpectedStatusCode);
142 // Store the ID returned from the first resource created
143 // for additional tests below.
144 if (knownHitId == null) {
145 knownHitId = extractId(res);
146 logger.debug("{}: knownHitId={}", testName, knownHitId);
149 // Store the IDs from every resource created by tests,
150 // so they can be deleted after tests have been run.
151 hitIdsCreated.add(extractId(res));
155 * Creates the person refs.
159 protected void createPersonRefs() throws Exception {
160 PersonClient personAuthClient = new PersonClient();
161 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
162 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
163 Response res = personAuthClient.create(multipart);
165 int statusCode = res.getStatus();
166 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
167 invalidStatusCodeMessage(testRequestType, statusCode));
168 Assert.assertEquals(statusCode, STATUS_CREATED);
169 personAuthCSID = extractId(res);
174 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
176 String csid = createPerson("Olivier", "Contact", "olivierContact", authRefName);
177 Assert.assertNotNull(csid);
178 currentContactPersonCSID = csid;
179 depositorContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
180 Assert.assertNotNull(depositorContactRefName);
181 personIdsCreated.add(csid);
183 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
184 Assert.assertNotNull(csid);
185 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
186 depositorPersonCSID = csid;
187 Assert.assertNotNull(depositorRefName);
188 personIdsCreated.add(csid);
190 csid = createPerson("Andrew", "ApprovalIndividual", "andrewApprovalIndividual", authRefName);
191 Assert.assertNotNull(csid);
192 externalApprovalIndividualRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
193 Assert.assertNotNull(externalApprovalIndividualRefName);
194 personIdsCreated.add(csid);
196 csid = createPerson("Ingrid", "Sender", "ingridSender", authRefName);
197 Assert.assertNotNull(csid);
198 insurerPersonCSID = csid;
199 correspondenceSenderRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
200 Assert.assertNotNull(correspondenceSenderRefName);
201 personIdsCreated.add(csid);
203 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
204 Assert.assertNotNull(csid);
205 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
206 logger.debug("valuerShortId={}", valuerShortId);
207 Assert.assertNotNull(valuerRefName);
208 personIdsCreated.add(csid);
211 protected String createPerson(String firstName, String surName, String shortId, String authRefName)
215 PersonClient personAuthClient = new PersonClient();
216 Map<String, String> personInfo = new HashMap<String, String>();
217 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
218 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
219 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
220 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
221 PersonTermGroup term = new PersonTermGroup();
222 String termName = firstName + " " + surName;
223 term.setTermDisplayName(termName);
224 term.setTermName(termName);
225 personTerms.add(term);
226 PoxPayloadOut multipart =
227 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
228 authRefName, personInfo, personTerms,
229 personAuthClient.getItemCommonPartName());
230 Response res = personAuthClient.createItem(personAuthCSID, multipart);
232 int statusCode = res.getStatus();
234 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
235 invalidStatusCodeMessage(testRequestType, statusCode));
236 Assert.assertEquals(statusCode, STATUS_CREATED);
237 result = extractId(res);
246 @Test(dataProvider = "testName", dependsOnMethods = {"createHitWithAuthRefs"})
247 public void readAndCheckAuthRefDocs(String testName) throws Exception {
249 testSetup(STATUS_OK, ServiceRequestType.READ);
251 // Get the auth ref docs and check them
253 PersonClient personAuthClient = new PersonClient();
254 Response res = personAuthClient.getReferencingObjects(personAuthCSID, currentContactPersonCSID);
255 AuthorityRefDocList list;
257 assertStatusCode(res, testName);
258 list = res.readEntity(AuthorityRefDocList.class);
265 List<AuthorityRefDocList.AuthorityRefDocItem> items = list.getAuthorityRefDocItem();
266 Assert.assertNotNull(items);
267 Assert.assertFalse(items.isEmpty());
269 // output additional data about list members for debugging.
270 boolean fFoundHit = false;
272 logger.debug("{}: Docs that use: {}", testName, depositorContactRefName);
273 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
274 logger.debug("{}: list-item[{}] {} ({}) Name:[{}] Number:[{}] in field:[{}]", testName, i,
275 item.getDocType(), item.getDocId(), item.getDocName(), item.getDocNumber(),
276 item.getSourceField());
277 if (!fFoundHit && knownHitId.equalsIgnoreCase(item.getDocId())) {
282 Assert.assertTrue(fFoundHit, "Did not find Hit with authref!");
285 // Get the referencing objects
287 personAuthClient = new PersonClient();
288 res = personAuthClient.getReferencingObjects(personAuthCSID, depositorPersonCSID);
290 assertStatusCode(res, testName);
291 list = res.readEntity(AuthorityRefDocList.class);
298 items = list.getAuthorityRefDocItem();
299 Assert.assertNotNull(items);
300 Assert.assertFalse(items.isEmpty());
301 Assert.assertNotNull(items.get(0));
303 // Optionally output additional data about list members for debugging.
305 logger.debug("{}: Docs that use: {}", testName, depositorRefName);
307 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
308 logger.debug("{}: list-item[{}] {} ({}) Name:[{}] Number:[{}] in field:[{}]", testName, i,
309 item.getDocType(), item.getDocId(), item.getDocName(), item.getDocNumber(),
310 item.getSourceField());
311 if (!fFoundHit && knownHitId.equalsIgnoreCase(item.getDocId())) {
316 Assert.assertTrue(fFoundHit, "Did not find Hit with authref!");
320 * Read and check the list of referencing objects, where the authRef field
321 * is a value instance of a repeatable scalar field.
323 @Test(dataProvider = "testName", dependsOnMethods = {"createHitWithAuthRefs"},
324 groups = {"repeatableScalar"})
325 public void readAndCheckAuthRefDocsRepeatableScalar(String testName) throws Exception {
327 testSetup(STATUS_OK, ServiceRequestType.READ);
329 // Get the auth ref docs and check them
331 // Single scalar field
332 PersonClient personAuthClient = new PersonClient();
333 Response res = personAuthClient.getReferencingObjects(personAuthCSID, insurerPersonCSID);
334 AuthorityRefDocList list;
336 assertStatusCode(res, testName);
337 list = res.readEntity(AuthorityRefDocList.class);
344 List<AuthorityRefDocList.AuthorityRefDocItem> items = list.getAuthorityRefDocItem();
345 Assert.assertNotNull(items);
346 Assert.assertFalse(items.isEmpty());
347 Assert.assertNotNull(items.get(0));
349 // Optionally output additional data about list members for debugging.
350 boolean fFoundHit = false;
352 logger.debug("{}: Docs that use: {}", testName, correspondenceSenderRefName);
353 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
354 logger.debug("{}: list-item[{}] {} ({}) Name:[{}] Number:[{}] in field:[{}]", testName, i,
355 item.getDocType(), item.getDocId(), item.getDocName(), item.getDocNumber(),
356 item.getSourceField());
357 if (!fFoundHit && knownHitId.equalsIgnoreCase(item.getDocId())) {
362 Assert.assertTrue(fFoundHit, "Did not find Hit with authref!");
366 // ---------------------------------------------------------------
367 // Cleanup of resources created during testing
368 // ---------------------------------------------------------------
371 * Deletes all resources created by tests, after all tests have been run.
373 * This cleanup method will always be run, even if one or more tests fail.
374 * For this reason, it attempts to remove all resources created
375 * at any point during testing, even if some of those resources
376 * may be expected to be deleted by certain tests.
380 @AfterClass(alwaysRun = true)
381 public void cleanUp() throws Exception {
382 String noTest = System.getProperty("noTestCleanup");
383 if (Boolean.parseBoolean(noTest)) {
384 logger.debug("Skipping Cleanup phase ...");
388 logger.debug("Cleaning up temporary resources created for testing ...");
389 HeldintrustClient heldInTrustClient = new HeldintrustClient();
390 // Note: Any non-success responses are ignored and not reported.
391 for (String resourceId : hitIdsCreated) {
392 heldInTrustClient.delete(resourceId).close();
394 // Delete persons before PersonAuth
395 PersonClient personAuthClient = new PersonClient();
396 for (String resourceId : personIdsCreated) {
397 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
399 if (personAuthCSID != null) {
400 personAuthClient.delete(personAuthCSID).close();
404 // ---------------------------------------------------------------
405 // Utility methods used by tests above
406 // ---------------------------------------------------------------
408 public String getServicePathComponent() {
409 return SERVICE_PATH_COMPONENT;
412 private PoxPayloadOut createHitInstance(String heldInTrustNumber,
413 String depositorContact,
415 String externalApprovalIndividual,
416 String correspondenceSender) throws Exception {
417 HeldintrustsCommon hit =
418 HeldintrustClientTestUtil.createHitInstance(heldInTrustNumber, depositorContact, depositor,
419 externalApprovalIndividual, correspondenceSender);
420 hit.setHeldInTrustNumber(heldInTrustNumber);
422 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
423 multipart.addPart(new HeldintrustClient().getCommonPartName(), hit);
425 logger.debug("to be created, hit common");
426 logger.debug("{}", objectAsXmlString(hit, HeldintrustsCommon.class));