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.CollectionObjectClient;
35 import org.collectionspace.services.client.PersonAuthorityClient;
36 import org.collectionspace.services.client.PersonAuthorityClientUtils;
37 import org.collectionspace.services.common.authorityref.AuthorityRefList;
38 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
39 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
41 import org.jboss.resteasy.client.ClientResponse;
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
45 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * CollectionObjectAuthRefsTest, carries out tests against a
55 * deployed and running CollectionObject Service.
57 * $LastChangedRevision: 1327 $
58 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60 public class CollectionObjectAuthRefsTest extends BaseServiceTest {
62 private final Logger logger =
63 LoggerFactory.getLogger(CollectionObjectAuthRefsTest.class);
65 // Instance variables specific to this test.
66 final String SERVICE_PATH_COMPONENT = "collectionobjects";
67 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
68 private String knownResourceId = null;
69 private List<String> collectionObjectIdsCreated = new ArrayList();
70 private List<String> personIdsCreated = new ArrayList();
71 private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
72 private int OK_STATUS = Response.Status.OK.getStatusCode();
73 private String personAuthCSID = null;
74 private String contentOrganizationRefName = null;
75 private String contentPeopleRefName = null;
76 private String contentPersonRefName = null;
77 private String inscriberRefName = null;
78 private final int NUM_AUTH_REFS_EXPECTED = 4;
80 // ---------------------------------------------------------------
81 // CRUD tests : CREATE tests
82 // ---------------------------------------------------------------
84 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
85 public void createWithAuthRefs(String testName) throws Exception {
87 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
89 // Submit the request to the service and store the response.
90 String identifier = createIdentifier();
92 // Create all the person refs and entities
95 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
96 MultipartOutput multipart = createCollectionObjectInstance(
99 contentOrganizationRefName,
100 contentPeopleRefName,
101 contentPersonRefName,
104 ClientResponse<Response> res = collectionObjectClient.create(multipart);
106 int statusCode = res.getStatus();
108 // Check the status code of the response: does it match
109 // the expected response(s)?
112 // Does it fall within the set of valid status codes?
113 // Does it exactly match the expected status code?
114 if(logger.isDebugEnabled()){
115 logger.debug(testName + ": status = " + statusCode);
117 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
118 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
119 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
121 // Store the ID returned from the first resource created
122 // for additional tests below.
123 if (knownResourceId == null){
124 knownResourceId = extractId(res);
125 if (logger.isDebugEnabled()) {
126 logger.debug(testName + ": knownResourceId=" + knownResourceId);
130 // Store the IDs from every resource created by tests,
131 // so they can be deleted after tests have been run.
132 collectionObjectIdsCreated.add(extractId(res));
135 protected void createPersonRefs(){
137 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
138 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
139 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
140 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
141 ClientResponse<Response> res = personAuthClient.create(multipart);
142 int statusCode = res.getStatus();
144 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
145 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
146 Assert.assertEquals(statusCode, CREATED_STATUS);
147 personAuthCSID = extractId(res);
149 contentOrganizationRefName = PersonAuthorityClientUtils.createPersonRefName(
150 authRefName, "Omni Org", true);
151 personIdsCreated.add(createPerson("Omni", "Org", contentOrganizationRefName));
153 contentPeopleRefName = PersonAuthorityClientUtils.createPersonRefName(
154 authRefName, "Pushy People", true);
155 personIdsCreated.add(createPerson("Pushy", "People", contentPeopleRefName));
157 contentPersonRefName = PersonAuthorityClientUtils.createPersonRefName(
158 authRefName, "Connie ContactPerson", true);
159 personIdsCreated.add(createPerson("Connie", "ContactPerson", contentPersonRefName));
161 inscriberRefName = PersonAuthorityClientUtils.createPersonRefName(
162 authRefName, "Ingrid Inscriber", true);
163 personIdsCreated.add(createPerson("Ingrid", "Inscriber", inscriberRefName));
166 protected String createPerson(String firstName, String surName, String refName ) {
167 Map<String, String> personInfo = new HashMap<String,String>();
168 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
169 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
170 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
171 MultipartOutput multipart =
172 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
173 refName, personInfo, personAuthClient.getItemCommonPartName());
174 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
175 int statusCode = res.getStatus();
177 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
178 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
179 Assert.assertEquals(statusCode, CREATED_STATUS);
180 return extractId(res);
184 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
185 dependsOnMethods = {"createWithAuthRefs"})
186 public void readAndCheckAuthRefs(String testName) throws Exception {
189 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
191 // Submit the request to the service and store the response.
192 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
193 ClientResponse<MultipartInput> res = collectionObjectClient.read(knownResourceId);
194 int statusCode = res.getStatus();
196 // Check the status code of the response: does it match
197 // the expected response(s)?
198 if(logger.isDebugEnabled()){
199 logger.debug(testName + ".read: status = " + statusCode);
201 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
202 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
203 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
205 MultipartInput input = (MultipartInput) res.getEntity();
206 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
207 collectionObjectClient.getCommonPartName(), CollectionobjectsCommon.class);
208 Assert.assertNotNull(collectionObject);
209 // Check a couple of fields
210 Assert.assertEquals(collectionObject.getContentOrganization(), contentOrganizationRefName);
211 Assert.assertEquals(collectionObject.getInscriber(), inscriberRefName);
213 // Get the auth refs and check them
214 ClientResponse<AuthorityRefList> res2 = collectionObjectClient.getAuthorityRefs(knownResourceId);
215 statusCode = res2.getStatus();
217 if(logger.isDebugEnabled()){
218 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
220 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
221 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
222 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
223 AuthorityRefList list = res2.getEntity();
225 // Optionally output additional data about list members for debugging.
226 boolean iterateThroughList = true;
227 if(iterateThroughList && logger.isDebugEnabled()){
228 List<AuthorityRefList.AuthorityRefItem> items =
229 list.getAuthorityRefItem();
231 for(AuthorityRefList.AuthorityRefItem item : items){
232 logger.debug(testName + ": list-item[" + i + "] Field:" +
233 item.getSourceField() + "= " +
234 item.getAuthDisplayName() +
235 item.getItemDisplayName());
236 logger.debug(testName + ": list-item[" + i + "] refName=" +
238 logger.debug(testName + ": list-item[" + i + "] URI=" +
242 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
247 // ---------------------------------------------------------------
248 // Cleanup of resources created during testing
249 // ---------------------------------------------------------------
252 * Deletes all resources created by tests, after all tests have been run.
254 * This cleanup method will always be run, even if one or more tests fail.
255 * For this reason, it attempts to remove all resources created
256 * at any point during testing, even if some of those resources
257 * may be expected to be deleted by certain tests.
259 @AfterClass(alwaysRun=true)
260 public void cleanUp() {
261 String noTest = System.getProperty("noTestCleanup");
262 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
263 if (logger.isDebugEnabled()) {
264 logger.debug("Skipping Cleanup phase ...");
268 if (logger.isDebugEnabled()) {
269 logger.debug("Cleaning up temporary resources created for testing ...");
271 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
272 for (String resourceId : collectionObjectIdsCreated) {
273 // Note: Any non-success responses are ignored and not reported.
274 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
276 // Note: Any non-success response is ignored and not reported.
277 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
278 // Delete persons before PersonAuth
279 for (String resourceId : personIdsCreated) {
280 // Note: Any non-success responses are ignored and not reported.
281 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
283 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
286 // ---------------------------------------------------------------
287 // Utility methods used by tests above
288 // ---------------------------------------------------------------
290 public String getServicePathComponent() {
291 return SERVICE_PATH_COMPONENT;
294 private MultipartOutput createCollectionObjectInstance(
297 String contentOrganization,
298 String contentPeople,
299 String contentPerson,
301 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
302 collectionObject.setTitle(title);
303 collectionObject.setObjectNumber(objNum);
304 collectionObject.setContentOrganization(contentOrganization);
305 collectionObject.setContentPeople(contentPeople);
306 collectionObject.setContentPerson(contentPerson);
307 collectionObject.setInscriber(inscriber);
308 MultipartOutput multipart = new MultipartOutput();
309 OutputPart commonPart =
310 multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
311 commonPart.getHeaders().add("label", new CollectionObjectClient().getCommonPartName());
313 if(logger.isDebugEnabled()){
314 logger.debug("to be created, collectionObject common");
315 logger.debug(objectAsXmlString(collectionObject, CollectionobjectsCommon.class));