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;
29 import javax.ws.rs.core.Response;
31 import org.collectionspace.services.client.AbstractCommonListUtils;
32 import org.collectionspace.services.client.AuthorityClient;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.PayloadOutputPart;
35 import org.collectionspace.services.client.PoxPayloadIn;
36 import org.collectionspace.services.client.PoxPayloadOut;
38 import org.collectionspace.services.client.ContactClient;
39 import org.collectionspace.services.client.ContactClientUtils;
40 import org.collectionspace.services.contact.AddressGroup;
41 import org.collectionspace.services.contact.AddressGroupList;
42 import org.collectionspace.services.contact.ContactsCommon;
44 import org.collectionspace.services.client.PersonAuthorityClient;
45 import org.collectionspace.services.client.PersonAuthorityClientUtils;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
47 import org.collectionspace.services.PersonJAXBSchema;
48 import org.collectionspace.services.person.PersonauthoritiesCommon;
49 import org.collectionspace.services.person.PersonTermGroup;
50 import org.collectionspace.services.person.PersonTermGroupList;
51 import org.collectionspace.services.person.PersonsCommon;
53 import org.jboss.resteasy.client.ClientResponse;
54 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.testng.Assert;
58 import org.testng.annotations.AfterClass;
59 import org.testng.annotations.Test;
62 * PersonAuthorityServiceTest, carries out tests against a
63 * deployed and running PersonAuthority Service.
65 * $LastChangedRevision: 753 $
66 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
68 public class PersonAuthorityServiceTest extends AbstractAuthorityServiceTest<PersonauthoritiesCommon, PersonsCommon> { //FIXME: Test classes for Vocab, Person, Org, and Location should have a base class!
71 private final String CLASS_NAME = PersonAuthorityServiceTest.class.getName();
72 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
73 private final String REFNAME = "refName";
74 private final String DISPLAYNAME = "displayName";
77 public String getServicePathComponent() {
78 return PersonAuthorityClient.SERVICE_PATH_COMPONENT;
82 protected String getServiceName() {
83 return PersonAuthorityClient.SERVICE_NAME;
86 public String getItemServicePathComponent() {
87 return AuthorityClient.ITEMS;
89 /** The test forename. */
90 final String TEST_FORE_NAME = "John";
91 /** The test middle name. */
92 final String TEST_MIDDLE_NAME = null;
93 /** The test surname. */
94 final String TEST_SUR_NAME = "Wayne";
95 /** The test birthdate. */
96 final String TEST_BIRTH_DATE = "May 26, 1907";
97 /** The test death date. */
98 final String TEST_DEATH_DATE = "June 11, 1979";
99 //private String knownResourceRefName = null;
100 private String knownItemResourceShortIdentifer = null;
101 // The resource ID of an item resource used for partial term matching tests.
102 private String knownItemPartialTermResourceId = null;
103 /** The known contact resource id. */
104 private String knownContactResourceId = null;
105 /** The all contact resource ids created. */
106 private Map<String, String> allContactResourceIdsCreated =
107 new HashMap<String, String>();
109 protected void setKnownResource(String id, String shortIdentifer,
111 knownResourceId = id;
112 knownResourceShortIdentifer = shortIdentifer;
113 //knownResourceRefName = refName;
116 protected void setKnownItemResource(String id, String shortIdentifer) {
117 knownItemResourceId = id;
118 knownItemResourceShortIdentifer = shortIdentifer;
122 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
125 protected CollectionSpaceClient getClientInstance() {
126 return new PersonAuthorityClient();
129 // ---------------------------------------------------------------
130 // CRUD tests : CREATE tests
131 // ---------------------------------------------------------------
134 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
137 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
138 // groups = {"create"})
139 public void create(String testName) throws Exception {
140 // Perform setup, such as initializing the type of service request
141 // (e.g. CREATE, DELETE), its valid and expected status codes, and
142 // its associated HTTP method name (e.g. POST, DELETE).
145 // Submit the request to the service and store the response.
146 PersonAuthorityClient client = new PersonAuthorityClient();
147 String shortId = createIdentifier();
148 String displayName = "displayName-" + shortId;
149 //String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(shortId, null);
150 PoxPayloadOut multipart =
151 PersonAuthorityClientUtils.createPersonAuthorityInstance(
152 displayName, shortId, client.getCommonPartName());
155 ClientResponse<Response> res = client.create(multipart);
157 assertStatusCode(res, testName);
158 newID = extractId(res);
161 res.releaseConnection();
164 // Save values for additional tests
165 if (knownResourceId == null) {
166 setKnownResource(newID, shortId, null ); //baseRefName);
167 if (logger.isDebugEnabled()) {
168 logger.debug(testName + ": knownResourceId=" + knownResourceId);
171 // Store the IDs from every resource created by tests,
172 // so they can be deleted after tests have been run.
173 allResourceIdsCreated.add(newID);
177 protected PoxPayloadOut createInstance(String identifier) {
178 PersonAuthorityClient client = new PersonAuthorityClient();
179 String displayName = "displayName-" + identifier;
180 PoxPayloadOut multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
181 displayName, identifier, client.getCommonPartName());
186 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
187 String headerLabel = new PersonAuthorityClient().getItemCommonPartName();
189 HashMap<String, String> personInfo = new HashMap<String, String>();
190 String shortId = "johnWayneTempActor";
191 personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
193 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
194 PersonTermGroup term = new PersonTermGroup();
195 term.setTermDisplayName("John Wayne Temp");
196 term.setTermName("JohnWayneTemp");
199 return PersonAuthorityClientUtils.createPersonInstance(parentCsid, identifier, personInfo, terms, headerLabel);
203 * Creates an item in an authority, using test data.
205 * @param vcsid the vcsid
206 * @param authRefName the auth ref name
210 protected String createItemInAuthority(String vcsid) {
212 final String testName = "createItemInAuthority";
213 if (logger.isDebugEnabled()) {
214 logger.debug(testName + ":" + vcsid + "...");
217 Map<String, String> johnWayneMap = new HashMap<String, String>();
219 // Fill the property map
221 String shortId = "johnWayneActor";
222 johnWayneMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
223 johnWayneMap.put(PersonJAXBSchema.GENDER, "male");
224 johnWayneMap.put(PersonJAXBSchema.BIRTH_DATE, TEST_BIRTH_DATE);
225 johnWayneMap.put(PersonJAXBSchema.BIRTH_PLACE, "Winterset, Iowa");
226 johnWayneMap.put(PersonJAXBSchema.DEATH_DATE, TEST_DEATH_DATE);
227 johnWayneMap.put(PersonJAXBSchema.BIO_NOTE, "born Marion Robert Morrison and better"
228 + "known by his stage name John Wayne, was an American film actor, director "
229 + "and producer. He epitomized rugged masculinity and has become an enduring "
230 + "American icon. He is famous for his distinctive voice, walk and height. "
231 + "He was also known for his conservative political views and his support in "
232 + "the 1950s for anti-communist positions.");
234 List<PersonTermGroup> johnWayneTerms = new ArrayList<PersonTermGroup>();
235 PersonTermGroup term = new PersonTermGroup();
236 term.setTermDisplayName("John Wayne DisplayName");
237 term.setTermName("John Wayne");
238 term.setForeName(TEST_FORE_NAME);
239 term.setSurName(TEST_SUR_NAME);
240 johnWayneTerms.add(term);
242 Map<String, List<String>> johnWayneRepeatablesMap = new HashMap<String, List<String>>();
243 List<String> johnWayneGroups = new ArrayList<String>();
244 johnWayneGroups.add("Irish");
245 johnWayneGroups.add("Scottish");
246 johnWayneRepeatablesMap.put(PersonJAXBSchema.GROUPS, johnWayneGroups);
248 return createItemInAuthority(vcsid, null /*authRefName*/, shortId, johnWayneMap, johnWayneTerms, johnWayneRepeatablesMap);
253 * Creates an item in an authority.
255 * @param vcsid the vcsid
256 * @param authRefName the auth ref name
257 * @param itemFieldProperties a set of properties specifying the values of fields.
258 * @param itemRepeatableFieldProperties a set of properties specifying the values of repeatable fields.
261 private String createItemInAuthority(String vcsid, String authRefName, String shortId,
262 Map itemFieldProperties, List<PersonTermGroup> terms, Map itemRepeatableFieldProperties) {
264 final String testName = "createItemInAuthority";
265 if (logger.isDebugEnabled()) {
266 logger.debug(testName + ":" + vcsid + "...");
269 // Submit the request to the service and store the response.
270 PersonAuthorityClient client = new PersonAuthorityClient();
271 PoxPayloadOut multipart =
272 PersonAuthorityClientUtils.createPersonInstance(vcsid, null /*authRefName*/, itemFieldProperties,
273 terms, itemRepeatableFieldProperties, client.getItemCommonPartName());
275 ClientResponse<Response> res = client.createItem(vcsid, multipart);
278 assertStatusCode(res, testName);
279 newID = PersonAuthorityClientUtils.extractId(res);
282 res.releaseConnection();
286 // Store the ID returned from the first item resource created
287 // for additional tests below.
288 if (knownItemResourceId == null) {
289 setKnownItemResource(newID, shortId);
290 if (logger.isDebugEnabled()) {
291 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
294 if (logger.isDebugEnabled()) {
295 logger.debug(testName + " (created):" + vcsid + "/(" + newID + "," + shortId + ")");
298 // Store the IDs from any item resources created
299 // by tests, along with the IDs of their parents, so these items
300 // can be deleted after all tests have been run.
301 allResourceItemIdsCreated.put(newID, vcsid);
307 * Creates the contact.
309 * @param testName the test name
311 @Test(dataProvider = "testName", groups = {"create"},
312 dependsOnMethods = {"createItem"})
313 public void createContact(String testName) {
315 String newID = createContactInItem(knownResourceId, knownItemResourceId);
319 * Creates the contact in item.
321 * @param parentcsid the parentcsid
322 * @param itemcsid the itemcsid
325 private String createContactInItem(String parentcsid, String itemcsid) {
327 final String testName = "createContactInItem";
328 if (logger.isDebugEnabled()) {
329 logger.debug(testName + ":...");
331 // Submit the request to the service and store the response.
332 PersonAuthorityClient client = new PersonAuthorityClient();
333 String identifier = createIdentifier();
334 PoxPayloadOut multipart = ContactClientUtils.createContactInstance(parentcsid,
335 itemcsid, identifier, new ContactClient().getCommonPartName());
338 ClientResponse<Response> res =
339 client.createContact(parentcsid, itemcsid, multipart);
342 assertStatusCode(res, testName);
343 newID = PersonAuthorityClientUtils.extractId(res);
346 res.releaseConnection();
350 // Store the ID returned from the first contact resource created
351 // for additional tests below.
352 if (knownContactResourceId == null) {
353 knownContactResourceId = newID;
354 if (logger.isDebugEnabled()) {
355 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
359 // Store the IDs from any contact resources created
360 // by tests, along with the IDs of their parent items,
361 // so these items can be deleted after all tests have been run.
362 allContactResourceIdsCreated.put(newID, itemcsid);
368 * Attempts to create an authority with an short identifier that contains
369 * non-word characters.
371 * @param testName the test name
373 @Test(dataProvider = "testName", groups = {"create", "nonWordCharsInShortId"})
374 public void createWithShortIdNonWordChars(String testName) throws Exception {
375 testExpectedStatusCode = STATUS_BAD_REQUEST;
376 testRequestType = ServiceRequestType.CREATE;
377 testSetup(testExpectedStatusCode, testRequestType);
379 // Create the payload to be included in the body of the request
380 PersonAuthorityClient client = new PersonAuthorityClient();
381 String shortId = createIdentifier() + "*" + createIdentifier();
382 String displayName = "displayName-" + shortId;
383 PoxPayloadOut multipart =
384 PersonAuthorityClientUtils.createPersonAuthorityInstance(
385 displayName, shortId, client.getCommonPartName());
387 // Submit the request to the service and store the response.
388 ClientResponse<Response> res = client.create(multipart);
390 // Check the status code of the response: does it match
391 // the expected response(s)?
393 assertStatusCode(res, testName);
396 res.releaseConnection();
402 * Attempts to create an item with an short identifier that contains
403 * non-word characters.
405 * @param testName the test name
407 @Test(dataProvider = "testName", groups = {"create", "nonWordCharsInShortId"},
408 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractServiceTestImpl.create"})
409 public void createItemWithShortIdNonWordChars(String testName) {
410 testExpectedStatusCode = STATUS_BAD_REQUEST;
411 testRequestType = ServiceRequestType.CREATE;
412 testSetup(testExpectedStatusCode, testRequestType);
414 PersonAuthorityClient client = new PersonAuthorityClient();
415 // Create the payload to be included in the body of the request
416 String shortId = "7-Eleven";
417 Map<String, String> fieldProperties = new HashMap<String, String>();
418 fieldProperties.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
420 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
421 PersonTermGroup term = new PersonTermGroup();
422 term.setTermDisplayName(shortId);
423 term.setTermName(shortId);
426 final Map NULL_REPEATABLE_FIELD_PROPERTIES = null;
427 PoxPayloadOut multipart =
428 PersonAuthorityClientUtils.createPersonInstance(knownResourceId,
429 null /*knownResourceRefName*/, fieldProperties, terms,
430 NULL_REPEATABLE_FIELD_PROPERTIES, client.getItemCommonPartName());
432 // Send the request and receive a response
433 ClientResponse<Response> res = client.createItem(knownResourceId, multipart);
434 // Check the status code of the response: does it match
435 // the expected response(s)?
437 assertStatusCode(res, testName);
440 res.releaseConnection();
445 // ---------------------------------------------------------------
446 // CRUD tests : CREATE LIST tests
447 // ---------------------------------------------------------------
450 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
453 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
454 // groups = {"createList"}, dependsOnGroups = {"create"})
455 public void createList(String testName) throws Exception {
456 for (int i = 0; i < nItemsToCreateInList; i++) {
462 * Creates the contact list.
464 * @param testName the test name
465 * @throws Exception the exception
467 @Test(dataProvider = "testName", groups = {"createList"},
468 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.createItemList"})
469 public void createContactList(String testName) throws Exception {
470 // Add contacts to the initially-created, known item record.
471 for (int j = 0; j < nItemsToCreateInList; j++) {
472 createContact(testName);
476 // ---------------------------------------------------------------
477 // CRUD tests : READ tests
478 // ---------------------------------------------------------------
481 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
484 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
485 // groups = {"read"}, dependsOnGroups = {"create"})
486 public void read(String testName) throws Exception {
487 readInternal(testName, knownResourceId, null);
493 * @param testName the test name
494 * @throws Exception the exception
497 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
498 // groups = {"read"}, dependsOnMethods = {"read"})
499 public void readByName(String testName) throws Exception {
500 readInternal(testName, null, knownResourceShortIdentifer);
503 protected void readInternal(String testName, String CSID, String shortId) {
504 // Submit the request to the service and store the response.
505 PersonAuthorityClient client = new PersonAuthorityClient();
506 ClientResponse<String> res = null;
509 res = client.read(CSID);
510 } else if (shortId != null) {
511 res = client.readByName(shortId);
513 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
516 assertStatusCode(res, testName);
517 //FIXME: remove the following try catch once Aron fixes signatures
519 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
520 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
521 client.getCommonPartName(), PersonauthoritiesCommon.class);
522 Assert.assertNotNull(personAuthority);
523 } catch (Exception e) {
524 throw new RuntimeException(e);
528 res.releaseConnection();
536 * @param testName the test name
537 * @throws Exception the exception
539 @Test(dataProvider = "testName", groups = {"readItem", "readNamedItemInNamedAuth"},
540 dependsOnMethods = {"readItemInNamedAuth"})
541 public void readNamedItem(String testName) throws Exception {
542 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
546 * Read item in Named Auth.
548 * @param testName the test name
549 * @throws Exception the exception
551 @Test(dataProvider = "testName", groups = {"readItem"},
552 dependsOnMethods = {"readItem"})
553 public void readItemInNamedAuth(String testName) throws Exception {
554 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
559 * Read Named item in Named Auth.
561 * @param testName the test name
562 * @throws Exception the exception
564 @Test(dataProvider = "testName", groups = {"readItem"},
565 dependsOnMethods = {"readNamedItem"})
566 public void readNamedItemInNamedAuth(String testName) throws Exception {
567 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
570 protected void readItemInternal(String testName,
571 String authCSID, String authShortId, String itemCSID, String itemShortId)
574 if (logger.isDebugEnabled()) {
575 logger.debug("Reading:" + ((authCSID != null) ? authCSID : authShortId) + "/"
576 + ((itemCSID != null) ? authCSID : itemShortId));
579 // Submit the request to the service and store the response.
580 PersonAuthorityClient client = new PersonAuthorityClient();
582 ClientResponse<String> res = null;
583 if (authCSID != null) {
584 if (itemCSID != null) {
585 res = client.readItem(authCSID, itemCSID);
586 } else if (itemShortId != null) {
587 res = client.readNamedItem(authCSID, itemShortId);
589 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
591 } else if (authShortId != null) {
592 if (itemCSID != null) {
593 res = client.readItemInNamedAuthority(authShortId, itemCSID);
594 } else if (itemShortId != null) {
595 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
597 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
600 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
603 assertStatusCode(res, testName);
604 // Check whether we've received a person.
605 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
606 PersonsCommon person = (PersonsCommon) extractPart(input,
607 client.getItemCommonPartName(), PersonsCommon.class);
608 Assert.assertNotNull(person);
609 boolean showFull = true;
610 if (showFull && logger.isDebugEnabled()) {
611 logger.debug(testName + ": returned payload:");
612 logger.debug(objectAsXmlString(person, PersonsCommon.class));
615 // Check that the person item is within the expected Person Authority.
616 Assert.assertEquals(person.getInAuthority(), knownResourceId);
618 // Verify the number and contents of values in a repeatable field,
619 // as created in the instance record used for testing.
620 List<String> groups = person.getGroups().getGroup();
621 Assert.assertTrue(groups.size() > 0);
622 Assert.assertNotNull(groups.get(0));
625 res.releaseConnection();
630 // FIXME: This test depends on server-side validation logic to require
631 // a non-null (and potentially, non-empty) displayname for each term,
632 // and will fail if that validation is not present.
635 * Verify illegal item display name.
637 * @param testName the test name
638 * @throws Exception the exception
640 @Test(dataProvider = "testName", groups = {"update"})
641 public void verifyIllegalItemDisplayName(String testName) throws Exception {
642 // Perform setup for read.
645 // Submit the request to the service and store the response.
646 PersonAuthorityClient client = new PersonAuthorityClient();
647 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
648 PoxPayloadIn input = null;
650 assertStatusCode(res, testName);
651 input = new PoxPayloadIn(res.getEntity());
654 res.releaseConnection();
658 // Make an invalid UPDATE request, without a display name
660 PersonsCommon person = (PersonsCommon) extractPart(input,
661 client.getItemCommonPartName(), PersonsCommon.class);
662 Assert.assertNotNull(person);
663 // Try to Update with no displayName
664 List<PersonTermGroup> johnWayneTerms = new ArrayList<PersonTermGroup>();
665 PersonTermGroup term = new PersonTermGroup();
666 term.setTermDisplayName("John Wayne DisplayName");
667 term.setTermName("John Wayne");
668 term.setForeName(TEST_FORE_NAME);
669 term.setSurName(TEST_SUR_NAME);
670 johnWayneTerms.add(term);
672 PersonTermGroupList termList = person.getPersonTermGroupList();
673 Assert.assertNotNull(termList);
674 List<PersonTermGroup> terms = termList.getPersonTermGroup();
675 Assert.assertNotNull(terms);
676 terms.get(0).setTermDisplayName(null);
678 // Submit the updated resource to the service and store the response.
679 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
680 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), person);
681 setupUpdateWithInvalidBody();
682 res = client.updateItem(knownResourceId, knownItemResourceId, output);
684 assertStatusCode(res, testName);
687 res.releaseConnection();
695 * @param testName the test name
696 * @throws Exception the exception
698 @Test(dataProvider = "testName", groups = {"readItem"},
699 dependsOnMethods = {"createContact", "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItem"})
700 public void readContact(String testName) throws Exception {
704 // Submit the request to the service and store the response.
705 PersonAuthorityClient client = new PersonAuthorityClient();
706 PoxPayloadIn input = null;
707 ClientResponse<String> res =
708 client.readContact(knownResourceId, knownItemResourceId,
709 knownContactResourceId);
711 assertStatusCode(res, testName);
712 // Check whether we've received a contact.
713 input = new PoxPayloadIn(res.getEntity());
716 res.releaseConnection();
720 ContactsCommon contact = (ContactsCommon) extractPart(input,
721 new ContactClient().getCommonPartName(), ContactsCommon.class);
722 Assert.assertNotNull(contact);
723 boolean showFull = true;
724 if (showFull && logger.isDebugEnabled()) {
725 logger.debug(testName + ": returned payload:");
726 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
728 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
729 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
734 * Read contact non existent.
736 * @param testName the test name
738 @Test(dataProvider = "testName", groups = {"readItem"},
739 dependsOnMethods = {"readContact"})
740 public void readContactNonExistent(String testName) {
742 setupReadNonExistent();
744 // Submit the request to the service and store the response.
745 PersonAuthorityClient client = new PersonAuthorityClient();
746 ClientResponse<String> res =
747 client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
749 assertStatusCode(res, testName);
752 res.releaseConnection();
757 // ---------------------------------------------------------------
758 // CRUD tests : READ_LIST tests
759 // ---------------------------------------------------------------
765 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
766 // groups = {"readList"}, dependsOnMethods = {"readList"})
767 public void readItemList(String testName) {
768 readItemList(knownAuthorityWithItems, null, testName);
772 * Read item list by authority name.
775 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
776 // groups = {"readList"}, dependsOnMethods = {"readItemList"})
777 public void readItemListByName(String testName) {
778 readItemList(null, READITEMS_SHORT_IDENTIFIER, testName);
784 * @param vcsid the vcsid
785 * @param name the name
787 private void readItemList(String vcsid, String name, String testName) {
789 // Submit the request to the service and store the response.
790 PersonAuthorityClient client = new PersonAuthorityClient();
791 ClientResponse<AbstractCommonList> res = null;
793 res = client.readItemList(vcsid, null, null);
794 } else if (name != null) {
795 res = client.readItemListForNamedAuthority(name, null, null);
797 Assert.fail("readItemList passed null csid and name!");
799 AbstractCommonList list = null;
801 assertStatusCode(res, testName);
802 list = res.getEntity();
805 res.releaseConnection();
809 List<AbstractCommonList.ListItem> items = list.getListItem();
810 int nItemsReturned = items.size();
811 // There will be 'nItemsToCreateInList'
812 // items created by the createItemList test,
813 // all associated with the same parent resource.
814 int nExpectedItems = nItemsToCreateInList;
815 if (logger.isDebugEnabled()) {
816 logger.debug(testName + ": Expected "
817 + nExpectedItems + " items; got: " + nItemsReturned);
819 Assert.assertEquals(nItemsReturned, nExpectedItems);
821 for (AbstractCommonList.ListItem item : items) {
823 AbstractCommonListUtils.ListItemGetElementValue(item, REFNAME);
824 Assert.assertTrue((null != value), "Item refName is null!");
826 AbstractCommonListUtils.ListItemGetElementValue(item, DISPLAYNAME);
827 Assert.assertTrue((null != value), "Item displayName is null!");
829 if (logger.isTraceEnabled()) {
830 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
837 @Test(groups = {"readList"},
838 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
839 public void readContactList() {
840 readContactList(knownResourceId, knownItemResourceId);
846 * @param parentcsid the parentcsid
847 * @param itemcsid the itemcsid
849 private void readContactList(String parentcsid, String itemcsid) {
850 final String testName = "readContactList";
855 // Submit the request to the service and store the response.
856 PersonAuthorityClient client = new PersonAuthorityClient();
857 AbstractCommonList list = null;
858 ClientResponse<AbstractCommonList> res =
859 client.readContactList(parentcsid, itemcsid);
861 assertStatusCode(res, testName);
862 list = res.getEntity();
865 res.releaseConnection();
869 List<AbstractCommonList.ListItem> listitems =
871 int nItemsReturned = listitems.size();
872 // There will be one item created, associated with a
873 // known parent resource, by the createItem test.
875 // In addition, there will be 'nItemsToCreateInList'
876 // additional items created by the createItemList test,
877 // all associated with the same parent resource.
878 int nExpectedItems = nItemsToCreateInList + 1;
879 if (logger.isDebugEnabled()) {
880 logger.debug(testName + ": Expected "
881 + nExpectedItems + " items; got: " + nItemsReturned);
883 Assert.assertEquals(nItemsReturned, nExpectedItems);
885 // Optionally output additional data about list members for debugging.
886 boolean iterateThroughList = false;
887 if (iterateThroughList && logger.isDebugEnabled()) {
888 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
893 // There are no failure outcome tests at present.
894 // ---------------------------------------------------------------
895 // CRUD tests : UPDATE tests
896 // ---------------------------------------------------------------
899 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
902 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
903 // groups = {"update"}, dependsOnGroups = {"readItem", "readList"})
904 public void update(String testName) throws Exception {
905 // Retrieve the contents of a resource to update.
906 PersonAuthorityClient client = new PersonAuthorityClient();
907 PoxPayloadIn input = null;
909 ClientResponse<String> res = client.read(knownResourceId);
911 assertStatusCode(res, testName);
912 if (logger.isDebugEnabled()) {
913 logger.debug("got PersonAuthority to update with ID: " + knownResourceId);
915 input = new PoxPayloadIn(res.getEntity());
918 res.releaseConnection();
922 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
923 client.getCommonPartName(), PersonauthoritiesCommon.class);
924 Assert.assertNotNull(personAuthority);
926 // Update the contents of this resource.
927 personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
928 personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
929 if (logger.isDebugEnabled()) {
930 logger.debug("to be updated PersonAuthority");
931 logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
934 // Submit the updated resource to the service and store the response.
935 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_PAYLOAD_NAME);
936 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), personAuthority);
938 res = client.update(knownResourceId, output);
940 assertStatusCode(res, testName);
941 // Retrieve the updated resource and verify that its contents exist.
942 input = new PoxPayloadIn(res.getEntity());
945 res.releaseConnection();
949 PersonauthoritiesCommon updatedPersonAuthority =
950 (PersonauthoritiesCommon) extractPart(input,
951 client.getCommonPartName(), PersonauthoritiesCommon.class);
952 Assert.assertNotNull(updatedPersonAuthority);
954 // Verify that the updated resource received the correct data.
955 Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
956 personAuthority.getDisplayName(),
957 "Data in updated object did not match submitted data.");
963 * @param testName the test name
964 * @throws Exception the exception
967 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
968 // groups = {"update"}, dependsOnMethods = {"update"})
969 public void updateItem(String testName) throws Exception {
970 // Retrieve the contents of a resource to update.
971 PersonAuthorityClient client = new PersonAuthorityClient();
972 PoxPayloadIn input = null;
974 ClientResponse<String> res =
975 client.readItem(knownResourceId, knownItemResourceId);
977 assertStatusCode(res, testName);
978 if (logger.isDebugEnabled()) {
979 logger.debug("got Person to update with ID: "
980 + knownItemResourceId
981 + " in PersonAuthority: " + knownResourceId);
983 input = new PoxPayloadIn(res.getEntity());
986 res.releaseConnection();
990 PersonsCommon person = (PersonsCommon) extractPart(input,
991 client.getItemCommonPartName(), PersonsCommon.class);
992 Assert.assertNotNull(person);
994 if (logger.isDebugEnabled() == true) {
995 logger.debug("About to update the following person...");
996 logger.debug(objectAsXmlString(person, PersonsCommon.class));
999 // Update the contents of this resource.
1000 person.setCsid(null);
1001 PersonTermGroupList termList = person.getPersonTermGroupList();
1002 Assert.assertNotNull(termList);
1003 List<PersonTermGroup> terms = termList.getPersonTermGroup();
1004 Assert.assertNotNull(terms);
1005 String foreName = terms.get(0).getForeName();
1006 terms.get(0).setForeName("updated-" + foreName);
1007 if (logger.isDebugEnabled()) {
1008 logger.debug("to be updated Person");
1009 logger.debug(objectAsXmlString(person,
1010 PersonsCommon.class));
1013 // Submit the updated resource to the service and store the response.
1014 PoxPayloadOut output = new PoxPayloadOut(PersonAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
1015 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), person);
1017 res = client.updateItem(knownResourceId, knownItemResourceId, output);
1019 assertStatusCode(res, testName);
1020 // Retrieve the updated resource and verify that its contents exist.
1021 input = new PoxPayloadIn(res.getEntity());
1024 res.releaseConnection();
1028 PersonsCommon updatedPerson =
1029 (PersonsCommon) extractPart(input,
1030 client.getItemCommonPartName(), PersonsCommon.class);
1031 Assert.assertNotNull(updatedPerson);
1033 if (logger.isDebugEnabled() == true) {
1034 logger.debug("Updated to following person to:");
1035 logger.debug(objectAsXmlString(updatedPerson, PersonsCommon.class));
1038 // Verify that the updated resource received the correct data.
1039 PersonTermGroupList updatedTermList = person.getPersonTermGroupList();
1040 Assert.assertNotNull(updatedTermList);
1041 List<PersonTermGroup> updatedTerms = termList.getPersonTermGroup();
1042 Assert.assertNotNull(updatedTerms);
1043 String updatedForeName = updatedTerms.get(0).getForeName();
1044 Assert.assertEquals(updatedForeName, foreName,
1045 "Data in updated Person did not match submitted data.");
1051 * @param testName the test name
1052 * @throws Exception the exception
1054 @Test(dataProvider = "testName", groups = {"update"},
1055 dependsOnMethods = {"readContact", "testContactSubmitRequest",
1056 "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
1057 public void updateContact(String testName) throws Exception {
1058 String contactsCommonLabel = new ContactClient().getCommonPartName();
1060 // Retrieve the contents of a resource to update.
1061 PersonAuthorityClient client = new PersonAuthorityClient();
1062 PoxPayloadIn input = null;
1064 ClientResponse<String> res =
1065 client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1067 assertStatusCode(res, testName);
1068 if (logger.isDebugEnabled()) {
1069 logger.debug("got Contact to update with ID: "
1070 + knownContactResourceId
1071 + " in item: " + knownItemResourceId
1072 + " in parent: " + knownResourceId);
1074 input = new PoxPayloadIn(res.getEntity());
1077 res.releaseConnection();
1081 ContactsCommon contact = (ContactsCommon) extractPart(input,
1082 contactsCommonLabel, ContactsCommon.class);
1083 Assert.assertNotNull(contact);
1085 // Verify the contents of this resource
1086 AddressGroupList addressGroupList = contact.getAddressGroupList();
1087 Assert.assertNotNull(addressGroupList);
1088 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
1089 Assert.assertNotNull(addressGroups);
1090 Assert.assertTrue(addressGroups.size() > 0);
1091 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
1092 Assert.assertNotNull(addressPlace1);
1094 // Update the contents of this resource.
1095 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
1096 contact.setAddressGroupList(addressGroupList);
1097 if (logger.isDebugEnabled()) {
1098 logger.debug("to be updated Contact");
1099 logger.debug(objectAsXmlString(contact,
1100 ContactsCommon.class));
1103 // Submit the updated resource to the service and store the response.
1104 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
1105 PayloadOutputPart commonPart = output.addPart(contactsCommonLabel, contact);
1107 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1109 assertStatusCode(res, testName);
1110 // Retrieve the updated resource and verify that its contents exist.
1111 input = new PoxPayloadIn(res.getEntity());;
1114 res.releaseConnection();
1117 ContactsCommon updatedContact = (ContactsCommon) extractPart(input,
1118 contactsCommonLabel, ContactsCommon.class);
1119 Assert.assertNotNull(updatedContact);
1121 // Verify that the updated resource received the correct data.
1122 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1123 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
1124 "Data in updated object did not match submitted data.");
1128 * Update non existent contact.
1130 * @param testName the test name
1131 * @throws Exception the exception
1133 @Test(dataProvider = "testName", groups = {"update"},
1134 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1135 public void updateNonExistentContact(String testName) throws Exception {
1136 // Currently a no-op test
1139 // ---------------------------------------------------------------
1140 // CRUD tests : DELETE tests
1141 // ---------------------------------------------------------------
1143 // Note: delete sub-resources in ascending hierarchical order,
1144 // before deleting their parents.
1148 * @param testName the test name
1149 * @throws Exception the exception
1151 @Test(dataProvider = "testName", groups = {"delete"},
1152 dependsOnMethods = {"updateContact"})
1153 public void deleteContact(String testName) throws Exception {
1154 if (logger.isDebugEnabled()) {
1155 logger.debug("parentcsid =" + knownResourceId
1156 + " itemcsid = " + knownItemResourceId
1157 + " csid = " + knownContactResourceId);
1160 // Submit the request to the service and store the response.
1161 PersonAuthorityClient client = new PersonAuthorityClient();
1163 ClientResponse<Response> res =
1164 client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1166 assertStatusCode(res, testName);
1169 res.releaseConnection();
1175 public void delete(String testName) throws Exception {
1176 // Do nothing. See localDelete(). This ensure proper test order.
1179 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
1180 public void localDelete(String testName) throws Exception {
1181 super.delete(testName);
1185 public void deleteItem(String testName) throws Exception {
1186 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
1187 // its dependencies will get run first and then we can call the base class' delete method.
1190 @Test(dataProvider = "testName", groups = {"delete"},
1191 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
1192 public void localDeleteItem(String testName) throws Exception {
1193 super.deleteItem(testName);
1197 * Delete non existent contact.
1199 * @param testName the test name
1201 @Test(dataProvider = "testName", groups = {"delete"},
1202 dependsOnMethods = {"deleteContact"})
1203 public void deleteNonExistentContact(String testName) {
1204 // Submit the request to the service and store the response.
1205 PersonAuthorityClient client = new PersonAuthorityClient();
1206 setupDeleteNonExistent();
1207 ClientResponse<Response> res =
1208 client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1210 assertStatusCode(res, testName);
1213 res.releaseConnection();
1219 * Test contact submit request.
1221 @Test(dataProvider = "testName",
1222 dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
1223 public void testContactSubmitRequest(String testName) {
1225 // Expected status code: 200 OK
1226 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1228 // Submit the request to the service and store the response.
1229 String method = ServiceRequestType.READ.httpMethodName();
1230 String url = getContactResourceURL(knownResourceId,
1231 knownItemResourceId, knownContactResourceId);
1232 int statusCode = submitRequest(method, url);
1234 // Check the status code of the response: does it match
1235 // the expected response(s)?
1236 if (logger.isDebugEnabled()) {
1237 logger.debug("testContactSubmitRequest: url=" + url
1238 + " status=" + statusCode);
1240 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1244 // ---------------------------------------------------------------
1245 // Cleanup of resources created during testing
1246 // ---------------------------------------------------------------
1248 * Deletes all resources created by tests, after all tests have been run.
1250 * This cleanup method will always be run, even if one or more tests fail.
1251 * For this reason, it attempts to remove all resources created
1252 * at any point during testing, even if some of those resources
1253 * may be expected to be deleted by certain tests.
1255 @AfterClass(alwaysRun = true)
1257 public void cleanUp() {
1258 String noTest = System.getProperty("noTestCleanup");
1259 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1260 if (logger.isDebugEnabled()) {
1261 logger.debug("Skipping Cleanup phase ...");
1265 if (logger.isDebugEnabled()) {
1266 logger.debug("Cleaning up temporary resources created for testing ...");
1268 String parentResourceId;
1269 String itemResourceId;
1270 String contactResourceId;
1271 // Clean up contact resources.
1272 PersonAuthorityClient client = new PersonAuthorityClient();
1273 parentResourceId = knownResourceId;
1274 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
1275 contactResourceId = entry.getKey();
1276 itemResourceId = entry.getValue();
1277 // Note: Any non-success responses from the delete operation
1278 // below are ignored and not reported.
1279 ClientResponse<Response> res =
1280 client.deleteContact(parentResourceId, itemResourceId, contactResourceId);
1281 res.releaseConnection();
1283 // Clean up item resources.
1284 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1285 itemResourceId = entry.getKey();
1286 parentResourceId = entry.getValue();
1287 // Note: Any non-success responses from the delete operation
1288 // below are ignored and not reported.
1289 ClientResponse<Response> res =
1290 client.deleteItem(parentResourceId, itemResourceId);
1291 res.releaseConnection();
1293 // Clean up parent resources.
1297 // ---------------------------------------------------------------
1298 // Utility methods used by tests above
1299 // ---------------------------------------------------------------
1301 * Gets the contact service path component.
1303 * @return the contact service path component
1305 public String getContactServicePathComponent() {
1306 return ContactClient.SERVICE_PATH_COMPONENT;
1310 * Returns the root URL for the item service.
1312 * This URL consists of a base URL for all services, followed by
1313 * a path component for the owning parent, followed by the
1314 * path component for the items.
1316 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1317 * parent authority resource of the relevant item resource.
1319 * @return The root URL for the item service.
1321 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1322 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1326 * Returns the URL of a specific item resource managed by a service, and
1327 * designated by an identifier (such as a universally unique ID, or UUID).
1329 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1330 * parent authority resource of the relevant item resource.
1332 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1335 * @return The URL of a specific item resource managed by a service.
1337 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1338 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1342 * Returns the root URL for the contact service.
1344 * This URL consists of a base URL for all services, followed by
1345 * a path component for the owning authority, followed by the
1346 * path component for the owning item, followed by the path component
1347 * for the contact service.
1349 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1350 * parent authority resource of the relevant item resource.
1352 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1355 * @return The root URL for the contact service.
1357 protected String getContactServiceRootURL(String parentResourceIdentifier,
1358 String itemResourceIdentifier) {
1359 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1360 + getContactServicePathComponent();
1364 * Returns the URL of a specific contact resource managed by a service, and
1365 * designated by an identifier (such as a universally unique ID, or UUID).
1367 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1368 * parent resource of the relevant item resource.
1370 * @param resourceIdentifier An identifier (such as a UUID) for an
1373 * @return The URL of a specific resource managed by a service.
1375 protected String getContactResourceURL(String parentResourceIdentifier,
1376 String itemResourceIdentifier, String contactResourceIdentifier) {
1377 return getContactServiceRootURL(parentResourceIdentifier,
1378 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1382 public void authorityTests(String testName) {
1383 // TODO Auto-generated method stub
1388 protected PersonsCommon updateItemInstance(PersonsCommon authorityItem) {
1389 // TODO Auto-generated method stub
1394 protected void compareUpdatedItemInstances(PersonsCommon original,
1395 PersonsCommon updated) throws Exception {
1396 // TODO Auto-generated method stub
1401 protected PoxPayloadOut createInstance(String commonPartName,
1402 String identifier) {
1403 String shortId = identifier;
1404 String displayName = "displayName-" + shortId;
1405 PoxPayloadOut result =
1406 PersonAuthorityClientUtils.createPersonAuthorityInstance(
1407 displayName, shortId, commonPartName);
1412 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1413 String identifier) {
1414 String displayName = "displayName-NON_EXISTENT_ID";
1415 PoxPayloadOut result = PersonAuthorityClientUtils.createPersonAuthorityInstance(
1416 displayName, "NON_EXISTENT_SHORT_ID", commonPartName);
1420 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier) {
1421 Map<String, String> nonexMap = new HashMap<String, String>();
1422 nonexMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, "nonEX");
1423 nonexMap.put(PersonJAXBSchema.GENDER, "male");
1425 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
1426 PersonTermGroup term = new PersonTermGroup();
1427 term.setForeName("John");
1428 term.setSurName("Wayne");
1431 Map<String, List<String>> nonexRepeatablesMap = new HashMap<String, List<String>>();
1432 PoxPayloadOut result =
1433 PersonAuthorityClientUtils.createPersonInstance(NON_EXISTENT_ID,
1434 null, //PersonAuthorityClientUtils.createPersonAuthRefName(NON_EXISTENT_ID, null),
1435 nonexMap, terms, nonexRepeatablesMap, commonPartName);
1441 protected PersonauthoritiesCommon updateInstance(
1442 PersonauthoritiesCommon commonPartObject) {
1443 // TODO Auto-generated method stub
1448 protected void compareUpdatedInstances(PersonauthoritiesCommon original,
1449 PersonauthoritiesCommon updated) throws Exception {
1450 // TODO Auto-generated method stub
1454 protected void verifyReadItemInstance(PersonsCommon item) throws Exception {
1455 // Do nothing for now. Add more 'read' validation checks here if applicable.