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;
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 OrgAuthorityClient.SERVICE_PATH_COMPONENT;
86 protected String getServiceName() {
87 return OrgAuthorityClient.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() {
117 return new OrgAuthorityClient();
121 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
122 return new OrgAuthorityClient(clientPropertiesFilename);
126 protected PoxPayloadOut createInstance(String identifier) {
127 OrgAuthorityClient client = new OrgAuthorityClient();
128 String displayName = "displayName-" + identifier;
129 PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
130 displayName, identifier, client.getCommonPartName());
135 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
136 String headerLabel = new OrgAuthorityClient().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, (OrgAuthorityClient) 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
216 @Test(dataProvider = "testName", groups = {"create"},
217 dependsOnMethods = {"createItem"})
218 public void createContact(String testName) {
220 String newID = createContactInItem(knownResourceId, knownItemResourceId);
224 * Creates the contact in item.
226 * @param parentcsid the parentcsid
227 * @param itemcsid the itemcsid
230 private String createContactInItem(String parentcsid, String itemcsid) {
232 final String testName = "createContactInItem";
233 if (logger.isDebugEnabled()) {
234 logger.debug(getTestBanner(testName, CLASS_NAME));
238 // Submit the request to the service and store the response.
239 OrgAuthorityClient client = new OrgAuthorityClient();
240 String identifier = createIdentifier();
241 PoxPayloadOut multipart =
242 ContactClientUtils.createContactInstance(parentcsid,
243 itemcsid, identifier, new ContactClient().getCommonPartName());
246 Response res = client.createContact(parentcsid, itemcsid, multipart);
248 assertStatusCode(res, testName);
249 newID = OrgAuthorityClientUtils.extractId(res);
256 // Store the ID returned from the first contact resource created
257 // for additional tests below.
258 if (knownContactResourceId == null) {
259 knownContactResourceId = newID;
260 if (logger.isDebugEnabled()) {
261 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
265 // Store the IDs from any contact resources created
266 // by tests, along with the IDs of their parent items,
267 // so these items can be deleted after all tests have been run.
268 allContactResourceIdsCreated.put(newID, itemcsid);
274 * Creates the contact list.
276 * @param testName the test name
277 * @throws Exception the exception
279 @Test(dataProvider = "testName", groups = {"createList"},
280 dependsOnMethods = {"createItemList"})
281 public void createContactList(String testName) throws Exception {
282 // Add contacts to the initially-created, known item record.
283 for (int j = 0; j < nItemsToCreateInList; j++) {
284 createContact(testName);
288 // ---------------------------------------------------------------
289 // CRUD tests : READ tests
290 // ---------------------------------------------------------------
293 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
296 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
297 // groups = {"read"}, dependsOnGroups = {"create"})
298 public void read(String testName) throws Exception {
299 readInternal(testName, knownResourceId, null);
305 * @param testName the test name
306 * @throws Exception the exception
308 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
309 // groups = {"read"}, dependsOnGroups = {"create"})
310 public void readByName(String testName) throws Exception {
311 readInternal(testName, null, knownResourceShortIdentifer);
314 protected void readInternal(String testName, String CSID, String shortId) {
318 // Submit the request to the service and store the response.
319 OrgAuthorityClient client = new OrgAuthorityClient();
322 res = client.read(CSID);
323 } else if (shortId != null) {
324 res = client.readByName(shortId);
326 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
329 assertStatusCode(res, testName);
330 //FIXME: remove the following try catch once Aron fixes signatures
332 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
333 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
334 new OrgAuthorityClient().getCommonPartName(), OrgauthoritiesCommon.class);
335 if (logger.isDebugEnabled()) {
336 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
338 Assert.assertNotNull(orgAuthority);
339 } catch (Exception e) {
340 throw new RuntimeException(e);
350 * Read item in Named Auth.
352 * TODO Enable this if we really need this - it is a funky case, where we would have
353 * the shortId of the item, but the CSID of the parent authority!? Unlikely.
355 * @param testName the test name
356 * @throws Exception the exception
357 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
358 groups = {"readItem"}, dependsOnGroups = {"read"})
359 public void readItemInNamedAuth(String testName) throws Exception {
360 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
367 * @param testName the test name
368 * @throws Exception the exception
370 @Test(dataProvider = "testName", groups = {"readItem"},
371 dependsOnMethods = {"readItemInNamedAuth"})
372 public void readNamedItem(String testName) throws Exception {
373 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
377 * Read item in Named Auth.
379 * @param testName the test name
380 * @throws Exception the exception
382 @Test(dataProvider = "testName", groups = {"readItem"},
383 dependsOnMethods = {"readItem"})
384 public void readItemInNamedAuth(String testName) throws Exception {
385 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
389 * Read Named item in Named Auth.
391 * @param testName the test name
392 * @throws Exception the exception
394 @Test(dataProvider = "testName", groups = {"readItem"},
395 dependsOnMethods = {"readItem"})
396 public void readNamedItemInNamedAuth(String testName) throws Exception {
397 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
400 protected void readItemInternal(String testName,
401 String authCSID, String authShortId, String itemCSID, String itemShortId)
406 // Submit the request to the service and store the response.
407 OrgAuthorityClient client = new OrgAuthorityClient();
409 if (authCSID != null) {
410 if (itemCSID != null) {
411 res = client.readItem(authCSID, itemCSID);
412 } else if (itemShortId != null) {
413 res = client.readNamedItem(authCSID, itemShortId);
415 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
417 } else if (authShortId != null) {
418 if (itemCSID != null) {
419 res = client.readItemInNamedAuthority(authShortId, itemCSID);
420 } else if (itemShortId != null) {
421 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
423 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
426 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
429 assertStatusCode(res, testName);
430 // Check whether we've received a organization.
431 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
432 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
433 client.getItemCommonPartName(), OrganizationsCommon.class);
434 Assert.assertNotNull(organization);
435 boolean showFull = true;
436 if (showFull && logger.isDebugEnabled()) {
437 logger.debug(testName + ": returned payload:");
438 logger.debug(objectAsXmlString(organization, OrganizationsCommon.class));
441 // Check that the organization item is within the expected OrgAuthority.
442 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
444 // Verify the number and contents of values in a repeatable field,
445 // as created in the instance record used for testing.
446 List<String> contactNames = organization.getContactNames().getContactName();
447 Assert.assertTrue(contactNames.size() > 0);
448 Assert.assertNotNull(contactNames.get(0));
458 protected void verifyReadItemInstance(OrganizationsCommon item) throws Exception {
459 List<String> contactNames = item.getContactNames().getContactName();
460 Assert.assertTrue(contactNames.size() > 0);
461 Assert.assertNotNull(contactNames.get(0));
465 * Verify illegal item display name.
467 * @param testName the test name
468 * @throws Exception the exception
470 @Test(dataProvider = "testName")
471 public void verifyIllegalItemDisplayName(String testName) throws Exception {
472 // Perform setup for read.
475 // First read our known resource.
477 OrgAuthorityClient client = new OrgAuthorityClient();
478 Response res = client.readItem(knownResourceId, knownItemResourceId);
479 OrganizationsCommon organization = null;
481 assertStatusCode(res, testName);
482 // Check whether organization has expected displayName.
483 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
484 organization = (OrganizationsCommon) extractPart(input,
485 client.getItemCommonPartName(), OrganizationsCommon.class);
486 Assert.assertNotNull(organization);
494 // Make an invalid UPDATE request, without a display name
496 OrgTermGroupList termList = organization.getOrgTermGroupList();
497 Assert.assertNotNull(termList);
498 List<OrgTermGroup> terms = termList.getOrgTermGroup();
499 Assert.assertNotNull(terms);
500 Assert.assertTrue(terms.size() > 0);
501 terms.get(0).setTermDisplayName(null);
502 terms.get(0).setTermName(null);
504 setupUpdateWithInvalidBody(); // we expect a failure
505 // Submit the updated resource to the service and store the response.
506 PoxPayloadOut output = new PoxPayloadOut(OrgAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
507 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(),
509 res = client.updateItem(knownResourceId, knownItemResourceId, output);
511 assertStatusCode(res, testName);
522 * @param testName the test name
523 * @throws Exception the exception
525 @Test(dataProvider = "testName", groups = {"readItem"},
526 dependsOnMethods = {"readItem"})
527 public void readContact(String testName) throws Exception {
531 // Submit the request to the service and store the response.
532 OrgAuthorityClient client = new OrgAuthorityClient();
533 Response res =client.readContact(knownResourceId, knownItemResourceId,
534 knownContactResourceId);
536 assertStatusCode(res, testName);
537 // Check whether we've received a contact.
538 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
539 ContactsCommon contact = (ContactsCommon) extractPart(input,
540 new ContactClient().getCommonPartName(), ContactsCommon.class);
541 Assert.assertNotNull(contact);
542 boolean showFull = true;
543 if (showFull && logger.isDebugEnabled()) {
544 logger.debug(testName + ": returned payload:");
545 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
547 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
548 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
557 * Read contact non existent.
559 * @param testName the test name
561 @Test(dataProvider = "testName", groups = {"readItem"},
562 dependsOnMethods = {"readContact"})
563 public void readContactNonExistent(String testName) {
565 setupReadNonExistent();
567 // Submit the request to the service and store the response.
568 OrgAuthorityClient client = new OrgAuthorityClient();
569 Response res = client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
571 int statusCode = res.getStatus();
573 // Check the status code of the response: does it match
574 // the expected response(s)?
575 if (logger.isDebugEnabled()) {
576 logger.debug(testName + ": status = " + statusCode);
578 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
579 invalidStatusCodeMessage(testRequestType, statusCode));
580 Assert.assertEquals(statusCode, testExpectedStatusCode);
592 // @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
593 public void readItemList(String testName) {
594 readItemList(knownAuthorityWithItems, null);
598 * Read item list by authority name.
601 // @Test(dataProvider = "testName",
602 // dependsOnMethods = {"readItem"})
603 public void readItemListByName(String testName) {
604 readItemList(null, READITEMS_SHORT_IDENTIFIER);
610 * @param vcsid the vcsid
611 * @param name the name
613 private void readItemList(String vcsid, String name) {
615 final String testName = "readItemList";
619 // Submit the request to the service and store the response.
620 OrgAuthorityClient client = new OrgAuthorityClient();
623 res = client.readItemList(vcsid, null, null);
624 } else if (name != null) {
625 res = client.readItemListForNamedAuthority(name, null, null);
627 Assert.fail("readItemList passed null csid and name!");
630 AbstractCommonList list = null;
632 assertStatusCode(res, testName);
633 list = res.readEntity(AbstractCommonList.class);
640 List<AbstractCommonList.ListItem> items = list.getListItem();
641 int nItemsReturned = items.size();
642 // There will be 'nItemsToCreateInList'
643 // items created by the createItemList test,
644 // all associated with the same parent resource.
645 int nExpectedItems = nItemsToCreateInList;
646 if (logger.isDebugEnabled()) {
647 logger.debug(testName + ": Expected "
648 + nExpectedItems + " items; got: " + nItemsReturned);
650 Assert.assertEquals(nItemsReturned, nExpectedItems);
652 for (AbstractCommonList.ListItem item : items) {
654 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.REF_NAME);
655 Assert.assertTrue((null != value), "Item refName is null!");
657 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.TERM_DISPLAY_NAME);
658 Assert.assertTrue((null != value), "Item termDisplayName is null!");
660 if (logger.isTraceEnabled()) {
661 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
668 @Test(groups = {"readList"},
669 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
670 public void readContactList() {
671 readContactList(knownResourceId, knownItemResourceId);
677 * @param parentcsid the parentcsid
678 * @param itemcsid the itemcsid
680 private void readContactList(String parentcsid, String itemcsid) {
681 final String testName = "readContactList";
685 // Submit the request to the service and store the response.
686 OrgAuthorityClient client = new OrgAuthorityClient();
687 Response res = client.readContactList(parentcsid, itemcsid);
688 AbstractCommonList list = null;
690 assertStatusCode(res, testName);
691 list = res.readEntity(AbstractCommonList.class);
698 List<AbstractCommonList.ListItem> listitems =
700 int nItemsReturned = listitems.size();
701 // There will be one item created, associated with a
702 // known parent resource, by the createItem test.
704 // In addition, there will be 'nItemsToCreateInList'
705 // additional items created by the createItemList test,
706 // all associated with the same parent resource.
707 int nExpectedItems = nItemsToCreateInList + 1;
708 if (logger.isDebugEnabled()) {
709 logger.debug(testName + ": Expected "
710 + nExpectedItems + " items; got: " + nItemsReturned);
712 Assert.assertEquals(nItemsReturned, nExpectedItems);
714 // Optionally output additional data about list members for debugging.
715 boolean iterateThroughList = false;
716 if (iterateThroughList && logger.isDebugEnabled()) {
717 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
724 * @param testName the test name
725 * @throws Exception the exception
727 @Test(dataProvider = "testName", groups = {"update"},
728 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
729 public void updateContact(String testName) throws Exception {
733 // Retrieve the contents of a resource to update.
734 OrgAuthorityClient client = new OrgAuthorityClient();
735 Response res = client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
736 ContactsCommon contact = null;
738 assertStatusCode(res, testName);
739 if (logger.isDebugEnabled()) {
740 logger.debug("got Contact to update with ID: "
741 + knownContactResourceId
742 + " in item: " + knownItemResourceId
743 + " in parent: " + knownResourceId);
745 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
746 contact = (ContactsCommon) extractPart(input,
747 new ContactClient().getCommonPartName(), ContactsCommon.class);
748 Assert.assertNotNull(contact);
755 // Verify the contents of this resource
756 AddressGroupList addressGroupList = contact.getAddressGroupList();
757 Assert.assertNotNull(addressGroupList);
758 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
759 Assert.assertNotNull(addressGroups);
760 Assert.assertTrue(addressGroups.size() > 0);
761 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
762 Assert.assertNotNull(addressPlace1);
764 // Update the contents of this resource.
765 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
766 contact.setAddressGroupList(addressGroupList);
767 if (logger.isDebugEnabled()) {
768 logger.debug("to be updated Contact");
769 logger.debug(objectAsXmlString(contact,
770 ContactsCommon.class));
773 // Submit the updated resource to the service and store the response.
774 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
775 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
776 commonPart.setLabel(new ContactClient().getCommonPartName());
778 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
780 assertStatusCode(res, testName);
781 // Retrieve the updated resource and verify that its contents exist.
782 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
783 ContactsCommon updatedContact =
784 (ContactsCommon) extractPart(input,
785 new ContactClient().getCommonPartName(), ContactsCommon.class);
786 Assert.assertNotNull(updatedContact);
788 // Verify that the updated resource received the correct data.
789 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
790 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
791 "Data in updated object did not match submitted data.");
800 * Update non existent contact.
802 * @param testName the test name
803 * @throws Exception the exception
805 @Test(dataProvider = "testName", groups = {"update"},
806 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
807 public void updateNonExistentContact(String testName) throws Exception {
808 // Currently a no-op test
811 // ---------------------------------------------------------------
812 // CRUD tests : DELETE tests
813 // ---------------------------------------------------------------
815 // Note: delete sub-resources in ascending hierarchical order,
816 // before deleting their parents.
820 * @param testName the test name
821 * @throws Exception the exception
823 @Test(dataProvider = "testName", groups = {"delete"},
824 dependsOnMethods = {"updateContact"})
825 public void deleteContact(String testName) throws Exception {
829 if (logger.isDebugEnabled()) {
830 logger.debug("parentcsid =" + knownResourceId
831 + " itemcsid = " + knownItemResourceId
832 + " csid = " + knownContactResourceId);
835 // Submit the request to the service and store the response.
836 OrgAuthorityClient client = new OrgAuthorityClient();
837 Response res = client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
839 assertStatusCode(res, testName);
848 public void delete(String testName) throws Exception {
849 // Do nothing. See localDelete(). This ensure proper test order.
852 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
853 public void localDelete(String testName) throws Exception {
854 super.delete(testName);
858 public void deleteItem(String testName) throws Exception {
859 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
860 // its dependencies will get run first and then we can call the base class' delete method.
863 @Test(dataProvider = "testName", groups = {"delete"},
864 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
865 public void localDeleteItem(String testName) throws Exception {
866 super.deleteItem(testName);
870 * Delete non existent contact.
872 * @param testName the test name
874 @Test(dataProvider = "testName", groups = {"delete"},
875 dependsOnMethods = {"deleteContact"})
876 public void deleteNonExistentContact(String testName) {
878 setupDeleteNonExistent();
880 // Submit the request to the service and store the response.
881 OrgAuthorityClient client = new OrgAuthorityClient();
882 Response res = client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
884 assertStatusCode(res, testName);
893 * Test contact submit request.
895 @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
896 public void testContactSubmitRequest() {
898 // Expected status code: 200 OK
899 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
901 // Submit the request to the service and store the response.
902 String method = ServiceRequestType.READ.httpMethodName();
903 String url = getContactResourceURL(knownResourceId,
904 knownItemResourceId, knownContactResourceId);
905 int statusCode = submitRequest(method, url);
907 // Check the status code of the response: does it match
908 // the expected response(s)?
909 if (logger.isDebugEnabled()) {
910 logger.debug("testContactSubmitRequest: url=" + url
911 + " status=" + statusCode);
913 Assert.assertEquals(statusCode, EXPECTED_STATUS);
917 // ---------------------------------------------------------------
918 // Cleanup of resources created during testing
919 // ---------------------------------------------------------------
921 * Deletes all resources created by tests, after all tests have been run.
923 * This cleanup method will always be run, even if one or more tests fail.
924 * For this reason, it attempts to remove all resources created
925 * at any point during testing, even if some of those resources
926 * may be expected to be deleted by certain tests.
928 @AfterClass(alwaysRun = true)
930 public void cleanUp() {
931 String noTest = System.getProperty("noTestCleanup");
932 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
933 if (logger.isDebugEnabled()) {
934 logger.debug("Skipping Cleanup phase ...");
938 if (logger.isDebugEnabled()) {
939 logger.debug("Cleaning up temporary resources created for testing ...");
942 String parentResourceId;
943 String itemResourceId;
944 String contactResourceId;
945 // Clean up contact resources.
946 parentResourceId = knownResourceId;
947 OrgAuthorityClient client = new OrgAuthorityClient();
948 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
949 contactResourceId = entry.getKey();
950 itemResourceId = entry.getValue();
951 // Note: Any non-success responses from the delete operation
952 // below are ignored and not reported.
953 client.deleteContact(parentResourceId, itemResourceId, contactResourceId).close();
956 // Clean up parent resources.
961 // ---------------------------------------------------------------
962 // Utility methods used by tests above
963 // ---------------------------------------------------------------
965 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
968 * Gets the item service path component.
970 * @return the item service path component
972 public String getItemServicePathComponent() {
973 return AuthorityClient.ITEMS;
977 * Gets the contact service path component.
979 * @return the contact service path component
981 public String getContactServicePathComponent() {
982 return ContactClient.SERVICE_PATH_COMPONENT;
986 * Returns the root URL for the item service.
988 * This URL consists of a base URL for all services, followed by
989 * a path component for the owning parent, followed by the
990 * path component for the items.
992 * @param parentResourceIdentifier An identifier (such as a UUID) for the
993 * parent authority resource of the relevant item resource.
995 * @return The root URL for the item service.
997 protected String getItemServiceRootURL(String parentResourceIdentifier) {
998 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1002 * Returns the URL of a specific item resource managed by a service, and
1003 * designated by an identifier (such as a universally unique ID, or UUID).
1005 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1006 * parent authority resource of the relevant item resource.
1008 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1011 * @return The URL of a specific item resource managed by a service.
1013 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1014 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1018 * Returns the root URL for the contact service.
1020 * This URL consists of a base URL for all services, followed by
1021 * a path component for the owning authority, followed by the
1022 * path component for the owning item, followed by the path component
1023 * for the contact service.
1025 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1026 * parent authority resource of the relevant item resource.
1028 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1031 * @return The root URL for the contact service.
1033 protected String getContactServiceRootURL(String parentResourceIdentifier,
1034 String itemResourceIdentifier) {
1035 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1036 + getContactServicePathComponent();
1040 * Returns the URL of a specific contact resource managed by a service, and
1041 * designated by an identifier (such as a universally unique ID, or UUID).
1043 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1044 * parent resource of the relevant item resource.
1046 * @param resourceIdentifier An identifier (such as a UUID) for an
1049 * @return The URL of a specific resource managed by a service.
1051 protected String getContactResourceURL(String parentResourceIdentifier,
1052 String itemResourceIdentifier, String contactResourceIdentifier) {
1053 return getContactServiceRootURL(parentResourceIdentifier,
1054 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1058 public void authorityTests(String testName) {
1059 // TODO Auto-generated method stub
1064 protected OrganizationsCommon updateItemInstance(OrganizationsCommon organizationsCommon) {
1066 OrgTermGroupList termList = organizationsCommon.getOrgTermGroupList();
1067 Assert.assertNotNull(termList);
1068 List<OrgTermGroup> terms = termList.getOrgTermGroup();
1069 Assert.assertNotNull(terms);
1070 Assert.assertTrue(terms.size() > 0);
1071 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1072 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1073 organizationsCommon.setOrgTermGroupList(termList);
1075 return organizationsCommon;
1079 protected void compareUpdatedItemInstances(OrganizationsCommon original,
1080 OrganizationsCommon updated) throws Exception {
1082 OrgTermGroupList originalTermList = original.getOrgTermGroupList();
1083 Assert.assertNotNull(originalTermList);
1084 List<OrgTermGroup> originalTerms = originalTermList.getOrgTermGroup();
1085 Assert.assertNotNull(originalTerms);
1086 Assert.assertTrue(originalTerms.size() > 0);
1088 OrgTermGroupList updatedTermList = updated.getOrgTermGroupList();
1089 Assert.assertNotNull(updatedTermList);
1090 List<OrgTermGroup> updatedTerms = updatedTermList.getOrgTermGroup();
1091 Assert.assertNotNull(updatedTerms);
1092 Assert.assertTrue(updatedTerms.size() > 0);
1094 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1095 originalTerms.get(0).getTermDisplayName(),
1096 "Value in updated record did not match submitted data.");
1100 protected PoxPayloadOut createInstance(String commonPartName,
1101 String identifier) {
1102 String shortId = identifier;
1103 String displayName = "displayName-" + shortId;
1104 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1105 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1106 displayName, shortId, commonPartName);
1111 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1112 String identifier) {
1113 String shortId = identifier;
1114 String displayName = "displayName-" + shortId;
1115 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1116 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1117 displayName, shortId, commonPartName);
1121 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName,
1122 String identifier) {
1123 Map<String, String> nonexOrgMap = new HashMap<String, String>();
1124 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
1125 PoxPayloadOut result =
1126 OrgAuthorityClientUtils.createOrganizationInstance(
1127 knownResourceRefName,
1128 nonexOrgMap, OrgAuthorityClientUtils.getTermGroupInstance(TEST_ORG_NAME), commonPartName);
1133 protected OrgauthoritiesCommon updateInstance(OrgauthoritiesCommon orgauthoritiesCommon) {
1134 OrgauthoritiesCommon result = new OrgauthoritiesCommon();
1136 result.setDisplayName("updated-" + orgauthoritiesCommon.getDisplayName());
1137 result.setVocabType("updated-" + orgauthoritiesCommon.getVocabType());
1143 protected void compareUpdatedInstances(OrgauthoritiesCommon original,
1144 OrgauthoritiesCommon updated) throws Exception {
1145 // Verify that the updated resource received the correct data.
1146 Assert.assertEquals(updated.getDisplayName(),
1147 original.getDisplayName(),
1148 "Display name in updated object did not match submitted data.");