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 (c)) 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.Response;
32 import org.collectionspace.services.client.AbstractCommonListUtils;
33 import org.collectionspace.services.client.AuthorityClient;
34 import org.collectionspace.services.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.PoxPayloadIn;
36 import org.collectionspace.services.client.PoxPayloadOut;
37 import org.collectionspace.services.client.ContactClient;
38 import org.collectionspace.services.client.ContactClientUtils;
39 import org.collectionspace.services.contact.AddressGroup;
40 import org.collectionspace.services.contact.AddressGroupList;
41 import org.collectionspace.services.contact.ContactsCommon;
42 import org.collectionspace.services.client.PersonAuthorityClient;
43 import org.collectionspace.services.client.PersonAuthorityClientUtils;
44 import org.collectionspace.services.jaxb.AbstractCommonList;
45 import org.collectionspace.services.PersonJAXBSchema;
46 import org.collectionspace.services.person.PersonauthoritiesCommon;
47 import org.collectionspace.services.person.PersonTermGroup;
48 import org.collectionspace.services.person.PersonTermGroupList;
49 import org.collectionspace.services.person.PersonsCommon;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.testng.Assert;
54 import org.testng.annotations.AfterClass;
55 import org.testng.annotations.Test;
58 * PersonAuthorityServiceTest, carries out tests against a
59 * deployed and running PersonAuthority Service.
61 * $LastChangedRevision: 753 $
62 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
64 public class PersonAuthorityServiceTest extends AbstractAuthorityServiceTest<PersonauthoritiesCommon, PersonsCommon> { //FIXME: Test classes for Vocab, Person, Org, and Location should have a base class!
67 private final String CLASS_NAME = PersonAuthorityServiceTest.class.getName();
68 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71 * Default constructor. Used to set the short ID for all tests authority items
73 public PersonAuthorityServiceTest() {
75 TEST_SHORTID = "johnWayneActor";
79 protected String getTestAuthorityItemShortId() {
80 return getTestAuthorityItemShortId(true); // The short ID of every person item we create should be unique
84 public String getServicePathComponent() {
85 return PersonAuthorityClient.SERVICE_PATH_COMPONENT;
89 protected String getServiceName() {
90 return PersonAuthorityClient.SERVICE_NAME;
93 public String getItemServicePathComponent() {
94 return AuthorityClient.ITEMS;
97 /** The test forename. */
98 final String TEST_FORE_NAME = "John";
99 /** The test middle name. */
100 final String TEST_MIDDLE_NAME = null;
101 /** The test surname. */
102 final String TEST_SUR_NAME = "Wayne";
103 /** The test birthdate. */
104 final String TEST_BIRTH_DATE = "May 26, 1907";
105 /** The test death date. */
106 final String TEST_DEATH_DATE = "June 11, 1979";
107 //private String knownResourceRefName = null;
108 private String knownItemResourceShortIdentifer = null;
109 /** The known contact resource id. */
110 private String knownContactResourceId = null;
111 /** The all contact resource ids created. */
112 private Map<String, String> allContactResourceIdsCreated =
113 new HashMap<String, String>();
116 protected void setKnownResource(String id, String shortIdentifer,
118 knownResourceId = id;
119 knownResourceShortIdentifer = shortIdentifer;
120 //knownResourceRefName = refName;
123 protected void setKnownItemResource(String id, String shortIdentifer) {
124 knownItemResourceId = id;
125 knownItemResourceShortIdentifer = shortIdentifer;
129 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
132 protected CollectionSpaceClient getClientInstance() {
133 return new PersonAuthorityClient();
137 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
138 return new PersonAuthorityClient(clientPropertiesFilename);
141 // ---------------------------------------------------------------
142 // CRUD tests : CREATE tests
143 // ---------------------------------------------------------------
146 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
149 public void create(String testName) throws Exception {
150 // Perform setup, such as initializing the type of service request
151 // (e.g. CREATE, DELETE), its valid and expected status codes, and
152 // its associated HTTP method name (e.g. POST, DELETE).
155 // Submit the request to the service and store the response.
156 PersonAuthorityClient client = new PersonAuthorityClient();
157 String shortId = createIdentifier();
158 String displayName = "displayName-" + shortId;
159 //String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(shortId, null);
160 PoxPayloadOut multipart =
161 PersonAuthorityClientUtils.createPersonAuthorityInstance(
162 displayName, shortId, client.getCommonPartName());
163 // Extract the short ID since it might have been randomized by the createPersonAuthRefName() method
164 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(multipart,
165 client.getCommonPartName(), PersonauthoritiesCommon.class);
166 shortId = personAuthority.getShortIdentifier();
169 Response res = client.create(multipart);
171 assertStatusCode(res, testName);
172 newID = extractId(res);
178 // Save values for additional tests
179 if (knownResourceId == null) {
180 setKnownResource(newID, shortId, null ); //baseRefName);
181 if (logger.isDebugEnabled()) {
182 logger.debug(testName + ": knownResourceId=" + knownResourceId);
185 // Store the IDs from every resource created by tests,
186 // so they can be deleted after tests have been run.
187 allResourceIdsCreated.add(newID);
191 protected PoxPayloadOut createInstance(String identifier) {
192 PersonAuthorityClient client = new PersonAuthorityClient();
193 String displayName = "displayName-" + identifier;
194 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
195 displayName, identifier, client.getCommonPartName());
200 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
201 String headerLabel = new PersonAuthorityClient().getItemCommonPartName();
203 HashMap<String, String> personInfo = new HashMap<String, String>();
204 String shortId = "MarkTwainAuthor" + identifier;
205 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
207 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
208 PersonTermGroup term = new PersonTermGroup();
209 term.setTermDisplayName("Mark Twain Primary");
210 term.setTermName("MarkTwainPrimary");
213 term = new PersonTermGroup();
214 term.setTermDisplayName("Samuel Langhorne Clemens");
215 term.setTermName("SamuelLanghorneClemens");
218 term = new PersonTermGroup();
219 term.setTermDisplayName("Sam Clemens");
220 term.setTermName("SamClemens");
223 term = new PersonTermGroup();
224 term.setTermDisplayName("Huck Fin");
225 term.setTermName("Huck Fin");
228 return PersonAuthorityClientUtils.createPersonInstance(parentCsid, identifier, personInfo, terms, headerLabel);
233 * Creates an item in an authority, using test data.
235 * @param vcsid the vcsid
236 * @param authRefName the auth ref name
240 protected String createItemInAuthority(AuthorityClient client, String vcsid, String shortId) {
242 final String testName = "createItemInAuthority";
243 if (logger.isDebugEnabled()) {
244 logger.debug(testName + ":" + vcsid + "...");
247 Map<String, String> johnWayneMap = new HashMap<String, String>();
249 // Fill the property map
251 johnWayneMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
252 johnWayneMap.put(PersonJAXBSchema.GENDER, "male");
253 johnWayneMap.put(PersonJAXBSchema.BIRTH_DATE, TEST_BIRTH_DATE);
254 johnWayneMap.put(PersonJAXBSchema.BIRTH_PLACE, "Winterset, Iowa");
255 johnWayneMap.put(PersonJAXBSchema.DEATH_DATE, TEST_DEATH_DATE);
256 johnWayneMap.put(PersonJAXBSchema.BIO_NOTE, "born Marion Robert Morrison and better"
257 + "known by his stage name John Wayne, was an American film actor, director "
258 + "and producer. He epitomized rugged masculinity and has become an enduring "
259 + "American icon. He is famous for his distinctive voice, walk and height. "
260 + "He was also known for his conservative political views and his support in "
261 + "the 1950s for anti-communist positions.");
263 List<PersonTermGroup> johnWayneTerms = new ArrayList<PersonTermGroup>();
264 PersonTermGroup term = new PersonTermGroup();
265 term.setTermDisplayName("John Wayne DisplayName");
266 term.setTermName("John Wayne");
267 term.setForeName(TEST_FORE_NAME);
268 term.setSurName(TEST_SUR_NAME);
269 johnWayneTerms.add(term);
271 Map<String, List<String>> johnWayneRepeatablesMap = new HashMap<String, List<String>>();
272 List<String> johnWayneGroups = new ArrayList<String>();
273 johnWayneGroups.add("Irish");
274 johnWayneGroups.add("Scottish");
275 johnWayneRepeatablesMap.put(PersonJAXBSchema.GROUPS, johnWayneGroups);
277 return createItemInAuthority(client, vcsid, null /*authRefName*/, shortId, johnWayneMap, johnWayneTerms, johnWayneRepeatablesMap);
282 * Creates an item in an authority.
284 * @param vcsid the vcsid
285 * @param authRefName the auth ref name
286 * @param itemFieldProperties a set of properties specifying the values of fields.
287 * @param itemRepeatableFieldProperties a set of properties specifying the values of repeatable fields.
290 private String createItemInAuthority(AuthorityClient client, String vcsid, String authRefName, String shortId,
291 Map<String, String> itemFieldProperties, List<PersonTermGroup> terms, Map<String, List<String>> itemRepeatableFieldProperties) {
293 final String testName = "createItemInAuthority";
294 if (logger.isDebugEnabled()) {
295 logger.debug(testName + ":" + vcsid + "...");
298 // Submit the request to the service and store the response.
299 if (client == null) {
300 client = new PersonAuthorityClient();
302 PoxPayloadOut multipart =
303 PersonAuthorityClientUtils.createPersonInstance(vcsid, null /*authRefName*/, itemFieldProperties,
304 terms, itemRepeatableFieldProperties, client.getItemCommonPartName());
306 Response res = client.createItem(vcsid, multipart);
309 assertStatusCode(res, testName);
310 newID = PersonAuthorityClientUtils.extractId(res);
317 // Store the ID returned from the first item resource created
318 // for additional tests below.
319 if (knownItemResourceId == null) {
320 setKnownItemResource(newID, shortId);
321 if (logger.isDebugEnabled()) {
322 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
326 if (logger.isDebugEnabled()) {
327 logger.debug(testName + " (created):" + vcsid + "/(" + newID + "," + shortId + ")");
330 // Store the IDs from any item resources created
331 // by tests, along with the IDs of their parents, so these items
332 // can be deleted after all tests have been run.
333 allResourceItemIdsCreated.put(newID, vcsid);
339 * Creates the contact.
341 * @param testName the test name
343 @Test(dataProvider = "testName", groups = {"create"},
344 dependsOnMethods = {"createItem"})
345 public void createContact(String testName) {
347 createContactInItem(knownResourceId, knownItemResourceId);
351 * Creates the contact in item.
353 * @param parentcsid the parentcsid
354 * @param itemcsid the itemcsid
357 private String createContactInItem(String parentcsid, String itemcsid) {
359 final String testName = "createContactInItem";
360 if (logger.isDebugEnabled()) {
361 logger.debug(testName + ":...");
363 // Submit the request to the service and store the response.
364 PersonAuthorityClient client = new PersonAuthorityClient();
365 String identifier = createIdentifier();
366 PoxPayloadOut multipart = ContactClientUtils.createContactInstance(parentcsid,
367 itemcsid, identifier, new ContactClient().getCommonPartName());
370 Response res = client.createContact(parentcsid, itemcsid, multipart);
373 assertStatusCode(res, testName);
374 newID = PersonAuthorityClientUtils.extractId(res);
381 // Store the ID returned from the first contact resource created
382 // for additional tests below.
383 if (knownContactResourceId == null) {
384 knownContactResourceId = newID;
385 if (logger.isDebugEnabled()) {
386 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
390 // Store the IDs from any contact resources created
391 // by tests, along with the IDs of their parent items,
392 // so these items can be deleted after all tests have been run.
393 allContactResourceIdsCreated.put(newID, itemcsid);
399 * Attempts to create an authority with an short identifier that contains
400 * non-word characters.
402 * @param testName the test name
404 @Test(dataProvider = "testName", groups = {"create", "nonWordCharsInShortId"})
405 public void createWithShortIdNonWordChars(String testName) throws Exception {
406 testExpectedStatusCode = STATUS_BAD_REQUEST;
407 testRequestType = ServiceRequestType.CREATE;
408 testSetup(testExpectedStatusCode, testRequestType);
410 // Create the payload to be included in the body of the request
411 PersonAuthorityClient client = new PersonAuthorityClient();
412 String shortId = createIdentifier() + "*" + createIdentifier();
413 String displayName = "displayName-" + shortId;
414 PoxPayloadOut multipart =
415 PersonAuthorityClientUtils.createPersonAuthorityInstance(
416 displayName, shortId, client.getCommonPartName());
418 // Submit the request to the service and store the response.
419 Response res = client.create(multipart);
420 // Check the status code of the response: does it match
421 // the expected response(s)? We expect failure here.
423 assertStatusCode(res, testName);
432 * Attempts to create an item with an short identifier that contains
433 * non-word characters.
435 * @param testName the test name
437 @Test(dataProvider = "testName", groups = {"create", "nonWordCharsInShortId"},
438 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractServiceTestImpl.create"})
439 public void createItemWithShortIdNonWordChars(String testName) {
440 testExpectedStatusCode = STATUS_BAD_REQUEST;
441 testRequestType = ServiceRequestType.CREATE;
442 testSetup(testExpectedStatusCode, testRequestType);
444 PersonAuthorityClient client = new PersonAuthorityClient();
445 // Create the payload to be included in the body of the request
446 String shortId = "7-Eleven";
447 Map<String, String> fieldProperties = new HashMap<String, String>();
448 fieldProperties.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
450 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
451 PersonTermGroup term = new PersonTermGroup();
452 term.setTermDisplayName(shortId);
453 term.setTermName(shortId);
456 final Map<String, List<String>> NULL_REPEATABLE_FIELD_PROPERTIES = null;
457 PoxPayloadOut multipart =
458 PersonAuthorityClientUtils.createPersonInstance(knownResourceId,
459 null /*knownResourceRefName*/, fieldProperties, terms,
460 NULL_REPEATABLE_FIELD_PROPERTIES, client.getItemCommonPartName());
462 // Send the request and receive a response
463 Response res = client.createItem(knownResourceId, multipart);
464 // Check the status code of the response: does it match
465 // the expected response(s)? We expect failure here, so there will be no
466 // new ID to keep track of for later cleanup.
468 assertStatusCode(res, testName);
476 // ---------------------------------------------------------------
477 // CRUD tests : CREATE LIST tests
478 // ---------------------------------------------------------------
481 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
484 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
485 // groups = {"createList"}, dependsOnGroups = {"create"})
486 public void createList(String testName) throws Exception {
487 for (int i = 0; i < nItemsToCreateInList; i++) {
493 * Creates the contact list.
495 * @param testName the test name
496 * @throws Exception the exception
498 @Test(dataProvider = "testName", groups = {"createList"},
499 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.createItemList"})
500 public void createContactList(String testName) throws Exception {
501 // Add contacts to the initially-created, known item record.
502 for (int j = 0; j < nItemsToCreateInList; j++) {
503 createContact(testName);
507 // ---------------------------------------------------------------
508 // CRUD tests : READ tests
509 // ---------------------------------------------------------------
512 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
515 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
516 // groups = {"read"}, dependsOnGroups = {"create"})
517 public void read(String testName) throws Exception {
518 readInternal(testName, knownResourceId, null);
524 * @param testName the test name
525 * @throws Exception the exception
528 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
529 // groups = {"read"}, dependsOnMethods = {"read"})
530 public void readByName(String testName) throws Exception {
531 readInternal(testName, null, knownResourceShortIdentifer);
534 protected void readInternal(String testName, String CSID, String shortId) {
535 // Submit the request to the service and store the response.
536 PersonAuthorityClient client = new PersonAuthorityClient();
540 res = client.read(CSID);
541 } else if (shortId != null) {
542 res = client.readByName(shortId);
544 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
547 assertStatusCode(res, testName);
548 //FIXME: remove the following try catch once Aron fixes signatures
550 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
551 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
552 client.getCommonPartName(), PersonauthoritiesCommon.class);
553 Assert.assertNotNull(personAuthority);
554 } catch (Exception e) {
555 throw new RuntimeException(e);
567 * @param testName the test name
568 * @throws Exception the exception
570 @Test(dataProvider = "testName", groups = {"readItem", "readNamedItemInNamedAuth"},
571 dependsOnMethods = {"readItemInNamedAuth"})
572 public void readNamedItem(String testName) throws Exception {
573 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
577 * Read item in Named Auth.
579 * @param testName the test name
580 * @throws Exception the exception
582 @Test(dataProvider = "testName", groups = {"readItem"},
583 dependsOnMethods = {"readItem"})
584 public void readItemInNamedAuth(String testName) throws Exception {
585 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
590 * Read Named item in Named Auth.
592 * @param testName the test name
593 * @throws Exception the exception
595 @Test(dataProvider = "testName", groups = {"readItem"},
596 dependsOnMethods = {"readNamedItem"})
597 public void readNamedItemInNamedAuth(String testName) throws Exception {
598 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
601 protected void readItemInternal(String testName,
602 String authCSID, String authShortId, String itemCSID, String itemShortId)
605 if (logger.isDebugEnabled()) {
606 logger.debug("Reading:" + ((authCSID != null) ? authCSID : authShortId) + "/"
607 + ((itemCSID != null) ? authCSID : itemShortId));
610 // Submit the request to the service and store the response.
611 PersonAuthorityClient client = new PersonAuthorityClient();
614 if (authCSID != null) {
615 if (itemCSID != null) {
616 res = client.readItem(authCSID, itemCSID);
617 } else if (itemShortId != null) {
618 res = client.readNamedItem(authCSID, itemShortId);
620 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
622 } else if (authShortId != null) {
623 if (itemCSID != null) {
624 res = client.readItemInNamedAuthority(authShortId, itemCSID);
625 } else if (itemShortId != null) {
626 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
628 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
631 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
635 assertStatusCode(res, testName);
636 // Check whether we've received a person.
637 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
638 PersonsCommon person = (PersonsCommon) extractPart(input,
639 client.getItemCommonPartName(), PersonsCommon.class);
640 Assert.assertNotNull(person);
641 boolean showFull = true;
642 if (showFull && logger.isDebugEnabled()) {
643 logger.debug(testName + ": returned payload:");
644 logger.debug(objectAsXmlString(person, PersonsCommon.class));
647 // Check that the person item is within the expected Person Authority.
648 Assert.assertEquals(person.getInAuthority(), knownResourceId);
650 // Verify the number and contents of values in a repeatable field,
651 // as created in the instance record used for testing.
652 List<String> groups = person.getGroups().getGroup();
653 Assert.assertTrue(groups.size() > 0);
654 Assert.assertNotNull(groups.get(0));
662 // Note: This test depends on server-side validation logic to require
663 // a non-null (and potentially, non-empty) displayname for each term,
664 // and will fail if that validation is not present.
667 * Verify illegal item display name.
669 * @param testName the test name
670 * @throws Exception the exception
672 @Test(dataProvider = "testName", groups = {"update"})
673 public void verifyIllegalItemDisplayName(String testName) throws Exception {
674 // Perform setup for read.
677 // Submit the request to the service and store the response.
678 PersonAuthorityClient client = new PersonAuthorityClient();
679 Response res = client.readItem(knownResourceId, knownItemResourceId);
680 PoxPayloadIn input = null;
682 assertStatusCode(res, testName);
683 input = new PoxPayloadIn(res.readEntity(String.class));
690 // Make an invalid UPDATE request, without a display name
692 PersonsCommon person = (PersonsCommon) extractPart(input,
693 client.getItemCommonPartName(), PersonsCommon.class);
694 Assert.assertNotNull(person);
695 // Try to Update with no displayName
696 PersonTermGroupList termList = person.getPersonTermGroupList();
697 Assert.assertNotNull(termList);
698 List<PersonTermGroup> terms = termList.getPersonTermGroup();
699 Assert.assertNotNull(terms);
700 Assert.assertTrue(terms.size() > 0);
701 terms.get(0).setTermDisplayName(null);
702 terms.get(0).setTermName(null);
704 // Submit the updated resource to the service and store the response.
705 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
706 output.addPart(client.getItemCommonPartName(), person);
707 setupUpdateWithInvalidBody();
708 res = client.updateItem(knownResourceId, knownItemResourceId, output);
710 assertStatusCode(res, testName);
721 * @param testName the test name
722 * @throws Exception the exception
724 @Test(dataProvider = "testName", groups = {"readItem"},
725 dependsOnMethods = {"createContact", "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItem"})
726 public void readContact(String testName) throws Exception {
730 // Submit the request to the service and store the response.
731 PersonAuthorityClient client = new PersonAuthorityClient();
732 PoxPayloadIn input = null;
733 Response res = client.readContact(knownResourceId, knownItemResourceId,
734 knownContactResourceId);
736 assertStatusCode(res, testName);
737 // Check whether we've received a contact.
738 input = new PoxPayloadIn(res.readEntity(String.class));
745 ContactsCommon contact = (ContactsCommon) extractPart(input,
746 new ContactClient().getCommonPartName(), ContactsCommon.class);
747 Assert.assertNotNull(contact);
748 boolean showFull = true;
749 if (showFull && logger.isDebugEnabled()) {
750 logger.debug(testName + ": returned payload:");
751 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
753 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
754 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
759 * Read contact non existent.
761 * @param testName the test name
763 @Test(dataProvider = "testName", groups = {"readItem"}, dependsOnMethods = {"readContact"})
764 public void readContactNonExistent(String testName) {
766 setupReadNonExistent();
768 // Submit the request to the service and store the response.
769 PersonAuthorityClient client = new PersonAuthorityClient();
770 Response res = client.readContact(knownResourceId, knownItemResourceId,
773 assertStatusCode(res, testName);
781 // ---------------------------------------------------------------
782 // CRUD tests : READ_LIST tests
783 // ---------------------------------------------------------------
788 @Test(groups = {"readList"},
789 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
790 public void readContactList() {
791 readContactList(knownResourceId, knownItemResourceId);
797 * @param parentcsid the parentcsid
798 * @param itemcsid the itemcsid
800 private void readContactList(String parentcsid, String itemcsid) {
801 final String testName = "readContactList";
806 // Submit the request to the service and store the response.
807 PersonAuthorityClient client = new PersonAuthorityClient();
808 AbstractCommonList list = null;
809 Response res = client.readContactList(parentcsid, itemcsid);
811 assertStatusCode(res, testName);
812 list = res.readEntity(AbstractCommonList.class);
819 List<AbstractCommonList.ListItem> listitems =
821 int nItemsReturned = listitems.size();
822 // There will be one item created, associated with a
823 // known parent resource, by the createItem test.
825 // In addition, there will be 'nItemsToCreateInList'
826 // additional items created by the createItemList test,
827 // all associated with the same parent resource.
828 int nExpectedItems = nItemsToCreateInList + 1;
829 if (logger.isDebugEnabled()) {
830 logger.debug(testName + ": Expected "
831 + nExpectedItems + " items; got: " + nItemsReturned);
833 Assert.assertEquals(nItemsReturned, nExpectedItems);
835 // Optionally output additional data about list members for debugging.
836 boolean iterateThroughList = false;
837 if (iterateThroughList && logger.isDebugEnabled()) {
838 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
843 // There are no failure outcome tests at present.
844 // ---------------------------------------------------------------
845 // CRUD tests : UPDATE tests
846 // ---------------------------------------------------------------
849 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
852 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
853 // groups = {"update"}, dependsOnGroups = {"readItem", "readList"})
854 public void update(String testName) throws Exception {
855 // Retrieve the contents of a resource to update.
856 PersonAuthorityClient client = new PersonAuthorityClient();
857 PoxPayloadIn input = null;
859 Response res = client.read(knownResourceId);
861 assertStatusCode(res, testName);
862 if (logger.isDebugEnabled()) {
863 logger.debug("got PersonAuthority to update with ID: " + knownResourceId);
865 input = new PoxPayloadIn(res.readEntity(String.class));
872 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
873 client.getCommonPartName(), PersonauthoritiesCommon.class);
874 Assert.assertNotNull(personAuthority);
876 // Update the contents of this resource.
877 personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
878 personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
879 if (logger.isDebugEnabled()) {
880 logger.debug("to be updated PersonAuthority");
881 logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
884 // Submit the updated resource to the service and store the response.
885 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_PAYLOAD_NAME);
886 output.addPart(client.getCommonPartName(), personAuthority);
888 res = client.update(knownResourceId, output);
890 assertStatusCode(res, testName);
891 // Retrieve the updated resource and verify that its contents exist.
892 input = new PoxPayloadIn(res.readEntity(String.class));
899 PersonauthoritiesCommon updatedPersonAuthority =
900 (PersonauthoritiesCommon) extractPart(input,
901 client.getCommonPartName(), PersonauthoritiesCommon.class);
902 Assert.assertNotNull(updatedPersonAuthority);
904 // Verify that the updated resource received the correct data.
905 Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
906 personAuthority.getDisplayName(),
907 "Data in updated object did not match submitted data.");
911 * Update item override -see immediate superclass.
913 * @param testName the test name
914 * @throws Exception the exception
917 public void updateItem(String testName) throws Exception {
918 // Retrieve the contents of a resource to update.
919 PersonAuthorityClient client = new PersonAuthorityClient();
920 PoxPayloadIn input = null;
922 Response res = client.readItem(knownResourceId, knownItemResourceId);
924 assertStatusCode(res, testName);
925 if (logger.isDebugEnabled()) {
926 logger.debug("got Person to update with ID: "
927 + knownItemResourceId
928 + " in PersonAuthority: " + knownResourceId);
930 input = new PoxPayloadIn(res.readEntity(String.class));
937 PersonsCommon person = (PersonsCommon) extractPart(input,
938 client.getItemCommonPartName(), PersonsCommon.class);
939 Assert.assertNotNull(person);
941 if (logger.isDebugEnabled() == true) {
942 logger.debug("About to update the following person...");
943 logger.debug(objectAsXmlString(person, PersonsCommon.class));
946 // Update the contents of this resource.
947 person.setCsid(null);
948 PersonTermGroupList termList = person.getPersonTermGroupList();
949 Assert.assertNotNull(termList);
950 List<PersonTermGroup> terms = termList.getPersonTermGroup();
951 Assert.assertNotNull(terms);
952 Assert.assertTrue(terms.size() > 0);
953 String foreName = terms.get(0).getForeName();
954 String updatedForeName = "updated-" + foreName;
955 terms.get(0).setForeName(updatedForeName);
956 if (logger.isDebugEnabled()) {
957 logger.debug("to be updated Person");
958 logger.debug(objectAsXmlString(person,
959 PersonsCommon.class));
962 // Submit the updated resource to the service and store the response.
963 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
964 output.addPart(client.getItemCommonPartName(), person);
966 res = client.updateItem(knownResourceId, knownItemResourceId, output);
968 assertStatusCode(res, testName);
969 // Retrieve the updated resource and verify that its contents exist.
970 input = new PoxPayloadIn(res.readEntity(String.class));
977 PersonsCommon updatedPerson =
978 (PersonsCommon) extractPart(input,
979 client.getItemCommonPartName(), PersonsCommon.class);
980 Assert.assertNotNull(updatedPerson);
982 if (logger.isDebugEnabled() == true) {
983 logger.debug("Updated to following person to:");
984 logger.debug(objectAsXmlString(updatedPerson, PersonsCommon.class));
987 // Verify that the updated resource received the correct data.
988 PersonTermGroupList updatedTermList = person.getPersonTermGroupList();
989 Assert.assertNotNull(updatedTermList);
990 List<PersonTermGroup> updatedTerms = termList.getPersonTermGroup();
991 Assert.assertNotNull(updatedTerms);
992 Assert.assertEquals(updatedTerms.get(0).getForeName(), updatedForeName,
993 "Data in updated Person did not match submitted data.");
999 * @param testName the test name
1000 * @throws Exception the exception
1002 @Test(dataProvider = "testName", groups = {"update"},
1003 dependsOnMethods = {"readContact", "testContactSubmitRequest",
1004 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
1005 public void updateContact(String testName) throws Exception {
1006 String contactsCommonLabel = new ContactClient().getCommonPartName();
1008 // Retrieve the contents of a resource to update.
1009 PersonAuthorityClient client = new PersonAuthorityClient();
1010 PoxPayloadIn input = null;
1012 Response res = client.readContact(knownResourceId, knownItemResourceId,
1013 knownContactResourceId);
1015 assertStatusCode(res, testName);
1016 if (logger.isDebugEnabled()) {
1017 logger.debug("got Contact to update with ID: "
1018 + knownContactResourceId
1019 + " in item: " + knownItemResourceId
1020 + " in parent: " + knownResourceId);
1022 input = new PoxPayloadIn(res.readEntity(String.class));
1029 ContactsCommon contact = (ContactsCommon) extractPart(input,
1030 contactsCommonLabel, ContactsCommon.class);
1031 Assert.assertNotNull(contact);
1033 // Verify the contents of this resource
1034 AddressGroupList addressGroupList = contact.getAddressGroupList();
1035 Assert.assertNotNull(addressGroupList);
1036 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
1037 Assert.assertNotNull(addressGroups);
1038 Assert.assertTrue(addressGroups.size() > 0);
1039 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
1040 Assert.assertNotNull(addressPlace1);
1042 // Update the contents of this resource.
1043 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
1044 contact.setAddressGroupList(addressGroupList);
1045 if (logger.isDebugEnabled()) {
1046 logger.debug("to be updated Contact");
1047 logger.debug(objectAsXmlString(contact,
1048 ContactsCommon.class));
1051 // Submit the updated resource to the service and store the response.
1052 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
1053 output.addPart(contactsCommonLabel, contact);
1055 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1057 assertStatusCode(res, testName);
1058 // Retrieve the updated resource and verify that its contents exist.
1059 input = new PoxPayloadIn(res.readEntity(String.class));;
1065 ContactsCommon updatedContact = (ContactsCommon) extractPart(input,
1066 contactsCommonLabel, ContactsCommon.class);
1067 Assert.assertNotNull(updatedContact);
1069 // Verify that the updated resource received the correct data.
1070 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1071 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1072 "Data in updated object did not match submitted data.");
1076 * Update non existent contact.
1078 * @param testName the test name
1079 * @throws Exception the exception
1081 @Test(dataProvider = "testName", groups = {"update"},
1082 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1083 public void updateNonExistentContact(String testName) throws Exception {
1084 // Currently a no-op test
1087 // ---------------------------------------------------------------
1088 // CRUD tests : DELETE tests
1089 // ---------------------------------------------------------------
1091 // Note: delete sub-resources in ascending hierarchical order,
1092 // before deleting their parents.
1096 * @param testName the test name
1097 * @throws Exception the exception
1099 @Test(dataProvider = "testName", groups = {"delete"},
1100 dependsOnMethods = {"updateContact"})
1101 public void deleteContact(String testName) throws Exception {
1102 if (logger.isDebugEnabled()) {
1103 logger.debug("parentcsid =" + knownResourceId
1104 + " itemcsid = " + knownItemResourceId
1105 + " csid = " + knownContactResourceId);
1108 // Submit the request to the service and store the response.
1109 PersonAuthorityClient client = new PersonAuthorityClient();
1111 Response res = client.deleteContact(knownResourceId, knownItemResourceId,
1112 knownContactResourceId);
1114 assertStatusCode(res, testName);
1123 public void delete(String testName) throws Exception {
1124 // Do nothing. See localDelete(). This ensure proper test order.
1127 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
1128 public void localDelete(String testName) throws Exception {
1129 super.delete(testName);
1133 public void deleteItem(String testName) throws Exception {
1134 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
1135 // its dependencies will get run first and then we can call the base class' delete method.
1138 @Test(dataProvider = "testName", groups = {"delete"},
1139 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
1140 public void localDeleteItem(String testName) throws Exception {
1141 super.deleteItem(testName);
1145 * Delete non existent contact.
1147 * @param testName the test name
1149 @Test(dataProvider = "testName", groups = {"delete"},
1150 dependsOnMethods = {"deleteContact"})
1151 public void deleteNonExistentContact(String testName) {
1152 // Submit the request to the service and store the response.
1153 PersonAuthorityClient client = new PersonAuthorityClient();
1154 setupDeleteNonExistent();
1155 Response res = client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1157 assertStatusCode(res, testName);
1166 * Test contact submit request.
1168 @Test(dataProvider = "testName",
1169 dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
1170 public void testContactSubmitRequest(String testName) {
1172 // Expected status code: 200 OK
1173 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1175 // Submit the request to the service and store the response.
1176 String method = ServiceRequestType.READ.httpMethodName();
1177 String url = getContactResourceURL(knownResourceId,
1178 knownItemResourceId, knownContactResourceId);
1179 int statusCode = submitRequest(method, url);
1181 // Check the status code of the response: does it match
1182 // the expected response(s)?
1183 if (logger.isDebugEnabled()) {
1184 logger.debug("testContactSubmitRequest: url=" + url
1185 + " status=" + statusCode);
1187 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1191 // ---------------------------------------------------------------
1192 // Cleanup of resources created during testing
1193 // ---------------------------------------------------------------
1195 * Deletes all resources created by tests, after all tests have been run.
1197 * This cleanup method will always be run, even if one or more tests fail.
1198 * For this reason, it attempts to remove all resources created
1199 * at any point during testing, even if some of those resources
1200 * may be expected to be deleted by certain tests.
1202 @AfterClass(alwaysRun = true)
1204 public void cleanUp() {
1205 String noTest = System.getProperty("noTestCleanup");
1206 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1207 if (logger.isDebugEnabled()) {
1208 logger.debug("Skipping Cleanup phase ...");
1213 if (logger.isDebugEnabled()) {
1214 logger.debug("Cleaning up temporary resources created for testing ...");
1217 String parentResourceId;
1218 String itemResourceId;
1219 String contactResourceId;
1220 // Clean up contact resources.
1221 PersonAuthorityClient client = new PersonAuthorityClient();
1222 parentResourceId = this.getKnowResourceId();
1223 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
1224 contactResourceId = entry.getKey();
1225 itemResourceId = entry.getValue();
1226 // Note: Any non-success responses from the delete operation
1227 // below are ignored and not reported.
1228 Response res = client.deleteContact(parentResourceId, itemResourceId,
1233 // Finally, clean call our superclass' cleanUp method.
1238 // ---------------------------------------------------------------
1239 // Utility methods used by tests above
1240 // ---------------------------------------------------------------
1242 * Gets the contact service path component.
1244 * @return the contact service path component
1246 public String getContactServicePathComponent() {
1247 return ContactClient.SERVICE_PATH_COMPONENT;
1251 * Returns the root URL for the item service.
1253 * This URL consists of a base URL for all services, followed by
1254 * a path component for the owning parent, followed by the
1255 * path component for the items.
1257 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1258 * parent authority resource of the relevant item resource.
1260 * @return The root URL for the item service.
1262 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1263 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1267 * Returns the URL of a specific item resource managed by a service, and
1268 * designated by an identifier (such as a universally unique ID, or UUID).
1270 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1271 * parent authority resource of the relevant item resource.
1273 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1276 * @return The URL of a specific item resource managed by a service.
1278 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1279 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1283 * Returns the root URL for the contact service.
1285 * This URL consists of a base URL for all services, followed by
1286 * a path component for the owning authority, followed by the
1287 * path component for the owning item, followed by the path component
1288 * for the contact service.
1290 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1291 * parent authority resource of the relevant item resource.
1293 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1296 * @return The root URL for the contact service.
1298 protected String getContactServiceRootURL(String parentResourceIdentifier,
1299 String itemResourceIdentifier) {
1300 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1301 + getContactServicePathComponent();
1305 * Returns the URL of a specific contact resource managed by a service, and
1306 * designated by an identifier (such as a universally unique ID, or UUID).
1308 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1309 * parent resource of the relevant item resource.
1311 * @param resourceIdentifier An identifier (such as a UUID) for an
1314 * @return The URL of a specific resource managed by a service.
1316 protected String getContactResourceURL(String parentResourceIdentifier,
1317 String itemResourceIdentifier, String contactResourceIdentifier) {
1318 return getContactServiceRootURL(parentResourceIdentifier,
1319 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1323 public void authorityTests(String testName) {
1324 // TODO Auto-generated method stub
1329 protected PersonsCommon updateItemInstance(PersonsCommon personsCommon) {
1331 PersonTermGroupList termList = personsCommon.getPersonTermGroupList();
1332 Assert.assertNotNull(termList);
1333 List<PersonTermGroup> terms = termList.getPersonTermGroup();
1334 Assert.assertNotNull(terms);
1335 Assert.assertTrue(terms.size() > 0);
1336 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1337 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1338 personsCommon.setPersonTermGroupList(termList);
1340 return personsCommon;
1344 protected void compareUpdatedItemInstances(PersonsCommon original,
1345 PersonsCommon updated) throws Exception {
1347 PersonTermGroupList originalTermList = original.getPersonTermGroupList();
1348 Assert.assertNotNull(originalTermList);
1349 List<PersonTermGroup> originalTerms = originalTermList.getPersonTermGroup();
1350 Assert.assertNotNull(originalTerms);
1351 Assert.assertTrue(originalTerms.size() > 0);
1353 PersonTermGroupList updatedTermList = updated.getPersonTermGroupList();
1354 Assert.assertNotNull(updatedTermList);
1355 List<PersonTermGroup> updatedTerms = updatedTermList.getPersonTermGroup();
1356 Assert.assertNotNull(updatedTerms);
1357 Assert.assertTrue(updatedTerms.size() > 0);
1359 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1360 originalTerms.get(0).getTermDisplayName(),
1361 "Value in updated record did not match submitted data.");
1365 protected PoxPayloadOut createInstance(String commonPartName,
1366 String identifier) {
1367 String shortId = identifier;
1368 String displayName = "displayName-" + shortId;
1369 PoxPayloadOut result =
1370 PersonAuthorityClientUtils.createPersonAuthorityInstance(
1371 displayName, shortId, commonPartName);
1376 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1377 String identifier) {
1378 String displayName = "displayName-NON_EXISTENT_ID";
1379 PoxPayloadOut result = PersonAuthorityClientUtils.createPersonAuthorityInstance(
1380 displayName, "NON_EXISTENT_SHORT_ID", commonPartName);
1384 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier) {
1385 Map<String, String> nonexMap = new HashMap<String, String>();
1386 nonexMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, "nonEX");
1387 nonexMap.put(PersonJAXBSchema.GENDER, "male");
1389 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
1390 PersonTermGroup term = new PersonTermGroup();
1391 term.setTermDisplayName("John Wayne");
1392 term.setTermName("John Wayne");
1393 term.setForeName("John");
1394 term.setSurName("Wayne");
1397 Map<String, List<String>> nonexRepeatablesMap = new HashMap<String, List<String>>();
1398 PoxPayloadOut result =
1399 PersonAuthorityClientUtils.createPersonInstance(NON_EXISTENT_ID,
1400 null, //PersonAuthorityClientUtils.createPersonAuthRefName(NON_EXISTENT_ID, null),
1401 nonexMap, terms, nonexRepeatablesMap, commonPartName);
1406 protected PersonauthoritiesCommon updateInstance(PersonauthoritiesCommon personauthoritiesCommon) {
1407 PersonauthoritiesCommon result = new PersonauthoritiesCommon();
1409 result.setDisplayName("updated-" + personauthoritiesCommon.getDisplayName());
1410 result.setVocabType("updated-" + personauthoritiesCommon.getVocabType());
1416 protected void compareUpdatedInstances(PersonauthoritiesCommon original,
1417 PersonauthoritiesCommon updated) throws Exception {
1418 // Verify that the updated resource received the correct data.
1419 Assert.assertEquals(updated.getDisplayName(),
1420 original.getDisplayName(),
1421 "Display name in updated object did not match submitted data.");
1425 protected void verifyReadItemInstance(PersonsCommon item) throws Exception {
1426 // Do nothing for now. Add more 'read' validation checks here if applicable.