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 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
67 private PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
68 final String SERVICE_PATH_COMPONENT = "collectionobjects";
69 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
70 private String knownResourceId = null;
71 private List<String> collectionObjectIdsCreated = new ArrayList();
72 private List<String> personIdsCreated = new ArrayList();
73 private int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
74 private int OK_STATUS = Response.Status.OK.getStatusCode();
75 private String personAuthCSID = null;
76 private String contentOrganizationRefName = null;
77 private String contentPeopleRefName = null;
78 private String contentPersonRefName = null;
79 private String inscriberRefName = null;
80 private final int NUM_AUTH_REFS_EXPECTED = 4;
82 // ---------------------------------------------------------------
83 // CRUD tests : CREATE tests
84 // ---------------------------------------------------------------
86 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
87 public void createWithAuthRefs(String testName) throws Exception {
89 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
91 // Submit the request to the service and store the response.
92 String identifier = createIdentifier();
94 // Create all the person refs and entities
97 MultipartOutput multipart = createCollectionObjectInstance(
100 contentOrganizationRefName,
101 contentPeopleRefName,
102 contentPersonRefName,
105 ClientResponse<Response> res = collectionObjectClient.create(multipart);
107 int statusCode = res.getStatus();
109 // Check the status code of the response: does it match
110 // the expected response(s)?
113 // Does it fall within the set of valid status codes?
114 // Does it exactly match the expected status code?
115 if(logger.isDebugEnabled()){
116 logger.debug(testName + ": status = " + statusCode);
118 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
119 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
120 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
122 // Store the ID returned from the first resource created
123 // for additional tests below.
124 if (knownResourceId == null){
125 knownResourceId = extractId(res);
126 if (logger.isDebugEnabled()) {
127 logger.debug(testName + ": knownResourceId=" + knownResourceId);
131 // Store the IDs from every resource created by tests,
132 // so they can be deleted after tests have been run.
133 collectionObjectIdsCreated.add(extractId(res));
136 protected void createPersonRefs(){
138 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
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 MultipartOutput multipart =
171 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
172 refName, personInfo, personAuthClient.getItemCommonPartName());
173 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
174 int statusCode = res.getStatus();
176 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
177 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
178 Assert.assertEquals(statusCode, CREATED_STATUS);
179 return extractId(res);
183 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
184 dependsOnMethods = {"createWithAuthRefs"})
185 public void readAndCheckAuthRefs(String testName) throws Exception {
188 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
190 // Submit the request to the service and store the response.
191 ClientResponse<MultipartInput> res = collectionObjectClient.read(knownResourceId);
192 int statusCode = res.getStatus();
194 // Check the status code of the response: does it match
195 // the expected response(s)?
196 if(logger.isDebugEnabled()){
197 logger.debug(testName + ".read: status = " + statusCode);
199 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
200 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
201 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
203 MultipartInput input = (MultipartInput) res.getEntity();
204 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
205 collectionObjectClient.getCommonPartName(), CollectionobjectsCommon.class);
206 Assert.assertNotNull(collectionObject);
207 // Check a couple of fields
208 Assert.assertEquals(collectionObject.getContentOrganization(), contentOrganizationRefName);
209 Assert.assertEquals(collectionObject.getInscriber(), inscriberRefName);
211 // Get the auth refs and check them
212 ClientResponse<AuthorityRefList> res2 = collectionObjectClient.getAuthorityRefs(knownResourceId);
213 statusCode = res2.getStatus();
215 if(logger.isDebugEnabled()){
216 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
221 AuthorityRefList list = res2.getEntity();
223 // Optionally output additional data about list members for debugging.
224 boolean iterateThroughList = true;
225 if(iterateThroughList && logger.isDebugEnabled()){
226 List<AuthorityRefList.AuthorityRefItem> items =
227 list.getAuthorityRefItem();
229 for(AuthorityRefList.AuthorityRefItem item : items){
230 logger.debug(testName + ": list-item[" + i + "] Field:" +
231 item.getSourceField() + "= " +
232 item.getAuthDisplayName() +
233 item.getItemDisplayName());
234 logger.debug(testName + ": list-item[" + i + "] refName=" +
236 logger.debug(testName + ": list-item[" + i + "] URI=" +
240 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
245 // ---------------------------------------------------------------
246 // Cleanup of resources created during testing
247 // ---------------------------------------------------------------
250 * Deletes all resources created by tests, after all tests have been run.
252 * This cleanup method will always be run, even if one or more tests fail.
253 * For this reason, it attempts to remove all resources created
254 * at any point during testing, even if some of those resources
255 * may be expected to be deleted by certain tests.
257 // @AfterClass(alwaysRun=true)
258 public void cleanUp() {
259 if (logger.isDebugEnabled()) {
260 logger.debug("Cleaning up temporary resources created for testing ...");
262 // Note: Any non-success responses are ignored and not reported.
263 for (String resourceId : collectionObjectIdsCreated) {
264 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
266 // Delete persons before PersonAuth
267 for (String resourceId : personIdsCreated) {
268 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
270 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
273 // ---------------------------------------------------------------
274 // Utility methods used by tests above
275 // ---------------------------------------------------------------
277 public String getServicePathComponent() {
278 return SERVICE_PATH_COMPONENT;
281 private MultipartOutput createCollectionObjectInstance(
284 String contentOrganization,
285 String contentPeople,
286 String contentPerson,
288 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
289 collectionObject.setTitle(title);
290 collectionObject.setObjectNumber(objNum);
291 collectionObject.setContentOrganization(contentOrganization);
292 collectionObject.setContentPeople(contentPeople);
293 collectionObject.setContentPerson(contentPerson);
294 collectionObject.setInscriber(inscriber);
295 MultipartOutput multipart = new MultipartOutput();
296 OutputPart commonPart =
297 multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
298 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
300 if(logger.isDebugEnabled()){
301 logger.debug("to be created, collectionObject common");
302 logger.debug(objectAsXmlString(collectionObject, CollectionobjectsCommon.class));