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.PoxPayloadIn;
37 import org.collectionspace.services.client.PoxPayloadOut;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList;
39 import org.collectionspace.services.heldintrust.HeldintrustsCommon;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.collectionspace.services.person.PersonTermGroup;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.testng.Assert;
45 import org.testng.annotations.AfterClass;
46 import org.testng.annotations.Test;
49 * HitAuthRefsTest, carries out tests against a
50 * deployed and running Hit Service.
52 * $LastChangedRevision: 1327 $
53 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
55 public class HeldintrustAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
57 // Instance variables specific to this test.
58 final String SERVICE_PATH_COMPONENT = HeldintrustClient.SERVICE_PATH_COMPONENT;
59 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
60 private final String CLASS_NAME = HeldintrustAuthRefsTest.class.getName();
61 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
63 private final List<String> hitIdsCreated = new ArrayList<String>();
64 private final List<String> personIdsCreated = new ArrayList<String>();
65 private String personAuthCSID = null;
66 private String depositorContactRefName = null;
67 private String depositorRefName = null;
68 private String externalApprovalIndividualRefName = null;
69 private String correspondenceSenderRefName = null;
70 private String valuerRefName = null;
73 protected String getServiceName() {
74 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
78 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
81 protected CollectionSpaceClient getClientInstance() {
82 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
86 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
87 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
91 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client
95 protected AbstractCommonList getCommonList(Response response) {
96 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
99 // ---------------------------------------------------------------
100 // CRUD tests : CREATE tests
101 // ---------------------------------------------------------------
103 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
104 public void createWithAuthRefs(String testName) throws Exception {
105 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
107 // Submit the request to the service and store the response.
108 String identifier = createIdentifier();
110 // Create all the person refs and entities
113 // Submit the request to the service and store the response.
114 HeldintrustClient heldInTrustClient = new HeldintrustClient();
115 PoxPayloadOut multipart = createHitInstance(
116 "entryNumber-" + identifier,
117 depositorContactRefName,
119 externalApprovalIndividualRefName,
120 correspondenceSenderRefName);
123 Response res = heldInTrustClient.create(multipart);
125 int statusCode = res.getStatus();
126 // Check the status code of the response: does it match
127 // the expected response(s)?
130 // Does it fall within the set of valid status codes?
131 // Does it exactly match the expected status code?
132 logger.debug("{}: status = {}", testName, statusCode);
133 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
134 invalidStatusCodeMessage(testRequestType, statusCode));
135 Assert.assertEquals(statusCode, testExpectedStatusCode);
136 newId = extractId(res);
141 // Store the ID returned from the first resource created
142 // for additional tests below.
143 if (knownResourceId == null) {
144 knownResourceId = newId;
145 logger.debug("{}: knownResourceId={}", testName, knownResourceId);
148 // Store the IDs from every resource created by tests,
149 // so they can be deleted after tests have been run.
150 hitIdsCreated.add(newId);
153 protected void createPersonRefs() throws Exception {
155 // First, create a new person authority
157 PersonClient personAuthClient = new PersonClient();
158 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
159 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
160 Response res = personAuthClient.create(multipart);
162 int statusCode = res.getStatus();
163 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
164 invalidStatusCodeMessage(testRequestType, statusCode));
165 Assert.assertEquals(statusCode, STATUS_CREATED);
166 personAuthCSID = extractId(res);
171 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
172 String csid = createPerson("Olivier", "Contact", "olivierContact", authRefName);
173 depositorContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
174 personIdsCreated.add(csid);
176 csid = createPerson("Debbie", "Depositor", "debbieDepositor", authRefName);
177 depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
178 personIdsCreated.add(csid);
180 csid = createPerson("Andrew", "ApprovalIndividual", "andrewApprovalIndividual", authRefName);
181 externalApprovalIndividualRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
182 personIdsCreated.add(csid);
184 csid = createPerson("Ingrid", "Sender", "ingridSender", authRefName);
185 correspondenceSenderRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
186 personIdsCreated.add(csid);
188 csid = createPerson("Vince", "Valuer", "vinceValuer", authRefName);
189 valuerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
190 personIdsCreated.add(csid);
193 protected String createPerson(String firstName, String surName, String shortId, String authRefName)
197 PersonClient personAuthClient = new PersonClient();
198 Map<String, String> personInfo = new HashMap<String, String>();
199 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
200 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
201 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
202 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
203 PersonTermGroup term = new PersonTermGroup();
204 String termName = firstName + " " + surName;
205 term.setTermDisplayName(termName);
206 term.setTermName(termName);
207 personTerms.add(term);
208 PoxPayloadOut multipart =
209 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
210 authRefName, personInfo, personTerms,
211 personAuthClient.getItemCommonPartName());
213 Response res = personAuthClient.createItem(personAuthCSID, multipart);
215 int statusCode = res.getStatus();
216 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
217 invalidStatusCodeMessage(testRequestType, statusCode));
218 Assert.assertEquals(statusCode, STATUS_CREATED);
219 result = extractId(res);
228 @Test(dataProvider = "testName", dependsOnMethods = {"createWithAuthRefs"})
229 public void readAndCheckAuthRefs(String testName) throws Exception {
231 testSetup(STATUS_OK, ServiceRequestType.READ);
233 // Submit the request to the service and store the response.
234 HeldintrustClient heldInTrustClient = new HeldintrustClient();
235 Response res = heldInTrustClient.read(knownResourceId);
237 assertStatusCode(res, testName);
238 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
239 HeldintrustsCommon hit = (HeldintrustsCommon) extractPart(input, heldInTrustClient.getCommonPartName(),
240 HeldintrustsCommon.class);
241 Assert.assertNotNull(hit);
242 // Check a couple of fields
249 // Get the auth refs and check them
250 res = heldInTrustClient.getAuthorityRefs(knownResourceId);
251 AuthorityRefList list;
253 assertStatusCode(res, testName);
254 list = res.readEntity(AuthorityRefList.class);
261 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
262 int numAuthRefsFound = items.size();
263 logger.debug("Expected {} authority references, found {}", personIdsCreated.size(), numAuthRefsFound);
265 // output additional data about list members for debugging.
267 for (AuthorityRefList.AuthorityRefItem item : items) {
268 logger.debug("{}: list-item[{}] Field:{}={}{}", testName, i, item.getSourceField(),
269 item.getAuthDisplayName(), item.getItemDisplayName());
270 logger.debug("{}: list-item[{}] refName={}", testName, i, item.getRefName());
271 logger.debug("{}: list-item[{}] URI={}", testName, i, item.getUri());
276 // Ensure we got the correct number of authRefs
277 Assert.assertEquals(numAuthRefsFound, personIdsCreated.size(),
278 "Did not find all expected authority references! " + "Expected " + personIdsCreated.size() +
279 ", found " + numAuthRefsFound);
283 // ---------------------------------------------------------------
284 // Cleanup of resources created during testing
285 // ---------------------------------------------------------------
288 * Deletes all resources created by tests, after all tests have been run.
290 * This cleanup method will always be run, even if one or more tests fail.
291 * For this reason, it attempts to remove all resources created
292 * at any point during testing, even if some of those resources
293 * may be expected to be deleted by certain tests.
297 @AfterClass(alwaysRun = true)
298 public void cleanUp() throws Exception {
299 String noTest = System.getProperty("noTestCleanup");
300 if (Boolean.parseBoolean(noTest)) {
301 logger.debug("Skipping Cleanup phase ...");
305 logger.debug("Cleaning up temporary resources created for testing ...");
306 HeldintrustClient heldInTrustClient = new HeldintrustClient();
307 // Note: Any non-success responses are ignored and not reported.
308 for (String resourceId : hitIdsCreated) {
309 heldInTrustClient.delete(resourceId).close();
312 // Delete all the person records then the parent resource
313 PersonClient personAuthClient = new PersonClient();
314 for (String resourceId : personIdsCreated) {
315 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
317 personAuthClient.delete(personAuthCSID).close();
320 // ---------------------------------------------------------------
321 // Utility methods used by tests above
322 // ---------------------------------------------------------------
324 public String getServicePathComponent() {
325 return SERVICE_PATH_COMPONENT;
328 private PoxPayloadOut createHitInstance(String heldInTrustNumber,
329 String depositorContact,
331 String externalApprovalIndividual,
332 String correspondenceSender) throws Exception {
333 HeldintrustsCommon hit =
334 HeldintrustClientTestUtil.createHitInstance(heldInTrustNumber, depositorContact, depositor,
335 externalApprovalIndividual, correspondenceSender);
336 hit.setHeldInTrustNumber(heldInTrustNumber);
338 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
339 multipart.addPart(new HeldintrustClient().getCommonPartName(), hit);
341 logger.debug("to be created, hit common");
342 logger.debug("{}", objectAsXmlString(hit, HeldintrustsCommon.class));