2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c)) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 package org.collectionspace.services.client.test;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.OrganizationJAXBSchema;
33 import org.collectionspace.services.client.AbstractCommonListUtils;
34 import org.collectionspace.services.client.AuthorityClient;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.ContactClient;
37 import org.collectionspace.services.client.ContactClientUtils;
38 import org.collectionspace.services.client.PayloadOutputPart;
39 import org.collectionspace.services.client.PoxPayloadIn;
40 import org.collectionspace.services.client.PoxPayloadOut;
41 import org.collectionspace.services.contact.AddressGroup;
42 import org.collectionspace.services.contact.AddressGroupList;
43 import org.collectionspace.services.contact.ContactsCommon;
44 import org.collectionspace.services.client.OrgAuthorityClient;
45 import org.collectionspace.services.client.OrgAuthorityClientUtils;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
47 import org.collectionspace.services.organization.OrgauthoritiesCommon;
48 import org.collectionspace.services.organization.OrganizationsCommon;
49 import org.collectionspace.services.organization.OrgTermGroup;
50 import org.collectionspace.services.organization.OrgTermGroupList;
52 import org.jboss.resteasy.client.ClientResponse;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.testng.Assert;
57 import org.testng.annotations.AfterClass;
58 import org.testng.annotations.Test;
61 * OrgAuthorityServiceTest, carries out tests against a
62 * deployed and running OrgAuthority Service.
64 * $LastChangedRevision$
67 public class OrgAuthorityServiceTest extends AbstractAuthorityServiceTest<OrgauthoritiesCommon, OrganizationsCommon> {
70 private final String CLASS_NAME = OrgAuthorityServiceTest.class.getName();
71 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
74 public String getServicePathComponent() {
75 return OrgAuthorityClient.SERVICE_PATH_COMPONENT;
79 protected String getServiceName() {
80 return OrgAuthorityClient.SERVICE_NAME;
82 private final String TEST_SHORT_ID = "TestOrg";
83 private final String TEST_ORG_NAME = "Test Org";
84 private final String TEST_ORG_MAIN_BODY_NAME = "The real official test organization";
85 private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
86 // FIXME: Change this to a structured date once this field changes in the schema.
87 private final String TEST_ORG_FOUNDING_DATE = "May 26, 1907";
89 private String knownItemResourceShortIdentifer = null;
91 /** The known contact resource id. */
92 private String knownContactResourceId = null;
94 /** The all contact resource ids created. */
95 private Map<String, String> allContactResourceIdsCreated =
96 new HashMap<String, String>();
98 protected void setKnownItemResource(String id, String shortIdentifer) {
99 knownItemResourceId = id;
100 knownItemResourceShortIdentifer = shortIdentifer;
104 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
107 protected CollectionSpaceClient getClientInstance() {
108 return new OrgAuthorityClient();
112 protected PoxPayloadOut createInstance(String identifier) {
113 OrgAuthorityClient client = new OrgAuthorityClient();
114 String displayName = "displayName-" + identifier;
115 PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
116 displayName, identifier, client.getCommonPartName());
121 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
122 String headerLabel = new OrgAuthorityClient().getItemCommonPartName();
124 String shortId = TEST_SHORT_ID;
125 Map<String, String> testOrgMap = new HashMap<String, String>();
126 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
127 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
128 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
130 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
131 OrgTermGroup term = new OrgTermGroup();
132 term.setTermDisplayName(TEST_ORG_NAME);
133 term.setTermName(TEST_ORG_NAME);
134 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
137 return OrgAuthorityClientUtils.createOrganizationInstance(identifier, testOrgMap, terms, headerLabel);
141 * Creates the item in authority.
143 * @param vcsid the vcsid
144 * @param authRefName the auth ref name
147 private String createItemInAuthority(String vcsid, String authRefName) {
149 final String testName = "createItemInAuthority";
150 if (logger.isDebugEnabled()) {
151 logger.debug(testName + ":...");
154 // Submit the request to the service and store the response.
155 OrgAuthorityClient client = new OrgAuthorityClient();
157 String shortId = TEST_SHORT_ID;
158 Map<String, String> testOrgMap = new HashMap<String, String>();
159 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
160 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
161 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
163 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
164 OrgTermGroup term = new OrgTermGroup();
165 term.setTermDisplayName(TEST_ORG_NAME);
166 term.setTermName(TEST_ORG_NAME);
167 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
170 Map<String, List<String>> testOrgRepeatablesMap = new HashMap<String, List<String>>();
171 List<String> testOrgContactNames = new ArrayList<String>();
172 testOrgContactNames.add("joe@example.org");
173 testOrgContactNames.add("sally@example.org");
174 testOrgRepeatablesMap.put(OrganizationJAXBSchema.CONTACT_NAMES, testOrgContactNames);
176 String newID = OrgAuthorityClientUtils.createItemInAuthority(
177 vcsid, authRefName, testOrgMap, terms, testOrgRepeatablesMap, client);
179 // Store the ID returned from the first item resource created
180 // for additional tests below.
181 if (knownItemResourceId == null) {
182 setKnownItemResource(newID, shortId);
183 if (logger.isDebugEnabled()) {
184 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
188 // Store the IDs from any item resources created
189 // by tests, along with the IDs of their parents, so these items
190 // can be deleted after all tests have been run.
191 allResourceItemIdsCreated.put(newID, vcsid);
197 * Creates the contact.
199 * @param testName the test name
201 @Test(dataProvider = "testName", groups = {"create"},
202 dependsOnMethods = {"createItem"})
203 public void createContact(String testName) {
205 String newID = createContactInItem(knownResourceId, knownItemResourceId);
209 * Creates the contact in item.
211 * @param parentcsid the parentcsid
212 * @param itemcsid the itemcsid
215 private String createContactInItem(String parentcsid, String itemcsid) {
217 final String testName = "createContactInItem";
218 if (logger.isDebugEnabled()) {
219 logger.debug(getTestBanner(testName, CLASS_NAME));
223 // Submit the request to the service and store the response.
224 OrgAuthorityClient client = new OrgAuthorityClient();
225 String identifier = createIdentifier();
226 PoxPayloadOut multipart =
227 ContactClientUtils.createContactInstance(parentcsid,
228 itemcsid, identifier, new ContactClient().getCommonPartName());
231 Response res = client.createContact(parentcsid, itemcsid, multipart);
233 assertStatusCode(res, testName);
234 newID = OrgAuthorityClientUtils.extractId(res);
241 // Store the ID returned from the first contact resource created
242 // for additional tests below.
243 if (knownContactResourceId == null) {
244 knownContactResourceId = newID;
245 if (logger.isDebugEnabled()) {
246 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
250 // Store the IDs from any contact resources created
251 // by tests, along with the IDs of their parent items,
252 // so these items can be deleted after all tests have been run.
253 allContactResourceIdsCreated.put(newID, itemcsid);
259 * Creates the contact list.
261 * @param testName the test name
262 * @throws Exception the exception
264 @Test(dataProvider = "testName", groups = {"createList"},
265 dependsOnMethods = {"createItemList"})
266 public void createContactList(String testName) throws Exception {
267 // Add contacts to the initially-created, known item record.
268 for (int j = 0; j < nItemsToCreateInList; j++) {
269 createContact(testName);
273 // ---------------------------------------------------------------
274 // CRUD tests : READ tests
275 // ---------------------------------------------------------------
278 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
281 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
282 // groups = {"read"}, dependsOnGroups = {"create"})
283 public void read(String testName) throws Exception {
284 readInternal(testName, knownResourceId, null);
290 * @param testName the test name
291 * @throws Exception the exception
293 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
294 // groups = {"read"}, dependsOnGroups = {"create"})
295 public void readByName(String testName) throws Exception {
296 readInternal(testName, null, knownResourceShortIdentifer);
299 protected void readInternal(String testName, String CSID, String shortId) {
303 // Submit the request to the service and store the response.
304 OrgAuthorityClient client = new OrgAuthorityClient();
307 res = client.read(CSID);
308 } else if (shortId != null) {
309 res = client.readByName(shortId);
311 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
314 assertStatusCode(res, testName);
315 //FIXME: remove the following try catch once Aron fixes signatures
317 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
318 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
319 new OrgAuthorityClient().getCommonPartName(), OrgauthoritiesCommon.class);
320 if (logger.isDebugEnabled()) {
321 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
323 Assert.assertNotNull(orgAuthority);
324 } catch (Exception e) {
325 throw new RuntimeException(e);
335 * Read item in Named Auth.
337 * TODO Enable this if we really need this - it is a funky case, where we would have
338 * the shortId of the item, but the CSID of the parent authority!? Unlikely.
340 * @param testName the test name
341 * @throws Exception the exception
342 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
343 groups = {"readItem"}, dependsOnGroups = {"read"})
344 public void readItemInNamedAuth(String testName) throws Exception {
345 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
352 * @param testName the test name
353 * @throws Exception the exception
355 @Test(dataProvider = "testName", groups = {"readItem"},
356 dependsOnMethods = {"readItemInNamedAuth"})
357 public void readNamedItem(String testName) throws Exception {
358 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
362 * Read item in Named Auth.
364 * @param testName the test name
365 * @throws Exception the exception
367 @Test(dataProvider = "testName", groups = {"readItem"},
368 dependsOnMethods = {"readItem"})
369 public void readItemInNamedAuth(String testName) throws Exception {
370 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
374 * Read Named item in Named Auth.
376 * @param testName the test name
377 * @throws Exception the exception
379 @Test(dataProvider = "testName", groups = {"readItem"},
380 dependsOnMethods = {"readItem"})
381 public void readNamedItemInNamedAuth(String testName) throws Exception {
382 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
385 protected void readItemInternal(String testName,
386 String authCSID, String authShortId, String itemCSID, String itemShortId)
391 // Submit the request to the service and store the response.
392 OrgAuthorityClient client = new OrgAuthorityClient();
394 if (authCSID != null) {
395 if (itemCSID != null) {
396 res = client.readItem(authCSID, itemCSID);
397 } else if (itemShortId != null) {
398 res = client.readNamedItem(authCSID, itemShortId);
400 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
402 } else if (authShortId != null) {
403 if (itemCSID != null) {
404 res = client.readItemInNamedAuthority(authShortId, itemCSID);
405 } else if (itemShortId != null) {
406 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
408 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
411 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
414 assertStatusCode(res, testName);
415 // Check whether we've received a organization.
416 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
417 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
418 client.getItemCommonPartName(), OrganizationsCommon.class);
419 Assert.assertNotNull(organization);
420 boolean showFull = true;
421 if (showFull && logger.isDebugEnabled()) {
422 logger.debug(testName + ": returned payload:");
423 logger.debug(objectAsXmlString(organization, OrganizationsCommon.class));
426 // Check that the organization item is within the expected OrgAuthority.
427 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
429 // Verify the number and contents of values in a repeatable field,
430 // as created in the instance record used for testing.
431 List<String> contactNames = organization.getContactNames().getContactName();
432 Assert.assertTrue(contactNames.size() > 0);
433 Assert.assertNotNull(contactNames.get(0));
443 protected void verifyReadItemInstance(OrganizationsCommon item) throws Exception {
444 List<String> contactNames = item.getContactNames().getContactName();
445 Assert.assertTrue(contactNames.size() > 0);
446 Assert.assertNotNull(contactNames.get(0));
450 * Verify illegal item display name.
452 * @param testName the test name
453 * @throws Exception the exception
455 @Test(dataProvider = "testName")
456 public void verifyIllegalItemDisplayName(String testName) throws Exception {
457 // Perform setup for read.
460 // First read our known resource.
462 OrgAuthorityClient client = new OrgAuthorityClient();
463 Response res = client.readItem(knownResourceId, knownItemResourceId);
464 OrganizationsCommon organization = null;
466 assertStatusCode(res, testName);
467 // Check whether organization has expected displayName.
468 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
469 organization = (OrganizationsCommon) extractPart(input,
470 client.getItemCommonPartName(), OrganizationsCommon.class);
471 Assert.assertNotNull(organization);
479 // Make an invalid UPDATE request, without a display name
481 OrgTermGroupList termList = organization.getOrgTermGroupList();
482 Assert.assertNotNull(termList);
483 List<OrgTermGroup> terms = termList.getOrgTermGroup();
484 Assert.assertNotNull(terms);
485 Assert.assertTrue(terms.size() > 0);
486 terms.get(0).setTermDisplayName(null);
487 terms.get(0).setTermName(null);
489 setupUpdateWithInvalidBody(); // we expect a failure
490 // Submit the updated resource to the service and store the response.
491 PoxPayloadOut output = new PoxPayloadOut(OrgAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
492 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(),
494 res = client.updateItem(knownResourceId, knownItemResourceId, output);
496 assertStatusCode(res, testName);
507 * @param testName the test name
508 * @throws Exception the exception
510 @Test(dataProvider = "testName", groups = {"readItem"},
511 dependsOnMethods = {"readItem"})
512 public void readContact(String testName) throws Exception {
516 // Submit the request to the service and store the response.
517 OrgAuthorityClient client = new OrgAuthorityClient();
518 Response res =client.readContact(knownResourceId, knownItemResourceId,
519 knownContactResourceId);
521 assertStatusCode(res, testName);
522 // Check whether we've received a contact.
523 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
524 ContactsCommon contact = (ContactsCommon) extractPart(input,
525 new ContactClient().getCommonPartName(), ContactsCommon.class);
526 Assert.assertNotNull(contact);
527 boolean showFull = true;
528 if (showFull && logger.isDebugEnabled()) {
529 logger.debug(testName + ": returned payload:");
530 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
532 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
533 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
542 * Read contact non existent.
544 * @param testName the test name
546 @Test(dataProvider = "testName", groups = {"readItem"},
547 dependsOnMethods = {"readContact"})
548 public void readContactNonExistent(String testName) {
550 setupReadNonExistent();
552 // Submit the request to the service and store the response.
553 OrgAuthorityClient client = new OrgAuthorityClient();
554 Response res = client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
556 int statusCode = res.getStatus();
558 // Check the status code of the response: does it match
559 // the expected response(s)?
560 if (logger.isDebugEnabled()) {
561 logger.debug(testName + ": status = " + statusCode);
563 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
564 invalidStatusCodeMessage(testRequestType, statusCode));
565 Assert.assertEquals(statusCode, testExpectedStatusCode);
577 // @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
578 public void readItemList(String testName) {
579 readItemList(knownAuthorityWithItems, null);
583 * Read item list by authority name.
586 // @Test(dataProvider = "testName",
587 // dependsOnMethods = {"readItem"})
588 public void readItemListByName(String testName) {
589 readItemList(null, READITEMS_SHORT_IDENTIFIER);
595 * @param vcsid the vcsid
596 * @param name the name
598 private void readItemList(String vcsid, String name) {
600 final String testName = "readItemList";
604 // Submit the request to the service and store the response.
605 OrgAuthorityClient client = new OrgAuthorityClient();
608 res = client.readItemList(vcsid, null, null);
609 } else if (name != null) {
610 res = client.readItemListForNamedAuthority(name, null, null);
612 Assert.fail("readItemList passed null csid and name!");
615 AbstractCommonList list = null;
617 assertStatusCode(res, testName);
618 list = res.readEntity(AbstractCommonList.class);
625 List<AbstractCommonList.ListItem> items = list.getListItem();
626 int nItemsReturned = items.size();
627 // There will be 'nItemsToCreateInList'
628 // items created by the createItemList test,
629 // all associated with the same parent resource.
630 int nExpectedItems = nItemsToCreateInList;
631 if (logger.isDebugEnabled()) {
632 logger.debug(testName + ": Expected "
633 + nExpectedItems + " items; got: " + nItemsReturned);
635 Assert.assertEquals(nItemsReturned, nExpectedItems);
637 for (AbstractCommonList.ListItem item : items) {
639 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.REF_NAME);
640 Assert.assertTrue((null != value), "Item refName is null!");
642 AbstractCommonListUtils.ListItemGetElementValue(item, OrganizationJAXBSchema.TERM_DISPLAY_NAME);
643 Assert.assertTrue((null != value), "Item termDisplayName is null!");
645 if (logger.isTraceEnabled()) {
646 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
653 @Test(groups = {"readList"},
654 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
655 public void readContactList() {
656 readContactList(knownResourceId, knownItemResourceId);
662 * @param parentcsid the parentcsid
663 * @param itemcsid the itemcsid
665 private void readContactList(String parentcsid, String itemcsid) {
666 final String testName = "readContactList";
670 // Submit the request to the service and store the response.
671 OrgAuthorityClient client = new OrgAuthorityClient();
672 Response res = client.readContactList(parentcsid, itemcsid);
673 AbstractCommonList list = null;
675 assertStatusCode(res, testName);
676 list = res.readEntity(AbstractCommonList.class);
683 List<AbstractCommonList.ListItem> listitems =
685 int nItemsReturned = listitems.size();
686 // There will be one item created, associated with a
687 // known parent resource, by the createItem test.
689 // In addition, there will be 'nItemsToCreateInList'
690 // additional items created by the createItemList test,
691 // all associated with the same parent resource.
692 int nExpectedItems = nItemsToCreateInList + 1;
693 if (logger.isDebugEnabled()) {
694 logger.debug(testName + ": Expected "
695 + nExpectedItems + " items; got: " + nItemsReturned);
697 Assert.assertEquals(nItemsReturned, nExpectedItems);
699 // Optionally output additional data about list members for debugging.
700 boolean iterateThroughList = false;
701 if (iterateThroughList && logger.isDebugEnabled()) {
702 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
709 * @param testName the test name
710 * @throws Exception the exception
712 @Test(dataProvider = "testName", groups = {"update"},
713 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
714 public void updateContact(String testName) throws Exception {
718 // Retrieve the contents of a resource to update.
719 OrgAuthorityClient client = new OrgAuthorityClient();
720 Response res = client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
721 ContactsCommon contact = null;
723 assertStatusCode(res, testName);
724 if (logger.isDebugEnabled()) {
725 logger.debug("got Contact to update with ID: "
726 + knownContactResourceId
727 + " in item: " + knownItemResourceId
728 + " in parent: " + knownResourceId);
730 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
731 contact = (ContactsCommon) extractPart(input,
732 new ContactClient().getCommonPartName(), ContactsCommon.class);
733 Assert.assertNotNull(contact);
740 // Verify the contents of this resource
741 AddressGroupList addressGroupList = contact.getAddressGroupList();
742 Assert.assertNotNull(addressGroupList);
743 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
744 Assert.assertNotNull(addressGroups);
745 Assert.assertTrue(addressGroups.size() > 0);
746 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
747 Assert.assertNotNull(addressPlace1);
749 // Update the contents of this resource.
750 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
751 contact.setAddressGroupList(addressGroupList);
752 if (logger.isDebugEnabled()) {
753 logger.debug("to be updated Contact");
754 logger.debug(objectAsXmlString(contact,
755 ContactsCommon.class));
758 // Submit the updated resource to the service and store the response.
759 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
760 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
761 commonPart.setLabel(new ContactClient().getCommonPartName());
763 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
765 assertStatusCode(res, testName);
766 // Retrieve the updated resource and verify that its contents exist.
767 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
768 ContactsCommon updatedContact =
769 (ContactsCommon) extractPart(input,
770 new ContactClient().getCommonPartName(), ContactsCommon.class);
771 Assert.assertNotNull(updatedContact);
773 // Verify that the updated resource received the correct data.
774 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
775 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
776 "Data in updated object did not match submitted data.");
785 * Update non existent contact.
787 * @param testName the test name
788 * @throws Exception the exception
790 @Test(dataProvider = "testName", groups = {"update"},
791 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
792 public void updateNonExistentContact(String testName) throws Exception {
793 // Currently a no-op test
796 // ---------------------------------------------------------------
797 // CRUD tests : DELETE tests
798 // ---------------------------------------------------------------
800 // Note: delete sub-resources in ascending hierarchical order,
801 // before deleting their parents.
805 * @param testName the test name
806 * @throws Exception the exception
808 @Test(dataProvider = "testName", groups = {"delete"},
809 dependsOnMethods = {"updateContact"})
810 public void deleteContact(String testName) throws Exception {
814 if (logger.isDebugEnabled()) {
815 logger.debug("parentcsid =" + knownResourceId
816 + " itemcsid = " + knownItemResourceId
817 + " csid = " + knownContactResourceId);
820 // Submit the request to the service and store the response.
821 OrgAuthorityClient client = new OrgAuthorityClient();
822 Response res = client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
824 assertStatusCode(res, testName);
833 public void delete(String testName) throws Exception {
834 // Do nothing. See localDelete(). This ensure proper test order.
837 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
838 public void localDelete(String testName) throws Exception {
839 super.delete(testName);
843 public void deleteItem(String testName) throws Exception {
844 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
845 // its dependencies will get run first and then we can call the base class' delete method.
848 @Test(dataProvider = "testName", groups = {"delete"},
849 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
850 public void localDeleteItem(String testName) throws Exception {
851 super.deleteItem(testName);
855 * Delete non existent contact.
857 * @param testName the test name
859 @Test(dataProvider = "testName", groups = {"delete"},
860 dependsOnMethods = {"deleteContact"})
861 public void deleteNonExistentContact(String testName) {
863 setupDeleteNonExistent();
865 // Submit the request to the service and store the response.
866 OrgAuthorityClient client = new OrgAuthorityClient();
867 Response res = client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
869 assertStatusCode(res, testName);
878 * Test contact submit request.
880 @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
881 public void testContactSubmitRequest() {
883 // Expected status code: 200 OK
884 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
886 // Submit the request to the service and store the response.
887 String method = ServiceRequestType.READ.httpMethodName();
888 String url = getContactResourceURL(knownResourceId,
889 knownItemResourceId, knownContactResourceId);
890 int statusCode = submitRequest(method, url);
892 // Check the status code of the response: does it match
893 // the expected response(s)?
894 if (logger.isDebugEnabled()) {
895 logger.debug("testContactSubmitRequest: url=" + url
896 + " status=" + statusCode);
898 Assert.assertEquals(statusCode, EXPECTED_STATUS);
902 // ---------------------------------------------------------------
903 // Cleanup of resources created during testing
904 // ---------------------------------------------------------------
906 * Deletes all resources created by tests, after all tests have been run.
908 * This cleanup method will always be run, even if one or more tests fail.
909 * For this reason, it attempts to remove all resources created
910 * at any point during testing, even if some of those resources
911 * may be expected to be deleted by certain tests.
913 @AfterClass(alwaysRun = true)
915 public void cleanUp() {
916 String noTest = System.getProperty("noTestCleanup");
917 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
918 if (logger.isDebugEnabled()) {
919 logger.debug("Skipping Cleanup phase ...");
923 if (logger.isDebugEnabled()) {
924 logger.debug("Cleaning up temporary resources created for testing ...");
927 String parentResourceId;
928 String itemResourceId;
929 String contactResourceId;
930 // Clean up contact resources.
931 parentResourceId = knownResourceId;
932 OrgAuthorityClient client = new OrgAuthorityClient();
933 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
934 contactResourceId = entry.getKey();
935 itemResourceId = entry.getValue();
936 // Note: Any non-success responses from the delete operation
937 // below are ignored and not reported.
938 client.deleteContact(parentResourceId, itemResourceId, contactResourceId).close();
940 // Clean up item resources.
941 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
942 itemResourceId = entry.getKey();
943 parentResourceId = entry.getValue();
944 // Note: Any non-success responses from the delete operation
945 // below are ignored and not reported.
946 client.deleteItem(parentResourceId, itemResourceId).close();
948 // Clean up parent resources.
953 // ---------------------------------------------------------------
954 // Utility methods used by tests above
955 // ---------------------------------------------------------------
957 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
960 * Gets the item service path component.
962 * @return the item service path component
964 public String getItemServicePathComponent() {
965 return AuthorityClient.ITEMS;
969 * Gets the contact service path component.
971 * @return the contact service path component
973 public String getContactServicePathComponent() {
974 return ContactClient.SERVICE_PATH_COMPONENT;
978 * Returns the root URL for the item service.
980 * This URL consists of a base URL for all services, followed by
981 * a path component for the owning parent, followed by the
982 * path component for the items.
984 * @param parentResourceIdentifier An identifier (such as a UUID) for the
985 * parent authority resource of the relevant item resource.
987 * @return The root URL for the item service.
989 protected String getItemServiceRootURL(String parentResourceIdentifier) {
990 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
994 * Returns the URL of a specific item resource managed by a service, and
995 * designated by an identifier (such as a universally unique ID, or UUID).
997 * @param parentResourceIdentifier An identifier (such as a UUID) for the
998 * parent authority resource of the relevant item resource.
1000 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1003 * @return The URL of a specific item resource managed by a service.
1005 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1006 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1010 * Returns the root URL for the contact service.
1012 * This URL consists of a base URL for all services, followed by
1013 * a path component for the owning authority, followed by the
1014 * path component for the owning item, followed by the path component
1015 * for the contact service.
1017 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1018 * parent authority resource of the relevant item resource.
1020 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1023 * @return The root URL for the contact service.
1025 protected String getContactServiceRootURL(String parentResourceIdentifier,
1026 String itemResourceIdentifier) {
1027 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1028 + getContactServicePathComponent();
1032 * Returns the URL of a specific contact resource managed by a service, and
1033 * designated by an identifier (such as a universally unique ID, or UUID).
1035 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1036 * parent resource of the relevant item resource.
1038 * @param resourceIdentifier An identifier (such as a UUID) for an
1041 * @return The URL of a specific resource managed by a service.
1043 protected String getContactResourceURL(String parentResourceIdentifier,
1044 String itemResourceIdentifier, String contactResourceIdentifier) {
1045 return getContactServiceRootURL(parentResourceIdentifier,
1046 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1050 public void authorityTests(String testName) {
1051 // TODO Auto-generated method stub
1056 protected String createItemInAuthority(String authorityId) {
1057 return createItemInAuthority(authorityId, null /*refname*/);
1061 protected OrganizationsCommon updateItemInstance(OrganizationsCommon organizationsCommon) {
1063 OrgTermGroupList termList = organizationsCommon.getOrgTermGroupList();
1064 Assert.assertNotNull(termList);
1065 List<OrgTermGroup> terms = termList.getOrgTermGroup();
1066 Assert.assertNotNull(terms);
1067 Assert.assertTrue(terms.size() > 0);
1068 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1069 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1070 organizationsCommon.setOrgTermGroupList(termList);
1072 return organizationsCommon;
1076 protected void compareUpdatedItemInstances(OrganizationsCommon original,
1077 OrganizationsCommon updated) throws Exception {
1079 OrgTermGroupList originalTermList = original.getOrgTermGroupList();
1080 Assert.assertNotNull(originalTermList);
1081 List<OrgTermGroup> originalTerms = originalTermList.getOrgTermGroup();
1082 Assert.assertNotNull(originalTerms);
1083 Assert.assertTrue(originalTerms.size() > 0);
1085 OrgTermGroupList updatedTermList = updated.getOrgTermGroupList();
1086 Assert.assertNotNull(updatedTermList);
1087 List<OrgTermGroup> updatedTerms = updatedTermList.getOrgTermGroup();
1088 Assert.assertNotNull(updatedTerms);
1089 Assert.assertTrue(updatedTerms.size() > 0);
1091 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1092 originalTerms.get(0).getTermDisplayName(),
1093 "Value in updated record did not match submitted data.");
1097 protected PoxPayloadOut createInstance(String commonPartName,
1098 String identifier) {
1099 String shortId = identifier;
1100 String displayName = "displayName-" + shortId;
1101 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1102 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1103 displayName, shortId, commonPartName);
1108 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1109 String identifier) {
1110 String shortId = identifier;
1111 String displayName = "displayName-" + shortId;
1112 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1113 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1114 displayName, shortId, commonPartName);
1118 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName,
1119 String identifier) {
1120 Map<String, String> nonexOrgMap = new HashMap<String, String>();
1121 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
1122 PoxPayloadOut result =
1123 OrgAuthorityClientUtils.createOrganizationInstance(
1124 knownResourceRefName,
1125 nonexOrgMap, OrgAuthorityClientUtils.getTermGroupInstance(TEST_ORG_NAME), commonPartName);
1130 protected OrgauthoritiesCommon updateInstance(OrgauthoritiesCommon orgauthoritiesCommon) {
1131 OrgauthoritiesCommon result = new OrgauthoritiesCommon();
1133 result.setDisplayName("updated-" + orgauthoritiesCommon.getDisplayName());
1134 result.setVocabType("updated-" + orgauthoritiesCommon.getVocabType());
1140 protected void compareUpdatedInstances(OrgauthoritiesCommon original,
1141 OrgauthoritiesCommon updated) throws Exception {
1142 // Verify that the updated resource received the correct data.
1143 Assert.assertEquals(updated.getDisplayName(),
1144 original.getDisplayName(),
1145 "Display name in updated object did not match submitted data.");