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 {
65 private final String CLASS_NAME = CollectionObjectAuthRefsTest.class.getName();
66 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
68 // Instance variables specific to this test.
69 /** The service path component. */
70 final String SERVICE_PATH_COMPONENT = "collectionobjects";
72 /** The person authority name. */
73 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
75 /** The known resource id. */
76 private String knownResourceId = null;
78 /** The collection object ids created. */
79 private List<String> collectionObjectIdsCreated = new ArrayList<String>();
81 /** The person ids created. */
82 private List<String> personIdsCreated = new ArrayList<String>();
84 /** The person auth csid. */
85 private String personAuthCSID = null;
87 /** The content organization ref name. */
88 private String contentOrganizationRefName = null;
90 /** The content people ref name. */
91 private String contentPeopleRefName = null;
93 /** The content person ref name. */
94 private String contentPersonRefName = null;
96 /** The inscriber ref name. */
97 private String inscriberRefName = null;
99 /** The number of authority references expected. */
100 private final int NUM_AUTH_REFS_EXPECTED = 4;
103 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
106 protected CollectionSpaceClient getClientInstance() {
107 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
111 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
114 protected AbstractCommonList getAbstractCommonList(
115 ClientResponse<AbstractCommonList> response) {
116 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
119 // ---------------------------------------------------------------
120 // CRUD tests : CREATE tests
121 // ---------------------------------------------------------------
124 * Creates the with auth refs.
126 * @param testName the test name
127 * @throws Exception the exception
129 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
130 public void createWithAuthRefs(String testName) throws Exception {
132 if (logger.isDebugEnabled()) {
133 logger.debug(testBanner(testName, CLASS_NAME));
135 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
137 // Submit the request to the service and store the response.
138 String identifier = createIdentifier();
140 // Create all the person refs and entities
143 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
144 MultipartOutput multipart = createCollectionObjectInstance(
147 contentOrganizationRefName,
148 contentPeopleRefName,
149 contentPersonRefName,
152 ClientResponse<Response> res = collectionObjectClient.create(multipart);
154 int statusCode = res.getStatus();
156 // Check the status code of the response: does it match
157 // the expected response(s)?
160 // Does it fall within the set of valid status codes?
161 // Does it exactly match the expected status code?
162 if(logger.isDebugEnabled()){
163 logger.debug(testName + ": status = " + statusCode);
165 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
166 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
167 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
169 // Store the ID returned from the first resource created
170 // for additional tests below.
171 if (knownResourceId == null){
172 knownResourceId = extractId(res);
173 if (logger.isDebugEnabled()) {
174 logger.debug(testName + ": knownResourceId=" + knownResourceId);
178 // Store the IDs from every resource created by tests,
179 // so they can be deleted after tests have been run.
180 collectionObjectIdsCreated.add(extractId(res));
184 * Creates the person refs.
186 protected void createPersonRefs(){
188 PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false);
189 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
190 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
191 PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName());
192 ClientResponse<Response> res = personAuthClient.create(multipart);
193 int statusCode = res.getStatus();
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, STATUS_CREATED);
198 personAuthCSID = extractId(res);
200 contentOrganizationRefName = PersonAuthorityClientUtils.createPersonRefName(
201 authRefName, "Omni Org", true);
202 personIdsCreated.add(createPerson("Omni", "Org", contentOrganizationRefName));
204 contentPeopleRefName = PersonAuthorityClientUtils.createPersonRefName(
205 authRefName, "Pushy People", true);
206 personIdsCreated.add(createPerson("Pushy", "People", contentPeopleRefName));
208 contentPersonRefName = PersonAuthorityClientUtils.createPersonRefName(
209 authRefName, "Connie ContactPerson", true);
210 personIdsCreated.add(createPerson("Connie", "ContactPerson", contentPersonRefName));
212 inscriberRefName = PersonAuthorityClientUtils.createPersonRefName(
213 authRefName, "Ingrid Inscriber", true);
214 personIdsCreated.add(createPerson("Ingrid", "Inscriber", inscriberRefName));
218 * Creates the person.
220 * @param firstName the first name
221 * @param surName the sur name
222 * @param refName the ref name
225 protected String createPerson(String firstName, String surName, String refName ) {
226 Map<String, String> personInfo = new HashMap<String,String>();
227 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
228 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
229 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
230 MultipartOutput multipart =
231 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
232 refName, personInfo, personAuthClient.getItemCommonPartName());
233 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
234 int statusCode = res.getStatus();
236 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
237 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
238 Assert.assertEquals(statusCode, STATUS_CREATED);
239 return extractId(res);
244 * Read and check auth refs.
246 * @param testName the test name
247 * @throws Exception the exception
249 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
250 dependsOnMethods = {"createWithAuthRefs"})
251 public void readAndCheckAuthRefs(String testName) throws Exception {
253 if (logger.isDebugEnabled()) {
254 logger.debug(testBanner(testName, CLASS_NAME));
257 testSetup(STATUS_OK, ServiceRequestType.READ);
259 // Submit the request to the service and store the response.
260 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
261 ClientResponse<MultipartInput> res = collectionObjectClient.read(knownResourceId);
262 int statusCode = res.getStatus();
264 // Check the status code of the response: does it match
265 // the expected response(s)?
266 if(logger.isDebugEnabled()){
267 logger.debug(testName + ".read: status = " + statusCode);
269 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
270 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
271 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
273 MultipartInput input = (MultipartInput) res.getEntity();
274 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
275 collectionObjectClient.getCommonPartName(), CollectionobjectsCommon.class);
276 Assert.assertNotNull(collectionObject);
277 // Check a couple of fields
278 Assert.assertEquals(collectionObject.getContentOrganization(), contentOrganizationRefName);
279 Assert.assertEquals(collectionObject.getInscriber(), inscriberRefName);
281 // Get the auth refs and check them
282 ClientResponse<AuthorityRefList> res2 = collectionObjectClient.getAuthorityRefs(knownResourceId);
283 statusCode = res2.getStatus();
285 if(logger.isDebugEnabled()){
286 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
288 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
291 AuthorityRefList list = res2.getEntity();
293 // Optionally output additional data about list members for debugging.
294 boolean iterateThroughList = true;
295 if(iterateThroughList && logger.isDebugEnabled()){
296 List<AuthorityRefList.AuthorityRefItem> items =
297 list.getAuthorityRefItem();
299 for(AuthorityRefList.AuthorityRefItem item : items){
300 logger.debug(testName + ": list-item[" + i + "] Field:" +
301 item.getSourceField() + "= " +
302 item.getAuthDisplayName() +
303 item.getItemDisplayName());
304 logger.debug(testName + ": list-item[" + i + "] refName=" +
306 logger.debug(testName + ": list-item[" + i + "] URI=" +
310 Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!");
315 // ---------------------------------------------------------------
316 // Cleanup of resources created during testing
317 // ---------------------------------------------------------------
320 * Deletes all resources created by tests, after all tests have been run.
322 * This cleanup method will always be run, even if one or more tests fail.
323 * For this reason, it attempts to remove all resources created
324 * at any point during testing, even if some of those resources
325 * may be expected to be deleted by certain tests.
327 @AfterClass(alwaysRun=true)
328 public void cleanUp() {
329 String noTest = System.getProperty("noTestCleanup");
330 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
331 if (logger.isDebugEnabled()) {
332 logger.debug("Skipping Cleanup phase ...");
336 if (logger.isDebugEnabled()) {
337 logger.debug("Cleaning up temporary resources created for testing ...");
339 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
340 for (String resourceId : collectionObjectIdsCreated) {
341 // Note: Any non-success responses are ignored and not reported.
342 collectionObjectClient.delete(resourceId).releaseConnection();
344 // Note: Any non-success response is ignored and not reported.
345 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
346 // Delete persons before PersonAuth
347 for (String resourceId : personIdsCreated) {
348 // Note: Any non-success responses are ignored and not reported.
349 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
351 personAuthClient.delete(personAuthCSID).releaseConnection();
354 // ---------------------------------------------------------------
355 // Utility methods used by tests above
356 // ---------------------------------------------------------------
358 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
361 public String getServicePathComponent() {
362 return SERVICE_PATH_COMPONENT;
366 * Creates the collection object instance.
368 * @param title the title
369 * @param objNum the obj num
370 * @param contentOrganization the content organization
371 * @param contentPeople the content people
372 * @param contentPerson the content person
373 * @param inscriber the inscriber
374 * @return the multipart output
376 private MultipartOutput createCollectionObjectInstance(
379 String contentOrganization,
380 String contentPeople,
381 String contentPerson,
383 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
384 collectionObject.setTitle(title);
385 collectionObject.setObjectNumber(objNum);
386 collectionObject.setContentOrganization(contentOrganization);
387 collectionObject.setContentPeople(contentPeople);
388 collectionObject.setContentPerson(contentPerson);
389 collectionObject.setInscriber(inscriber);
390 MultipartOutput multipart = new MultipartOutput();
391 OutputPart commonPart =
392 multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
393 commonPart.getHeaders().add("label", new CollectionObjectClient().getCommonPartName());
395 if(logger.isDebugEnabled()){
396 logger.debug("to be created, collectionObject common");
397 logger.debug(objectAsXmlString(collectionObject, CollectionobjectsCommon.class));