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.OrgAuthorityClient;
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.jboss.resteasy.client.ClientResponse;
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 public String getServicePathComponent() {
74 return OrgAuthorityClient.SERVICE_PATH_COMPONENT;
78 protected String getServiceName() {
79 return OrgAuthorityClient.SERVICE_NAME;
81 private final String TEST_SHORT_ID = "TestOrg";
82 private final String TEST_ORG_NAME = "Test Org";
83 private final String TEST_ORG_MAIN_BODY_NAME = "The real official test organization";
84 private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
85 // FIXME: Change this to a structured date once this field changes in the schema.
86 private final String TEST_ORG_FOUNDING_DATE = "May 26, 1907";
88 private String knownItemResourceShortIdentifer = null;
90 /** The known contact resource id. */
91 private String knownContactResourceId = null;
93 /** The all contact resource ids created. */
94 private Map<String, String> allContactResourceIdsCreated =
95 new HashMap<String, String>();
97 protected void setKnownItemResource(String id, String shortIdentifer) {
98 knownItemResourceId = id;
99 knownItemResourceShortIdentifer = shortIdentifer;
103 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
106 protected CollectionSpaceClient getClientInstance() {
107 return new OrgAuthorityClient();
111 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
112 return new OrgAuthorityClient(clientPropertiesFilename);
116 protected PoxPayloadOut createInstance(String identifier) {
117 OrgAuthorityClient client = new OrgAuthorityClient();
118 String displayName = "displayName-" + identifier;
119 PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
120 displayName, identifier, client.getCommonPartName());
125 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
126 String headerLabel = new OrgAuthorityClient().getItemCommonPartName();
128 String shortId = TEST_SHORT_ID + identifier;
129 Map<String, String> testOrgMap = new HashMap<String, String>();
130 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
131 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
132 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
134 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
135 OrgTermGroup term = new OrgTermGroup();
136 term.setTermDisplayName(TEST_ORG_NAME);
137 term.setTermName(TEST_ORG_NAME);
138 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
141 return OrgAuthorityClientUtils.createOrganizationInstance(identifier, testOrgMap, terms, headerLabel);
145 * Creates the item in authority.
147 * @param vcsid the vcsid
148 * @param authRefName the auth ref name
151 private String createItemInAuthority(AuthorityClient client, String vcsid, String authRefName) {
153 final String testName = "createItemInAuthority";
154 if (logger.isDebugEnabled()) {
155 logger.debug(testName + ":...");
158 String shortId = TEST_SHORT_ID + System.currentTimeMillis();
159 Map<String, String> testOrgMap = new HashMap<String, String>();
160 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
161 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
162 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
164 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
165 OrgTermGroup term = new OrgTermGroup();
166 term.setTermDisplayName(TEST_ORG_NAME);
167 term.setTermName(TEST_ORG_NAME);
168 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
171 Map<String, List<String>> testOrgRepeatablesMap = new HashMap<String, List<String>>();
172 List<String> testOrgContactNames = new ArrayList<String>();
173 testOrgContactNames.add("joe@example.org");
174 testOrgContactNames.add("sally@example.org");
175 testOrgRepeatablesMap.put(OrganizationJAXBSchema.CONTACT_NAMES, testOrgContactNames);
177 String newID = OrgAuthorityClientUtils.createItemInAuthority(
178 vcsid, authRefName, testOrgMap, terms, testOrgRepeatablesMap, (OrgAuthorityClient) client);
180 // Store the ID returned from the first item resource created
181 // for additional tests below.
182 if (knownItemResourceId == null) {
183 setKnownItemResource(newID, shortId);
184 if (logger.isDebugEnabled()) {
185 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
189 // Store the IDs from any item resources created
190 // by tests, along with the IDs of their parents, so these items
191 // can be deleted after all tests have been run.
192 allResourceItemIdsCreated.put(newID, vcsid);
198 * Creates the contact.
200 * @param testName the test name
202 @Test(dataProvider = "testName", groups = {"create"},
203 dependsOnMethods = {"createItem"})
204 public void createContact(String testName) {
206 String newID = createContactInItem(knownResourceId, knownItemResourceId);
210 * Creates the contact in item.
212 * @param parentcsid the parentcsid
213 * @param itemcsid the itemcsid
216 private String createContactInItem(String parentcsid, String itemcsid) {
218 final String testName = "createContactInItem";
219 if (logger.isDebugEnabled()) {
220 logger.debug(getTestBanner(testName, CLASS_NAME));
224 // Submit the request to the service and store the response.
225 OrgAuthorityClient client = new OrgAuthorityClient();
226 String identifier = createIdentifier();
227 PoxPayloadOut multipart =
228 ContactClientUtils.createContactInstance(parentcsid,
229 itemcsid, identifier, new ContactClient().getCommonPartName());
232 Response res = client.createContact(parentcsid, itemcsid, multipart);
234 assertStatusCode(res, testName);
235 newID = OrgAuthorityClientUtils.extractId(res);
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(res.readEntity(String.class));
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(res.readEntity(String.class));
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(res.readEntity(String.class));
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 Response res =client.readContact(knownResourceId, knownItemResourceId,
520 knownContactResourceId);
522 assertStatusCode(res, testName);
523 // Check whether we've received a contact.
524 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
525 ContactsCommon contact = (ContactsCommon) extractPart(input,
526 new ContactClient().getCommonPartName(), ContactsCommon.class);
527 Assert.assertNotNull(contact);
528 boolean showFull = true;
529 if (showFull && logger.isDebugEnabled()) {
530 logger.debug(testName + ": returned payload:");
531 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
533 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
534 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
543 * Read contact non existent.
545 * @param testName the test name
547 @Test(dataProvider = "testName", groups = {"readItem"},
548 dependsOnMethods = {"readContact"})
549 public void readContactNonExistent(String testName) {
551 setupReadNonExistent();
553 // Submit the request to the service and store the response.
554 OrgAuthorityClient client = new OrgAuthorityClient();
555 Response res = client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
557 int statusCode = res.getStatus();
559 // Check the status code of the response: does it match
560 // the expected response(s)?
561 if (logger.isDebugEnabled()) {
562 logger.debug(testName + ": status = " + statusCode);
564 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
565 invalidStatusCodeMessage(testRequestType, statusCode));
566 Assert.assertEquals(statusCode, testExpectedStatusCode);
578 // @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
579 public void readItemList(String testName) {
580 readItemList(knownAuthorityWithItems, null);
584 * Read item list by authority name.
587 // @Test(dataProvider = "testName",
588 // dependsOnMethods = {"readItem"})
589 public void readItemListByName(String testName) {
590 readItemList(null, READITEMS_SHORT_IDENTIFIER);
596 * @param vcsid the vcsid
597 * @param name the name
599 private void readItemList(String vcsid, String name) {
601 final String testName = "readItemList";
605 // Submit the request to the service and store the response.
606 OrgAuthorityClient client = new OrgAuthorityClient();
609 res = client.readItemList(vcsid, null, null);
610 } else if (name != null) {
611 res = client.readItemListForNamedAuthority(name, null, null);
613 Assert.fail("readItemList passed null csid and name!");
616 AbstractCommonList list = null;
618 assertStatusCode(res, testName);
619 list = res.readEntity(AbstractCommonList.class);
626 List<AbstractCommonList.ListItem> items = list.getListItem();
627 int nItemsReturned = items.size();
628 // There will be 'nItemsToCreateInList'
629 // items created by the createItemList test,
630 // all associated with the same parent resource.
631 int nExpectedItems = nItemsToCreateInList;
632 if (logger.isDebugEnabled()) {
633 logger.debug(testName + ": Expected "
634 + nExpectedItems + " items; got: " + nItemsReturned);
636 Assert.assertEquals(nItemsReturned, nExpectedItems);
638 for (AbstractCommonList.ListItem item : items) {
640 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.REF_NAME);
641 Assert.assertTrue((null != value), "Item refName is null!");
643 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.TERM_DISPLAY_NAME);
644 Assert.assertTrue((null != value), "Item termDisplayName is null!");
646 if (logger.isTraceEnabled()) {
647 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
654 @Test(groups = {"readList"},
655 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
656 public void readContactList() {
657 readContactList(knownResourceId, knownItemResourceId);
663 * @param parentcsid the parentcsid
664 * @param itemcsid the itemcsid
666 private void readContactList(String parentcsid, String itemcsid) {
667 final String testName = "readContactList";
671 // Submit the request to the service and store the response.
672 OrgAuthorityClient client = new OrgAuthorityClient();
673 Response res = client.readContactList(parentcsid, itemcsid);
674 AbstractCommonList list = null;
676 assertStatusCode(res, testName);
677 list = res.readEntity(AbstractCommonList.class);
684 List<AbstractCommonList.ListItem> listitems =
686 int nItemsReturned = listitems.size();
687 // There will be one item created, associated with a
688 // known parent resource, by the createItem test.
690 // In addition, there will be 'nItemsToCreateInList'
691 // additional items created by the createItemList test,
692 // all associated with the same parent resource.
693 int nExpectedItems = nItemsToCreateInList + 1;
694 if (logger.isDebugEnabled()) {
695 logger.debug(testName + ": Expected "
696 + nExpectedItems + " items; got: " + nItemsReturned);
698 Assert.assertEquals(nItemsReturned, nExpectedItems);
700 // Optionally output additional data about list members for debugging.
701 boolean iterateThroughList = false;
702 if (iterateThroughList && logger.isDebugEnabled()) {
703 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
710 * @param testName the test name
711 * @throws Exception the exception
713 @Test(dataProvider = "testName", groups = {"update"},
714 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
715 public void updateContact(String testName) throws Exception {
719 // Retrieve the contents of a resource to update.
720 OrgAuthorityClient client = new OrgAuthorityClient();
721 Response res = client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
722 ContactsCommon contact = null;
724 assertStatusCode(res, testName);
725 if (logger.isDebugEnabled()) {
726 logger.debug("got Contact to update with ID: "
727 + knownContactResourceId
728 + " in item: " + knownItemResourceId
729 + " in parent: " + knownResourceId);
731 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
732 contact = (ContactsCommon) extractPart(input,
733 new ContactClient().getCommonPartName(), ContactsCommon.class);
734 Assert.assertNotNull(contact);
741 // Verify the contents of this resource
742 AddressGroupList addressGroupList = contact.getAddressGroupList();
743 Assert.assertNotNull(addressGroupList);
744 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
745 Assert.assertNotNull(addressGroups);
746 Assert.assertTrue(addressGroups.size() > 0);
747 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
748 Assert.assertNotNull(addressPlace1);
750 // Update the contents of this resource.
751 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
752 contact.setAddressGroupList(addressGroupList);
753 if (logger.isDebugEnabled()) {
754 logger.debug("to be updated Contact");
755 logger.debug(objectAsXmlString(contact,
756 ContactsCommon.class));
759 // Submit the updated resource to the service and store the response.
760 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
761 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
762 commonPart.setLabel(new ContactClient().getCommonPartName());
764 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
766 assertStatusCode(res, testName);
767 // Retrieve the updated resource and verify that its contents exist.
768 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
769 ContactsCommon updatedContact =
770 (ContactsCommon) extractPart(input,
771 new ContactClient().getCommonPartName(), ContactsCommon.class);
772 Assert.assertNotNull(updatedContact);
774 // Verify that the updated resource received the correct data.
775 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
776 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
777 "Data in updated object did not match submitted data.");
786 * Update non existent contact.
788 * @param testName the test name
789 * @throws Exception the exception
791 @Test(dataProvider = "testName", groups = {"update"},
792 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
793 public void updateNonExistentContact(String testName) throws Exception {
794 // Currently a no-op test
797 // ---------------------------------------------------------------
798 // CRUD tests : DELETE tests
799 // ---------------------------------------------------------------
801 // Note: delete sub-resources in ascending hierarchical order,
802 // before deleting their parents.
806 * @param testName the test name
807 * @throws Exception the exception
809 @Test(dataProvider = "testName", groups = {"delete"},
810 dependsOnMethods = {"updateContact"})
811 public void deleteContact(String testName) throws Exception {
815 if (logger.isDebugEnabled()) {
816 logger.debug("parentcsid =" + knownResourceId
817 + " itemcsid = " + knownItemResourceId
818 + " csid = " + knownContactResourceId);
821 // Submit the request to the service and store the response.
822 OrgAuthorityClient client = new OrgAuthorityClient();
823 Response res = client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
825 assertStatusCode(res, testName);
834 public void delete(String testName) throws Exception {
835 // Do nothing. See localDelete(). This ensure proper test order.
838 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
839 public void localDelete(String testName) throws Exception {
840 super.delete(testName);
844 public void deleteItem(String testName) throws Exception {
845 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
846 // its dependencies will get run first and then we can call the base class' delete method.
849 @Test(dataProvider = "testName", groups = {"delete"},
850 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
851 public void localDeleteItem(String testName) throws Exception {
852 super.deleteItem(testName);
856 * Delete non existent contact.
858 * @param testName the test name
860 @Test(dataProvider = "testName", groups = {"delete"},
861 dependsOnMethods = {"deleteContact"})
862 public void deleteNonExistentContact(String testName) {
864 setupDeleteNonExistent();
866 // Submit the request to the service and store the response.
867 OrgAuthorityClient client = new OrgAuthorityClient();
868 Response res = client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
870 assertStatusCode(res, testName);
879 * Test contact submit request.
881 @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
882 public void testContactSubmitRequest() {
884 // Expected status code: 200 OK
885 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
887 // Submit the request to the service and store the response.
888 String method = ServiceRequestType.READ.httpMethodName();
889 String url = getContactResourceURL(knownResourceId,
890 knownItemResourceId, knownContactResourceId);
891 int statusCode = submitRequest(method, url);
893 // Check the status code of the response: does it match
894 // the expected response(s)?
895 if (logger.isDebugEnabled()) {
896 logger.debug("testContactSubmitRequest: url=" + url
897 + " status=" + statusCode);
899 Assert.assertEquals(statusCode, EXPECTED_STATUS);
903 // ---------------------------------------------------------------
904 // Cleanup of resources created during testing
905 // ---------------------------------------------------------------
907 * Deletes all resources created by tests, after all tests have been run.
909 * This cleanup method will always be run, even if one or more tests fail.
910 * For this reason, it attempts to remove all resources created
911 * at any point during testing, even if some of those resources
912 * may be expected to be deleted by certain tests.
914 @AfterClass(alwaysRun = true)
916 public void cleanUp() {
917 String noTest = System.getProperty("noTestCleanup");
918 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
919 if (logger.isDebugEnabled()) {
920 logger.debug("Skipping Cleanup phase ...");
924 if (logger.isDebugEnabled()) {
925 logger.debug("Cleaning up temporary resources created for testing ...");
928 String parentResourceId;
929 String itemResourceId;
930 String contactResourceId;
931 // Clean up contact resources.
932 parentResourceId = knownResourceId;
933 OrgAuthorityClient client = new OrgAuthorityClient();
934 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
935 contactResourceId = entry.getKey();
936 itemResourceId = entry.getValue();
937 // Note: Any non-success responses from the delete operation
938 // below are ignored and not reported.
939 client.deleteContact(parentResourceId, itemResourceId, contactResourceId).close();
942 // Clean up parent resources.
947 // ---------------------------------------------------------------
948 // Utility methods used by tests above
949 // ---------------------------------------------------------------
951 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
954 * Gets the item service path component.
956 * @return the item service path component
958 public String getItemServicePathComponent() {
959 return AuthorityClient.ITEMS;
963 * Gets the contact service path component.
965 * @return the contact service path component
967 public String getContactServicePathComponent() {
968 return ContactClient.SERVICE_PATH_COMPONENT;
972 * Returns the root URL for the item service.
974 * This URL consists of a base URL for all services, followed by
975 * a path component for the owning parent, followed by the
976 * path component for the items.
978 * @param parentResourceIdentifier An identifier (such as a UUID) for the
979 * parent authority resource of the relevant item resource.
981 * @return The root URL for the item service.
983 protected String getItemServiceRootURL(String parentResourceIdentifier) {
984 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
988 * Returns the URL of a specific item resource managed by a service, and
989 * designated by an identifier (such as a universally unique ID, or UUID).
991 * @param parentResourceIdentifier An identifier (such as a UUID) for the
992 * parent authority resource of the relevant item resource.
994 * @param itemResourceIdentifier An identifier (such as a UUID) for an
997 * @return The URL of a specific item resource managed by a service.
999 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1000 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1004 * Returns the root URL for the contact service.
1006 * This URL consists of a base URL for all services, followed by
1007 * a path component for the owning authority, followed by the
1008 * path component for the owning item, followed by the path component
1009 * for the contact service.
1011 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1012 * parent authority resource of the relevant item resource.
1014 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1017 * @return The root URL for the contact service.
1019 protected String getContactServiceRootURL(String parentResourceIdentifier,
1020 String itemResourceIdentifier) {
1021 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1022 + getContactServicePathComponent();
1026 * Returns the URL of a specific contact resource managed by a service, and
1027 * designated by an identifier (such as a universally unique ID, or UUID).
1029 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1030 * parent resource of the relevant item resource.
1032 * @param resourceIdentifier An identifier (such as a UUID) for an
1035 * @return The URL of a specific resource managed by a service.
1037 protected String getContactResourceURL(String parentResourceIdentifier,
1038 String itemResourceIdentifier, String contactResourceIdentifier) {
1039 return getContactServiceRootURL(parentResourceIdentifier,
1040 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1044 public void authorityTests(String testName) {
1045 // TODO Auto-generated method stub
1050 protected String createItemInAuthority(AuthorityClient client, String authorityId) {
1051 return createItemInAuthority(client, authorityId, null /*refname*/);
1055 protected OrganizationsCommon updateItemInstance(OrganizationsCommon organizationsCommon) {
1057 OrgTermGroupList termList = organizationsCommon.getOrgTermGroupList();
1058 Assert.assertNotNull(termList);
1059 List<OrgTermGroup> terms = termList.getOrgTermGroup();
1060 Assert.assertNotNull(terms);
1061 Assert.assertTrue(terms.size() > 0);
1062 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1063 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1064 organizationsCommon.setOrgTermGroupList(termList);
1066 return organizationsCommon;
1070 protected void compareUpdatedItemInstances(OrganizationsCommon original,
1071 OrganizationsCommon updated) throws Exception {
1073 OrgTermGroupList originalTermList = original.getOrgTermGroupList();
1074 Assert.assertNotNull(originalTermList);
1075 List<OrgTermGroup> originalTerms = originalTermList.getOrgTermGroup();
1076 Assert.assertNotNull(originalTerms);
1077 Assert.assertTrue(originalTerms.size() > 0);
1079 OrgTermGroupList updatedTermList = updated.getOrgTermGroupList();
1080 Assert.assertNotNull(updatedTermList);
1081 List<OrgTermGroup> updatedTerms = updatedTermList.getOrgTermGroup();
1082 Assert.assertNotNull(updatedTerms);
1083 Assert.assertTrue(updatedTerms.size() > 0);
1085 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1086 originalTerms.get(0).getTermDisplayName(),
1087 "Value in updated record did not match submitted data.");
1091 protected PoxPayloadOut createInstance(String commonPartName,
1092 String identifier) {
1093 String shortId = identifier;
1094 String displayName = "displayName-" + shortId;
1095 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1096 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1097 displayName, shortId, commonPartName);
1102 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1103 String identifier) {
1104 String shortId = identifier;
1105 String displayName = "displayName-" + shortId;
1106 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1107 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1108 displayName, shortId, commonPartName);
1112 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName,
1113 String identifier) {
1114 Map<String, String> nonexOrgMap = new HashMap<String, String>();
1115 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
1116 PoxPayloadOut result =
1117 OrgAuthorityClientUtils.createOrganizationInstance(
1118 knownResourceRefName,
1119 nonexOrgMap, OrgAuthorityClientUtils.getTermGroupInstance(TEST_ORG_NAME), commonPartName);
1124 protected OrgauthoritiesCommon updateInstance(OrgauthoritiesCommon orgauthoritiesCommon) {
1125 OrgauthoritiesCommon result = new OrgauthoritiesCommon();
1127 result.setDisplayName("updated-" + orgauthoritiesCommon.getDisplayName());
1128 result.setVocabType("updated-" + orgauthoritiesCommon.getVocabType());
1134 protected void compareUpdatedInstances(OrgauthoritiesCommon original,
1135 OrgauthoritiesCommon updated) throws Exception {
1136 // Verify that the updated resource received the correct data.
1137 Assert.assertEquals(updated.getDisplayName(),
1138 original.getDisplayName(),
1139 "Display name in updated object did not match submitted data.");