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.OrganizationJAXBSchema;
34 import org.collectionspace.services.PersonJAXBSchema;
35 import org.collectionspace.services.client.CollectionObjectClient;
36 import org.collectionspace.services.client.CollectionSpaceClient;
37 import org.collectionspace.services.client.OrgAuthorityClient;
38 import org.collectionspace.services.client.OrgAuthorityClientUtils;
39 import org.collectionspace.services.client.PersonAuthorityClient;
40 import org.collectionspace.services.client.PersonAuthorityClientUtils;
41 import org.collectionspace.services.common.authorityref.AuthorityRefList;
42 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
43 import org.collectionspace.services.jaxb.AbstractCommonList;
45 import org.jboss.resteasy.client.ClientResponse;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
48 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
49 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
50 import org.testng.Assert;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.Test;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * CollectionObjectAuthRefsTest, carries out tests against a
59 * deployed and running CollectionObject Service.
61 * $LastChangedRevision: 1327 $
62 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
64 public class CollectionObjectAuthRefsTest extends BaseServiceTest {
67 private final String CLASS_NAME = CollectionObjectAuthRefsTest.class.getName();
68 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
70 // Instance variables specific to this test.
71 /** The service path component. */
72 final String SERVICE_PATH_COMPONENT = "collectionobjects";
74 /** The person authority name. */
75 final String PERSON_AUTHORITY_NAME = "TestPersonAuth";
77 /** The organization authority name. */
78 final String ORG_AUTHORITY_NAME = "TestOrgAuth";
80 /** The known resource id. */
81 private String knownResourceId = null;
83 /** The collection object ids created. */
84 private List<String> collectionObjectIdsCreated = new ArrayList<String>();
86 /** The person ids created. */
87 private List<String> personIdsCreated = new ArrayList<String>();
89 /** The person authority csid and refName. */
90 private String personAuthCSID = null;
91 private String personAuthRefName = null;
93 /** The organization ids created. */
94 private List<String> orgIdsCreated = new ArrayList<String>();
96 /** The org authority csid and refName. */
97 private String orgAuthCSID = null;
98 private String orgAuthRefName = null;
100 /** The content organization ref name. */
101 private String contentOrganizationRefName = null;
103 /** The content person ref name. */
104 private String contentPersonRefName = null;
106 /** The inscriber ref names. */
107 private String contentInscriberRefName = null;
108 private String descriptionInscriberRefName = null;
110 /** The object history and association ref names. */
111 private String associatedEventOrganizationRefName = null;
112 private String associatedEventPersonRefName = null;
113 private String associatedOrganizationRefName = null;
114 private String associatedPersonRefName = null;
115 private String ownerRefName = null;
117 /** The number of authority references expected. */
118 private final int NUM_AUTH_REFS_EXPECTED = 9;
121 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
124 protected CollectionSpaceClient getClientInstance() {
125 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
129 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
132 protected AbstractCommonList getAbstractCommonList(
133 ClientResponse<AbstractCommonList> response) {
134 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
137 // ---------------------------------------------------------------
138 // CRUD tests : CREATE tests
139 // ---------------------------------------------------------------
142 * Creates the with auth refs.
144 * @param testName the test name
145 * @throws Exception the exception
147 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
148 public void createWithAuthRefs(String testName) throws Exception {
150 if (logger.isDebugEnabled()) {
151 logger.debug(testBanner(testName, CLASS_NAME));
153 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
155 // Create all the person refs and entities
158 // Create all the organization refs and entities
159 createOrganizationRefs();
161 // Create an object record payload, containing
162 // authority reference values in a number of its fields
163 String identifier = createIdentifier();
164 MultipartOutput multipart =
165 createCollectionObjectInstance(
167 "ObjNum" + "-" + identifier,
168 contentOrganizationRefName,
169 contentPersonRefName,
170 contentInscriberRefName,
171 descriptionInscriberRefName,
172 associatedEventOrganizationRefName,
173 associatedEventPersonRefName,
174 associatedOrganizationRefName,
175 associatedPersonRefName,
179 // Submit the request to the service and store the response.
180 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
181 ClientResponse<Response> res = collectionObjectClient.create(multipart);
183 int statusCode = res.getStatus();
185 // Check the status code of the response: does it match
186 // the expected response(s)?
189 // Does it fall within the set of valid status codes?
190 // Does it exactly match the expected status code?
191 if(logger.isDebugEnabled()){
192 logger.debug(testName + ": status = " + statusCode);
194 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
195 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
196 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
198 // Store the ID returned from the first resource created
199 // for additional tests below.
200 if (knownResourceId == null){
201 knownResourceId = extractId(res);
202 if (logger.isDebugEnabled()) {
203 logger.debug(testName + ": knownResourceId=" + knownResourceId);
207 // Store the IDs from every resource created by tests,
208 // so they can be deleted after tests have been run.
209 collectionObjectIdsCreated.add(extractId(res));
213 * Creates a Person Authority.
215 * @param displayName the display name of the authority
216 * @param shortIdentifier the short identifier for the authority
218 private void createPersonAuthority(String displayName, String shortIdentifier) {
219 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
220 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
221 MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
222 displayName, shortIdentifier, personAuthClient.getCommonPartName());
223 ClientResponse<Response> res = personAuthClient.create(multipart);
224 int statusCode = res.getStatus();
226 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
227 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
228 Assert.assertEquals(statusCode, STATUS_CREATED);
229 personAuthCSID = extractId(res);
230 personAuthRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
234 * Creates a person item.
236 * @param firstName the person's first name
237 * @param surName the person's surname
238 * @param shortIdentifier the short identifier for the item
239 * @return the CSID of the newly-created person record
241 protected String createPerson(String firstName, String surName, String shortIdentifier ) {
242 Map<String, String> personInfo = new HashMap<String,String>();
243 personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
244 personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
245 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortIdentifier);
246 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
247 MultipartOutput multipart =
248 PersonAuthorityClientUtils.createPersonInstance(personAuthCSID,
249 personAuthRefName, personInfo, personAuthClient.getItemCommonPartName());
250 ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
251 int statusCode = res.getStatus();
253 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
254 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
255 Assert.assertEquals(statusCode, STATUS_CREATED);
256 return extractId(res);
260 * Creates multiple Person items within a Person Authority,
261 * and stores the refNames referring to each.
263 protected void createPersonRefs(){
265 createPersonAuthority(PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME);
269 csid = createPerson("Connie", "ContactPerson", "connieContactPerson");
270 contentPersonRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
271 personIdsCreated.add(csid);
273 csid = createPerson("Ingrid", "ContentInscriber", "ingridContentInscriber");
274 contentInscriberRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
275 personIdsCreated.add(csid);
277 csid = createPerson("Dessie", "DescriptionInscriber", "dessieDescriptionInscriber");
278 descriptionInscriberRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
279 personIdsCreated.add(csid);
281 csid = createPerson("Asok", "AssociatedEventPerson", "asokAssociatedEventPerson");
282 associatedEventPersonRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
283 personIdsCreated.add(csid);
285 csid = createPerson("Andrew", "AssociatedPerson", "andrewAssociatedPerson");
286 associatedPersonRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
287 personIdsCreated.add(csid);
289 csid = createPerson("Owen", "Owner", "owenOwner");
290 ownerRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
291 personIdsCreated.add(csid);
295 * Creates an organization authority.
297 * @param displayName the display name of the authority
298 * @param shortIdentifier the short identifier for the authority
300 private void createOrgAuthority(String displayName, String shortIdentifier) {
301 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
302 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
303 MultipartOutput multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
304 displayName, shortIdentifier, orgAuthClient.getCommonPartName());
305 ClientResponse<Response> res = orgAuthClient.create(multipart);
306 int statusCode = res.getStatus();
308 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
309 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
310 Assert.assertEquals(statusCode, STATUS_CREATED);
311 orgAuthCSID = extractId(res);
312 orgAuthRefName = OrgAuthorityClientUtils.getAuthorityRefName(orgAuthCSID, null);
316 * Creates an organization item.
318 * @param shortName the organization's short name
319 * @param foundingPlace the organization's founding place
320 * @param shortIdentifier the short identifier for the item
321 * @return the CSID of the newly-created organization record
323 protected String createOrganization(String shortName, String foundingPlace, String shortIdentifier ) {
324 Map<String, String> orgInfo = new HashMap<String,String>();
325 orgInfo.put(OrganizationJAXBSchema.SHORT_NAME, shortName);
326 orgInfo.put(OrganizationJAXBSchema.FOUNDING_PLACE, foundingPlace);
327 orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortIdentifier);
328 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
329 MultipartOutput multipart =
330 OrgAuthorityClientUtils.createOrganizationInstance(orgAuthCSID,
331 orgAuthRefName, orgInfo, orgAuthClient.getItemCommonPartName());
332 ClientResponse<Response> res = orgAuthClient.createItem(orgAuthCSID, multipart);
333 int statusCode = res.getStatus();
335 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
336 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
337 Assert.assertEquals(statusCode, STATUS_CREATED);
338 return extractId(res);
342 * Creates multiple Organization items within an Organization Authority,
343 * and stores the refNames referring to each.
345 private void createOrganizationRefs() {
347 createOrgAuthority(ORG_AUTHORITY_NAME, ORG_AUTHORITY_NAME);
351 csid = createOrganization("Content Org", "Content Org Town", "contentOrg");
352 contentOrganizationRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, csid, null);
353 orgIdsCreated.add(csid);
355 csid = createOrganization("Associated Event Org", "Associated Event Org City", "associatedEventOrg");
356 associatedEventOrganizationRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, csid, null);
357 orgIdsCreated.add(csid);
359 csid = createOrganization("Associated Org", "Associated Org City", "associatedOrg");
360 associatedOrganizationRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, csid, null);
361 orgIdsCreated.add(csid);
367 * Read and check auth refs.
369 * @param testName the test name
370 * @throws Exception the exception
372 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
373 dependsOnMethods = {"createWithAuthRefs"})
374 public void readAndCheckAuthRefs(String testName) throws Exception {
376 if (logger.isDebugEnabled()) {
377 logger.debug(testBanner(testName, CLASS_NAME));
380 testSetup(STATUS_OK, ServiceRequestType.READ);
382 // Submit the request to the service and store the response.
383 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
384 ClientResponse<MultipartInput> res = collectionObjectClient.read(knownResourceId);
385 int statusCode = res.getStatus();
387 // Check the status code of the response: does it match
388 // the expected response(s)?
389 if(logger.isDebugEnabled()){
390 logger.debug(testName + ".read: status = " + statusCode);
392 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
393 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
394 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
396 MultipartInput input = (MultipartInput) res.getEntity();
397 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
398 collectionObjectClient.getCommonPartName(), CollectionobjectsCommon.class);
399 Assert.assertNotNull(collectionObject);
401 // Check a sample of one or more person authority ref fields
402 Assert.assertEquals(collectionObject.getInscriptionContentInscriber(), contentInscriberRefName);
403 Assert.assertEquals(collectionObject.getAssociatedPerson(), associatedPersonRefName);
404 Assert.assertEquals(collectionObject.getOwner(), ownerRefName);
406 // Check a sample of one or more organization authority ref fields
407 Assert.assertEquals(collectionObject.getContentOrganization(), contentOrganizationRefName);
408 Assert.assertEquals(collectionObject.getAssociatedEventOrganization(), associatedEventOrganizationRefName);
410 // Get all of the auth refs and check that the expected number is returned
411 ClientResponse<AuthorityRefList> res2 = collectionObjectClient.getAuthorityRefs(knownResourceId);
412 statusCode = res2.getStatus();
414 if(logger.isDebugEnabled()){
415 logger.debug(testName + ".getAuthorityRefs: status = " + statusCode);
417 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
418 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
419 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
420 AuthorityRefList list = res2.getEntity();
422 List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
423 int numAuthRefsFound = items.size();
424 if(logger.isDebugEnabled()){
425 logger.debug("Expected " + NUM_AUTH_REFS_EXPECTED +
426 " authority references, found " + numAuthRefsFound);
428 Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
429 "Did not find all expected authority references! " +
430 "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
432 // Optionally output additional data about list members for debugging.
433 boolean iterateThroughList = true;
434 if(iterateThroughList && logger.isDebugEnabled()){;
436 for(AuthorityRefList.AuthorityRefItem item : items){
437 logger.debug(testName + ": list-item[" + i + "] Field:" +
438 item.getSourceField() + " =" +
439 " item display name = " + item.getAuthDisplayName() +
440 " auth display name = " + item.getItemDisplayName());
441 logger.debug(testName + ": list-item[" + i + "] refName=" +
443 logger.debug(testName + ": list-item[" + i + "] URI=" +
451 // ---------------------------------------------------------------
452 // Cleanup of resources created during testing
453 // ---------------------------------------------------------------
456 * Deletes all resources created by tests, after all tests have been run.
458 * This cleanup method will always be run, even if one or more tests fail.
459 * For this reason, it attempts to remove all resources created
460 * at any point during testing, even if some of those resources
461 * may be expected to be deleted by certain tests.
463 @AfterClass(alwaysRun=true)
464 public void cleanUp() {
465 String noTest = System.getProperty("noTestCleanup");
466 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
467 if (logger.isDebugEnabled()) {
468 logger.debug("Skipping Cleanup phase ...");
472 if (logger.isDebugEnabled()) {
473 logger.debug("Cleaning up temporary resources created for testing ...");
475 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
476 for (String resourceId : collectionObjectIdsCreated) {
477 // Note: Any non-success responses are ignored and not reported.
478 collectionObjectClient.delete(resourceId).releaseConnection();
480 // Note: Any non-success response is ignored and not reported.
481 PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
482 // Delete persons before PersonAuth
483 for (String resourceId : personIdsCreated) {
484 // Note: Any non-success responses are ignored and not reported.
485 personAuthClient.deleteItem(personAuthCSID, resourceId).releaseConnection();
487 personAuthClient.delete(personAuthCSID).releaseConnection();
488 // Note: Any non-success response is ignored and not reported.
489 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
490 // Delete organizations before OrgAuth
491 for (String resourceId : orgIdsCreated) {
492 // Note: Any non-success responses are ignored and not reported.
493 orgAuthClient.deleteItem(orgAuthCSID, resourceId).releaseConnection();
495 orgAuthClient.delete(orgAuthCSID).releaseConnection();
498 // ---------------------------------------------------------------
499 // Utility methods used by tests above
500 // ---------------------------------------------------------------
502 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
505 public String getServicePathComponent() {
506 return SERVICE_PATH_COMPONENT;
510 * Creates the collection object instance.
512 * @param title the title
513 * @param objNum the obj num
514 * @param contentOrganization the content organization
515 * @param contentPeople the content people
516 * @param contentPerson the content person
517 * @param inscriber the inscriber
518 * @return the multipart output
520 private MultipartOutput createCollectionObjectInstance(
523 String contentOrganization,
524 String contentPerson,
525 String contentInscriber,
526 String descriptionInscriber,
527 String associatedEventOrganization,
528 String associatedEventPerson,
529 String associatedOrganization,
530 String associatedPerson,
532 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
533 collectionObject.setTitle(title);
534 collectionObject.setObjectNumber(objNum);
535 collectionObject.setContentOrganization(contentOrganization);
536 collectionObject.setContentPerson(contentPerson);
537 collectionObject.setInscriptionContentInscriber(contentInscriber);
538 collectionObject.setInscriptionDescriptionInscriber(descriptionInscriber);
539 collectionObject.setAssociatedEventOrganization(associatedEventOrganization);
540 collectionObject.setAssociatedEventPerson(associatedEventPerson);
541 collectionObject.setAssociatedOrganization(associatedOrganization);
542 collectionObject.setAssociatedPerson(associatedPerson);
543 collectionObject.setOwner(owner);
544 MultipartOutput multipart = new MultipartOutput();
545 OutputPart commonPart =
546 multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
547 commonPart.getHeaders().add("label", new CollectionObjectClient().getCommonPartName());
549 if(logger.isDebugEnabled()){
550 logger.debug("to be created, collectionObject common");
551 logger.debug(objectAsXmlString(collectionObject, CollectionobjectsCommon.class));