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() throws Exception {
132 return new PersonAuthorityClient();
136 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
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) throws Exception {
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) throws Exception {
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
242 protected String createItemInAuthority(AuthorityClient client, String vcsid, String shortId) throws Exception {
244 final String testName = "createItemInAuthority";
245 if (logger.isDebugEnabled()) {
246 logger.debug(testName + ":" + vcsid + "...");
249 Map<String, String> johnWayneMap = new HashMap<String, String>();
251 // Fill the property map
253 johnWayneMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
254 johnWayneMap.put(PersonJAXBSchema.GENDER, "male");
255 johnWayneMap.put(PersonJAXBSchema.BIRTH_DATE, TEST_BIRTH_DATE);
256 johnWayneMap.put(PersonJAXBSchema.BIRTH_PLACE, "Winterset, Iowa");
257 johnWayneMap.put(PersonJAXBSchema.DEATH_DATE, TEST_DEATH_DATE);
258 johnWayneMap.put(PersonJAXBSchema.BIO_NOTE, "born Marion Robert Morrison and better"
259 + "known by his stage name John Wayne, was an American film actor, director "
260 + "and producer. He epitomized rugged masculinity and has become an enduring "
261 + "American icon. He is famous for his distinctive voice, walk and height. "
262 + "He was also known for his conservative political views and his support in "
263 + "the 1950s for anti-communist positions.");
265 List<PersonTermGroup> johnWayneTerms = new ArrayList<PersonTermGroup>();
266 PersonTermGroup term = new PersonTermGroup();
267 term.setTermDisplayName("John Wayne DisplayName");
268 term.setTermName("John Wayne");
269 term.setForeName(TEST_FORE_NAME);
270 term.setSurName(TEST_SUR_NAME);
271 johnWayneTerms.add(term);
273 Map<String, List<String>> johnWayneRepeatablesMap = new HashMap<String, List<String>>();
274 List<String> johnWayneGroups = new ArrayList<String>();
275 johnWayneGroups.add("Irish");
276 johnWayneGroups.add("Scottish");
277 johnWayneRepeatablesMap.put(PersonJAXBSchema.GROUPS, johnWayneGroups);
279 return createItemInAuthority(client, vcsid, null /*authRefName*/, shortId, johnWayneMap, johnWayneTerms, johnWayneRepeatablesMap);
284 * Creates an item in an authority.
286 * @param vcsid the vcsid
287 * @param authRefName the auth ref name
288 * @param itemFieldProperties a set of properties specifying the values of fields.
289 * @param itemRepeatableFieldProperties a set of properties specifying the values of repeatable fields.
293 private String createItemInAuthority(AuthorityClient client, String vcsid, String authRefName, String shortId,
294 Map<String, String> itemFieldProperties, List<PersonTermGroup> terms, Map<String, List<String>> itemRepeatableFieldProperties) throws Exception {
296 final String testName = "createItemInAuthority";
297 if (logger.isDebugEnabled()) {
298 logger.debug(testName + ":" + vcsid + "...");
301 // Submit the request to the service and store the response.
302 if (client == null) {
303 client = new PersonAuthorityClient();
305 PoxPayloadOut multipart =
306 PersonAuthorityClientUtils.createPersonInstance(vcsid, null /*authRefName*/, itemFieldProperties,
307 terms, itemRepeatableFieldProperties, client.getItemCommonPartName());
309 Response res = client.createItem(vcsid, multipart);
312 assertStatusCode(res, testName);
313 newID = PersonAuthorityClientUtils.extractId(res);
320 // Store the ID returned from the first item resource created
321 // for additional tests below.
322 if (knownItemResourceId == null) {
323 setKnownItemResource(newID, shortId);
324 if (logger.isDebugEnabled()) {
325 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
329 if (logger.isDebugEnabled()) {
330 logger.debug(testName + " (created):" + vcsid + "/(" + newID + "," + shortId + ")");
333 // Store the IDs from any item resources created
334 // by tests, along with the IDs of their parents, so these items
335 // can be deleted after all tests have been run.
336 allResourceItemIdsCreated.put(newID, vcsid);
342 * This override asks for a unique identifier (short ID in the case of authority tests).
345 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createResource(java.lang.String, java.lang.String)
348 protected String createResource(String testName, String identifier) throws Exception {
349 String result = null;
351 CollectionSpaceClient client = this.getClientInstance();
352 result = createResource(client, testName, identifier, true);
358 * Creates the contact.
360 * @param testName the test name
363 @Test(dataProvider = "testName", groups = {"create"}, dependsOnMethods = {"createItem"})
364 public void createContact(String testName) throws Exception {
366 createContactInItem(knownResourceId, knownItemResourceId);
370 * Creates the contact in item.
372 * @param parentcsid the parentcsid
373 * @param itemcsid the itemcsid
377 private String createContactInItem(String parentcsid, String itemcsid) throws Exception {
379 final String testName = "createContactInItem";
380 if (logger.isDebugEnabled()) {
381 logger.debug(testName + ":...");
383 // Submit the request to the service and store the response.
384 PersonAuthorityClient client = new PersonAuthorityClient();
385 String identifier = createIdentifier();
386 PoxPayloadOut multipart = ContactClientUtils.createContactInstance(parentcsid,
387 itemcsid, identifier, new ContactClient().getCommonPartName());
390 Response res = client.createContact(parentcsid, itemcsid, multipart);
393 assertStatusCode(res, testName);
394 newID = PersonAuthorityClientUtils.extractId(res);
401 // Store the ID returned from the first contact resource created
402 // for additional tests below.
403 if (knownContactResourceId == null) {
404 knownContactResourceId = newID;
405 if (logger.isDebugEnabled()) {
406 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
410 // Store the IDs from any contact resources created
411 // by tests, along with the IDs of their parent items,
412 // so these items can be deleted after all tests have been run.
413 allContactResourceIdsCreated.put(newID, itemcsid);
419 * Attempts to create an authority with an short identifier that contains
420 * non-word characters.
422 * @param testName the test name
424 @Test(dataProvider = "testName", groups = {"create", "nonWordCharsInShortId"})
425 public void createWithShortIdNonWordChars(String testName) throws Exception {
426 testExpectedStatusCode = STATUS_BAD_REQUEST;
427 testRequestType = ServiceRequestType.CREATE;
428 testSetup(testExpectedStatusCode, testRequestType);
430 // Create the payload to be included in the body of the request
431 PersonAuthorityClient client = new PersonAuthorityClient();
432 String shortId = createIdentifier() + "*" + createIdentifier();
433 String displayName = "displayName-" + shortId;
434 PoxPayloadOut multipart =
435 PersonAuthorityClientUtils.createPersonAuthorityInstance(
436 displayName, shortId, client.getCommonPartName());
438 // Submit the request to the service and store the response.
439 Response res = client.create(multipart);
440 // Check the status code of the response: does it match
441 // the expected response(s)? We expect failure here.
443 assertStatusCode(res, testName);
452 * Attempts to create an item with an short identifier that contains
453 * non-word characters.
455 * @param testName the test name
458 @Test(dataProvider = "testName", groups = {"create", "nonWordCharsInShortId"},
459 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractServiceTestImpl.create"})
460 public void createItemWithShortIdNonWordChars(String testName) throws Exception {
461 testExpectedStatusCode = STATUS_BAD_REQUEST;
462 testRequestType = ServiceRequestType.CREATE;
463 testSetup(testExpectedStatusCode, testRequestType);
465 PersonAuthorityClient client = new PersonAuthorityClient();
466 // Create the payload to be included in the body of the request
467 String shortId = "7-Eleven";
468 Map<String, String> fieldProperties = new HashMap<String, String>();
469 fieldProperties.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
471 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
472 PersonTermGroup term = new PersonTermGroup();
473 term.setTermDisplayName(shortId);
474 term.setTermName(shortId);
477 final Map<String, List<String>> NULL_REPEATABLE_FIELD_PROPERTIES = null;
478 PoxPayloadOut multipart =
479 PersonAuthorityClientUtils.createPersonInstance(knownResourceId,
480 null /*knownResourceRefName*/, fieldProperties, terms,
481 NULL_REPEATABLE_FIELD_PROPERTIES, client.getItemCommonPartName());
483 // Send the request and receive a response
484 Response res = client.createItem(knownResourceId, multipart);
485 // Check the status code of the response: does it match
486 // the expected response(s)? We expect failure here, so there will be no
487 // new ID to keep track of for later cleanup.
489 assertStatusCode(res, testName);
497 // ---------------------------------------------------------------
498 // CRUD tests : CREATE LIST tests
499 // ---------------------------------------------------------------
502 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
505 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
506 // groups = {"createList"}, dependsOnGroups = {"create"})
507 public void createList(String testName) throws Exception {
508 for (int i = 0; i < nItemsToCreateInList; i++) {
514 * Creates the contact list.
516 * @param testName the test name
517 * @throws Exception the exception
519 @Test(dataProvider = "testName", groups = {"createList"},
520 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.createItemList"})
521 public void createContactList(String testName) throws Exception {
522 // Add contacts to the initially-created, known item record.
523 for (int j = 0; j < nItemsToCreateInList; j++) {
524 createContact(testName);
528 // ---------------------------------------------------------------
529 // CRUD tests : READ tests
530 // ---------------------------------------------------------------
533 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
536 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
537 // groups = {"read"}, dependsOnGroups = {"create"})
538 public void read(String testName) throws Exception {
539 readInternal(testName, knownResourceId, null);
545 * @param testName the test name
546 * @throws Exception the exception
549 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
550 // groups = {"read"}, dependsOnMethods = {"read"})
551 public void readByName(String testName) throws Exception {
552 readInternal(testName, null, knownResourceShortIdentifer);
555 protected void readInternal(String testName, String CSID, String shortId) throws Exception {
556 // Submit the request to the service and store the response.
557 PersonAuthorityClient client = new PersonAuthorityClient();
561 res = client.read(CSID);
562 } else if (shortId != null) {
563 res = client.readByName(shortId);
565 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
568 assertStatusCode(res, testName);
569 //FIXME: remove the following try catch once Aron fixes signatures
571 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
572 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
573 client.getCommonPartName(), PersonauthoritiesCommon.class);
574 Assert.assertNotNull(personAuthority);
575 } catch (Exception e) {
576 throw new RuntimeException(e);
588 * @param testName the test name
589 * @throws Exception the exception
591 @Test(dataProvider = "testName", groups = {"readItem", "readNamedItemInNamedAuth"},
592 dependsOnMethods = {"readItemInNamedAuth"})
593 public void readNamedItem(String testName) throws Exception {
594 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
598 * Read item in Named Auth.
600 * @param testName the test name
601 * @throws Exception the exception
603 @Test(dataProvider = "testName", groups = {"readItem"},
604 dependsOnMethods = {"readItem"})
605 public void readItemInNamedAuth(String testName) throws Exception {
606 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
611 * Read Named item in Named Auth.
613 * @param testName the test name
614 * @throws Exception the exception
616 @Test(dataProvider = "testName", groups = {"readItem"},
617 dependsOnMethods = {"readNamedItem"})
618 public void readNamedItemInNamedAuth(String testName) throws Exception {
619 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
622 protected void readItemInternal(String testName,
623 String authCSID, String authShortId, String itemCSID, String itemShortId)
626 if (logger.isDebugEnabled()) {
627 logger.debug("Reading:" + ((authCSID != null) ? authCSID : authShortId) + "/"
628 + ((itemCSID != null) ? authCSID : itemShortId));
631 // Submit the request to the service and store the response.
632 PersonAuthorityClient client = new PersonAuthorityClient();
635 if (authCSID != null) {
636 if (itemCSID != null) {
637 res = client.readItem(authCSID, itemCSID);
638 } else if (itemShortId != null) {
639 res = client.readNamedItem(authCSID, itemShortId);
641 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
643 } else if (authShortId != null) {
644 if (itemCSID != null) {
645 res = client.readItemInNamedAuthority(authShortId, itemCSID);
646 } else if (itemShortId != null) {
647 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
649 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
652 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
656 assertStatusCode(res, testName);
657 // Check whether we've received a person.
658 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
659 PersonsCommon person = (PersonsCommon) extractPart(input,
660 client.getItemCommonPartName(), PersonsCommon.class);
661 Assert.assertNotNull(person);
662 boolean showFull = true;
663 if (showFull && logger.isDebugEnabled()) {
664 logger.debug(testName + ": returned payload:");
665 logger.debug(objectAsXmlString(person, PersonsCommon.class));
668 // Check that the person item is within the expected Person Authority.
669 Assert.assertEquals(person.getInAuthority(), knownResourceId);
671 // Verify the number and contents of values in a repeatable field,
672 // as created in the instance record used for testing.
673 List<String> groups = person.getGroups().getGroup();
674 Assert.assertTrue(groups.size() > 0);
675 Assert.assertNotNull(groups.get(0));
683 // Note: This test depends on server-side validation logic to require
684 // a non-null (and potentially, non-empty) displayname for each term,
685 // and will fail if that validation is not present.
688 * Verify illegal item display name.
690 * @param testName the test name
691 * @throws Exception the exception
693 @Test(dataProvider = "testName", groups = {"update"})
694 public void verifyIllegalItemDisplayName(String testName) throws Exception {
695 // Perform setup for read.
698 // Submit the request to the service and store the response.
699 PersonAuthorityClient client = new PersonAuthorityClient();
700 Response res = client.readItem(knownResourceId, knownItemResourceId);
701 PoxPayloadIn input = null;
703 assertStatusCode(res, testName);
704 input = new PoxPayloadIn(res.readEntity(String.class));
711 // Make an invalid UPDATE request, without a display name
713 PersonsCommon person = (PersonsCommon) extractPart(input,
714 client.getItemCommonPartName(), PersonsCommon.class);
715 Assert.assertNotNull(person);
716 // Try to Update with no displayName
717 PersonTermGroupList termList = person.getPersonTermGroupList();
718 Assert.assertNotNull(termList);
719 List<PersonTermGroup> terms = termList.getPersonTermGroup();
720 Assert.assertNotNull(terms);
721 Assert.assertTrue(terms.size() > 0);
722 terms.get(0).setTermDisplayName(null);
723 terms.get(0).setTermName(null);
725 // Submit the updated resource to the service and store the response.
726 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
727 output.addPart(client.getItemCommonPartName(), person);
728 setupUpdateWithInvalidBody();
729 res = client.updateItem(knownResourceId, knownItemResourceId, output);
731 assertStatusCode(res, testName);
742 * @param testName the test name
743 * @throws Exception the exception
745 @Test(dataProvider = "testName", groups = {"readItem"},
746 dependsOnMethods = {"createContact", "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItem"})
747 public void readContact(String testName) throws Exception {
751 // Submit the request to the service and store the response.
752 PersonAuthorityClient client = new PersonAuthorityClient();
753 PoxPayloadIn input = null;
754 Response res = client.readContact(knownResourceId, knownItemResourceId,
755 knownContactResourceId);
757 assertStatusCode(res, testName);
758 // Check whether we've received a contact.
759 input = new PoxPayloadIn(res.readEntity(String.class));
766 ContactsCommon contact = (ContactsCommon) extractPart(input,
767 new ContactClient().getCommonPartName(), ContactsCommon.class);
768 Assert.assertNotNull(contact);
769 boolean showFull = true;
770 if (showFull && logger.isDebugEnabled()) {
771 logger.debug(testName + ": returned payload:");
772 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
774 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
775 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
780 * Read contact non existent.
782 * @param testName the test name
785 @Test(dataProvider = "testName", groups = {"readItem"}, dependsOnMethods = {"readContact"})
786 public void readContactNonExistent(String testName) throws Exception {
788 setupReadNonExistent();
790 // Submit the request to the service and store the response.
791 PersonAuthorityClient client = new PersonAuthorityClient();
792 Response res = client.readContact(knownResourceId, knownItemResourceId,
795 assertStatusCode(res, testName);
803 // ---------------------------------------------------------------
804 // CRUD tests : READ_LIST tests
805 // ---------------------------------------------------------------
811 @Test(groups = {"readList"},
812 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
813 public void readContactList() throws Exception {
814 readContactList(knownResourceId, knownItemResourceId);
820 * @param parentcsid the parentcsid
821 * @param itemcsid the itemcsid
824 private void readContactList(String parentcsid, String itemcsid) throws Exception {
825 final String testName = "readContactList";
830 // Submit the request to the service and store the response.
831 PersonAuthorityClient client = new PersonAuthorityClient();
832 AbstractCommonList list = null;
833 Response res = client.readContactList(parentcsid, itemcsid);
835 assertStatusCode(res, testName);
836 list = res.readEntity(AbstractCommonList.class);
843 List<AbstractCommonList.ListItem> listitems =
845 int nItemsReturned = listitems.size();
846 // There will be one item created, associated with a
847 // known parent resource, by the createItem test.
849 // In addition, there will be 'nItemsToCreateInList'
850 // additional items created by the createItemList test,
851 // all associated with the same parent resource.
852 int nExpectedItems = nItemsToCreateInList + 1;
853 if (logger.isDebugEnabled()) {
854 logger.debug(testName + ": Expected "
855 + nExpectedItems + " items; got: " + nItemsReturned);
857 Assert.assertEquals(nItemsReturned, nExpectedItems);
859 // Optionally output additional data about list members for debugging.
860 boolean iterateThroughList = false;
861 if (iterateThroughList && logger.isDebugEnabled()) {
862 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
867 // There are no failure outcome tests at present.
868 // ---------------------------------------------------------------
869 // CRUD tests : UPDATE tests
870 // ---------------------------------------------------------------
873 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
876 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
877 // groups = {"update"}, dependsOnGroups = {"readItem", "readList"})
878 public void update(String testName) throws Exception {
879 // Retrieve the contents of a resource to update.
880 PersonAuthorityClient client = new PersonAuthorityClient();
881 PoxPayloadIn input = null;
883 Response res = client.read(knownResourceId);
885 assertStatusCode(res, testName);
886 if (logger.isDebugEnabled()) {
887 logger.debug("got PersonAuthority to update with ID: " + knownResourceId);
889 input = new PoxPayloadIn(res.readEntity(String.class));
896 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
897 client.getCommonPartName(), PersonauthoritiesCommon.class);
898 Assert.assertNotNull(personAuthority);
900 // Update the contents of this resource.
901 personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
902 personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
903 if (logger.isDebugEnabled()) {
904 logger.debug("to be updated PersonAuthority");
905 logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
908 // Submit the updated resource to the service and store the response.
909 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_PAYLOAD_NAME);
910 output.addPart(client.getCommonPartName(), personAuthority);
912 res = client.update(knownResourceId, output);
914 assertStatusCode(res, testName);
915 // Retrieve the updated resource and verify that its contents exist.
916 input = new PoxPayloadIn(res.readEntity(String.class));
923 PersonauthoritiesCommon updatedPersonAuthority =
924 (PersonauthoritiesCommon) extractPart(input,
925 client.getCommonPartName(), PersonauthoritiesCommon.class);
926 Assert.assertNotNull(updatedPersonAuthority);
928 // Verify that the updated resource received the correct data.
929 Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
930 personAuthority.getDisplayName(),
931 "Data in updated object did not match submitted data.");
935 * Update item override -see immediate superclass.
937 * @param testName the test name
938 * @throws Exception the exception
941 public void updateItem(String testName) throws Exception {
942 // Retrieve the contents of a resource to update.
943 PersonAuthorityClient client = new PersonAuthorityClient();
944 PoxPayloadIn input = null;
946 Response res = client.readItem(knownResourceId, knownItemResourceId);
948 assertStatusCode(res, testName);
949 if (logger.isDebugEnabled()) {
950 logger.debug("got Person to update with ID: "
951 + knownItemResourceId
952 + " in PersonAuthority: " + knownResourceId);
954 input = new PoxPayloadIn(res.readEntity(String.class));
961 PersonsCommon person = (PersonsCommon) extractPart(input,
962 client.getItemCommonPartName(), PersonsCommon.class);
963 Assert.assertNotNull(person);
965 if (logger.isDebugEnabled() == true) {
966 logger.debug("About to update the following person...");
967 logger.debug(objectAsXmlString(person, PersonsCommon.class));
970 // Update the contents of this resource.
971 person.setCsid(null);
972 PersonTermGroupList termList = person.getPersonTermGroupList();
973 Assert.assertNotNull(termList);
974 List<PersonTermGroup> terms = termList.getPersonTermGroup();
975 Assert.assertNotNull(terms);
976 Assert.assertTrue(terms.size() > 0);
977 String foreName = terms.get(0).getForeName();
978 String updatedForeName = "updated-" + foreName;
979 terms.get(0).setForeName(updatedForeName);
980 if (logger.isDebugEnabled()) {
981 logger.debug("to be updated Person");
982 logger.debug(objectAsXmlString(person,
983 PersonsCommon.class));
986 // Submit the updated resource to the service and store the response.
987 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
988 output.addPart(client.getItemCommonPartName(), person);
990 res = client.updateItem(knownResourceId, knownItemResourceId, output);
992 assertStatusCode(res, testName);
993 // Retrieve the updated resource and verify that its contents exist.
994 input = new PoxPayloadIn(res.readEntity(String.class));
1001 PersonsCommon updatedPerson =
1002 (PersonsCommon) extractPart(input,
1003 client.getItemCommonPartName(), PersonsCommon.class);
1004 Assert.assertNotNull(updatedPerson);
1006 if (logger.isDebugEnabled() == true) {
1007 logger.debug("Updated to following person to:");
1008 logger.debug(objectAsXmlString(updatedPerson, PersonsCommon.class));
1011 // Verify that the updated resource received the correct data.
1012 PersonTermGroupList updatedTermList = person.getPersonTermGroupList();
1013 Assert.assertNotNull(updatedTermList);
1014 List<PersonTermGroup> updatedTerms = termList.getPersonTermGroup();
1015 Assert.assertNotNull(updatedTerms);
1016 Assert.assertEquals(updatedTerms.get(0).getForeName(), updatedForeName,
1017 "Data in updated Person did not match submitted data.");
1023 * @param testName the test name
1024 * @throws Exception the exception
1026 @Test(dataProvider = "testName", groups = {"update"},
1027 dependsOnMethods = {"readContact", "testContactSubmitRequest",
1028 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
1029 public void updateContact(String testName) throws Exception {
1030 String contactsCommonLabel = new ContactClient().getCommonPartName();
1032 // Retrieve the contents of a resource to update.
1033 PersonAuthorityClient client = new PersonAuthorityClient();
1034 PoxPayloadIn input = null;
1036 Response res = client.readContact(knownResourceId, knownItemResourceId,
1037 knownContactResourceId);
1039 assertStatusCode(res, testName);
1040 if (logger.isDebugEnabled()) {
1041 logger.debug("got Contact to update with ID: "
1042 + knownContactResourceId
1043 + " in item: " + knownItemResourceId
1044 + " in parent: " + knownResourceId);
1046 input = new PoxPayloadIn(res.readEntity(String.class));
1053 ContactsCommon contact = (ContactsCommon) extractPart(input,
1054 contactsCommonLabel, ContactsCommon.class);
1055 Assert.assertNotNull(contact);
1057 // Verify the contents of this resource
1058 AddressGroupList addressGroupList = contact.getAddressGroupList();
1059 Assert.assertNotNull(addressGroupList);
1060 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
1061 Assert.assertNotNull(addressGroups);
1062 Assert.assertTrue(addressGroups.size() > 0);
1063 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
1064 Assert.assertNotNull(addressPlace1);
1066 // Update the contents of this resource.
1067 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
1068 contact.setAddressGroupList(addressGroupList);
1069 if (logger.isDebugEnabled()) {
1070 logger.debug("to be updated Contact");
1071 logger.debug(objectAsXmlString(contact,
1072 ContactsCommon.class));
1075 // Submit the updated resource to the service and store the response.
1076 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
1077 output.addPart(contactsCommonLabel, contact);
1079 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1081 assertStatusCode(res, testName);
1082 // Retrieve the updated resource and verify that its contents exist.
1083 input = new PoxPayloadIn(res.readEntity(String.class));;
1089 ContactsCommon updatedContact = (ContactsCommon) extractPart(input,
1090 contactsCommonLabel, ContactsCommon.class);
1091 Assert.assertNotNull(updatedContact);
1093 // Verify that the updated resource received the correct data.
1094 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1095 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1096 "Data in updated object did not match submitted data.");
1100 * Update non existent contact.
1102 * @param testName the test name
1103 * @throws Exception the exception
1105 @Test(dataProvider = "testName", groups = {"update"},
1106 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1107 public void updateNonExistentContact(String testName) throws Exception {
1108 // Currently a no-op test
1111 // ---------------------------------------------------------------
1112 // CRUD tests : DELETE tests
1113 // ---------------------------------------------------------------
1115 // Note: delete sub-resources in ascending hierarchical order,
1116 // before deleting their parents.
1120 * @param testName the test name
1121 * @throws Exception the exception
1123 @Test(dataProvider = "testName", groups = {"delete"},
1124 dependsOnMethods = {"updateContact"})
1125 public void deleteContact(String testName) throws Exception {
1126 if (logger.isDebugEnabled()) {
1127 logger.debug("parentcsid =" + knownResourceId
1128 + " itemcsid = " + knownItemResourceId
1129 + " csid = " + knownContactResourceId);
1132 // Submit the request to the service and store the response.
1133 PersonAuthorityClient client = new PersonAuthorityClient();
1135 Response res = client.deleteContact(knownResourceId, knownItemResourceId,
1136 knownContactResourceId);
1138 assertStatusCode(res, testName);
1147 public void delete(String testName) throws Exception {
1148 // Do nothing. See localDelete(). This ensure proper test order.
1151 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
1152 public void localDelete(String testName) throws Exception {
1153 super.delete(testName);
1157 public void deleteItem(String testName) throws Exception {
1158 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
1159 // its dependencies will get run first and then we can call the base class' delete method.
1162 @Test(dataProvider = "testName", groups = {"delete"},
1163 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
1164 public void localDeleteItem(String testName) throws Exception {
1165 super.deleteItem(testName);
1169 * Delete non existent contact.
1171 * @param testName the test name
1174 @Test(dataProvider = "testName", groups = {"delete"},
1175 dependsOnMethods = {"deleteContact"})
1176 public void deleteNonExistentContact(String testName) throws Exception {
1177 // Submit the request to the service and store the response.
1178 PersonAuthorityClient client = new PersonAuthorityClient();
1179 setupDeleteNonExistent();
1180 Response res = client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1182 assertStatusCode(res, testName);
1191 * Test contact submit request.
1193 @Test(dataProvider = "testName",
1194 dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
1195 public void testContactSubmitRequest(String testName) {
1197 // Expected status code: 200 OK
1198 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1200 // Submit the request to the service and store the response.
1201 String method = ServiceRequestType.READ.httpMethodName();
1202 String url = getContactResourceURL(knownResourceId,
1203 knownItemResourceId, knownContactResourceId);
1204 int statusCode = submitRequest(method, url);
1206 // Check the status code of the response: does it match
1207 // the expected response(s)?
1208 if (logger.isDebugEnabled()) {
1209 logger.debug("testContactSubmitRequest: url=" + url
1210 + " status=" + statusCode);
1212 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1216 // ---------------------------------------------------------------
1217 // Cleanup of resources created during testing
1218 // ---------------------------------------------------------------
1220 * Deletes all resources created by tests, after all tests have been run.
1222 * This cleanup method will always be run, even if one or more tests fail.
1223 * For this reason, it attempts to remove all resources created
1224 * at any point during testing, even if some of those resources
1225 * may be expected to be deleted by certain tests.
1228 @AfterClass(alwaysRun = true)
1230 public void cleanUp() throws Exception {
1231 String noTest = System.getProperty("noTestCleanup");
1232 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1233 if (logger.isDebugEnabled()) {
1234 logger.debug("Skipping Cleanup phase ...");
1239 if (logger.isDebugEnabled()) {
1240 logger.debug("Cleaning up temporary resources created for testing ...");
1243 String parentResourceId;
1244 String itemResourceId;
1245 String contactResourceId;
1246 // Clean up contact resources.
1247 PersonAuthorityClient client = new PersonAuthorityClient();
1248 parentResourceId = this.getKnowResourceId();
1249 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
1250 contactResourceId = entry.getKey();
1251 itemResourceId = entry.getValue();
1252 // Note: Any non-success responses from the delete operation
1253 // below are ignored and not reported.
1254 Response res = client.deleteContact(parentResourceId, itemResourceId,
1259 // Finally, clean call our superclass' cleanUp method.
1264 // ---------------------------------------------------------------
1265 // Utility methods used by tests above
1266 // ---------------------------------------------------------------
1268 * Gets the contact service path component.
1270 * @return the contact service path component
1272 public String getContactServicePathComponent() {
1273 return ContactClient.SERVICE_PATH_COMPONENT;
1277 * Returns the root URL for the item service.
1279 * This URL consists of a base URL for all services, followed by
1280 * a path component for the owning parent, followed by the
1281 * path component for the items.
1283 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1284 * parent authority resource of the relevant item resource.
1286 * @return The root URL for the item service.
1288 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1289 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1293 * Returns the URL of a specific item resource managed by a service, and
1294 * designated by an identifier (such as a universally unique ID, or UUID).
1296 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1297 * parent authority resource of the relevant item resource.
1299 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1302 * @return The URL of a specific item resource managed by a service.
1304 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1305 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1309 * Returns the root URL for the contact service.
1311 * This URL consists of a base URL for all services, followed by
1312 * a path component for the owning authority, followed by the
1313 * path component for the owning item, followed by the path component
1314 * for the contact service.
1316 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1317 * parent authority resource of the relevant item resource.
1319 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1322 * @return The root URL for the contact service.
1324 protected String getContactServiceRootURL(String parentResourceIdentifier,
1325 String itemResourceIdentifier) {
1326 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1327 + getContactServicePathComponent();
1331 * Returns the URL of a specific contact resource managed by a service, and
1332 * designated by an identifier (such as a universally unique ID, or UUID).
1334 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1335 * parent resource of the relevant item resource.
1337 * @param resourceIdentifier An identifier (such as a UUID) for an
1340 * @return The URL of a specific resource managed by a service.
1342 protected String getContactResourceURL(String parentResourceIdentifier,
1343 String itemResourceIdentifier, String contactResourceIdentifier) {
1344 return getContactServiceRootURL(parentResourceIdentifier,
1345 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1349 public void authorityTests(String testName) {
1350 // TODO Auto-generated method stub
1355 protected PersonsCommon updateItemInstance(PersonsCommon personsCommon) {
1357 PersonTermGroupList termList = personsCommon.getPersonTermGroupList();
1358 Assert.assertNotNull(termList);
1359 List<PersonTermGroup> terms = termList.getPersonTermGroup();
1360 Assert.assertNotNull(terms);
1361 Assert.assertTrue(terms.size() > 0);
1362 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1363 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1364 personsCommon.setPersonTermGroupList(termList);
1366 return personsCommon;
1370 protected void compareUpdatedItemInstances(PersonsCommon original,
1371 PersonsCommon updated,
1372 boolean compareRevNumbers) throws Exception {
1374 PersonTermGroupList originalTermList = original.getPersonTermGroupList();
1375 Assert.assertNotNull(originalTermList);
1376 List<PersonTermGroup> originalTerms = originalTermList.getPersonTermGroup();
1377 Assert.assertNotNull(originalTerms);
1378 Assert.assertTrue(originalTerms.size() > 0);
1380 PersonTermGroupList updatedTermList = updated.getPersonTermGroupList();
1381 Assert.assertNotNull(updatedTermList);
1382 List<PersonTermGroup> updatedTerms = updatedTermList.getPersonTermGroup();
1383 Assert.assertNotNull(updatedTerms);
1384 Assert.assertTrue(updatedTerms.size() > 0);
1386 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1387 originalTerms.get(0).getTermDisplayName(),
1388 "Value in updated record did not match submitted data.");
1390 if (compareRevNumbers == true) {
1391 Assert.assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
1396 protected PoxPayloadOut createInstance(String commonPartName,
1397 String identifier) {
1398 String shortId = identifier;
1399 String displayName = "displayName-" + shortId;
1400 PoxPayloadOut result =
1401 PersonAuthorityClientUtils.createPersonAuthorityInstance(
1402 displayName, shortId, commonPartName);
1407 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1408 String identifier) {
1409 String displayName = "displayName-NON_EXISTENT_ID";
1410 PoxPayloadOut result = PersonAuthorityClientUtils.createPersonAuthorityInstance(
1411 displayName, "NON_EXISTENT_SHORT_ID", commonPartName);
1415 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier) {
1416 Map<String, String> nonexMap = new HashMap<String, String>();
1417 nonexMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, "nonEX");
1418 nonexMap.put(PersonJAXBSchema.GENDER, "male");
1420 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
1421 PersonTermGroup term = new PersonTermGroup();
1422 term.setTermDisplayName("John Wayne");
1423 term.setTermName("John Wayne");
1424 term.setForeName("John");
1425 term.setSurName("Wayne");
1428 Map<String, List<String>> nonexRepeatablesMap = new HashMap<String, List<String>>();
1429 PoxPayloadOut result =
1430 PersonAuthorityClientUtils.createPersonInstance(NON_EXISTENT_ID,
1431 null, //PersonAuthorityClientUtils.createPersonAuthRefName(NON_EXISTENT_ID, null),
1432 nonexMap, terms, nonexRepeatablesMap, commonPartName);
1437 protected PersonauthoritiesCommon updateInstance(PersonauthoritiesCommon personauthoritiesCommon) {
1438 PersonauthoritiesCommon result = new PersonauthoritiesCommon();
1440 result.setDisplayName("updated-" + personauthoritiesCommon.getDisplayName());
1441 result.setVocabType("updated-" + personauthoritiesCommon.getVocabType());
1447 protected void compareUpdatedInstances(PersonauthoritiesCommon original,
1448 PersonauthoritiesCommon updated) throws Exception {
1449 // Verify that the updated resource received the correct data.
1450 Assert.assertEquals(updated.getDisplayName(),
1451 original.getDisplayName(),
1452 "Display name in updated object did not match submitted data.");
1456 protected void verifyReadItemInstance(PersonsCommon item) throws Exception {
1457 // Do nothing for now. Add more 'read' validation checks here if applicable.