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.MediaType;
31 import javax.ws.rs.core.Response;
33 import org.collectionspace.services.OrganizationJAXBSchema;
34 import org.collectionspace.services.client.AbstractCommonListUtils;
35 import org.collectionspace.services.client.AuthorityClient;
36 import org.collectionspace.services.client.CollectionSpaceClient;
37 import org.collectionspace.services.client.ContactClient;
38 import org.collectionspace.services.client.ContactClientUtils;
39 import org.collectionspace.services.client.PayloadOutputPart;
40 import org.collectionspace.services.client.PoxPayloadIn;
41 import org.collectionspace.services.client.PoxPayloadOut;
42 import org.collectionspace.services.contact.AddressGroup;
43 import org.collectionspace.services.contact.AddressGroupList;
44 import org.collectionspace.services.contact.ContactsCommon;
45 import org.collectionspace.services.client.OrganizationClient;
46 import org.collectionspace.services.client.OrgAuthorityClientUtils;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
48 import org.collectionspace.services.organization.OrgauthoritiesCommon;
49 import org.collectionspace.services.organization.OrganizationsCommon;
50 import org.collectionspace.services.organization.ContactGroup;
51 import org.collectionspace.services.organization.OrgTermGroup;
52 import org.collectionspace.services.organization.OrgTermGroupList;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.testng.Assert;
56 import org.testng.annotations.AfterClass;
57 import org.testng.annotations.Test;
60 * OrgAuthorityServiceTest, carries out tests against a
61 * deployed and running OrgAuthority Service.
63 * $LastChangedRevision$
66 public class OrgAuthorityServiceTest extends AbstractAuthorityServiceTest<OrgauthoritiesCommon, OrganizationsCommon> {
69 private final String CLASS_NAME = OrgAuthorityServiceTest.class.getName();
70 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
73 * Default constructor. Used to set the short ID for all tests authority items
75 public OrgAuthorityServiceTest() {
77 TEST_SHORTID = "TestOrg";
81 public String getServicePathComponent() {
82 return OrganizationClient.SERVICE_PATH_COMPONENT;
86 protected String getServiceName() {
87 return OrganizationClient.SERVICE_NAME;
90 private final String TEST_ORG_NAME = "Test Org";
91 private final String TEST_ORG_MAIN_BODY_NAME = "The real official test organization";
92 private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
93 private final String TEST_ORG_FOUNDING_DATE = "May 26, 1907";
95 /** The known item resource short ID. */
96 private String knownItemResourceShortIdentifer = null;
98 /** The known contact resource id. */
99 private String knownContactResourceId = null;
101 /** The all contact resource ids created. */
102 private Map<String, String> allContactResourceIdsCreated = new HashMap<String, String>();
107 protected void setKnownItemResource(String id, String shortIdentifer) {
108 knownItemResourceId = id;
109 knownItemResourceShortIdentifer = shortIdentifer;
113 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
116 protected CollectionSpaceClient getClientInstance() throws Exception {
117 return new OrganizationClient();
121 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
122 return new OrganizationClient(clientPropertiesFilename);
126 protected PoxPayloadOut createInstance(String identifier) throws Exception {
127 OrganizationClient client = new OrganizationClient();
128 String displayName = "displayName-" + identifier;
129 PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
130 displayName, identifier, client.getCommonPartName());
135 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) throws Exception {
136 String headerLabel = new OrganizationClient().getItemCommonPartName();
138 String shortId = TEST_SHORTID + identifier;
139 Map<String, String> testOrgMap = new HashMap<String, String>();
140 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
141 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
142 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
144 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
145 OrgTermGroup term = new OrgTermGroup();
146 term.setTermDisplayName(TEST_ORG_NAME);
147 term.setTermName(TEST_ORG_NAME);
148 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
151 return OrgAuthorityClientUtils.createOrganizationInstance(identifier, testOrgMap, terms, headerLabel);
155 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
156 return createItemInAuthority(client, authorityId, shortId, null /*refname*/);
160 * Creates the item in authority.
162 * @param vcsid the vcsid
163 * @param authRefName the auth ref name
166 private String createItemInAuthority(AuthorityClient client, String vcsid, String shortId, String authRefName) {
168 final String testName = "createItemInAuthority";
169 if (logger.isDebugEnabled()) {
170 logger.debug(testName + ":...");
173 Map<String, String> testOrgMap = new HashMap<String, String>();
174 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
175 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
176 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
178 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
179 OrgTermGroup term = new OrgTermGroup();
180 term.setTermDisplayName(TEST_ORG_NAME);
181 term.setTermName(TEST_ORG_NAME);
182 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
185 Map<String, List<String>> testOrgRepeatablesMap = new HashMap<String, List<String>>();
186 List<String> testOrgContactNames = new ArrayList<String>();
187 testOrgContactNames.add("joe@example.org");
188 testOrgContactNames.add("sally@example.org");
189 testOrgRepeatablesMap.put(OrganizationJAXBSchema.CONTACT_NAMES, testOrgContactNames);
191 String newID = OrgAuthorityClientUtils.createItemInAuthority(
192 vcsid, authRefName, testOrgMap, terms, testOrgRepeatablesMap, (OrganizationClient) client);
194 // Store the ID returned from the first item resource created
195 // for additional tests below.
196 if (knownItemResourceId == null) {
197 setKnownItemResource(newID, shortId);
198 if (logger.isDebugEnabled()) {
199 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
203 // Store the IDs from any item resources created
204 // by tests, along with the IDs of their parents, so these items
205 // can be deleted after all tests have been run.
206 allResourceItemIdsCreated.put(newID, vcsid);
212 * Creates the contact.
214 * @param testName the test name
217 @Test(dataProvider = "testName", groups = {"create"},
218 dependsOnMethods = {"createItem"})
219 public void createContact(String testName) throws Exception {
221 String newID = createContactInItem(knownResourceId, knownItemResourceId);
225 * Creates the contact in item.
227 * @param parentcsid the parentcsid
228 * @param itemcsid the itemcsid
232 private String createContactInItem(String parentcsid, String itemcsid) throws Exception {
234 final String testName = "createContactInItem";
235 if (logger.isDebugEnabled()) {
236 logger.debug(getTestBanner(testName, CLASS_NAME));
240 // Submit the request to the service and store the response.
241 OrganizationClient client = new OrganizationClient();
242 String identifier = createIdentifier();
243 PoxPayloadOut multipart =
244 ContactClientUtils.createContactInstance(parentcsid,
245 itemcsid, identifier, new ContactClient().getCommonPartName());
248 Response res = client.createContact(parentcsid, itemcsid, multipart);
250 assertStatusCode(res, testName);
251 newID = OrgAuthorityClientUtils.extractId(res);
258 // Store the ID returned from the first contact resource created
259 // for additional tests below.
260 if (knownContactResourceId == null) {
261 knownContactResourceId = newID;
262 if (logger.isDebugEnabled()) {
263 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
267 // Store the IDs from any contact resources created
268 // by tests, along with the IDs of their parent items,
269 // so these items can be deleted after all tests have been run.
270 allContactResourceIdsCreated.put(newID, itemcsid);
276 * Creates the contact list.
278 * @param testName the test name
279 * @throws Exception the exception
281 @Test(dataProvider = "testName", groups = {"createList"},
282 dependsOnMethods = {"createItemList"})
283 public void createContactList(String testName) throws Exception {
284 // Add contacts to the initially-created, known item record.
285 for (int j = 0; j < nItemsToCreateInList; j++) {
286 createContact(testName);
290 // ---------------------------------------------------------------
291 // CRUD tests : READ tests
292 // ---------------------------------------------------------------
295 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
298 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
299 // groups = {"read"}, dependsOnGroups = {"create"})
300 public void read(String testName) throws Exception {
301 readInternal(testName, knownResourceId, null);
307 * @param testName the test name
308 * @throws Exception the exception
310 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
311 // groups = {"read"}, dependsOnGroups = {"create"})
312 public void readByName(String testName) throws Exception {
313 readInternal(testName, null, knownResourceShortIdentifer);
316 protected void readInternal(String testName, String CSID, String shortId) throws Exception {
320 // Submit the request to the service and store the response.
321 OrganizationClient client = new OrganizationClient();
324 res = client.read(CSID);
325 } else if (shortId != null) {
326 res = client.readByName(shortId);
328 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
331 assertStatusCode(res, testName);
332 //FIXME: remove the following try catch once Aron fixes signatures
334 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
335 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
336 new OrganizationClient().getCommonPartName(), OrgauthoritiesCommon.class);
337 if (logger.isDebugEnabled()) {
338 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
340 Assert.assertNotNull(orgAuthority);
341 } catch (Exception e) {
342 throw new RuntimeException(e);
352 * Read item in Named Auth.
354 * TODO Enable this if we really need this - it is a funky case, where we would have
355 * the shortId of the item, but the CSID of the parent authority!? Unlikely.
357 * @param testName the test name
358 * @throws Exception the exception
359 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
360 groups = {"readItem"}, dependsOnGroups = {"read"})
361 public void readItemInNamedAuth(String testName) throws Exception {
362 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
369 * @param testName the test name
370 * @throws Exception the exception
372 @Test(dataProvider = "testName", groups = {"readItem"},
373 dependsOnMethods = {"readItemInNamedAuth"})
374 public void readNamedItem(String testName) throws Exception {
375 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
379 * Read item in Named Auth.
381 * @param testName the test name
382 * @throws Exception the exception
384 @Test(dataProvider = "testName", groups = {"readItem"},
385 dependsOnMethods = {"readItem"})
386 public void readItemInNamedAuth(String testName) throws Exception {
387 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
391 * Read Named item in Named Auth.
393 * @param testName the test name
394 * @throws Exception the exception
396 @Test(dataProvider = "testName", groups = {"readItem"},
397 dependsOnMethods = {"readItem"})
398 public void readNamedItemInNamedAuth(String testName) throws Exception {
399 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
402 protected void readItemInternal(String testName,
403 String authCSID, String authShortId, String itemCSID, String itemShortId)
408 // Submit the request to the service and store the response.
409 OrganizationClient client = new OrganizationClient();
411 if (authCSID != null) {
412 if (itemCSID != null) {
413 res = client.readItem(authCSID, itemCSID);
414 } else if (itemShortId != null) {
415 res = client.readNamedItem(authCSID, itemShortId);
417 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
419 } else if (authShortId != null) {
420 if (itemCSID != null) {
421 res = client.readItemInNamedAuthority(authShortId, itemCSID);
422 } else if (itemShortId != null) {
423 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
425 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
428 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
431 assertStatusCode(res, testName);
432 // Check whether we've received a organization.
433 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
434 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
435 client.getItemCommonPartName(), OrganizationsCommon.class);
436 Assert.assertNotNull(organization);
437 boolean showFull = true;
438 if (showFull && logger.isDebugEnabled()) {
439 logger.debug(testName + ": returned payload:");
440 logger.debug(objectAsXmlString(organization, OrganizationsCommon.class));
443 // Check that the organization item is within the expected OrgAuthority.
444 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
446 // Verify the number and contents of values in a repeatable field,
447 // as created in the instance record used for testing.
449 List<ContactGroup> contactGroups = organization.getContactGroupList().getContactGroup();
450 Assert.assertTrue(contactGroups.size() > 0);
451 Assert.assertNotNull(contactGroups.get(0).getContactName());
460 protected void verifyReadItemInstance(OrganizationsCommon item) throws Exception {
461 List<ContactGroup> contactGroups = item.getContactGroupList().getContactGroup();
462 Assert.assertTrue(contactGroups.size() > 0);
463 Assert.assertNotNull(contactGroups.get(0).getContactName());
467 * Verify illegal item display name.
469 * @param testName the test name
470 * @throws Exception the exception
472 @Test(dataProvider = "testName")
473 public void verifyIllegalItemDisplayName(String testName) throws Exception {
474 // Perform setup for read.
477 // First read our known resource.
479 OrganizationClient client = new OrganizationClient();
480 Response res = client.readItem(knownResourceId, knownItemResourceId);
481 OrganizationsCommon organization = null;
483 assertStatusCode(res, testName);
484 // Check whether organization has expected displayName.
485 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
486 organization = (OrganizationsCommon) extractPart(input,
487 client.getItemCommonPartName(), OrganizationsCommon.class);
488 Assert.assertNotNull(organization);
496 // Make an invalid UPDATE request, without a display name
498 OrgTermGroupList termList = organization.getOrgTermGroupList();
499 Assert.assertNotNull(termList);
500 List<OrgTermGroup> terms = termList.getOrgTermGroup();
501 Assert.assertNotNull(terms);
502 Assert.assertTrue(terms.size() > 0);
503 terms.get(0).setTermDisplayName(null);
504 terms.get(0).setTermName(null);
506 setupUpdateWithInvalidBody(); // we expect a failure
507 // Submit the updated resource to the service and store the response.
508 PoxPayloadOut output = new PoxPayloadOut(OrganizationClient.SERVICE_ITEM_PAYLOAD_NAME);
509 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(),
511 res = client.updateItem(knownResourceId, knownItemResourceId, output);
513 assertStatusCode(res, testName);
524 * @param testName the test name
525 * @throws Exception the exception
527 @Test(dataProvider = "testName", groups = {"readItem"},
528 dependsOnMethods = {"readItem"})
529 public void readContact(String testName) throws Exception {
533 // Submit the request to the service and store the response.
534 OrganizationClient client = new OrganizationClient();
535 Response res =client.readContact(knownResourceId, knownItemResourceId,
536 knownContactResourceId);
538 assertStatusCode(res, testName);
539 // Check whether we've received a contact.
540 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
541 ContactsCommon contact = (ContactsCommon) extractPart(input,
542 new ContactClient().getCommonPartName(), ContactsCommon.class);
543 Assert.assertNotNull(contact);
544 boolean showFull = true;
545 if (showFull && logger.isDebugEnabled()) {
546 logger.debug(testName + ": returned payload:");
547 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
549 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
550 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
559 * Read contact non existent.
561 * @param testName the test name
564 @Test(dataProvider = "testName", groups = {"readItem"},
565 dependsOnMethods = {"readContact"})
566 public void readContactNonExistent(String testName) throws Exception {
568 setupReadNonExistent();
570 // Submit the request to the service and store the response.
571 OrganizationClient client = new OrganizationClient();
572 Response res = client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
574 int statusCode = res.getStatus();
576 // Check the status code of the response: does it match
577 // the expected response(s)?
578 if (logger.isDebugEnabled()) {
579 logger.debug(testName + ": status = " + statusCode);
581 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
582 invalidStatusCodeMessage(testRequestType, statusCode));
583 Assert.assertEquals(statusCode, testExpectedStatusCode);
596 // @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
597 public void readItemList(String testName) throws Exception {
598 readItemList(knownAuthorityWithItems, null);
602 * Read item list by authority name.
606 // @Test(dataProvider = "testName",
607 // dependsOnMethods = {"readItem"})
608 public void readItemListByName(String testName) throws Exception {
609 readItemList(null, READITEMS_SHORT_IDENTIFIER);
615 * @param vcsid the vcsid
616 * @param name the name
619 private void readItemList(String vcsid, String name) throws Exception {
621 final String testName = "readItemList";
625 // Submit the request to the service and store the response.
626 OrganizationClient client = new OrganizationClient();
629 res = client.readItemList(vcsid, null, null);
630 } else if (name != null) {
631 res = client.readItemListForNamedAuthority(name, null, null);
633 Assert.fail("readItemList passed null csid and name!");
636 AbstractCommonList list = null;
638 assertStatusCode(res, testName);
639 list = res.readEntity(AbstractCommonList.class);
646 List<AbstractCommonList.ListItem> items = list.getListItem();
647 int nItemsReturned = items.size();
648 // There will be 'nItemsToCreateInList'
649 // items created by the createItemList test,
650 // all associated with the same parent resource.
651 int nExpectedItems = nItemsToCreateInList;
652 if (logger.isDebugEnabled()) {
653 logger.debug(testName + ": Expected "
654 + nExpectedItems + " items; got: " + nItemsReturned);
656 Assert.assertEquals(nItemsReturned, nExpectedItems);
658 for (AbstractCommonList.ListItem item : items) {
660 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.REF_NAME);
661 Assert.assertTrue((null != value), "Item refName is null!");
663 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.TERM_DISPLAY_NAME);
664 Assert.assertTrue((null != value), "Item termDisplayName is null!");
666 if (logger.isTraceEnabled()) {
667 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
675 @Test(groups = {"readList"},
676 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
677 public void readContactList() throws Exception {
678 readContactList(knownResourceId, knownItemResourceId);
684 * @param parentcsid the parentcsid
685 * @param itemcsid the itemcsid
688 private void readContactList(String parentcsid, String itemcsid) throws Exception {
689 final String testName = "readContactList";
693 // Submit the request to the service and store the response.
694 OrganizationClient client = new OrganizationClient();
695 Response res = client.readContactList(parentcsid, itemcsid);
696 AbstractCommonList list = null;
698 assertStatusCode(res, testName);
699 list = res.readEntity(AbstractCommonList.class);
706 List<AbstractCommonList.ListItem> listitems =
708 int nItemsReturned = listitems.size();
709 // There will be one item created, associated with a
710 // known parent resource, by the createItem test.
712 // In addition, there will be 'nItemsToCreateInList'
713 // additional items created by the createItemList test,
714 // all associated with the same parent resource.
715 int nExpectedItems = nItemsToCreateInList + 1;
716 if (logger.isDebugEnabled()) {
717 logger.debug(testName + ": Expected "
718 + nExpectedItems + " items; got: " + nItemsReturned);
720 Assert.assertEquals(nItemsReturned, nExpectedItems);
722 // Optionally output additional data about list members for debugging.
723 boolean iterateThroughList = false;
724 if (iterateThroughList && logger.isDebugEnabled()) {
725 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
732 * @param testName the test name
733 * @throws Exception the exception
735 @Test(dataProvider = "testName", groups = {"update"},
736 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
737 public void updateContact(String testName) throws Exception {
741 // Retrieve the contents of a resource to update.
742 OrganizationClient client = new OrganizationClient();
743 Response res = client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
744 ContactsCommon contact = null;
746 assertStatusCode(res, testName);
747 if (logger.isDebugEnabled()) {
748 logger.debug("got Contact to update with ID: "
749 + knownContactResourceId
750 + " in item: " + knownItemResourceId
751 + " in parent: " + knownResourceId);
753 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
754 contact = (ContactsCommon) extractPart(input,
755 new ContactClient().getCommonPartName(), ContactsCommon.class);
756 Assert.assertNotNull(contact);
763 // Verify the contents of this resource
764 AddressGroupList addressGroupList = contact.getAddressGroupList();
765 Assert.assertNotNull(addressGroupList);
766 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
767 Assert.assertNotNull(addressGroups);
768 Assert.assertTrue(addressGroups.size() > 0);
769 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
770 Assert.assertNotNull(addressPlace1);
772 // Update the contents of this resource.
773 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
774 contact.setAddressGroupList(addressGroupList);
775 if (logger.isDebugEnabled()) {
776 logger.debug("to be updated Contact");
777 logger.debug(objectAsXmlString(contact,
778 ContactsCommon.class));
781 // Submit the updated resource to the service and store the response.
782 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
783 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
784 commonPart.setLabel(new ContactClient().getCommonPartName());
786 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
788 assertStatusCode(res, testName);
789 // Retrieve the updated resource and verify that its contents exist.
790 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
791 ContactsCommon updatedContact =
792 (ContactsCommon) extractPart(input,
793 new ContactClient().getCommonPartName(), ContactsCommon.class);
794 Assert.assertNotNull(updatedContact);
796 // Verify that the updated resource received the correct data.
797 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
798 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
799 "Data in updated object did not match submitted data.");
808 * Update non existent contact.
810 * @param testName the test name
811 * @throws Exception the exception
813 @Test(dataProvider = "testName", groups = {"update"},
814 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
815 public void updateNonExistentContact(String testName) throws Exception {
816 // Currently a no-op test
819 // ---------------------------------------------------------------
820 // CRUD tests : DELETE tests
821 // ---------------------------------------------------------------
823 // Note: delete sub-resources in ascending hierarchical order,
824 // before deleting their parents.
828 * @param testName the test name
829 * @throws Exception the exception
831 @Test(dataProvider = "testName", groups = {"delete"},
832 dependsOnMethods = {"updateContact"})
833 public void deleteContact(String testName) throws Exception {
837 if (logger.isDebugEnabled()) {
838 logger.debug("parentcsid =" + knownResourceId
839 + " itemcsid = " + knownItemResourceId
840 + " csid = " + knownContactResourceId);
843 // Submit the request to the service and store the response.
844 OrganizationClient client = new OrganizationClient();
845 Response res = client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
847 assertStatusCode(res, testName);
856 public void delete(String testName) throws Exception {
857 // Do nothing. See localDelete(). This ensure proper test order.
860 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
861 public void localDelete(String testName) throws Exception {
862 super.delete(testName);
866 public void deleteItem(String testName) throws Exception {
867 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
868 // its dependencies will get run first and then we can call the base class' delete method.
871 @Test(dataProvider = "testName", groups = {"delete"},
872 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
873 public void localDeleteItem(String testName) throws Exception {
874 super.deleteItem(testName);
878 * Delete non existent contact.
880 * @param testName the test name
883 @Test(dataProvider = "testName", groups = {"delete"},
884 dependsOnMethods = {"deleteContact"})
885 public void deleteNonExistentContact(String testName) throws Exception {
887 setupDeleteNonExistent();
889 // Submit the request to the service and store the response.
890 OrganizationClient client = new OrganizationClient();
891 Response res = client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
893 assertStatusCode(res, testName);
902 * Test contact submit request.
904 @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
905 public void testContactSubmitRequest() {
907 // Expected status code: 200 OK
908 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
910 // Submit the request to the service and store the response.
911 String method = ServiceRequestType.READ.httpMethodName();
912 String url = getContactResourceURL(knownResourceId,
913 knownItemResourceId, knownContactResourceId);
914 int statusCode = submitRequest(method, url);
916 // Check the status code of the response: does it match
917 // the expected response(s)?
918 if (logger.isDebugEnabled()) {
919 logger.debug("testContactSubmitRequest: url=" + url
920 + " status=" + statusCode);
922 Assert.assertEquals(statusCode, EXPECTED_STATUS);
926 // ---------------------------------------------------------------
927 // Cleanup of resources created during testing
928 // ---------------------------------------------------------------
930 * Deletes all resources created by tests, after all tests have been run.
932 * This cleanup method will always be run, even if one or more tests fail.
933 * For this reason, it attempts to remove all resources created
934 * at any point during testing, even if some of those resources
935 * may be expected to be deleted by certain tests.
938 @AfterClass(alwaysRun = true)
940 public void cleanUp() throws Exception {
941 String noTest = System.getProperty("noTestCleanup");
942 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
943 if (logger.isDebugEnabled()) {
944 logger.debug("Skipping Cleanup phase ...");
948 if (logger.isDebugEnabled()) {
949 logger.debug("Cleaning up temporary resources created for testing ...");
952 String parentResourceId;
953 String itemResourceId;
954 String contactResourceId;
955 // Clean up contact resources.
956 parentResourceId = knownResourceId;
957 OrganizationClient client = new OrganizationClient();
958 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
959 contactResourceId = entry.getKey();
960 itemResourceId = entry.getValue();
961 // Note: Any non-success responses from the delete operation
962 // below are ignored and not reported.
963 client.deleteContact(parentResourceId, itemResourceId, contactResourceId).close();
966 // Clean up parent resources.
971 // ---------------------------------------------------------------
972 // Utility methods used by tests above
973 // ---------------------------------------------------------------
975 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
978 * Gets the item service path component.
980 * @return the item service path component
982 public String getItemServicePathComponent() {
983 return AuthorityClient.ITEMS;
987 * Gets the contact service path component.
989 * @return the contact service path component
991 public String getContactServicePathComponent() {
992 return ContactClient.SERVICE_PATH_COMPONENT;
996 * Returns the root URL for the item service.
998 * This URL consists of a base URL for all services, followed by
999 * a path component for the owning parent, followed by the
1000 * path component for the items.
1002 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1003 * parent authority resource of the relevant item resource.
1005 * @return The root URL for the item service.
1007 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1008 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1012 * Returns the URL of a specific item resource managed by a service, and
1013 * designated by an identifier (such as a universally unique ID, or UUID).
1015 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1016 * parent authority resource of the relevant item resource.
1018 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1021 * @return The URL of a specific item resource managed by a service.
1023 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1024 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1028 * Returns the root URL for the contact service.
1030 * This URL consists of a base URL for all services, followed by
1031 * a path component for the owning authority, followed by the
1032 * path component for the owning item, followed by the path component
1033 * for the contact service.
1035 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1036 * parent authority resource of the relevant item resource.
1038 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1041 * @return The root URL for the contact service.
1043 protected String getContactServiceRootURL(String parentResourceIdentifier,
1044 String itemResourceIdentifier) {
1045 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1046 + getContactServicePathComponent();
1050 * Returns the URL of a specific contact resource managed by a service, and
1051 * designated by an identifier (such as a universally unique ID, or UUID).
1053 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1054 * parent resource of the relevant item resource.
1056 * @param resourceIdentifier An identifier (such as a UUID) for an
1059 * @return The URL of a specific resource managed by a service.
1061 protected String getContactResourceURL(String parentResourceIdentifier,
1062 String itemResourceIdentifier, String contactResourceIdentifier) {
1063 return getContactServiceRootURL(parentResourceIdentifier,
1064 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1068 public void authorityTests(String testName) {
1069 // TODO Auto-generated method stub
1074 protected OrganizationsCommon updateItemInstance(OrganizationsCommon organizationsCommon) {
1076 OrgTermGroupList termList = organizationsCommon.getOrgTermGroupList();
1077 Assert.assertNotNull(termList);
1078 List<OrgTermGroup> terms = termList.getOrgTermGroup();
1079 Assert.assertNotNull(terms);
1080 Assert.assertTrue(terms.size() > 0);
1081 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1082 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1083 organizationsCommon.setOrgTermGroupList(termList);
1085 return organizationsCommon;
1089 protected void compareUpdatedItemInstances(OrganizationsCommon original,
1090 OrganizationsCommon updated,
1091 boolean compareRevNumbers) throws Exception {
1093 OrgTermGroupList originalTermList = original.getOrgTermGroupList();
1094 Assert.assertNotNull(originalTermList);
1095 List<OrgTermGroup> originalTerms = originalTermList.getOrgTermGroup();
1096 Assert.assertNotNull(originalTerms);
1097 Assert.assertTrue(originalTerms.size() > 0);
1099 OrgTermGroupList updatedTermList = updated.getOrgTermGroupList();
1100 Assert.assertNotNull(updatedTermList);
1101 List<OrgTermGroup> updatedTerms = updatedTermList.getOrgTermGroup();
1102 Assert.assertNotNull(updatedTerms);
1103 Assert.assertTrue(updatedTerms.size() > 0);
1105 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1106 originalTerms.get(0).getTermDisplayName(),
1107 "Value in updated record did not match submitted data.");
1109 if (compareRevNumbers == true) {
1110 Assert.assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
1115 protected PoxPayloadOut createInstance(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);
1126 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1127 String identifier) {
1128 String shortId = identifier;
1129 String displayName = "displayName-" + shortId;
1130 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1131 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1132 displayName, shortId, commonPartName);
1136 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName,
1137 String identifier) {
1138 Map<String, String> nonexOrgMap = new HashMap<String, String>();
1139 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
1140 PoxPayloadOut result =
1141 OrgAuthorityClientUtils.createOrganizationInstance(
1142 knownResourceRefName,
1143 nonexOrgMap, OrgAuthorityClientUtils.getTermGroupInstance(TEST_ORG_NAME), commonPartName);
1148 protected OrgauthoritiesCommon updateInstance(OrgauthoritiesCommon orgauthoritiesCommon) {
1149 OrgauthoritiesCommon result = new OrgauthoritiesCommon();
1151 result.setDisplayName("updated-" + orgauthoritiesCommon.getDisplayName());
1152 result.setVocabType("updated-" + orgauthoritiesCommon.getVocabType());
1158 protected void compareUpdatedInstances(OrgauthoritiesCommon original,
1159 OrgauthoritiesCommon updated) throws Exception {
1160 // Verify that the updated resource received the correct data.
1161 Assert.assertEquals(updated.getDisplayName(),
1162 original.getDisplayName(),
1163 "Display name in updated object did not match submitted data.");