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.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.OrganizationJAXBSchema;
33 import org.collectionspace.services.client.AbstractCommonListUtils;
34 import org.collectionspace.services.client.AuthorityClient;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.ContactClient;
37 import org.collectionspace.services.client.ContactClientUtils;
38 import org.collectionspace.services.client.PayloadOutputPart;
39 import org.collectionspace.services.client.PoxPayloadIn;
40 import org.collectionspace.services.client.PoxPayloadOut;
41 import org.collectionspace.services.contact.AddressGroup;
42 import org.collectionspace.services.contact.AddressGroupList;
43 import org.collectionspace.services.contact.ContactsCommon;
44 import org.collectionspace.services.client.OrgAuthorityClient;
45 import org.collectionspace.services.client.OrgAuthorityClientUtils;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
47 import org.collectionspace.services.organization.OrgauthoritiesCommon;
48 import org.collectionspace.services.organization.OrganizationsCommon;
49 import org.collectionspace.services.organization.OrgTermGroup;
50 import org.collectionspace.services.organization.OrgTermGroupList;
52 import org.jboss.resteasy.client.ClientResponse;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.testng.Assert;
57 import org.testng.annotations.AfterClass;
58 import org.testng.annotations.Test;
61 * OrgAuthorityServiceTest, carries out tests against a
62 * deployed and running OrgAuthority Service.
64 * $LastChangedRevision$
67 public class OrgAuthorityServiceTest extends AbstractAuthorityServiceTest<OrgauthoritiesCommon, OrganizationsCommon> {
70 private final String CLASS_NAME = OrgAuthorityServiceTest.class.getName();
71 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
74 public String getServicePathComponent() {
75 return OrgAuthorityClient.SERVICE_PATH_COMPONENT;
79 protected String getServiceName() {
80 return OrgAuthorityClient.SERVICE_NAME;
82 private final String TEST_SHORT_ID = "TestOrg";
83 private final String TEST_ORG_NAME = "Test Org";
84 private final String TEST_ORG_MAIN_BODY_NAME = "The real official test organization";
85 private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
86 // FIXME: Change this to a structured date once this field changes in the schema.
87 private final String TEST_ORG_FOUNDING_DATE = "May 26, 1907";
89 private String knownItemResourceShortIdentifer = null;
91 /** The known contact resource id. */
92 private String knownContactResourceId = null;
94 /** The all contact resource ids created. */
95 private Map<String, String> allContactResourceIdsCreated =
96 new HashMap<String, String>();
98 protected void setKnownItemResource(String id, String shortIdentifer) {
99 knownItemResourceId = id;
100 knownItemResourceShortIdentifer = shortIdentifer;
104 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
107 protected CollectionSpaceClient getClientInstance() {
108 return new OrgAuthorityClient();
112 protected PoxPayloadOut createInstance(String identifier) {
113 OrgAuthorityClient client = new OrgAuthorityClient();
114 String displayName = "displayName-" + identifier;
115 PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
116 displayName, identifier, client.getCommonPartName());
121 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
122 String headerLabel = new OrgAuthorityClient().getItemCommonPartName();
124 String shortId = TEST_SHORT_ID;
125 Map<String, String> testOrgMap = new HashMap<String, String>();
126 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
127 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
128 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
130 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
131 OrgTermGroup term = new OrgTermGroup();
132 term.setTermDisplayName(TEST_ORG_NAME);
133 term.setTermName(TEST_ORG_NAME);
134 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
137 return OrgAuthorityClientUtils.createOrganizationInstance(identifier, testOrgMap, terms, headerLabel);
141 * Creates the item in authority.
143 * @param vcsid the vcsid
144 * @param authRefName the auth ref name
147 private String createItemInAuthority(String vcsid, String authRefName) {
149 final String testName = "createItemInAuthority";
150 if (logger.isDebugEnabled()) {
151 logger.debug(testName + ":...");
154 // Submit the request to the service and store the response.
155 OrgAuthorityClient client = new OrgAuthorityClient();
157 String shortId = TEST_SHORT_ID;
158 Map<String, String> testOrgMap = new HashMap<String, String>();
159 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
160 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
161 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
163 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
164 OrgTermGroup term = new OrgTermGroup();
165 term.setTermDisplayName(TEST_ORG_NAME);
166 term.setTermName(TEST_ORG_NAME);
167 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
170 Map<String, List<String>> testOrgRepeatablesMap = new HashMap<String, List<String>>();
171 List<String> testOrgContactNames = new ArrayList<String>();
172 testOrgContactNames.add("joe@example.org");
173 testOrgContactNames.add("sally@example.org");
174 testOrgRepeatablesMap.put(OrganizationJAXBSchema.CONTACT_NAMES, testOrgContactNames);
176 String newID = OrgAuthorityClientUtils.createItemInAuthority(
177 vcsid, authRefName, testOrgMap, terms, testOrgRepeatablesMap, client);
179 // Store the ID returned from the first item resource created
180 // for additional tests below.
181 if (knownItemResourceId == null) {
182 setKnownItemResource(newID, shortId);
183 if (logger.isDebugEnabled()) {
184 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
188 // Store the IDs from any item resources created
189 // by tests, along with the IDs of their parents, so these items
190 // can be deleted after all tests have been run.
191 allResourceItemIdsCreated.put(newID, vcsid);
197 * Creates the contact.
199 * @param testName the test name
201 @Test(dataProvider = "testName", groups = {"create"},
202 dependsOnMethods = {"createItem"})
203 public void createContact(String testName) {
205 String newID = createContactInItem(knownResourceId, knownItemResourceId);
209 * Creates the contact in item.
211 * @param parentcsid the parentcsid
212 * @param itemcsid the itemcsid
215 private String createContactInItem(String parentcsid, String itemcsid) {
217 final String testName = "createContactInItem";
218 if (logger.isDebugEnabled()) {
219 logger.debug(getTestBanner(testName, CLASS_NAME));
223 // Submit the request to the service and store the response.
224 OrgAuthorityClient client = new OrgAuthorityClient();
225 String identifier = createIdentifier();
226 PoxPayloadOut multipart =
227 ContactClientUtils.createContactInstance(parentcsid,
228 itemcsid, identifier, new ContactClient().getCommonPartName());
231 ClientResponse<Response> res =
232 client.createContact(parentcsid, itemcsid, multipart);
234 assertStatusCode(res, testName);
235 newID = OrgAuthorityClientUtils.extractId(res);
238 res.releaseConnection();
242 // Store the ID returned from the first contact resource created
243 // for additional tests below.
244 if (knownContactResourceId == null) {
245 knownContactResourceId = newID;
246 if (logger.isDebugEnabled()) {
247 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
251 // Store the IDs from any contact resources created
252 // by tests, along with the IDs of their parent items,
253 // so these items can be deleted after all tests have been run.
254 allContactResourceIdsCreated.put(newID, itemcsid);
260 * Creates the contact list.
262 * @param testName the test name
263 * @throws Exception the exception
265 @Test(dataProvider = "testName", groups = {"createList"},
266 dependsOnMethods = {"createItemList"})
267 public void createContactList(String testName) throws Exception {
268 // Add contacts to the initially-created, known item record.
269 for (int j = 0; j < nItemsToCreateInList; j++) {
270 createContact(testName);
274 // ---------------------------------------------------------------
275 // CRUD tests : READ tests
276 // ---------------------------------------------------------------
279 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
282 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
283 // groups = {"read"}, dependsOnGroups = {"create"})
284 public void read(String testName) throws Exception {
285 readInternal(testName, knownResourceId, null);
291 * @param testName the test name
292 * @throws Exception the exception
294 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
295 // groups = {"read"}, dependsOnGroups = {"create"})
296 public void readByName(String testName) throws Exception {
297 readInternal(testName, null, knownResourceShortIdentifer);
300 protected void readInternal(String testName, String CSID, String shortId) {
304 // Submit the request to the service and store the response.
305 OrgAuthorityClient client = new OrgAuthorityClient();
308 res = client.read(CSID);
309 } else if (shortId != null) {
310 res = client.readByName(shortId);
312 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
315 assertStatusCode(res, testName);
316 //FIXME: remove the following try catch once Aron fixes signatures
318 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
319 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
320 new OrgAuthorityClient().getCommonPartName(), OrgauthoritiesCommon.class);
321 if (logger.isDebugEnabled()) {
322 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
324 Assert.assertNotNull(orgAuthority);
325 } catch (Exception e) {
326 throw new RuntimeException(e);
336 * Read item in Named Auth.
338 * TODO Enable this if we really need this - it is a funky case, where we would have
339 * the shortId of the item, but the CSID of the parent authority!? Unlikely.
341 * @param testName the test name
342 * @throws Exception the exception
343 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
344 groups = {"readItem"}, dependsOnGroups = {"read"})
345 public void readItemInNamedAuth(String testName) throws Exception {
346 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
353 * @param testName the test name
354 * @throws Exception the exception
356 @Test(dataProvider = "testName", groups = {"readItem"},
357 dependsOnMethods = {"readItemInNamedAuth"})
358 public void readNamedItem(String testName) throws Exception {
359 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
363 * Read item in Named Auth.
365 * @param testName the test name
366 * @throws Exception the exception
368 @Test(dataProvider = "testName", groups = {"readItem"},
369 dependsOnMethods = {"readItem"})
370 public void readItemInNamedAuth(String testName) throws Exception {
371 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
375 * Read Named item in Named Auth.
377 * @param testName the test name
378 * @throws Exception the exception
380 @Test(dataProvider = "testName", groups = {"readItem"},
381 dependsOnMethods = {"readItem"})
382 public void readNamedItemInNamedAuth(String testName) throws Exception {
383 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
386 protected void readItemInternal(String testName,
387 String authCSID, String authShortId, String itemCSID, String itemShortId)
392 // Submit the request to the service and store the response.
393 OrgAuthorityClient client = new OrgAuthorityClient();
395 if (authCSID != null) {
396 if (itemCSID != null) {
397 res = client.readItem(authCSID, itemCSID);
398 } else if (itemShortId != null) {
399 res = client.readNamedItem(authCSID, itemShortId);
401 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
403 } else if (authShortId != null) {
404 if (itemCSID != null) {
405 res = client.readItemInNamedAuthority(authShortId, itemCSID);
406 } else if (itemShortId != null) {
407 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
409 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
412 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
415 assertStatusCode(res, testName);
416 // Check whether we've received a organization.
417 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
418 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
419 client.getItemCommonPartName(), OrganizationsCommon.class);
420 Assert.assertNotNull(organization);
421 boolean showFull = true;
422 if (showFull && logger.isDebugEnabled()) {
423 logger.debug(testName + ": returned payload:");
424 logger.debug(objectAsXmlString(organization, OrganizationsCommon.class));
427 // Check that the organization item is within the expected OrgAuthority.
428 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
430 // Verify the number and contents of values in a repeatable field,
431 // as created in the instance record used for testing.
432 List<String> contactNames = organization.getContactNames().getContactName();
433 Assert.assertTrue(contactNames.size() > 0);
434 Assert.assertNotNull(contactNames.get(0));
444 protected void verifyReadItemInstance(OrganizationsCommon item) throws Exception {
445 List<String> contactNames = item.getContactNames().getContactName();
446 Assert.assertTrue(contactNames.size() > 0);
447 Assert.assertNotNull(contactNames.get(0));
451 * Verify illegal item display name.
453 * @param testName the test name
454 * @throws Exception the exception
456 @Test(dataProvider = "testName")
457 public void verifyIllegalItemDisplayName(String testName) throws Exception {
458 // Perform setup for read.
461 // First read our known resource.
463 OrgAuthorityClient client = new OrgAuthorityClient();
464 Response res = client.readItem(knownResourceId, knownItemResourceId);
465 OrganizationsCommon organization = null;
467 assertStatusCode(res, testName);
468 // Check whether organization has expected displayName.
469 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
470 organization = (OrganizationsCommon) extractPart(input,
471 client.getItemCommonPartName(), OrganizationsCommon.class);
472 Assert.assertNotNull(organization);
480 // Make an invalid UPDATE request, without a display name
482 OrgTermGroupList termList = organization.getOrgTermGroupList();
483 Assert.assertNotNull(termList);
484 List<OrgTermGroup> terms = termList.getOrgTermGroup();
485 Assert.assertNotNull(terms);
486 Assert.assertTrue(terms.size() > 0);
487 terms.get(0).setTermDisplayName(null);
488 terms.get(0).setTermName(null);
490 setupUpdateWithInvalidBody(); // we expect a failure
491 // Submit the updated resource to the service and store the response.
492 PoxPayloadOut output = new PoxPayloadOut(OrgAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
493 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(),
495 res = client.updateItem(knownResourceId, knownItemResourceId, output);
497 assertStatusCode(res, testName);
508 * @param testName the test name
509 * @throws Exception the exception
511 @Test(dataProvider = "testName", groups = {"readItem"},
512 dependsOnMethods = {"readItem"})
513 public void readContact(String testName) throws Exception {
517 // Submit the request to the service and store the response.
518 OrgAuthorityClient client = new OrgAuthorityClient();
519 ClientResponse<String> res =
520 client.readContact(knownResourceId, knownItemResourceId,
521 knownContactResourceId);
523 assertStatusCode(res, testName);
524 // Check whether we've received a contact.
525 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
526 ContactsCommon contact = (ContactsCommon) extractPart(input,
527 new ContactClient().getCommonPartName(), ContactsCommon.class);
528 Assert.assertNotNull(contact);
529 boolean showFull = true;
530 if (showFull && logger.isDebugEnabled()) {
531 logger.debug(testName + ": returned payload:");
532 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
534 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
535 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
544 * Read contact non existent.
546 * @param testName the test name
548 @Test(dataProvider = "testName", groups = {"readItem"},
549 dependsOnMethods = {"readContact"})
550 public void readContactNonExistent(String testName) {
552 setupReadNonExistent();
554 // Submit the request to the service and store the response.
555 OrgAuthorityClient client = new OrgAuthorityClient();
556 ClientResponse<String> res =
557 client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
559 int statusCode = res.getStatus();
561 // Check the status code of the response: does it match
562 // the expected response(s)?
563 if (logger.isDebugEnabled()) {
564 logger.debug(testName + ": status = " + statusCode);
566 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
567 invalidStatusCodeMessage(testRequestType, statusCode));
568 Assert.assertEquals(statusCode, testExpectedStatusCode);
580 // @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
581 public void readItemList(String testName) {
582 readItemList(knownAuthorityWithItems, null);
586 * Read item list by authority name.
589 // @Test(dataProvider = "testName",
590 // dependsOnMethods = {"readItem"})
591 public void readItemListByName(String testName) {
592 readItemList(null, READITEMS_SHORT_IDENTIFIER);
598 * @param vcsid the vcsid
599 * @param name the name
601 private void readItemList(String vcsid, String name) {
603 final String testName = "readItemList";
607 // Submit the request to the service and store the response.
608 OrgAuthorityClient client = new OrgAuthorityClient();
609 ClientResponse<AbstractCommonList> res = null;
611 res = client.readItemList(vcsid, null, null);
612 } else if (name != null) {
613 res = client.readItemListForNamedAuthority(name, null, null);
615 Assert.fail("readItemList passed null csid and name!");
618 AbstractCommonList list = null;
620 assertStatusCode(res, testName);
621 list = res.getEntity();
628 List<AbstractCommonList.ListItem> items = list.getListItem();
629 int nItemsReturned = items.size();
630 // There will be 'nItemsToCreateInList'
631 // items created by the createItemList test,
632 // all associated with the same parent resource.
633 int nExpectedItems = nItemsToCreateInList;
634 if (logger.isDebugEnabled()) {
635 logger.debug(testName + ": Expected "
636 + nExpectedItems + " items; got: " + nItemsReturned);
638 Assert.assertEquals(nItemsReturned, nExpectedItems);
640 for (AbstractCommonList.ListItem item : items) {
642 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.REF_NAME);
643 Assert.assertTrue((null != value), "Item refName is null!");
645 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.TERM_DISPLAY_NAME);
646 Assert.assertTrue((null != value), "Item termDisplayName is null!");
648 if (logger.isTraceEnabled()) {
649 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
656 @Test(groups = {"readList"},
657 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
658 public void readContactList() {
659 readContactList(knownResourceId, knownItemResourceId);
665 * @param parentcsid the parentcsid
666 * @param itemcsid the itemcsid
668 private void readContactList(String parentcsid, String itemcsid) {
669 final String testName = "readContactList";
673 // Submit the request to the service and store the response.
674 OrgAuthorityClient client = new OrgAuthorityClient();
675 ClientResponse<AbstractCommonList> res =
676 client.readContactList(parentcsid, itemcsid);
677 AbstractCommonList list = null;
679 assertStatusCode(res, testName);
680 list = res.getEntity();
687 List<AbstractCommonList.ListItem> listitems =
689 int nItemsReturned = listitems.size();
690 // There will be one item created, associated with a
691 // known parent resource, by the createItem test.
693 // In addition, there will be 'nItemsToCreateInList'
694 // additional items created by the createItemList test,
695 // all associated with the same parent resource.
696 int nExpectedItems = nItemsToCreateInList + 1;
697 if (logger.isDebugEnabled()) {
698 logger.debug(testName + ": Expected "
699 + nExpectedItems + " items; got: " + nItemsReturned);
701 Assert.assertEquals(nItemsReturned, nExpectedItems);
703 // Optionally output additional data about list members for debugging.
704 boolean iterateThroughList = false;
705 if (iterateThroughList && logger.isDebugEnabled()) {
706 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
713 * @param testName the test name
714 * @throws Exception the exception
716 @Test(dataProvider = "testName", groups = {"update"},
717 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
718 public void updateContact(String testName) throws Exception {
722 // Retrieve the contents of a resource to update.
723 OrgAuthorityClient client = new OrgAuthorityClient();
724 ClientResponse<String> res =
725 client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
726 ContactsCommon contact = null;
728 assertStatusCode(res, testName);
729 if (logger.isDebugEnabled()) {
730 logger.debug("got Contact to update with ID: "
731 + knownContactResourceId
732 + " in item: " + knownItemResourceId
733 + " in parent: " + knownResourceId);
735 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
736 contact = (ContactsCommon) extractPart(input,
737 new ContactClient().getCommonPartName(), ContactsCommon.class);
738 Assert.assertNotNull(contact);
745 // Verify the contents of this resource
746 AddressGroupList addressGroupList = contact.getAddressGroupList();
747 Assert.assertNotNull(addressGroupList);
748 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
749 Assert.assertNotNull(addressGroups);
750 Assert.assertTrue(addressGroups.size() > 0);
751 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
752 Assert.assertNotNull(addressPlace1);
754 // Update the contents of this resource.
755 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
756 contact.setAddressGroupList(addressGroupList);
757 if (logger.isDebugEnabled()) {
758 logger.debug("to be updated Contact");
759 logger.debug(objectAsXmlString(contact,
760 ContactsCommon.class));
763 // Submit the updated resource to the service and store the response.
764 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
765 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
766 commonPart.setLabel(new ContactClient().getCommonPartName());
768 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
770 assertStatusCode(res, testName);
771 // Retrieve the updated resource and verify that its contents exist.
772 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
773 ContactsCommon updatedContact =
774 (ContactsCommon) extractPart(input,
775 new ContactClient().getCommonPartName(), ContactsCommon.class);
776 Assert.assertNotNull(updatedContact);
778 // Verify that the updated resource received the correct data.
779 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
780 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
781 "Data in updated object did not match submitted data.");
790 * Update non existent contact.
792 * @param testName the test name
793 * @throws Exception the exception
795 @Test(dataProvider = "testName", groups = {"update"},
796 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
797 public void updateNonExistentContact(String testName) throws Exception {
798 // Currently a no-op test
801 // ---------------------------------------------------------------
802 // CRUD tests : DELETE tests
803 // ---------------------------------------------------------------
805 // Note: delete sub-resources in ascending hierarchical order,
806 // before deleting their parents.
810 * @param testName the test name
811 * @throws Exception the exception
813 @Test(dataProvider = "testName", groups = {"delete"},
814 dependsOnMethods = {"updateContact"})
815 public void deleteContact(String testName) throws Exception {
819 if (logger.isDebugEnabled()) {
820 logger.debug("parentcsid =" + knownResourceId
821 + " itemcsid = " + knownItemResourceId
822 + " csid = " + knownContactResourceId);
825 // Submit the request to the service and store the response.
826 OrgAuthorityClient client = new OrgAuthorityClient();
827 ClientResponse<Response> res =
828 client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
830 assertStatusCode(res, testName);
839 public void delete(String testName) throws Exception {
840 // Do nothing. See localDelete(). This ensure proper test order.
843 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
844 public void localDelete(String testName) throws Exception {
845 super.delete(testName);
849 public void deleteItem(String testName) throws Exception {
850 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
851 // its dependencies will get run first and then we can call the base class' delete method.
854 @Test(dataProvider = "testName", groups = {"delete"},
855 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
856 public void localDeleteItem(String testName) throws Exception {
857 super.deleteItem(testName);
861 * Delete non existent contact.
863 * @param testName the test name
865 @Test(dataProvider = "testName", groups = {"delete"},
866 dependsOnMethods = {"deleteContact"})
867 public void deleteNonExistentContact(String testName) {
869 setupDeleteNonExistent();
871 // Submit the request to the service and store the response.
872 OrgAuthorityClient client = new OrgAuthorityClient();
873 ClientResponse<Response> res =
874 client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
876 assertStatusCode(res, testName);
885 * Test contact submit request.
887 @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
888 public void testContactSubmitRequest() {
890 // Expected status code: 200 OK
891 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
893 // Submit the request to the service and store the response.
894 String method = ServiceRequestType.READ.httpMethodName();
895 String url = getContactResourceURL(knownResourceId,
896 knownItemResourceId, knownContactResourceId);
897 int statusCode = submitRequest(method, url);
899 // Check the status code of the response: does it match
900 // the expected response(s)?
901 if (logger.isDebugEnabled()) {
902 logger.debug("testContactSubmitRequest: url=" + url
903 + " status=" + statusCode);
905 Assert.assertEquals(statusCode, EXPECTED_STATUS);
909 // ---------------------------------------------------------------
910 // Cleanup of resources created during testing
911 // ---------------------------------------------------------------
913 * Deletes all resources created by tests, after all tests have been run.
915 * This cleanup method will always be run, even if one or more tests fail.
916 * For this reason, it attempts to remove all resources created
917 * at any point during testing, even if some of those resources
918 * may be expected to be deleted by certain tests.
920 @AfterClass(alwaysRun = true)
922 public void cleanUp() {
923 String noTest = System.getProperty("noTestCleanup");
924 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
925 if (logger.isDebugEnabled()) {
926 logger.debug("Skipping Cleanup phase ...");
930 if (logger.isDebugEnabled()) {
931 logger.debug("Cleaning up temporary resources created for testing ...");
934 String parentResourceId;
935 String itemResourceId;
936 String contactResourceId;
937 // Clean up contact resources.
938 parentResourceId = knownResourceId;
939 OrgAuthorityClient client = new OrgAuthorityClient();
940 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
941 contactResourceId = entry.getKey();
942 itemResourceId = entry.getValue();
943 // Note: Any non-success responses from the delete operation
944 // below are ignored and not reported.
945 client.deleteContact(parentResourceId, itemResourceId, contactResourceId).close();
947 // Clean up item resources.
948 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
949 itemResourceId = entry.getKey();
950 parentResourceId = entry.getValue();
951 // Note: Any non-success responses from the delete operation
952 // below are ignored and not reported.
953 client.deleteItem(parentResourceId, itemResourceId).close();
955 // Clean up parent resources.
960 // ---------------------------------------------------------------
961 // Utility methods used by tests above
962 // ---------------------------------------------------------------
964 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
967 * Gets the item service path component.
969 * @return the item service path component
971 public String getItemServicePathComponent() {
972 return AuthorityClient.ITEMS;
976 * Gets the contact service path component.
978 * @return the contact service path component
980 public String getContactServicePathComponent() {
981 return ContactClient.SERVICE_PATH_COMPONENT;
985 * Returns the root URL for the item service.
987 * This URL consists of a base URL for all services, followed by
988 * a path component for the owning parent, followed by the
989 * path component for the items.
991 * @param parentResourceIdentifier An identifier (such as a UUID) for the
992 * parent authority resource of the relevant item resource.
994 * @return The root URL for the item service.
996 protected String getItemServiceRootURL(String parentResourceIdentifier) {
997 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1001 * Returns the URL of a specific item resource managed by a service, and
1002 * designated by an identifier (such as a universally unique ID, or UUID).
1004 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1005 * parent authority resource of the relevant item resource.
1007 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1010 * @return The URL of a specific item resource managed by a service.
1012 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1013 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1017 * Returns the root URL for the contact service.
1019 * This URL consists of a base URL for all services, followed by
1020 * a path component for the owning authority, followed by the
1021 * path component for the owning item, followed by the path component
1022 * for the contact service.
1024 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1025 * parent authority resource of the relevant item resource.
1027 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1030 * @return The root URL for the contact service.
1032 protected String getContactServiceRootURL(String parentResourceIdentifier,
1033 String itemResourceIdentifier) {
1034 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1035 + getContactServicePathComponent();
1039 * Returns the URL of a specific contact resource managed by a service, and
1040 * designated by an identifier (such as a universally unique ID, or UUID).
1042 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1043 * parent resource of the relevant item resource.
1045 * @param resourceIdentifier An identifier (such as a UUID) for an
1048 * @return The URL of a specific resource managed by a service.
1050 protected String getContactResourceURL(String parentResourceIdentifier,
1051 String itemResourceIdentifier, String contactResourceIdentifier) {
1052 return getContactServiceRootURL(parentResourceIdentifier,
1053 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1057 public void authorityTests(String testName) {
1058 // TODO Auto-generated method stub
1063 protected String createItemInAuthority(String authorityId) {
1064 return createItemInAuthority(authorityId, null /*refname*/);
1068 protected OrganizationsCommon updateItemInstance(OrganizationsCommon organizationsCommon) {
1070 OrgTermGroupList termList = organizationsCommon.getOrgTermGroupList();
1071 Assert.assertNotNull(termList);
1072 List<OrgTermGroup> terms = termList.getOrgTermGroup();
1073 Assert.assertNotNull(terms);
1074 Assert.assertTrue(terms.size() > 0);
1075 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1076 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1077 organizationsCommon.setOrgTermGroupList(termList);
1079 return organizationsCommon;
1083 protected void compareUpdatedItemInstances(OrganizationsCommon original,
1084 OrganizationsCommon updated) throws Exception {
1086 OrgTermGroupList originalTermList = original.getOrgTermGroupList();
1087 Assert.assertNotNull(originalTermList);
1088 List<OrgTermGroup> originalTerms = originalTermList.getOrgTermGroup();
1089 Assert.assertNotNull(originalTerms);
1090 Assert.assertTrue(originalTerms.size() > 0);
1092 OrgTermGroupList updatedTermList = updated.getOrgTermGroupList();
1093 Assert.assertNotNull(updatedTermList);
1094 List<OrgTermGroup> updatedTerms = updatedTermList.getOrgTermGroup();
1095 Assert.assertNotNull(updatedTerms);
1096 Assert.assertTrue(updatedTerms.size() > 0);
1098 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1099 originalTerms.get(0).getTermDisplayName(),
1100 "Value in updated record did not match submitted data.");
1104 protected PoxPayloadOut createInstance(String commonPartName,
1105 String identifier) {
1106 String shortId = identifier;
1107 String displayName = "displayName-" + shortId;
1108 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1109 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1110 displayName, shortId, commonPartName);
1115 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1116 String identifier) {
1117 String shortId = identifier;
1118 String displayName = "displayName-" + shortId;
1119 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1120 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1121 displayName, shortId, commonPartName);
1125 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName,
1126 String identifier) {
1127 Map<String, String> nonexOrgMap = new HashMap<String, String>();
1128 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
1129 PoxPayloadOut result =
1130 OrgAuthorityClientUtils.createOrganizationInstance(
1131 knownResourceRefName,
1132 nonexOrgMap, OrgAuthorityClientUtils.getTermGroupInstance(TEST_ORG_NAME), commonPartName);
1137 protected OrgauthoritiesCommon updateInstance(OrgauthoritiesCommon orgauthoritiesCommon) {
1138 OrgauthoritiesCommon result = new OrgauthoritiesCommon();
1140 result.setDisplayName("updated-" + orgauthoritiesCommon.getDisplayName());
1141 result.setVocabType("updated-" + orgauthoritiesCommon.getVocabType());
1147 protected void compareUpdatedInstances(OrgauthoritiesCommon original,
1148 OrgauthoritiesCommon updated) throws Exception {
1149 // Verify that the updated resource received the correct data.
1150 Assert.assertEquals(updated.getDisplayName(),
1151 original.getDisplayName(),
1152 "Display name in updated object did not match submitted data.");