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.OrgTermGroup;
51 import org.collectionspace.services.organization.OrgTermGroupList;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.testng.Assert;
55 import org.testng.annotations.AfterClass;
56 import org.testng.annotations.Test;
59 * OrgAuthorityServiceTest, carries out tests against a
60 * deployed and running OrgAuthority Service.
62 * $LastChangedRevision$
65 public class OrgAuthorityServiceTest extends AbstractAuthorityServiceTest<OrgauthoritiesCommon, OrganizationsCommon> {
68 private final String CLASS_NAME = OrgAuthorityServiceTest.class.getName();
69 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
72 * Default constructor. Used to set the short ID for all tests authority items
74 public OrgAuthorityServiceTest() {
76 TEST_SHORTID = "TestOrg";
80 public String getServicePathComponent() {
81 return OrganizationClient.SERVICE_PATH_COMPONENT;
85 protected String getServiceName() {
86 return OrganizationClient.SERVICE_NAME;
89 private final String TEST_ORG_NAME = "Test Org";
90 private final String TEST_ORG_MAIN_BODY_NAME = "The real official test organization";
91 private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
92 private final String TEST_ORG_FOUNDING_DATE = "May 26, 1907";
94 /** The known item resource short ID. */
95 private String knownItemResourceShortIdentifer = null;
97 /** The known contact resource id. */
98 private String knownContactResourceId = null;
100 /** The all contact resource ids created. */
101 private Map<String, String> allContactResourceIdsCreated = new HashMap<String, String>();
106 protected void setKnownItemResource(String id, String shortIdentifer) {
107 knownItemResourceId = id;
108 knownItemResourceShortIdentifer = shortIdentifer;
112 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
115 protected CollectionSpaceClient getClientInstance() throws Exception {
116 return new OrganizationClient();
120 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
121 return new OrganizationClient(clientPropertiesFilename);
125 protected PoxPayloadOut createInstance(String identifier) throws Exception {
126 OrganizationClient client = new OrganizationClient();
127 String displayName = "displayName-" + identifier;
128 PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
129 displayName, identifier, client.getCommonPartName());
134 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) throws Exception {
135 String headerLabel = new OrganizationClient().getItemCommonPartName();
137 String shortId = TEST_SHORTID + identifier;
138 Map<String, String> testOrgMap = new HashMap<String, String>();
139 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
140 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
141 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
143 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
144 OrgTermGroup term = new OrgTermGroup();
145 term.setTermDisplayName(TEST_ORG_NAME);
146 term.setTermName(TEST_ORG_NAME);
147 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
150 return OrgAuthorityClientUtils.createOrganizationInstance(identifier, testOrgMap, terms, headerLabel);
154 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
155 return createItemInAuthority(client, authorityId, shortId, null /*refname*/);
159 * Creates the item in authority.
161 * @param vcsid the vcsid
162 * @param authRefName the auth ref name
165 private String createItemInAuthority(AuthorityClient client, String vcsid, String shortId, String authRefName) {
167 final String testName = "createItemInAuthority";
168 if (logger.isDebugEnabled()) {
169 logger.debug(testName + ":...");
172 Map<String, String> testOrgMap = new HashMap<String, String>();
173 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
174 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
175 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
177 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
178 OrgTermGroup term = new OrgTermGroup();
179 term.setTermDisplayName(TEST_ORG_NAME);
180 term.setTermName(TEST_ORG_NAME);
181 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
184 Map<String, List<String>> testOrgRepeatablesMap = new HashMap<String, List<String>>();
185 List<String> testOrgContactNames = new ArrayList<String>();
186 testOrgContactNames.add("joe@example.org");
187 testOrgContactNames.add("sally@example.org");
188 testOrgRepeatablesMap.put(OrganizationJAXBSchema.CONTACT_NAMES, testOrgContactNames);
190 String newID = OrgAuthorityClientUtils.createItemInAuthority(
191 vcsid, authRefName, testOrgMap, terms, testOrgRepeatablesMap, (OrganizationClient) client);
193 // Store the ID returned from the first item resource created
194 // for additional tests below.
195 if (knownItemResourceId == null) {
196 setKnownItemResource(newID, shortId);
197 if (logger.isDebugEnabled()) {
198 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
202 // Store the IDs from any item resources created
203 // by tests, along with the IDs of their parents, so these items
204 // can be deleted after all tests have been run.
205 allResourceItemIdsCreated.put(newID, vcsid);
211 * Creates the contact.
213 * @param testName the test name
216 @Test(dataProvider = "testName", groups = {"create"},
217 dependsOnMethods = {"createItem"})
218 public void createContact(String testName) throws Exception {
220 String newID = createContactInItem(knownResourceId, knownItemResourceId);
224 * Creates the contact in item.
226 * @param parentcsid the parentcsid
227 * @param itemcsid the itemcsid
231 private String createContactInItem(String parentcsid, String itemcsid) throws Exception {
233 final String testName = "createContactInItem";
234 if (logger.isDebugEnabled()) {
235 logger.debug(getTestBanner(testName, CLASS_NAME));
239 // Submit the request to the service and store the response.
240 OrganizationClient client = new OrganizationClient();
241 String identifier = createIdentifier();
242 PoxPayloadOut multipart =
243 ContactClientUtils.createContactInstance(parentcsid,
244 itemcsid, identifier, new ContactClient().getCommonPartName());
247 Response res = client.createContact(parentcsid, itemcsid, multipart);
249 assertStatusCode(res, testName);
250 newID = OrgAuthorityClientUtils.extractId(res);
257 // Store the ID returned from the first contact resource created
258 // for additional tests below.
259 if (knownContactResourceId == null) {
260 knownContactResourceId = newID;
261 if (logger.isDebugEnabled()) {
262 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
266 // Store the IDs from any contact resources created
267 // by tests, along with the IDs of their parent items,
268 // so these items can be deleted after all tests have been run.
269 allContactResourceIdsCreated.put(newID, itemcsid);
275 * Creates the contact list.
277 * @param testName the test name
278 * @throws Exception the exception
280 @Test(dataProvider = "testName", groups = {"createList"},
281 dependsOnMethods = {"createItemList"})
282 public void createContactList(String testName) throws Exception {
283 // Add contacts to the initially-created, known item record.
284 for (int j = 0; j < nItemsToCreateInList; j++) {
285 createContact(testName);
289 // ---------------------------------------------------------------
290 // CRUD tests : READ tests
291 // ---------------------------------------------------------------
294 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
297 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
298 // groups = {"read"}, dependsOnGroups = {"create"})
299 public void read(String testName) throws Exception {
300 readInternal(testName, knownResourceId, null);
306 * @param testName the test name
307 * @throws Exception the exception
309 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
310 // groups = {"read"}, dependsOnGroups = {"create"})
311 public void readByName(String testName) throws Exception {
312 readInternal(testName, null, knownResourceShortIdentifer);
315 protected void readInternal(String testName, String CSID, String shortId) throws Exception {
319 // Submit the request to the service and store the response.
320 OrganizationClient client = new OrganizationClient();
323 res = client.read(CSID);
324 } else if (shortId != null) {
325 res = client.readByName(shortId);
327 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
330 assertStatusCode(res, testName);
331 //FIXME: remove the following try catch once Aron fixes signatures
333 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
334 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
335 new OrganizationClient().getCommonPartName(), OrgauthoritiesCommon.class);
336 if (logger.isDebugEnabled()) {
337 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
339 Assert.assertNotNull(orgAuthority);
340 } catch (Exception e) {
341 throw new RuntimeException(e);
351 * Read item in Named Auth.
353 * TODO Enable this if we really need this - it is a funky case, where we would have
354 * the shortId of the item, but the CSID of the parent authority!? Unlikely.
356 * @param testName the test name
357 * @throws Exception the exception
358 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
359 groups = {"readItem"}, dependsOnGroups = {"read"})
360 public void readItemInNamedAuth(String testName) throws Exception {
361 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
368 * @param testName the test name
369 * @throws Exception the exception
371 @Test(dataProvider = "testName", groups = {"readItem"},
372 dependsOnMethods = {"readItemInNamedAuth"})
373 public void readNamedItem(String testName) throws Exception {
374 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
378 * Read item in Named Auth.
380 * @param testName the test name
381 * @throws Exception the exception
383 @Test(dataProvider = "testName", groups = {"readItem"},
384 dependsOnMethods = {"readItem"})
385 public void readItemInNamedAuth(String testName) throws Exception {
386 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
390 * Read Named item in Named Auth.
392 * @param testName the test name
393 * @throws Exception the exception
395 @Test(dataProvider = "testName", groups = {"readItem"},
396 dependsOnMethods = {"readItem"})
397 public void readNamedItemInNamedAuth(String testName) throws Exception {
398 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
401 protected void readItemInternal(String testName,
402 String authCSID, String authShortId, String itemCSID, String itemShortId)
407 // Submit the request to the service and store the response.
408 OrganizationClient client = new OrganizationClient();
410 if (authCSID != null) {
411 if (itemCSID != null) {
412 res = client.readItem(authCSID, itemCSID);
413 } else if (itemShortId != null) {
414 res = client.readNamedItem(authCSID, itemShortId);
416 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
418 } else if (authShortId != null) {
419 if (itemCSID != null) {
420 res = client.readItemInNamedAuthority(authShortId, itemCSID);
421 } else if (itemShortId != null) {
422 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
424 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
427 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
430 assertStatusCode(res, testName);
431 // Check whether we've received a organization.
432 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
433 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
434 client.getItemCommonPartName(), OrganizationsCommon.class);
435 Assert.assertNotNull(organization);
436 boolean showFull = true;
437 if (showFull && logger.isDebugEnabled()) {
438 logger.debug(testName + ": returned payload:");
439 logger.debug(objectAsXmlString(organization, OrganizationsCommon.class));
442 // Check that the organization item is within the expected OrgAuthority.
443 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
445 // Verify the number and contents of values in a repeatable field,
446 // as created in the instance record used for testing.
447 List<String> contactNames = organization.getContactNames().getContactName();
448 Assert.assertTrue(contactNames.size() > 0);
449 Assert.assertNotNull(contactNames.get(0));
459 protected void verifyReadItemInstance(OrganizationsCommon item) throws Exception {
460 List<String> contactNames = item.getContactNames().getContactName();
461 Assert.assertTrue(contactNames.size() > 0);
462 Assert.assertNotNull(contactNames.get(0));
466 * Verify illegal item display name.
468 * @param testName the test name
469 * @throws Exception the exception
471 @Test(dataProvider = "testName")
472 public void verifyIllegalItemDisplayName(String testName) throws Exception {
473 // Perform setup for read.
476 // First read our known resource.
478 OrganizationClient client = new OrganizationClient();
479 Response res = client.readItem(knownResourceId, knownItemResourceId);
480 OrganizationsCommon organization = null;
482 assertStatusCode(res, testName);
483 // Check whether organization has expected displayName.
484 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
485 organization = (OrganizationsCommon) extractPart(input,
486 client.getItemCommonPartName(), OrganizationsCommon.class);
487 Assert.assertNotNull(organization);
495 // Make an invalid UPDATE request, without a display name
497 OrgTermGroupList termList = organization.getOrgTermGroupList();
498 Assert.assertNotNull(termList);
499 List<OrgTermGroup> terms = termList.getOrgTermGroup();
500 Assert.assertNotNull(terms);
501 Assert.assertTrue(terms.size() > 0);
502 terms.get(0).setTermDisplayName(null);
503 terms.get(0).setTermName(null);
505 setupUpdateWithInvalidBody(); // we expect a failure
506 // Submit the updated resource to the service and store the response.
507 PoxPayloadOut output = new PoxPayloadOut(OrganizationClient.SERVICE_ITEM_PAYLOAD_NAME);
508 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(),
510 res = client.updateItem(knownResourceId, knownItemResourceId, output);
512 assertStatusCode(res, testName);
523 * @param testName the test name
524 * @throws Exception the exception
526 @Test(dataProvider = "testName", groups = {"readItem"},
527 dependsOnMethods = {"readItem"})
528 public void readContact(String testName) throws Exception {
532 // Submit the request to the service and store the response.
533 OrganizationClient client = new OrganizationClient();
534 Response res =client.readContact(knownResourceId, knownItemResourceId,
535 knownContactResourceId);
537 assertStatusCode(res, testName);
538 // Check whether we've received a contact.
539 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
540 ContactsCommon contact = (ContactsCommon) extractPart(input,
541 new ContactClient().getCommonPartName(), ContactsCommon.class);
542 Assert.assertNotNull(contact);
543 boolean showFull = true;
544 if (showFull && logger.isDebugEnabled()) {
545 logger.debug(testName + ": returned payload:");
546 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
548 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
549 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
558 * Read contact non existent.
560 * @param testName the test name
563 @Test(dataProvider = "testName", groups = {"readItem"},
564 dependsOnMethods = {"readContact"})
565 public void readContactNonExistent(String testName) throws Exception {
567 setupReadNonExistent();
569 // Submit the request to the service and store the response.
570 OrganizationClient client = new OrganizationClient();
571 Response res = client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
573 int statusCode = res.getStatus();
575 // Check the status code of the response: does it match
576 // the expected response(s)?
577 if (logger.isDebugEnabled()) {
578 logger.debug(testName + ": status = " + statusCode);
580 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
581 invalidStatusCodeMessage(testRequestType, statusCode));
582 Assert.assertEquals(statusCode, testExpectedStatusCode);
595 // @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
596 public void readItemList(String testName) throws Exception {
597 readItemList(knownAuthorityWithItems, null);
601 * Read item list by authority name.
605 // @Test(dataProvider = "testName",
606 // dependsOnMethods = {"readItem"})
607 public void readItemListByName(String testName) throws Exception {
608 readItemList(null, READITEMS_SHORT_IDENTIFIER);
614 * @param vcsid the vcsid
615 * @param name the name
618 private void readItemList(String vcsid, String name) throws Exception {
620 final String testName = "readItemList";
624 // Submit the request to the service and store the response.
625 OrganizationClient client = new OrganizationClient();
628 res = client.readItemList(vcsid, null, null);
629 } else if (name != null) {
630 res = client.readItemListForNamedAuthority(name, null, null);
632 Assert.fail("readItemList passed null csid and name!");
635 AbstractCommonList list = null;
637 assertStatusCode(res, testName);
638 list = res.readEntity(AbstractCommonList.class);
645 List<AbstractCommonList.ListItem> items = list.getListItem();
646 int nItemsReturned = items.size();
647 // There will be 'nItemsToCreateInList'
648 // items created by the createItemList test,
649 // all associated with the same parent resource.
650 int nExpectedItems = nItemsToCreateInList;
651 if (logger.isDebugEnabled()) {
652 logger.debug(testName + ": Expected "
653 + nExpectedItems + " items; got: " + nItemsReturned);
655 Assert.assertEquals(nItemsReturned, nExpectedItems);
657 for (AbstractCommonList.ListItem item : items) {
659 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.REF_NAME);
660 Assert.assertTrue((null != value), "Item refName is null!");
662 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.TERM_DISPLAY_NAME);
663 Assert.assertTrue((null != value), "Item termDisplayName is null!");
665 if (logger.isTraceEnabled()) {
666 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
674 @Test(groups = {"readList"},
675 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
676 public void readContactList() throws Exception {
677 readContactList(knownResourceId, knownItemResourceId);
683 * @param parentcsid the parentcsid
684 * @param itemcsid the itemcsid
687 private void readContactList(String parentcsid, String itemcsid) throws Exception {
688 final String testName = "readContactList";
692 // Submit the request to the service and store the response.
693 OrganizationClient client = new OrganizationClient();
694 Response res = client.readContactList(parentcsid, itemcsid);
695 AbstractCommonList list = null;
697 assertStatusCode(res, testName);
698 list = res.readEntity(AbstractCommonList.class);
705 List<AbstractCommonList.ListItem> listitems =
707 int nItemsReturned = listitems.size();
708 // There will be one item created, associated with a
709 // known parent resource, by the createItem test.
711 // In addition, there will be 'nItemsToCreateInList'
712 // additional items created by the createItemList test,
713 // all associated with the same parent resource.
714 int nExpectedItems = nItemsToCreateInList + 1;
715 if (logger.isDebugEnabled()) {
716 logger.debug(testName + ": Expected "
717 + nExpectedItems + " items; got: " + nItemsReturned);
719 Assert.assertEquals(nItemsReturned, nExpectedItems);
721 // Optionally output additional data about list members for debugging.
722 boolean iterateThroughList = false;
723 if (iterateThroughList && logger.isDebugEnabled()) {
724 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
731 * @param testName the test name
732 * @throws Exception the exception
734 @Test(dataProvider = "testName", groups = {"update"},
735 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
736 public void updateContact(String testName) throws Exception {
740 // Retrieve the contents of a resource to update.
741 OrganizationClient client = new OrganizationClient();
742 Response res = client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
743 ContactsCommon contact = null;
745 assertStatusCode(res, testName);
746 if (logger.isDebugEnabled()) {
747 logger.debug("got Contact to update with ID: "
748 + knownContactResourceId
749 + " in item: " + knownItemResourceId
750 + " in parent: " + knownResourceId);
752 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
753 contact = (ContactsCommon) extractPart(input,
754 new ContactClient().getCommonPartName(), ContactsCommon.class);
755 Assert.assertNotNull(contact);
762 // Verify the contents of this resource
763 AddressGroupList addressGroupList = contact.getAddressGroupList();
764 Assert.assertNotNull(addressGroupList);
765 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
766 Assert.assertNotNull(addressGroups);
767 Assert.assertTrue(addressGroups.size() > 0);
768 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
769 Assert.assertNotNull(addressPlace1);
771 // Update the contents of this resource.
772 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
773 contact.setAddressGroupList(addressGroupList);
774 if (logger.isDebugEnabled()) {
775 logger.debug("to be updated Contact");
776 logger.debug(objectAsXmlString(contact,
777 ContactsCommon.class));
780 // Submit the updated resource to the service and store the response.
781 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
782 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
783 commonPart.setLabel(new ContactClient().getCommonPartName());
785 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
787 assertStatusCode(res, testName);
788 // Retrieve the updated resource and verify that its contents exist.
789 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
790 ContactsCommon updatedContact =
791 (ContactsCommon) extractPart(input,
792 new ContactClient().getCommonPartName(), ContactsCommon.class);
793 Assert.assertNotNull(updatedContact);
795 // Verify that the updated resource received the correct data.
796 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
797 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
798 "Data in updated object did not match submitted data.");
807 * Update non existent contact.
809 * @param testName the test name
810 * @throws Exception the exception
812 @Test(dataProvider = "testName", groups = {"update"},
813 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
814 public void updateNonExistentContact(String testName) throws Exception {
815 // Currently a no-op test
818 // ---------------------------------------------------------------
819 // CRUD tests : DELETE tests
820 // ---------------------------------------------------------------
822 // Note: delete sub-resources in ascending hierarchical order,
823 // before deleting their parents.
827 * @param testName the test name
828 * @throws Exception the exception
830 @Test(dataProvider = "testName", groups = {"delete"},
831 dependsOnMethods = {"updateContact"})
832 public void deleteContact(String testName) throws Exception {
836 if (logger.isDebugEnabled()) {
837 logger.debug("parentcsid =" + knownResourceId
838 + " itemcsid = " + knownItemResourceId
839 + " csid = " + knownContactResourceId);
842 // Submit the request to the service and store the response.
843 OrganizationClient client = new OrganizationClient();
844 Response res = client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
846 assertStatusCode(res, testName);
855 public void delete(String testName) throws Exception {
856 // Do nothing. See localDelete(). This ensure proper test order.
859 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
860 public void localDelete(String testName) throws Exception {
861 super.delete(testName);
865 public void deleteItem(String testName) throws Exception {
866 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
867 // its dependencies will get run first and then we can call the base class' delete method.
870 @Test(dataProvider = "testName", groups = {"delete"},
871 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
872 public void localDeleteItem(String testName) throws Exception {
873 super.deleteItem(testName);
877 * Delete non existent contact.
879 * @param testName the test name
882 @Test(dataProvider = "testName", groups = {"delete"},
883 dependsOnMethods = {"deleteContact"})
884 public void deleteNonExistentContact(String testName) throws Exception {
886 setupDeleteNonExistent();
888 // Submit the request to the service and store the response.
889 OrganizationClient client = new OrganizationClient();
890 Response res = client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
892 assertStatusCode(res, testName);
901 * Test contact submit request.
903 @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
904 public void testContactSubmitRequest() {
906 // Expected status code: 200 OK
907 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
909 // Submit the request to the service and store the response.
910 String method = ServiceRequestType.READ.httpMethodName();
911 String url = getContactResourceURL(knownResourceId,
912 knownItemResourceId, knownContactResourceId);
913 int statusCode = submitRequest(method, url);
915 // Check the status code of the response: does it match
916 // the expected response(s)?
917 if (logger.isDebugEnabled()) {
918 logger.debug("testContactSubmitRequest: url=" + url
919 + " status=" + statusCode);
921 Assert.assertEquals(statusCode, EXPECTED_STATUS);
925 // ---------------------------------------------------------------
926 // Cleanup of resources created during testing
927 // ---------------------------------------------------------------
929 * Deletes all resources created by tests, after all tests have been run.
931 * This cleanup method will always be run, even if one or more tests fail.
932 * For this reason, it attempts to remove all resources created
933 * at any point during testing, even if some of those resources
934 * may be expected to be deleted by certain tests.
937 @AfterClass(alwaysRun = true)
939 public void cleanUp() throws Exception {
940 String noTest = System.getProperty("noTestCleanup");
941 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
942 if (logger.isDebugEnabled()) {
943 logger.debug("Skipping Cleanup phase ...");
947 if (logger.isDebugEnabled()) {
948 logger.debug("Cleaning up temporary resources created for testing ...");
951 String parentResourceId;
952 String itemResourceId;
953 String contactResourceId;
954 // Clean up contact resources.
955 parentResourceId = knownResourceId;
956 OrganizationClient client = new OrganizationClient();
957 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
958 contactResourceId = entry.getKey();
959 itemResourceId = entry.getValue();
960 // Note: Any non-success responses from the delete operation
961 // below are ignored and not reported.
962 client.deleteContact(parentResourceId, itemResourceId, contactResourceId).close();
965 // Clean up parent resources.
970 // ---------------------------------------------------------------
971 // Utility methods used by tests above
972 // ---------------------------------------------------------------
974 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
977 * Gets the item service path component.
979 * @return the item service path component
981 public String getItemServicePathComponent() {
982 return AuthorityClient.ITEMS;
986 * Gets the contact service path component.
988 * @return the contact service path component
990 public String getContactServicePathComponent() {
991 return ContactClient.SERVICE_PATH_COMPONENT;
995 * Returns the root URL for the item service.
997 * This URL consists of a base URL for all services, followed by
998 * a path component for the owning parent, followed by the
999 * path component for the items.
1001 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1002 * parent authority resource of the relevant item resource.
1004 * @return The root URL for the item service.
1006 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1007 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1011 * Returns the URL of a specific item resource managed by a service, and
1012 * designated by an identifier (such as a universally unique ID, or UUID).
1014 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1015 * parent authority resource of the relevant item resource.
1017 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1020 * @return The URL of a specific item resource managed by a service.
1022 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1023 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1027 * Returns the root URL for the contact service.
1029 * This URL consists of a base URL for all services, followed by
1030 * a path component for the owning authority, followed by the
1031 * path component for the owning item, followed by the path component
1032 * for the contact service.
1034 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1035 * parent authority resource of the relevant item resource.
1037 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1040 * @return The root URL for the contact service.
1042 protected String getContactServiceRootURL(String parentResourceIdentifier,
1043 String itemResourceIdentifier) {
1044 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1045 + getContactServicePathComponent();
1049 * Returns the URL of a specific contact resource managed by a service, and
1050 * designated by an identifier (such as a universally unique ID, or UUID).
1052 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1053 * parent resource of the relevant item resource.
1055 * @param resourceIdentifier An identifier (such as a UUID) for an
1058 * @return The URL of a specific resource managed by a service.
1060 protected String getContactResourceURL(String parentResourceIdentifier,
1061 String itemResourceIdentifier, String contactResourceIdentifier) {
1062 return getContactServiceRootURL(parentResourceIdentifier,
1063 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1067 public void authorityTests(String testName) {
1068 // TODO Auto-generated method stub
1073 protected OrganizationsCommon updateItemInstance(OrganizationsCommon organizationsCommon) {
1075 OrgTermGroupList termList = organizationsCommon.getOrgTermGroupList();
1076 Assert.assertNotNull(termList);
1077 List<OrgTermGroup> terms = termList.getOrgTermGroup();
1078 Assert.assertNotNull(terms);
1079 Assert.assertTrue(terms.size() > 0);
1080 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1081 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1082 organizationsCommon.setOrgTermGroupList(termList);
1084 return organizationsCommon;
1088 protected void compareUpdatedItemInstances(OrganizationsCommon original,
1089 OrganizationsCommon updated,
1090 boolean compareRevNumbers) throws Exception {
1092 OrgTermGroupList originalTermList = original.getOrgTermGroupList();
1093 Assert.assertNotNull(originalTermList);
1094 List<OrgTermGroup> originalTerms = originalTermList.getOrgTermGroup();
1095 Assert.assertNotNull(originalTerms);
1096 Assert.assertTrue(originalTerms.size() > 0);
1098 OrgTermGroupList updatedTermList = updated.getOrgTermGroupList();
1099 Assert.assertNotNull(updatedTermList);
1100 List<OrgTermGroup> updatedTerms = updatedTermList.getOrgTermGroup();
1101 Assert.assertNotNull(updatedTerms);
1102 Assert.assertTrue(updatedTerms.size() > 0);
1104 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1105 originalTerms.get(0).getTermDisplayName(),
1106 "Value in updated record did not match submitted data.");
1108 if (compareRevNumbers == true) {
1109 Assert.assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
1114 protected PoxPayloadOut createInstance(String commonPartName,
1115 String identifier) {
1116 String shortId = identifier;
1117 String displayName = "displayName-" + shortId;
1118 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1119 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1120 displayName, shortId, commonPartName);
1125 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1126 String identifier) {
1127 String shortId = identifier;
1128 String displayName = "displayName-" + shortId;
1129 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1130 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1131 displayName, shortId, commonPartName);
1135 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName,
1136 String identifier) {
1137 Map<String, String> nonexOrgMap = new HashMap<String, String>();
1138 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
1139 PoxPayloadOut result =
1140 OrgAuthorityClientUtils.createOrganizationInstance(
1141 knownResourceRefName,
1142 nonexOrgMap, OrgAuthorityClientUtils.getTermGroupInstance(TEST_ORG_NAME), commonPartName);
1147 protected OrgauthoritiesCommon updateInstance(OrgauthoritiesCommon orgauthoritiesCommon) {
1148 OrgauthoritiesCommon result = new OrgauthoritiesCommon();
1150 result.setDisplayName("updated-" + orgauthoritiesCommon.getDisplayName());
1151 result.setVocabType("updated-" + orgauthoritiesCommon.getVocabType());
1157 protected void compareUpdatedInstances(OrgauthoritiesCommon original,
1158 OrgauthoritiesCommon updated) throws Exception {
1159 // Verify that the updated resource received the correct data.
1160 Assert.assertEquals(updated.getDisplayName(),
1161 original.getDisplayName(),
1162 "Display name in updated object did not match submitted data.");