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.math.BigInteger;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
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.PropagationClient;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.client.PayloadOutputPart;
39 import org.collectionspace.services.client.PoxPayloadIn;
40 import org.collectionspace.services.client.PoxPayloadOut;
41 import org.collectionspace.services.common.authorityref.AuthorityRefList;
42 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
43 import org.collectionspace.services.jaxb.AbstractCommonList;
44 import org.collectionspace.services.propagation.PropActivityGroup;
45 import org.collectionspace.services.propagation.PropActivityGroupList;
46 import org.collectionspace.services.propagation.PropagationsCommon;
47 import org.collectionspace.services.person.PersonTermGroup;
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 * PropagationAuthRefsTest, carries out Authority References tests against a
58 * deployed and running Propagation (aka Loans In) Service.
60 * $LastChangedRevision$
63 public class PropagationAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
65 private final String CLASS_NAME = PropagationAuthRefsTest.class.getName();
66 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
68 // Instance variables specific to this test.
69 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
70 private String knownResourceId = null;
71 private List<String> propagationIdsCreated = new ArrayList<String>();
72 private List<String> personIdsCreated = new ArrayList<String>();
73 private String personAuthCSID = null;
74 private String propagatedByRefName = null; // an authRef field
75 private final static String CURRENT_DATE_UTC = GregorianCalendarDateTimeUtils.currentDateUTC();
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
85 // ---------------------------------------------------------------
86 // CRUD tests : CREATE tests
87 // ---------------------------------------------------------------
89 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
90 public void createWithAuthRefs(String testName) throws Exception {
91 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
93 // Submit the request to the service and store the response.
94 String identifier = createIdentifier();
96 // Create all the person refs and entities
99 // Create a new Loans In resource.
101 // One or more fields in this resource will be PersonAuthority
102 // references, and will refer to Person resources by their refNames.
103 PropagationClient propagationClient = new PropagationClient();
104 PoxPayloadOut propagationInstance = createPropagationInstance(
105 "propagationNumber-" + identifier,
106 this.propagatedByRefName,
108 Response response = propagationClient.create(propagationInstance);
110 int statusCode = response.getStatus();
111 if (logger.isDebugEnabled()) {
112 logger.debug(testName + ": status = " + statusCode);
114 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode), invalidStatusCodeMessage(testRequestType, statusCode));
115 Assert.assertEquals(statusCode, testExpectedStatusCode);
117 // Store the ID returned from the first resource created
118 // for additional tests below.
119 if (knownResourceId == null) {
120 knownResourceId = extractId(response);
123 // Store the IDs from every resource created by tests,
124 // so they can be deleted after tests have been run.
125 propagationIdsCreated.add(extractId(response));
132 * Create one or more Person records that will be used to create refNames (referenced terms) in our
133 * test propagation records.
137 protected void createPersonRefs() throws Exception {
138 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
139 // Create a temporary PersonAuthority resource, and its corresponding
140 // refName by which it can be identified.
141 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
142 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
143 Response res = personAuthClient.create(multipart);
145 int statusCode = res.getStatus();
146 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
147 invalidStatusCodeMessage(testRequestType, statusCode));
148 Assert.assertEquals(statusCode, STATUS_CREATED);
149 personAuthCSID = extractId(res);
154 // Create temporary Person resources, and their corresponding refNames
155 // by which they can be identified.
156 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, personAuthClient);
157 String csid = createPerson("Propye", "ThePropagator", "proppy", authRefName);
158 personIdsCreated.add(csid);
160 // Safe the refName for later use -see createWithAuthRefs() method
161 this.propagatedByRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, personAuthClient);
164 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) throws Exception {
165 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
166 Map<String, String> personInfo = new HashMap<String,String>();
167 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
168 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
169 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
170 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
171 PersonTermGroup term = new PersonTermGroup();
172 String termName = firstName + " " + surName;
173 term.setTermDisplayName(termName);
174 term.setTermName(termName);
175 personTerms.add(term);
176 PoxPayloadOut multipart =
177 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
178 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
180 Response res = personAuthClient.createItem(personAuthCSID, multipart);
182 int statusCode = res.getStatus();
184 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
185 invalidStatusCodeMessage(testRequestType, statusCode));
186 Assert.assertEquals(statusCode, STATUS_CREATED);
187 return extractId(res);
194 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
195 dependsOnMethods = {"createWithAuthRefs"})
196 public void readAndCheckAuthRefs(String testName) throws Exception {
198 testSetup(STATUS_OK, ServiceRequestType.READ);
200 PropagationClient propagationClient = new PropagationClient();
201 Response res = propagationClient.read(knownResourceId);
203 assertStatusCode(res, testName);
204 // Extract the common part from the response.
205 PoxPayloadIn input = new PoxPayloadIn((String)res.readEntity(String.class));
206 PropagationsCommon propagationCommon = (PropagationsCommon) extractPart(input,
207 propagationClient.getCommonPartName(), PropagationsCommon.class);
208 Assert.assertNotNull(propagationCommon);
209 if (logger.isDebugEnabled()){
210 logger.debug(objectAsXmlString(propagationCommon, PropagationsCommon.class));
218 // Get the authority references
219 res = propagationClient.getAuthorityRefs(knownResourceId); // AuthorityRefList
220 AuthorityRefList list = null;
222 assertStatusCode(res, testName);
223 list = (AuthorityRefList) res.getEntity();
224 Assert.assertNotNull(list);
231 int expectedAuthRefs = personIdsCreated.size();
232 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
233 int numAuthRefsFound = items.size();
234 if(logger.isDebugEnabled()){
235 logger.debug("Expected " + expectedAuthRefs + " authority references, found " + numAuthRefsFound);
238 // Optionally output additional data about list members for debugging.
239 boolean iterateThroughList = true;
240 if(iterateThroughList && logger.isDebugEnabled()){
242 for(AuthorityRefList.AuthorityRefItem item : items){
243 logger.debug(testName + ": list-item[" + i + "] Field:" +
244 item.getSourceField() + "= " +
245 item.getAuthDisplayName() +
246 item.getItemDisplayName());
247 logger.debug(testName + ": list-item[" + i + "] refName=" +
249 logger.debug(testName + ": list-item[" + i + "] URI=" +
255 Assert.assertEquals(numAuthRefsFound, expectedAuthRefs,
256 "Did not find all expected authority references! " + "Expected " + expectedAuthRefs + ", found " + numAuthRefsFound);
260 // ---------------------------------------------------------------
261 // Cleanup of resources created during testing
262 // ---------------------------------------------------------------
265 * Deletes all resources created by tests, after all tests have been run.
267 * This cleanup method will always be run, even if one or more tests fail.
268 * For this reason, it attempts to remove all resources created
269 * at any point during testing, even if some of those resources
270 * may be expected to be deleted by certain tests.
273 @AfterClass(alwaysRun=true)
274 public void cleanUp() throws Exception {
275 String noTest = System.getProperty("noTestCleanup");
276 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
277 if (logger.isDebugEnabled()) {
278 logger.debug("Skipping Cleanup phase ...");
282 if (logger.isDebugEnabled()) {
283 logger.debug("Cleaning up temporary resources created for testing ...");
287 // Delete all the propagation records we created
288 PropagationClient propagationClient = new PropagationClient();
289 for (String resourceId : propagationIdsCreated) {
290 // Note: Any non-success responses are ignored and not reported.
291 propagationClient.delete(resourceId).close(); // alternative to propagationClient.delete(resourceId).releaseConnection();
295 // Delete Person resource(s) (before PersonAuthority resources).
296 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
297 for (String resourceId : personIdsCreated) {
298 // Note: Any non-success responses are ignored and not reported.
299 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
301 if (personAuthCSID != null) {
302 personAuthClient.delete(personAuthCSID).close();
306 // ---------------------------------------------------------------
307 // Utility methods used by tests above
308 // ---------------------------------------------------------------
309 public String getServiceName() {
310 return PropagationClient.SERVICE_NAME;
314 public String getServicePathComponent() {
315 return PropagationClient.SERVICE_PATH_COMPONENT;
318 private PoxPayloadOut createPropagationInstance(String propagationNumber,
320 String returnDate) throws Exception {
321 PropagationsCommon propagationCommon = new PropagationsCommon();
322 propagationCommon.setPropNumber(propagationNumber);
323 propagationCommon.setPropBy(propagatedBy);
324 propagationCommon.setPropNumber(returnDate);
326 PropActivityGroupList propActivityGroupList = new PropActivityGroupList();
327 PropActivityGroup propActivityGroup = new PropActivityGroup();
328 propActivityGroup.setOrder(BigInteger.valueOf(42));
329 propActivityGroupList.getPropActivityGroup().add(propActivityGroup);
330 propagationCommon.setPropActivityGroupList(propActivityGroupList);
332 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
333 PayloadOutputPart commonPart = multipart.addPart(new PropagationClient().getCommonPartName(), propagationCommon);
335 if (logger.isDebugEnabled()) {
336 logger.debug("to be created, propagation common");
337 logger.debug(objectAsXmlString(propagationCommon, PropagationsCommon.class));
344 protected Class<AbstractCommonList> getCommonListType() {
345 return AbstractCommonList.class;
349 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
350 // TODO Auto-generated method stub
351 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class