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.CollectionSpaceClient;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList;
39 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
40 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.jboss.resteasy.client.ClientResponse;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
47 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
48 import org.testng.Assert;
49 import org.testng.annotations.AfterClass;
50 import org.testng.annotations.Test;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * CollectionObjectAuthRefsTest, carries out tests against a
57 * deployed and running CollectionObject Service.
59 * $LastChangedRevision: 1327 $
60 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
62 public class CollectionObjectAuthRefsTest extends BaseServiceTest {
64 private final Logger logger =
65 LoggerFactory.getLogger(CollectionObjectAuthRefsTest.class);
67 // Instance variables specific to this test.
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;
83 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
86 protected CollectionSpaceClient getClientInstance() {
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.ClientResponse)
94 protected AbstractCommonList getAbstractCommonList(
95 ClientResponse<AbstractCommonList> 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 {
106 testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName);
108 // Submit the request to the service and store the response.
109 String identifier = createIdentifier();
111 // Create all the person refs and entities
114 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
115 MultipartOutput multipart = createCollectionObjectInstance(
118 contentOrganizationRefName,
119 contentPeopleRefName,
120 contentPersonRefName,
123 ClientResponse<Response> res = collectionObjectClient.create(multipart);
125 int statusCode = res.getStatus();
127 // Check the status code of the response: does it match
128 // the expected response(s)?
131 // Does it fall within the set of valid status codes?
132 // Does it exactly match the expected status code?
133 if(logger.isDebugEnabled()){
134 logger.debug(testName + ": status = " + statusCode);
136 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
137 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
138 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
140 // Store the ID returned from the first resource created
141 // for additional tests below.
142 if (knownResourceId == null){
143 knownResourceId = extractId(res);
144 if (logger.isDebugEnabled()) {
145 logger.debug(testName + ": knownResourceId=" + knownResourceId);
149 // Store the IDs from every resource created by tests,
150 // so they can be deleted after tests have been run.
151 collectionObjectIdsCreated.add(extractId(res));
154 protected void createPersonRefs(){
156 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
157 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
158 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
159 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
160 ClientResponse<Response> res = personAuthClient.create(multipart);
161 int statusCode = res.getStatus();
163 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
164 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
165 Assert.assertEquals(statusCode, CREATED_STATUS);
166 personAuthCSID = extractId(res);
168 contentOrganizationRefName = PersonAuthorityClientUtils.createPersonRefName(
169 authRefName, "Omni Org", true);
170 personIdsCreated.add(createPerson("Omni", "Org", contentOrganizationRefName));
172 contentPeopleRefName = PersonAuthorityClientUtils.createPersonRefName(
173 authRefName, "Pushy People", true);
174 personIdsCreated.add(createPerson("Pushy", "People", contentPeopleRefName));
176 contentPersonRefName = PersonAuthorityClientUtils.createPersonRefName(
177 authRefName, "Connie ContactPerson", true);
178 personIdsCreated.add(createPerson("Connie", "ContactPerson", contentPersonRefName));
180 inscriberRefName = PersonAuthorityClientUtils.createPersonRefName(
181 authRefName, "Ingrid Inscriber", true);
182 personIdsCreated.add(createPerson("Ingrid", "Inscriber", inscriberRefName));
185 protected String createPerson(String firstName, String surName, String refName ) {
186 Map<String, String> personInfo = new HashMap<String,String>();
187 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
188 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
189 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
190 MultipartOutput multipart =
191 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
192 refName, personInfo, personAuthClient.getItemCommonPartName());
193 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
194 int statusCode = res.getStatus();
196 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
197 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
198 Assert.assertEquals(statusCode, CREATED_STATUS);
199 return extractId(res);
203 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
204 dependsOnMethods = {"createWithAuthRefs"})
205 public void readAndCheckAuthRefs(String testName) throws Exception {
208 testSetup(OK_STATUS, ServiceRequestType.READ,testName);
210 // Submit the request to the service and store the response.
211 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
212 ClientResponse<MultipartInput> res = collectionObjectClient.read(knownResourceId);
213 int statusCode = res.getStatus();
215 // Check the status code of the response: does it match
216 // the expected response(s)?
217 if(logger.isDebugEnabled()){
218 logger.debug(testName + ".read: status = " + statusCode);
220 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
221 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
222 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
224 MultipartInput input = (MultipartInput) res.getEntity();
225 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
226 collectionObjectClient.getCommonPartName(), CollectionobjectsCommon.class);
227 Assert.assertNotNull(collectionObject);
228 // Check a couple of fields
229 Assert.assertEquals(collectionObject.getContentOrganization(), contentOrganizationRefName);
230 Assert.assertEquals(collectionObject.getInscriber(), inscriberRefName);
232 // Get the auth refs and check them
233 ClientResponse<AuthorityRefList> res2 = collectionObjectClient.getAuthorityRefs(knownResourceId);
234 statusCode = res2.getStatus();
236 if(logger.isDebugEnabled()){
237 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
239 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
240 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
241 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
242 AuthorityRefList list = res2.getEntity();
244 // Optionally output additional data about list members for debugging.
245 boolean iterateThroughList = true;
246 if(iterateThroughList && logger.isDebugEnabled()){
247 List<AuthorityRefList.AuthorityRefItem> items =
248 list.getAuthorityRefItem();
250 for(AuthorityRefList.AuthorityRefItem item : items){
251 logger.debug(testName + ": list-item[" + i + "] Field:" +
252 item.getSourceField() + "= " +
253 item.getAuthDisplayName() +
254 item.getItemDisplayName());
255 logger.debug(testName + ": list-item[" + i + "] refName=" +
257 logger.debug(testName + ": list-item[" + i + "] URI=" +
261 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
266 // ---------------------------------------------------------------
267 // Cleanup of resources created during testing
268 // ---------------------------------------------------------------
271 * Deletes all resources created by tests, after all tests have been run.
273 * This cleanup method will always be run, even if one or more tests fail.
274 * For this reason, it attempts to remove all resources created
275 * at any point during testing, even if some of those resources
276 * may be expected to be deleted by certain tests.
278 @AfterClass(alwaysRun=true)
279 public void cleanUp() {
280 String noTest = System.getProperty("noTestCleanup");
281 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
282 if (logger.isDebugEnabled()) {
283 logger.debug("Skipping Cleanup phase ...");
287 if (logger.isDebugEnabled()) {
288 logger.debug("Cleaning up temporary resources created for testing ...");
290 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
291 for (String resourceId : collectionObjectIdsCreated) {
292 // Note: Any non-success responses are ignored and not reported.
293 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
295 // Note: Any non-success response is ignored and not reported.
296 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
297 // Delete persons before PersonAuth
298 for (String resourceId : personIdsCreated) {
299 // Note: Any non-success responses are ignored and not reported.
300 ClientResponse<Response> res = personAuthClient.deleteItem(personAuthCSID, resourceId);
302 ClientResponse<Response> res = personAuthClient.delete(personAuthCSID);
305 // ---------------------------------------------------------------
306 // Utility methods used by tests above
307 // ---------------------------------------------------------------
309 public String getServicePathComponent() {
310 return SERVICE_PATH_COMPONENT;
313 private MultipartOutput createCollectionObjectInstance(
316 String contentOrganization,
317 String contentPeople,
318 String contentPerson,
320 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
321 collectionObject.setTitle(title);
322 collectionObject.setObjectNumber(objNum);
323 collectionObject.setContentOrganization(contentOrganization);
324 collectionObject.setContentPeople(contentPeople);
325 collectionObject.setContentPerson(contentPerson);
326 collectionObject.setInscriber(inscriber);
327 MultipartOutput multipart = new MultipartOutput();
328 OutputPart commonPart =
329 multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
330 commonPart.getHeaders().add("label", new CollectionObjectClient().getCommonPartName());
332 if(logger.isDebugEnabled()){
333 logger.debug("to be created, collectionObject common");
334 logger.debug(objectAsXmlString(collectionObject, CollectionobjectsCommon.class));