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.MovementClient;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.client.PayloadInputPart;
39 import org.collectionspace.services.client.PayloadOutputPart;
40 import org.collectionspace.services.client.PoxPayloadIn;
41 import org.collectionspace.services.client.PoxPayloadOut;
42 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
43 import org.collectionspace.services.common.authorityref.AuthorityRefList;
44 import org.collectionspace.services.jaxb.AbstractCommonList;
45 import org.collectionspace.services.movement.MovementsCommon;
46 import org.collectionspace.services.person.PersonTermGroup;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.Test;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * MovementAuthRefsTest, carries out Authority References tests against a
56 * deployed and running Movement Service.
58 * $LastChangedRevision$
61 public class MovementAuthRefsTest extends BaseServiceTest<AbstractCommonList> {
63 private final String CLASS_NAME = MovementAuthRefsTest.class.getName();
64 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
65 final String SERVICE_NAME = "movements";
66 final String SERVICE_PATH_COMPONENT = "movements";
68 // Instance variables specific to this test.
69 final String PERSON_AUTHORITY_NAME = "TestPersonAuthForMovementTest";
70 private List<String> movementIdsCreated = new ArrayList<String>();
71 private List<String> personIdsCreated = new ArrayList<String>();
72 private String personAuthCSID = null;
73 private String movementContactRefName = null;
75 // FIXME: Can add 'current location' and 'normal location'
76 // as authRefs to tests below, and increase the
77 // number of expected authRefs to 3.
78 private final int NUM_AUTH_REFS_EXPECTED = 1;
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.ClientResponse)
97 protected AbstractCommonList getCommonList(Response response) {
98 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
101 // ---------------------------------------------------------------
102 // CRUD tests : CREATE tests
103 // ---------------------------------------------------------------
105 @Test(dataProvider="testName")
106 public void createWithAuthRefs(String testName) throws Exception {
107 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
109 // Submit the request to the service and store the response.
110 String identifier = createIdentifier();
112 // Create all the person refs and entities
115 // Create a new Movement resource.
117 // One or more fields in this resource will be PersonAuthority
118 // references, and will refer to Person resources by their refNames.
119 MovementClient movementClient = new MovementClient();
120 PoxPayloadOut multipart = createMovementInstance(
121 "movementReferenceNumber-" + identifier,
122 GregorianCalendarDateTimeUtils.timestampUTC(),
123 movementContactRefName);
125 Response res = movementClient.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 if(logger.isDebugEnabled()){
135 logger.debug(testName + ": status = " + statusCode);
137 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
138 invalidStatusCodeMessage(testRequestType, statusCode));
139 Assert.assertEquals(statusCode, testExpectedStatusCode);
140 newId = extractId(res);
145 // Store the ID returned from the first resource created
146 // for additional tests below.
147 if (knownResourceId == null){
148 knownResourceId = newId;
149 if (logger.isDebugEnabled()) {
150 logger.debug(testName + ": knownResourceId=" + knownResourceId);
154 // Store the IDs from every resource created by tests,
155 // so they can be deleted after tests have been run.
156 movementIdsCreated.add(newId);
159 protected void createPersonRefs(){
160 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
161 // Create a temporary PersonAuthority resource, and its corresponding
162 // refName by which it can be identified.
163 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
164 PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
165 Response res = personAuthClient.create(multipart);
167 int statusCode = res.getStatus();
169 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
170 invalidStatusCodeMessage(testRequestType, statusCode));
171 Assert.assertEquals(statusCode, STATUS_CREATED);
172 personAuthCSID = extractId(res);
177 String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
179 // Create temporary Person resources, and their corresponding refNames
180 // by which they can be identified.
181 String csid = createPerson("Melvin", "MovementContact", "melvinMovementContact", authRefName);
182 personIdsCreated.add(csid);
183 movementContactRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
186 protected String createPerson(String firstName, String surName, String shortId, String authRefName ) {
187 String result = null;
189 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
190 Map<String, String> personInfo = new HashMap<String,String>();
191 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
192 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
193 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
194 List<PersonTermGroup> personTerms = new ArrayList<PersonTermGroup>();
195 PersonTermGroup term = new PersonTermGroup();
196 String termName = firstName + " " + surName;
197 term.setTermDisplayName(termName);
198 term.setTermName(termName);
199 personTerms.add(term);
200 PoxPayloadOut multipart =
201 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
202 authRefName, personInfo, personTerms, personAuthClient.getItemCommonPartName());
203 Response res = personAuthClient.createItem(personAuthCSID, multipart);
205 int statusCode = res.getStatus();
206 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
207 invalidStatusCodeMessage(testRequestType, statusCode));
208 Assert.assertEquals(statusCode, STATUS_CREATED);
209 result = extractId(res);
218 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
219 dependsOnMethods = {"createWithAuthRefs"})
220 public void readAndCheckAuthRefs(String testName) throws Exception {
222 testSetup(STATUS_OK, ServiceRequestType.READ);
224 // Submit the request to the service and store the response.
225 MovementClient movementClient = new MovementClient();
226 Response res = movementClient.read(knownResourceId);
227 MovementsCommon movementCommon = null;
229 assertStatusCode(res, testName);
230 // Extract and return the common part of the record.
231 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
232 PayloadInputPart payloadInputPart = input.getPart(movementClient.getCommonPartName());
233 if (payloadInputPart != null) {
234 movementCommon = (MovementsCommon) payloadInputPart.getBody();
236 Assert.assertNotNull(movementCommon);
237 if(logger.isDebugEnabled()){
238 logger.debug(objectAsXmlString(movementCommon, MovementsCommon.class));
245 // Check a couple of fields
247 Assert.assertEquals(movementCommon.getMovementContact(), movementContactRefName);
249 // Get the auth refs and check them
250 Response res2 = movementClient.getAuthorityRefs(knownResourceId);
251 AuthorityRefList list = null;
253 assertStatusCode(res2, testName);
254 list = res2.readEntity(AuthorityRefList.class);
261 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
262 int numAuthRefsFound = items.size();
263 if(logger.isDebugEnabled()){
264 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
265 " authority references, found " + numAuthRefsFound);
267 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
268 "Did not find all expected authority references! " +
269 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
271 // Optionally output additional data about list members for debugging.
272 boolean iterateThroughList = true;
273 if(iterateThroughList && logger.isDebugEnabled()){
275 for(AuthorityRefList.AuthorityRefItem item : items){
276 logger.debug(testName + ": list-item[" + i + "] Field:" +
277 item.getSourceField() + "= " +
278 item.getAuthDisplayName() +
279 item.getItemDisplayName());
280 logger.debug(testName + ": list-item[" + i + "] refName=" +
282 logger.debug(testName + ": list-item[" + i + "] URI=" +
290 // ---------------------------------------------------------------
291 // Cleanup of resources created during testing
292 // ---------------------------------------------------------------
295 * Deletes all resources created by tests, after all tests have been run.
297 * This cleanup method will always be run, even if one or more tests fail.
298 * For this reason, it attempts to remove all resources created
299 * at any point during testing, even if some of those resources
300 * may be expected to be deleted by certain tests.
302 @AfterClass(alwaysRun=true)
303 public void cleanUp() {
304 String noTest = System.getProperty("noTestCleanup");
305 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
306 if (logger.isDebugEnabled()) {
307 logger.debug("Skipping Cleanup phase ...");
311 if (logger.isDebugEnabled()) {
312 logger.debug("Cleaning up temporary resources created for testing ...");
314 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
315 // Delete Person resource(s) (before PersonAuthority resources).
316 for (String resourceId : personIdsCreated) {
317 // Note: Any non-success responses are ignored and not reported.
318 personAuthClient.deleteItem(personAuthCSID, resourceId).close();
320 // Delete PersonAuthority resource(s).
321 // Note: Any non-success response is ignored and not reported.
322 if (personAuthCSID != null) {
323 personAuthClient.delete(personAuthCSID).close();
325 // Delete Movement resource(s).
326 MovementClient movementClient = new MovementClient();
327 for (String resourceId : movementIdsCreated) {
328 // Note: Any non-success responses are ignored and not reported.
329 movementClient.delete(resourceId).close();
333 // ---------------------------------------------------------------
334 // Utility methods used by tests above
335 // ---------------------------------------------------------------
338 protected String getServiceName() {
343 public String getServicePathComponent() {
344 return SERVICE_PATH_COMPONENT;
347 private PoxPayloadOut createMovementInstance(String movementReferenceNumber,
349 String movementContact) {
350 MovementsCommon movementCommon = new MovementsCommon();
351 movementCommon.setMovementReferenceNumber(movementReferenceNumber);
352 movementCommon.setLocationDate(locationDate);
353 movementCommon.setMovementContact(movementContact);
354 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
355 PayloadOutputPart commonPart =
356 multipart.addPart(movementCommon, MediaType.APPLICATION_XML_TYPE);
357 commonPart.setLabel(new MovementClient().getCommonPartName());
359 if(logger.isDebugEnabled()){
360 logger.debug("to be created, movement common");
361 logger.debug(objectAsXmlString(movementCommon, MovementsCommon.class));