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;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.testng.Assert;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.Test;
57 * PersonAuthorityServiceTest, carries out tests against a
58 * deployed and running PersonAuthority Service.
60 * $LastChangedRevision: 753 $
61 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
63 public class PersonAuthorityServiceTest extends AbstractAuthorityServiceTest<PersonauthoritiesCommon, PersonsCommon> { //FIXME: Test classes for Vocab, Person, Org, and Location should have a base class!
66 private final String CLASS_NAME = PersonAuthorityServiceTest.class.getName();
67 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
70 * Default constructor. Used to set the short ID for all tests authority items
72 public PersonAuthorityServiceTest() {
74 TEST_SHORTID = "johnWayneActor";
78 protected String getTestAuthorityItemShortId() {
79 return getTestAuthorityItemShortId(true); // The short ID of every person item we create should be unique
83 public String getServicePathComponent() {
84 return PersonAuthorityClient.SERVICE_PATH_COMPONENT;
88 protected String getServiceName() {
89 return PersonAuthorityClient.SERVICE_NAME;
92 public String getItemServicePathComponent() {
93 return AuthorityClient.ITEMS;
96 /** The test forename. */
97 final String TEST_FORE_NAME = "John";
98 /** The test middle name. */
99 final String TEST_MIDDLE_NAME = null;
100 /** The test surname. */
101 final String TEST_SUR_NAME = "Wayne";
102 /** The test birthdate. */
103 final String TEST_BIRTH_DATE = "May 26, 1907";
104 /** The test death date. */
105 final String TEST_DEATH_DATE = "June 11, 1979";
106 //private String knownResourceRefName = null;
107 private String knownItemResourceShortIdentifer = null;
108 /** The known contact resource id. */
109 private String knownContactResourceId = null;
110 /** The all contact resource ids created. */
111 private Map<String, String> allContactResourceIdsCreated =
112 new HashMap<String, String>();
115 protected void setKnownResource(String id, String shortIdentifer,
117 knownResourceId = id;
118 knownResourceShortIdentifer = shortIdentifer;
119 //knownResourceRefName = refName;
122 protected void setKnownItemResource(String id, String shortIdentifer) {
123 knownItemResourceId = id;
124 knownItemResourceShortIdentifer = shortIdentifer;
128 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
131 protected CollectionSpaceClient getClientInstance() {
132 return new PersonAuthorityClient();
136 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
137 return new PersonAuthorityClient(clientPropertiesFilename);
140 // ---------------------------------------------------------------
141 // CRUD tests : CREATE tests
142 // ---------------------------------------------------------------
145 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
148 public void create(String testName) throws Exception {
149 // Perform setup, such as initializing the type of service request
150 // (e.g. CREATE, DELETE), its valid and expected status codes, and
151 // its associated HTTP method name (e.g. POST, DELETE).
154 // Submit the request to the service and store the response.
155 PersonAuthorityClient client = new PersonAuthorityClient();
156 String shortId = createIdentifier();
157 String displayName = "displayName-" + shortId;
158 //String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(shortId, null);
159 PoxPayloadOut multipart =
160 PersonAuthorityClientUtils.createPersonAuthorityInstance(
161 displayName, shortId, client.getCommonPartName());
162 // Extract the short ID since it might have been randomized by the createPersonAuthRefName() method
163 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(multipart,
164 client.getCommonPartName(), PersonauthoritiesCommon.class);
165 shortId = personAuthority.getShortIdentifier();
168 Response res = client.create(multipart);
170 assertStatusCode(res, testName);
171 newID = extractId(res);
177 // Save values for additional tests
178 if (knownResourceId == null) {
179 setKnownResource(newID, shortId, null ); //baseRefName);
180 if (logger.isDebugEnabled()) {
181 logger.debug(testName + ": knownResourceId=" + knownResourceId);
184 // Store the IDs from every resource created by tests,
185 // so they can be deleted after tests have been run.
186 allResourceIdsCreated.add(newID);
190 protected PoxPayloadOut createInstance(String identifier) {
191 PersonAuthorityClient client = new PersonAuthorityClient();
193 String displayName = "displayName-" + identifier;
194 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
195 displayName, identifier, client.getCommonPartName());
201 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
202 String headerLabel = new PersonAuthorityClient().getItemCommonPartName();
204 HashMap<String, String> personInfo = new HashMap<String, String>();
205 String shortId = "MarkTwainAuthor" + identifier;
206 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
208 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
209 PersonTermGroup term = new PersonTermGroup();
210 term.setTermDisplayName("Mark Twain Primary");
211 term.setTermName("MarkTwainPrimary");
214 term = new PersonTermGroup();
215 term.setTermDisplayName("Samuel Langhorne Clemens");
216 term.setTermName("SamuelLanghorneClemens");
219 term = new PersonTermGroup();
220 term.setTermDisplayName("Sam Clemens");
221 term.setTermName("SamClemens");
224 term = new PersonTermGroup();
225 term.setTermDisplayName("Huck Fin");
226 term.setTermName("Huck Fin");
229 return PersonAuthorityClientUtils.createPersonInstance(parentCsid, identifier, personInfo, terms, headerLabel);
234 * Creates an item in an authority, using test data.
236 * @param vcsid the vcsid
237 * @param authRefName the auth ref name
241 protected String createItemInAuthority(AuthorityClient client, String vcsid, String shortId) {
243 final String testName = "createItemInAuthority";
244 if (logger.isDebugEnabled()) {
245 logger.debug(testName + ":" + vcsid + "...");
248 Map<String, String> johnWayneMap = new HashMap<String, String>();
250 // Fill the property map
252 johnWayneMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
253 johnWayneMap.put(PersonJAXBSchema.GENDER, "male");
254 johnWayneMap.put(PersonJAXBSchema.BIRTH_DATE, TEST_BIRTH_DATE);
255 johnWayneMap.put(PersonJAXBSchema.BIRTH_PLACE, "Winterset, Iowa");
256 johnWayneMap.put(PersonJAXBSchema.DEATH_DATE, TEST_DEATH_DATE);
257 johnWayneMap.put(PersonJAXBSchema.BIO_NOTE, "born Marion Robert Morrison and better"
258 + "known by his stage name John Wayne, was an American film actor, director "
259 + "and producer. He epitomized rugged masculinity and has become an enduring "
260 + "American icon. He is famous for his distinctive voice, walk and height. "
261 + "He was also known for his conservative political views and his support in "
262 + "the 1950s for anti-communist positions.");
264 List<PersonTermGroup> johnWayneTerms = new ArrayList<PersonTermGroup>();
265 PersonTermGroup term = new PersonTermGroup();
266 term.setTermDisplayName("John Wayne DisplayName");
267 term.setTermName("John Wayne");
268 term.setForeName(TEST_FORE_NAME);
269 term.setSurName(TEST_SUR_NAME);
270 johnWayneTerms.add(term);
272 Map<String, List<String>> johnWayneRepeatablesMap = new HashMap<String, List<String>>();
273 List<String> johnWayneGroups = new ArrayList<String>();
274 johnWayneGroups.add("Irish");
275 johnWayneGroups.add("Scottish");
276 johnWayneRepeatablesMap.put(PersonJAXBSchema.GROUPS, johnWayneGroups);
278 return createItemInAuthority(client, vcsid, null /*authRefName*/, shortId, johnWayneMap, johnWayneTerms, johnWayneRepeatablesMap);
283 * Creates an item in an authority.
285 * @param vcsid the vcsid
286 * @param authRefName the auth ref name
287 * @param itemFieldProperties a set of properties specifying the values of fields.
288 * @param itemRepeatableFieldProperties a set of properties specifying the values of repeatable fields.
291 private String createItemInAuthority(AuthorityClient client, String vcsid, String authRefName, String shortId,
292 Map<String, String> itemFieldProperties, List<PersonTermGroup> terms, Map<String, List<String>> itemRepeatableFieldProperties) {
294 final String testName = "createItemInAuthority";
295 if (logger.isDebugEnabled()) {
296 logger.debug(testName + ":" + vcsid + "...");
299 // Submit the request to the service and store the response.
300 if (client == null) {
301 client = new PersonAuthorityClient();
303 PoxPayloadOut multipart =
304 PersonAuthorityClientUtils.createPersonInstance(vcsid, null /*authRefName*/, itemFieldProperties,
305 terms, itemRepeatableFieldProperties, client.getItemCommonPartName());
307 Response res = client.createItem(vcsid, multipart);
310 assertStatusCode(res, testName);
311 newID = PersonAuthorityClientUtils.extractId(res);
318 // Store the ID returned from the first item resource created
319 // for additional tests below.
320 if (knownItemResourceId == null) {
321 setKnownItemResource(newID, shortId);
322 if (logger.isDebugEnabled()) {
323 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
327 if (logger.isDebugEnabled()) {
328 logger.debug(testName + " (created):" + vcsid + "/(" + newID + "," + shortId + ")");
331 // Store the IDs from any item resources created
332 // by tests, along with the IDs of their parents, so these items
333 // can be deleted after all tests have been run.
334 allResourceItemIdsCreated.put(newID, vcsid);
340 * This override asks for a unique identifier (short ID in the case of authority tests).
343 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createResource(java.lang.String, java.lang.String)
346 protected String createResource(String testName, String identifier) throws Exception {
347 String result = null;
349 CollectionSpaceClient client = this.getClientInstance();
350 result = createResource(client, testName, identifier, true);
356 * Creates the contact.
358 * @param testName the test name
360 @Test(dataProvider = "testName", groups = {"create"},
361 dependsOnMethods = {"createItem"})
362 public void createContact(String testName) {
364 createContactInItem(knownResourceId, knownItemResourceId);
368 * Creates the contact in item.
370 * @param parentcsid the parentcsid
371 * @param itemcsid the itemcsid
374 private String createContactInItem(String parentcsid, String itemcsid) {
376 final String testName = "createContactInItem";
377 if (logger.isDebugEnabled()) {
378 logger.debug(testName + ":...");
380 // Submit the request to the service and store the response.
381 PersonAuthorityClient client = new PersonAuthorityClient();
382 String identifier = createIdentifier();
383 PoxPayloadOut multipart = ContactClientUtils.createContactInstance(parentcsid,
384 itemcsid, identifier, new ContactClient().getCommonPartName());
387 Response res = client.createContact(parentcsid, itemcsid, multipart);
390 assertStatusCode(res, testName);
391 newID = PersonAuthorityClientUtils.extractId(res);
398 // Store the ID returned from the first contact resource created
399 // for additional tests below.
400 if (knownContactResourceId == null) {
401 knownContactResourceId = newID;
402 if (logger.isDebugEnabled()) {
403 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
407 // Store the IDs from any contact resources created
408 // by tests, along with the IDs of their parent items,
409 // so these items can be deleted after all tests have been run.
410 allContactResourceIdsCreated.put(newID, itemcsid);
416 * Attempts to create an authority with an short identifier that contains
417 * non-word characters.
419 * @param testName the test name
421 @Test(dataProvider = "testName", groups = {"create", "nonWordCharsInShortId"})
422 public void createWithShortIdNonWordChars(String testName) throws Exception {
423 testExpectedStatusCode = STATUS_BAD_REQUEST;
424 testRequestType = ServiceRequestType.CREATE;
425 testSetup(testExpectedStatusCode, testRequestType);
427 // Create the payload to be included in the body of the request
428 PersonAuthorityClient client = new PersonAuthorityClient();
429 String shortId = createIdentifier() + "*" + createIdentifier();
430 String displayName = "displayName-" + shortId;
431 PoxPayloadOut multipart =
432 PersonAuthorityClientUtils.createPersonAuthorityInstance(
433 displayName, shortId, client.getCommonPartName());
435 // Submit the request to the service and store the response.
436 Response res = client.create(multipart);
437 // Check the status code of the response: does it match
438 // the expected response(s)? We expect failure here.
440 assertStatusCode(res, testName);
449 * Attempts to create an item with an short identifier that contains
450 * non-word characters.
452 * @param testName the test name
454 @Test(dataProvider = "testName", groups = {"create", "nonWordCharsInShortId"},
455 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractServiceTestImpl.create"})
456 public void createItemWithShortIdNonWordChars(String testName) {
457 testExpectedStatusCode = STATUS_BAD_REQUEST;
458 testRequestType = ServiceRequestType.CREATE;
459 testSetup(testExpectedStatusCode, testRequestType);
461 PersonAuthorityClient client = new PersonAuthorityClient();
462 // Create the payload to be included in the body of the request
463 String shortId = "7-Eleven";
464 Map<String, String> fieldProperties = new HashMap<String, String>();
465 fieldProperties.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
467 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
468 PersonTermGroup term = new PersonTermGroup();
469 term.setTermDisplayName(shortId);
470 term.setTermName(shortId);
473 final Map<String, List<String>> NULL_REPEATABLE_FIELD_PROPERTIES = null;
474 PoxPayloadOut multipart =
475 PersonAuthorityClientUtils.createPersonInstance(knownResourceId,
476 null /*knownResourceRefName*/, fieldProperties, terms,
477 NULL_REPEATABLE_FIELD_PROPERTIES, client.getItemCommonPartName());
479 // Send the request and receive a response
480 Response res = client.createItem(knownResourceId, multipart);
481 // Check the status code of the response: does it match
482 // the expected response(s)? We expect failure here, so there will be no
483 // new ID to keep track of for later cleanup.
485 assertStatusCode(res, testName);
493 // ---------------------------------------------------------------
494 // CRUD tests : CREATE LIST tests
495 // ---------------------------------------------------------------
498 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
501 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
502 // groups = {"createList"}, dependsOnGroups = {"create"})
503 public void createList(String testName) throws Exception {
504 for (int i = 0; i < nItemsToCreateInList; i++) {
510 * Creates the contact list.
512 * @param testName the test name
513 * @throws Exception the exception
515 @Test(dataProvider = "testName", groups = {"createList"},
516 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.createItemList"})
517 public void createContactList(String testName) throws Exception {
518 // Add contacts to the initially-created, known item record.
519 for (int j = 0; j < nItemsToCreateInList; j++) {
520 createContact(testName);
524 // ---------------------------------------------------------------
525 // CRUD tests : READ tests
526 // ---------------------------------------------------------------
529 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
532 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
533 // groups = {"read"}, dependsOnGroups = {"create"})
534 public void read(String testName) throws Exception {
535 readInternal(testName, knownResourceId, null);
541 * @param testName the test name
542 * @throws Exception the exception
545 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
546 // groups = {"read"}, dependsOnMethods = {"read"})
547 public void readByName(String testName) throws Exception {
548 readInternal(testName, null, knownResourceShortIdentifer);
551 protected void readInternal(String testName, String CSID, String shortId) {
552 // Submit the request to the service and store the response.
553 PersonAuthorityClient client = new PersonAuthorityClient();
557 res = client.read(CSID);
558 } else if (shortId != null) {
559 res = client.readByName(shortId);
561 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
564 assertStatusCode(res, testName);
565 //FIXME: remove the following try catch once Aron fixes signatures
567 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
568 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
569 client.getCommonPartName(), PersonauthoritiesCommon.class);
570 Assert.assertNotNull(personAuthority);
571 } catch (Exception e) {
572 throw new RuntimeException(e);
584 * @param testName the test name
585 * @throws Exception the exception
587 @Test(dataProvider = "testName", groups = {"readItem", "readNamedItemInNamedAuth"},
588 dependsOnMethods = {"readItemInNamedAuth"})
589 public void readNamedItem(String testName) throws Exception {
590 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
594 * Read item in Named Auth.
596 * @param testName the test name
597 * @throws Exception the exception
599 @Test(dataProvider = "testName", groups = {"readItem"},
600 dependsOnMethods = {"readItem"})
601 public void readItemInNamedAuth(String testName) throws Exception {
602 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
607 * Read Named item in Named Auth.
609 * @param testName the test name
610 * @throws Exception the exception
612 @Test(dataProvider = "testName", groups = {"readItem"},
613 dependsOnMethods = {"readNamedItem"})
614 public void readNamedItemInNamedAuth(String testName) throws Exception {
615 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
618 protected void readItemInternal(String testName,
619 String authCSID, String authShortId, String itemCSID, String itemShortId)
622 if (logger.isDebugEnabled()) {
623 logger.debug("Reading:" + ((authCSID != null) ? authCSID : authShortId) + "/"
624 + ((itemCSID != null) ? authCSID : itemShortId));
627 // Submit the request to the service and store the response.
628 PersonAuthorityClient client = new PersonAuthorityClient();
631 if (authCSID != null) {
632 if (itemCSID != null) {
633 res = client.readItem(authCSID, itemCSID);
634 } else if (itemShortId != null) {
635 res = client.readNamedItem(authCSID, itemShortId);
637 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
639 } else if (authShortId != null) {
640 if (itemCSID != null) {
641 res = client.readItemInNamedAuthority(authShortId, itemCSID);
642 } else if (itemShortId != null) {
643 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
645 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
648 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
652 assertStatusCode(res, testName);
653 // Check whether we've received a person.
654 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
655 PersonsCommon person = (PersonsCommon) extractPart(input,
656 client.getItemCommonPartName(), PersonsCommon.class);
657 Assert.assertNotNull(person);
658 boolean showFull = true;
659 if (showFull && logger.isDebugEnabled()) {
660 logger.debug(testName + ": returned payload:");
661 logger.debug(objectAsXmlString(person, PersonsCommon.class));
664 // Check that the person item is within the expected Person Authority.
665 Assert.assertEquals(person.getInAuthority(), knownResourceId);
667 // Verify the number and contents of values in a repeatable field,
668 // as created in the instance record used for testing.
669 List<String> groups = person.getGroups().getGroup();
670 Assert.assertTrue(groups.size() > 0);
671 Assert.assertNotNull(groups.get(0));
679 // Note: This test depends on server-side validation logic to require
680 // a non-null (and potentially, non-empty) displayname for each term,
681 // and will fail if that validation is not present.
684 * Verify illegal item display name.
686 * @param testName the test name
687 * @throws Exception the exception
689 @Test(dataProvider = "testName", groups = {"update"})
690 public void verifyIllegalItemDisplayName(String testName) throws Exception {
691 // Perform setup for read.
694 // Submit the request to the service and store the response.
695 PersonAuthorityClient client = new PersonAuthorityClient();
696 Response res = client.readItem(knownResourceId, knownItemResourceId);
697 PoxPayloadIn input = null;
699 assertStatusCode(res, testName);
700 input = new PoxPayloadIn(res.readEntity(String.class));
707 // Make an invalid UPDATE request, without a display name
709 PersonsCommon person = (PersonsCommon) extractPart(input,
710 client.getItemCommonPartName(), PersonsCommon.class);
711 Assert.assertNotNull(person);
712 // Try to Update with no displayName
713 PersonTermGroupList termList = person.getPersonTermGroupList();
714 Assert.assertNotNull(termList);
715 List<PersonTermGroup> terms = termList.getPersonTermGroup();
716 Assert.assertNotNull(terms);
717 Assert.assertTrue(terms.size() > 0);
718 terms.get(0).setTermDisplayName(null);
719 terms.get(0).setTermName(null);
721 // Submit the updated resource to the service and store the response.
722 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
723 output.addPart(client.getItemCommonPartName(), person);
724 setupUpdateWithInvalidBody();
725 res = client.updateItem(knownResourceId, knownItemResourceId, output);
727 assertStatusCode(res, testName);
738 * @param testName the test name
739 * @throws Exception the exception
741 @Test(dataProvider = "testName", groups = {"readItem"},
742 dependsOnMethods = {"createContact", "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItem"})
743 public void readContact(String testName) throws Exception {
747 // Submit the request to the service and store the response.
748 PersonAuthorityClient client = new PersonAuthorityClient();
749 PoxPayloadIn input = null;
750 Response res = client.readContact(knownResourceId, knownItemResourceId,
751 knownContactResourceId);
753 assertStatusCode(res, testName);
754 // Check whether we've received a contact.
755 input = new PoxPayloadIn(res.readEntity(String.class));
762 ContactsCommon contact = (ContactsCommon) extractPart(input,
763 new ContactClient().getCommonPartName(), ContactsCommon.class);
764 Assert.assertNotNull(contact);
765 boolean showFull = true;
766 if (showFull && logger.isDebugEnabled()) {
767 logger.debug(testName + ": returned payload:");
768 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
770 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
771 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
776 * Read contact non existent.
778 * @param testName the test name
780 @Test(dataProvider = "testName", groups = {"readItem"}, dependsOnMethods = {"readContact"})
781 public void readContactNonExistent(String testName) {
783 setupReadNonExistent();
785 // Submit the request to the service and store the response.
786 PersonAuthorityClient client = new PersonAuthorityClient();
787 Response res = client.readContact(knownResourceId, knownItemResourceId,
790 assertStatusCode(res, testName);
798 // ---------------------------------------------------------------
799 // CRUD tests : READ_LIST tests
800 // ---------------------------------------------------------------
805 @Test(groups = {"readList"},
806 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
807 public void readContactList() {
808 readContactList(knownResourceId, knownItemResourceId);
814 * @param parentcsid the parentcsid
815 * @param itemcsid the itemcsid
817 private void readContactList(String parentcsid, String itemcsid) {
818 final String testName = "readContactList";
823 // Submit the request to the service and store the response.
824 PersonAuthorityClient client = new PersonAuthorityClient();
825 AbstractCommonList list = null;
826 Response res = client.readContactList(parentcsid, itemcsid);
828 assertStatusCode(res, testName);
829 list = res.readEntity(AbstractCommonList.class);
836 List<AbstractCommonList.ListItem> listitems =
838 int nItemsReturned = listitems.size();
839 // There will be one item created, associated with a
840 // known parent resource, by the createItem test.
842 // In addition, there will be 'nItemsToCreateInList'
843 // additional items created by the createItemList test,
844 // all associated with the same parent resource.
845 int nExpectedItems = nItemsToCreateInList + 1;
846 if (logger.isDebugEnabled()) {
847 logger.debug(testName + ": Expected "
848 + nExpectedItems + " items; got: " + nItemsReturned);
850 Assert.assertEquals(nItemsReturned, nExpectedItems);
852 // Optionally output additional data about list members for debugging.
853 boolean iterateThroughList = false;
854 if (iterateThroughList && logger.isDebugEnabled()) {
855 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
860 // There are no failure outcome tests at present.
861 // ---------------------------------------------------------------
862 // CRUD tests : UPDATE tests
863 // ---------------------------------------------------------------
866 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
869 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
870 // groups = {"update"}, dependsOnGroups = {"readItem", "readList"})
871 public void update(String testName) throws Exception {
872 // Retrieve the contents of a resource to update.
873 PersonAuthorityClient client = new PersonAuthorityClient();
874 PoxPayloadIn input = null;
876 Response res = client.read(knownResourceId);
878 assertStatusCode(res, testName);
879 if (logger.isDebugEnabled()) {
880 logger.debug("got PersonAuthority to update with ID: " + knownResourceId);
882 input = new PoxPayloadIn(res.readEntity(String.class));
889 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
890 client.getCommonPartName(), PersonauthoritiesCommon.class);
891 Assert.assertNotNull(personAuthority);
893 // Update the contents of this resource.
894 personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
895 personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
896 if (logger.isDebugEnabled()) {
897 logger.debug("to be updated PersonAuthority");
898 logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
901 // Submit the updated resource to the service and store the response.
902 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_PAYLOAD_NAME);
903 output.addPart(client.getCommonPartName(), personAuthority);
905 res = client.update(knownResourceId, output);
907 assertStatusCode(res, testName);
908 // Retrieve the updated resource and verify that its contents exist.
909 input = new PoxPayloadIn(res.readEntity(String.class));
916 PersonauthoritiesCommon updatedPersonAuthority =
917 (PersonauthoritiesCommon) extractPart(input,
918 client.getCommonPartName(), PersonauthoritiesCommon.class);
919 Assert.assertNotNull(updatedPersonAuthority);
921 // Verify that the updated resource received the correct data.
922 Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
923 personAuthority.getDisplayName(),
924 "Data in updated object did not match submitted data.");
928 * Update item override -see immediate superclass.
930 * @param testName the test name
931 * @throws Exception the exception
934 public void updateItem(String testName) throws Exception {
935 // Retrieve the contents of a resource to update.
936 PersonAuthorityClient client = new PersonAuthorityClient();
937 PoxPayloadIn input = null;
939 Response res = client.readItem(knownResourceId, knownItemResourceId);
941 assertStatusCode(res, testName);
942 if (logger.isDebugEnabled()) {
943 logger.debug("got Person to update with ID: "
944 + knownItemResourceId
945 + " in PersonAuthority: " + knownResourceId);
947 input = new PoxPayloadIn(res.readEntity(String.class));
954 PersonsCommon person = (PersonsCommon) extractPart(input,
955 client.getItemCommonPartName(), PersonsCommon.class);
956 Assert.assertNotNull(person);
958 if (logger.isDebugEnabled() == true) {
959 logger.debug("About to update the following person...");
960 logger.debug(objectAsXmlString(person, PersonsCommon.class));
963 // Update the contents of this resource.
964 person.setCsid(null);
965 PersonTermGroupList termList = person.getPersonTermGroupList();
966 Assert.assertNotNull(termList);
967 List<PersonTermGroup> terms = termList.getPersonTermGroup();
968 Assert.assertNotNull(terms);
969 Assert.assertTrue(terms.size() > 0);
970 String foreName = terms.get(0).getForeName();
971 String updatedForeName = "updated-" + foreName;
972 terms.get(0).setForeName(updatedForeName);
973 if (logger.isDebugEnabled()) {
974 logger.debug("to be updated Person");
975 logger.debug(objectAsXmlString(person,
976 PersonsCommon.class));
979 // Submit the updated resource to the service and store the response.
980 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
981 output.addPart(client.getItemCommonPartName(), person);
983 res = client.updateItem(knownResourceId, knownItemResourceId, output);
985 assertStatusCode(res, testName);
986 // Retrieve the updated resource and verify that its contents exist.
987 input = new PoxPayloadIn(res.readEntity(String.class));
994 PersonsCommon updatedPerson =
995 (PersonsCommon) extractPart(input,
996 client.getItemCommonPartName(), PersonsCommon.class);
997 Assert.assertNotNull(updatedPerson);
999 if (logger.isDebugEnabled() == true) {
1000 logger.debug("Updated to following person to:");
1001 logger.debug(objectAsXmlString(updatedPerson, PersonsCommon.class));
1004 // Verify that the updated resource received the correct data.
1005 PersonTermGroupList updatedTermList = person.getPersonTermGroupList();
1006 Assert.assertNotNull(updatedTermList);
1007 List<PersonTermGroup> updatedTerms = termList.getPersonTermGroup();
1008 Assert.assertNotNull(updatedTerms);
1009 Assert.assertEquals(updatedTerms.get(0).getForeName(), updatedForeName,
1010 "Data in updated Person did not match submitted data.");
1016 * @param testName the test name
1017 * @throws Exception the exception
1019 @Test(dataProvider = "testName", groups = {"update"},
1020 dependsOnMethods = {"readContact", "testContactSubmitRequest",
1021 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
1022 public void updateContact(String testName) throws Exception {
1023 String contactsCommonLabel = new ContactClient().getCommonPartName();
1025 // Retrieve the contents of a resource to update.
1026 PersonAuthorityClient client = new PersonAuthorityClient();
1027 PoxPayloadIn input = null;
1029 Response res = client.readContact(knownResourceId, knownItemResourceId,
1030 knownContactResourceId);
1032 assertStatusCode(res, testName);
1033 if (logger.isDebugEnabled()) {
1034 logger.debug("got Contact to update with ID: "
1035 + knownContactResourceId
1036 + " in item: " + knownItemResourceId
1037 + " in parent: " + knownResourceId);
1039 input = new PoxPayloadIn(res.readEntity(String.class));
1046 ContactsCommon contact = (ContactsCommon) extractPart(input,
1047 contactsCommonLabel, ContactsCommon.class);
1048 Assert.assertNotNull(contact);
1050 // Verify the contents of this resource
1051 AddressGroupList addressGroupList = contact.getAddressGroupList();
1052 Assert.assertNotNull(addressGroupList);
1053 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
1054 Assert.assertNotNull(addressGroups);
1055 Assert.assertTrue(addressGroups.size() > 0);
1056 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
1057 Assert.assertNotNull(addressPlace1);
1059 // Update the contents of this resource.
1060 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
1061 contact.setAddressGroupList(addressGroupList);
1062 if (logger.isDebugEnabled()) {
1063 logger.debug("to be updated Contact");
1064 logger.debug(objectAsXmlString(contact,
1065 ContactsCommon.class));
1068 // Submit the updated resource to the service and store the response.
1069 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
1070 output.addPart(contactsCommonLabel, contact);
1072 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1074 assertStatusCode(res, testName);
1075 // Retrieve the updated resource and verify that its contents exist.
1076 input = new PoxPayloadIn(res.readEntity(String.class));;
1082 ContactsCommon updatedContact = (ContactsCommon) extractPart(input,
1083 contactsCommonLabel, ContactsCommon.class);
1084 Assert.assertNotNull(updatedContact);
1086 // Verify that the updated resource received the correct data.
1087 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1088 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1089 "Data in updated object did not match submitted data.");
1093 * Update non existent contact.
1095 * @param testName the test name
1096 * @throws Exception the exception
1098 @Test(dataProvider = "testName", groups = {"update"},
1099 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1100 public void updateNonExistentContact(String testName) throws Exception {
1101 // Currently a no-op test
1104 // ---------------------------------------------------------------
1105 // CRUD tests : DELETE tests
1106 // ---------------------------------------------------------------
1108 // Note: delete sub-resources in ascending hierarchical order,
1109 // before deleting their parents.
1113 * @param testName the test name
1114 * @throws Exception the exception
1116 @Test(dataProvider = "testName", groups = {"delete"},
1117 dependsOnMethods = {"updateContact"})
1118 public void deleteContact(String testName) throws Exception {
1119 if (logger.isDebugEnabled()) {
1120 logger.debug("parentcsid =" + knownResourceId
1121 + " itemcsid = " + knownItemResourceId
1122 + " csid = " + knownContactResourceId);
1125 // Submit the request to the service and store the response.
1126 PersonAuthorityClient client = new PersonAuthorityClient();
1128 Response res = client.deleteContact(knownResourceId, knownItemResourceId,
1129 knownContactResourceId);
1131 assertStatusCode(res, testName);
1140 public void delete(String testName) throws Exception {
1141 // Do nothing. See localDelete(). This ensure proper test order.
1144 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
1145 public void localDelete(String testName) throws Exception {
1146 super.delete(testName);
1150 public void deleteItem(String testName) throws Exception {
1151 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
1152 // its dependencies will get run first and then we can call the base class' delete method.
1155 @Test(dataProvider = "testName", groups = {"delete"},
1156 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
1157 public void localDeleteItem(String testName) throws Exception {
1158 super.deleteItem(testName);
1162 * Delete non existent contact.
1164 * @param testName the test name
1166 @Test(dataProvider = "testName", groups = {"delete"},
1167 dependsOnMethods = {"deleteContact"})
1168 public void deleteNonExistentContact(String testName) {
1169 // Submit the request to the service and store the response.
1170 PersonAuthorityClient client = new PersonAuthorityClient();
1171 setupDeleteNonExistent();
1172 Response res = client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1174 assertStatusCode(res, testName);
1183 * Test contact submit request.
1185 @Test(dataProvider = "testName",
1186 dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
1187 public void testContactSubmitRequest(String testName) {
1189 // Expected status code: 200 OK
1190 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1192 // Submit the request to the service and store the response.
1193 String method = ServiceRequestType.READ.httpMethodName();
1194 String url = getContactResourceURL(knownResourceId,
1195 knownItemResourceId, knownContactResourceId);
1196 int statusCode = submitRequest(method, url);
1198 // Check the status code of the response: does it match
1199 // the expected response(s)?
1200 if (logger.isDebugEnabled()) {
1201 logger.debug("testContactSubmitRequest: url=" + url
1202 + " status=" + statusCode);
1204 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1208 // ---------------------------------------------------------------
1209 // Cleanup of resources created during testing
1210 // ---------------------------------------------------------------
1212 * Deletes all resources created by tests, after all tests have been run.
1214 * This cleanup method will always be run, even if one or more tests fail.
1215 * For this reason, it attempts to remove all resources created
1216 * at any point during testing, even if some of those resources
1217 * may be expected to be deleted by certain tests.
1219 @AfterClass(alwaysRun = true)
1221 public void cleanUp() {
1222 String noTest = System.getProperty("noTestCleanup");
1223 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1224 if (logger.isDebugEnabled()) {
1225 logger.debug("Skipping Cleanup phase ...");
1230 if (logger.isDebugEnabled()) {
1231 logger.debug("Cleaning up temporary resources created for testing ...");
1234 String parentResourceId;
1235 String itemResourceId;
1236 String contactResourceId;
1237 // Clean up contact resources.
1238 PersonAuthorityClient client = new PersonAuthorityClient();
1239 parentResourceId = this.getKnowResourceId();
1240 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
1241 contactResourceId = entry.getKey();
1242 itemResourceId = entry.getValue();
1243 // Note: Any non-success responses from the delete operation
1244 // below are ignored and not reported.
1245 Response res = client.deleteContact(parentResourceId, itemResourceId,
1250 // Finally, clean call our superclass' cleanUp method.
1255 // ---------------------------------------------------------------
1256 // Utility methods used by tests above
1257 // ---------------------------------------------------------------
1259 * Gets the contact service path component.
1261 * @return the contact service path component
1263 public String getContactServicePathComponent() {
1264 return ContactClient.SERVICE_PATH_COMPONENT;
1268 * Returns the root URL for the item service.
1270 * This URL consists of a base URL for all services, followed by
1271 * a path component for the owning parent, followed by the
1272 * path component for the items.
1274 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1275 * parent authority resource of the relevant item resource.
1277 * @return The root URL for the item service.
1279 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1280 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1284 * Returns the URL of a specific item resource managed by a service, and
1285 * designated by an identifier (such as a universally unique ID, or UUID).
1287 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1288 * parent authority resource of the relevant item resource.
1290 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1293 * @return The URL of a specific item resource managed by a service.
1295 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1296 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1300 * Returns the root URL for the contact service.
1302 * This URL consists of a base URL for all services, followed by
1303 * a path component for the owning authority, followed by the
1304 * path component for the owning item, followed by the path component
1305 * for the contact service.
1307 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1308 * parent authority resource of the relevant item resource.
1310 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1313 * @return The root URL for the contact service.
1315 protected String getContactServiceRootURL(String parentResourceIdentifier,
1316 String itemResourceIdentifier) {
1317 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1318 + getContactServicePathComponent();
1322 * Returns the URL of a specific contact resource managed by a service, and
1323 * designated by an identifier (such as a universally unique ID, or UUID).
1325 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1326 * parent resource of the relevant item resource.
1328 * @param resourceIdentifier An identifier (such as a UUID) for an
1331 * @return The URL of a specific resource managed by a service.
1333 protected String getContactResourceURL(String parentResourceIdentifier,
1334 String itemResourceIdentifier, String contactResourceIdentifier) {
1335 return getContactServiceRootURL(parentResourceIdentifier,
1336 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1340 public void authorityTests(String testName) {
1341 // TODO Auto-generated method stub
1346 protected PersonsCommon updateItemInstance(PersonsCommon personsCommon) {
1348 PersonTermGroupList termList = personsCommon.getPersonTermGroupList();
1349 Assert.assertNotNull(termList);
1350 List<PersonTermGroup> terms = termList.getPersonTermGroup();
1351 Assert.assertNotNull(terms);
1352 Assert.assertTrue(terms.size() > 0);
1353 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1354 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1355 personsCommon.setPersonTermGroupList(termList);
1357 return personsCommon;
1361 protected void compareUpdatedItemInstances(PersonsCommon original,
1362 PersonsCommon updated) throws Exception {
1364 PersonTermGroupList originalTermList = original.getPersonTermGroupList();
1365 Assert.assertNotNull(originalTermList);
1366 List<PersonTermGroup> originalTerms = originalTermList.getPersonTermGroup();
1367 Assert.assertNotNull(originalTerms);
1368 Assert.assertTrue(originalTerms.size() > 0);
1370 PersonTermGroupList updatedTermList = updated.getPersonTermGroupList();
1371 Assert.assertNotNull(updatedTermList);
1372 List<PersonTermGroup> updatedTerms = updatedTermList.getPersonTermGroup();
1373 Assert.assertNotNull(updatedTerms);
1374 Assert.assertTrue(updatedTerms.size() > 0);
1376 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1377 originalTerms.get(0).getTermDisplayName(),
1378 "Value in updated record did not match submitted data.");
1382 protected PoxPayloadOut createInstance(String commonPartName,
1383 String identifier) {
1384 String shortId = identifier;
1385 String displayName = "displayName-" + shortId;
1386 PoxPayloadOut result =
1387 PersonAuthorityClientUtils.createPersonAuthorityInstance(
1388 displayName, shortId, commonPartName);
1393 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1394 String identifier) {
1395 String displayName = "displayName-NON_EXISTENT_ID";
1396 PoxPayloadOut result = PersonAuthorityClientUtils.createPersonAuthorityInstance(
1397 displayName, "NON_EXISTENT_SHORT_ID", commonPartName);
1401 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier) {
1402 Map<String, String> nonexMap = new HashMap<String, String>();
1403 nonexMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, "nonEX");
1404 nonexMap.put(PersonJAXBSchema.GENDER, "male");
1406 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
1407 PersonTermGroup term = new PersonTermGroup();
1408 term.setTermDisplayName("John Wayne");
1409 term.setTermName("John Wayne");
1410 term.setForeName("John");
1411 term.setSurName("Wayne");
1414 Map<String, List<String>> nonexRepeatablesMap = new HashMap<String, List<String>>();
1415 PoxPayloadOut result =
1416 PersonAuthorityClientUtils.createPersonInstance(NON_EXISTENT_ID,
1417 null, //PersonAuthorityClientUtils.createPersonAuthRefName(NON_EXISTENT_ID, null),
1418 nonexMap, terms, nonexRepeatablesMap, commonPartName);
1423 protected PersonauthoritiesCommon updateInstance(PersonauthoritiesCommon personauthoritiesCommon) {
1424 PersonauthoritiesCommon result = new PersonauthoritiesCommon();
1426 result.setDisplayName("updated-" + personauthoritiesCommon.getDisplayName());
1427 result.setVocabType("updated-" + personauthoritiesCommon.getVocabType());
1433 protected void compareUpdatedInstances(PersonauthoritiesCommon original,
1434 PersonauthoritiesCommon updated) throws Exception {
1435 // Verify that the updated resource received the correct data.
1436 Assert.assertEquals(updated.getDisplayName(),
1437 original.getDisplayName(),
1438 "Display name in updated object did not match submitted data.");
1442 protected void verifyReadItemInstance(PersonsCommon item) throws Exception {
1443 // Do nothing for now. Add more 'read' validation checks here if applicable.