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);
72 private final String REFNAME = "refName";
73 private final String DISPLAYNAME = "displayName";
76 public String getServicePathComponent() {
77 return OrgAuthorityClient.SERVICE_PATH_COMPONENT;
81 protected String getServiceName() {
82 return OrgAuthorityClient.SERVICE_NAME;
84 private final String TEST_SHORT_ID = "TestOrg";
85 private final String TEST_ORG_NAME = "Test Org";
86 private final String TEST_ORG_MAIN_BODY_NAME = "The real official test organization";
87 private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
88 // FIXME: Change this to a structured date once this field changes in the schema.
89 private final String TEST_ORG_FOUNDING_DATE = "May 26, 1907";
91 private String knownItemResourceShortIdentifer = null;
93 /** The known contact resource id. */
94 private String knownContactResourceId = null;
96 /** The all contact resource ids created. */
97 private Map<String, String> allContactResourceIdsCreated =
98 new HashMap<String, String>();
100 protected void setKnownItemResource(String id, String shortIdentifer) {
101 knownItemResourceId = id;
102 knownItemResourceShortIdentifer = shortIdentifer;
106 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
109 protected CollectionSpaceClient getClientInstance() {
110 return new OrgAuthorityClient();
114 protected PoxPayloadOut createInstance(String identifier) {
115 OrgAuthorityClient client = new OrgAuthorityClient();
116 String displayName = "displayName-" + identifier;
117 PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
118 displayName, identifier, client.getCommonPartName());
123 protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
124 String headerLabel = new OrgAuthorityClient().getItemCommonPartName();
126 String shortId = TEST_SHORT_ID;
127 Map<String, String> testOrgMap = new HashMap<String, String>();
128 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
129 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
130 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
132 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
133 OrgTermGroup term = new OrgTermGroup();
134 term.setTermDisplayName(TEST_ORG_NAME);
135 term.setTermName(TEST_ORG_NAME);
136 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
139 return OrgAuthorityClientUtils.createOrganizationInstance(identifier, testOrgMap, terms, headerLabel);
143 * Creates the item in authority.
145 * @param vcsid the vcsid
146 * @param authRefName the auth ref name
149 private String createItemInAuthority(String vcsid, String authRefName) {
151 final String testName = "createItemInAuthority";
152 if (logger.isDebugEnabled()) {
153 logger.debug(testName + ":...");
156 // Submit the request to the service and store the response.
157 OrgAuthorityClient client = new OrgAuthorityClient();
159 String shortId = TEST_SHORT_ID;
160 Map<String, String> testOrgMap = new HashMap<String, String>();
161 testOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
162 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, TEST_ORG_FOUNDING_DATE);
163 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
165 List<OrgTermGroup> terms = new ArrayList<OrgTermGroup>();
166 OrgTermGroup term = new OrgTermGroup();
167 term.setTermDisplayName(TEST_ORG_NAME);
168 term.setTermName(TEST_ORG_NAME);
169 term.setMainBodyName(TEST_ORG_MAIN_BODY_NAME);
172 Map<String, List<String>> testOrgRepeatablesMap = new HashMap<String, List<String>>();
173 List<String> testOrgContactNames = new ArrayList<String>();
174 testOrgContactNames.add("joe@example.org");
175 testOrgContactNames.add("sally@example.org");
176 testOrgRepeatablesMap.put(OrganizationJAXBSchema.CONTACT_NAMES, testOrgContactNames);
178 String newID = OrgAuthorityClientUtils.createItemInAuthority(
179 vcsid, authRefName, testOrgMap, terms, testOrgRepeatablesMap, client);
181 // Store the ID returned from the first item resource created
182 // for additional tests below.
183 if (knownItemResourceId == null) {
184 setKnownItemResource(newID, shortId);
185 if (logger.isDebugEnabled()) {
186 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
190 // Store the IDs from any item resources created
191 // by tests, along with the IDs of their parents, so these items
192 // can be deleted after all tests have been run.
193 allResourceItemIdsCreated.put(newID, vcsid);
199 * Creates the contact.
201 * @param testName the test name
203 @Test(dataProvider = "testName", groups = {"create"},
204 dependsOnMethods = {"createItem"})
205 public void createContact(String testName) {
207 String newID = createContactInItem(knownResourceId, knownItemResourceId);
211 * Creates the contact in item.
213 * @param parentcsid the parentcsid
214 * @param itemcsid the itemcsid
217 private String createContactInItem(String parentcsid, String itemcsid) {
219 final String testName = "createContactInItem";
220 if (logger.isDebugEnabled()) {
221 logger.debug(getTestBanner(testName, CLASS_NAME));
225 // Submit the request to the service and store the response.
226 OrgAuthorityClient client = new OrgAuthorityClient();
227 String identifier = createIdentifier();
228 PoxPayloadOut multipart =
229 ContactClientUtils.createContactInstance(parentcsid,
230 itemcsid, identifier, new ContactClient().getCommonPartName());
233 ClientResponse<Response> res =
234 client.createContact(parentcsid, itemcsid, multipart);
236 assertStatusCode(res, testName);
237 newID = OrgAuthorityClientUtils.extractId(res);
240 res.releaseConnection();
244 // Store the ID returned from the first contact resource created
245 // for additional tests below.
246 if (knownContactResourceId == null) {
247 knownContactResourceId = newID;
248 if (logger.isDebugEnabled()) {
249 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
253 // Store the IDs from any contact resources created
254 // by tests, along with the IDs of their parent items,
255 // so these items can be deleted after all tests have been run.
256 allContactResourceIdsCreated.put(newID, itemcsid);
262 * Creates the contact list.
264 * @param testName the test name
265 * @throws Exception the exception
267 @Test(dataProvider = "testName", groups = {"createList"},
268 dependsOnMethods = {"createItemList"})
269 public void createContactList(String testName) throws Exception {
270 // Add contacts to the initially-created, known item record.
271 for (int j = 0; j < nItemsToCreateInList; j++) {
272 createContact(testName);
276 // ---------------------------------------------------------------
277 // CRUD tests : READ tests
278 // ---------------------------------------------------------------
281 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
284 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
285 // groups = {"read"}, dependsOnGroups = {"create"})
286 public void read(String testName) throws Exception {
287 readInternal(testName, knownResourceId, null);
293 * @param testName the test name
294 * @throws Exception the exception
296 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
297 // groups = {"read"}, dependsOnGroups = {"create"})
298 public void readByName(String testName) throws Exception {
299 readInternal(testName, null, knownResourceShortIdentifer);
302 protected void readInternal(String testName, String CSID, String shortId) {
306 // Submit the request to the service and store the response.
307 OrgAuthorityClient client = new OrgAuthorityClient();
308 ClientResponse<String> res = null;
310 res = client.read(CSID);
311 } else if (shortId != null) {
312 res = client.readByName(shortId);
314 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
317 assertStatusCode(res, testName);
318 //FIXME: remove the following try catch once Aron fixes signatures
320 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
321 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
322 new OrgAuthorityClient().getCommonPartName(), OrgauthoritiesCommon.class);
323 if (logger.isDebugEnabled()) {
324 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
326 Assert.assertNotNull(orgAuthority);
327 } catch (Exception e) {
328 throw new RuntimeException(e);
332 res.releaseConnection();
338 * Read item in Named Auth.
340 * TODO Enable this if we really need this - it is a funky case, where we would have
341 * the shortId of the item, but the CSID of the parent authority!? Unlikely.
343 * @param testName the test name
344 * @throws Exception the exception
345 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
346 groups = {"readItem"}, dependsOnGroups = {"read"})
347 public void readItemInNamedAuth(String testName) throws Exception {
348 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
355 * @param testName the test name
356 * @throws Exception the exception
358 @Test(dataProvider = "testName", groups = {"readItem"},
359 dependsOnMethods = {"readItemInNamedAuth"})
360 public void readNamedItem(String testName) throws Exception {
361 readItemInternal(testName, knownResourceId, null, null, knownItemResourceShortIdentifer);
365 * Read item in Named Auth.
367 * @param testName the test name
368 * @throws Exception the exception
370 @Test(dataProvider = "testName", groups = {"readItem"},
371 dependsOnMethods = {"readItem"})
372 public void readItemInNamedAuth(String testName) throws Exception {
373 readItemInternal(testName, null, knownResourceShortIdentifer, knownItemResourceId, null);
377 * Read Named 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 readNamedItemInNamedAuth(String testName) throws Exception {
385 readItemInternal(testName, null, knownResourceShortIdentifer, null, knownItemResourceShortIdentifer);
388 protected void readItemInternal(String testName,
389 String authCSID, String authShortId, String itemCSID, String itemShortId)
394 // Submit the request to the service and store the response.
395 OrgAuthorityClient client = new OrgAuthorityClient();
396 ClientResponse<String> res = null;
397 if (authCSID != null) {
398 if (itemCSID != null) {
399 res = client.readItem(authCSID, itemCSID);
400 } else if (itemShortId != null) {
401 res = client.readNamedItem(authCSID, itemShortId);
403 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
405 } else if (authShortId != null) {
406 if (itemCSID != null) {
407 res = client.readItemInNamedAuthority(authShortId, itemCSID);
408 } else if (itemShortId != null) {
409 res = client.readNamedItemInNamedAuthority(authShortId, itemShortId);
411 Assert.fail("readInternal: Internal error. One of CSID or shortId must be non-null");
414 Assert.fail("readInternal: Internal error. One of authCSID or authShortId must be non-null");
417 assertStatusCode(res, testName);
418 // Check whether we've received a organization.
419 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
420 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
421 client.getItemCommonPartName(), OrganizationsCommon.class);
422 Assert.assertNotNull(organization);
423 boolean showFull = true;
424 if (showFull && logger.isDebugEnabled()) {
425 logger.debug(testName + ": returned payload:");
426 logger.debug(objectAsXmlString(organization, OrganizationsCommon.class));
429 // Check that the organization item is within the expected OrgAuthority.
430 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
432 // Verify the number and contents of values in a repeatable field,
433 // as created in the instance record used for testing.
434 List<String> contactNames = organization.getContactNames().getContactName();
435 Assert.assertTrue(contactNames.size() > 0);
436 Assert.assertNotNull(contactNames.get(0));
440 res.releaseConnection();
446 protected void verifyReadItemInstance(OrganizationsCommon item) throws Exception {
447 List<String> contactNames = item.getContactNames().getContactName();
448 Assert.assertTrue(contactNames.size() > 0);
449 Assert.assertNotNull(contactNames.get(0));
453 * Verify illegal item display name.
455 * @param testName the test name
456 * @throws Exception the exception
458 @Test(dataProvider = "testName")
459 public void verifyIllegalItemDisplayName(String testName) throws Exception {
460 // Perform setup for read.
463 // First read our known resource.
465 OrgAuthorityClient client = new OrgAuthorityClient();
466 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
467 OrganizationsCommon organization = null;
469 assertStatusCode(res, testName);
470 // Check whether organization has expected displayName.
471 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
472 organization = (OrganizationsCommon) extractPart(input,
473 client.getItemCommonPartName(), OrganizationsCommon.class);
474 Assert.assertNotNull(organization);
477 res.releaseConnection();
482 // Make an invalid UPDATE request, without a display name
484 OrgTermGroupList termList = organization.getOrgTermGroupList();
485 Assert.assertNotNull(termList);
486 List<OrgTermGroup> terms = termList.getOrgTermGroup();
487 Assert.assertNotNull(terms);
488 Assert.assertTrue(terms.size() > 0);
489 terms.get(0).setTermDisplayName(null);
490 terms.get(0).setTermName(null);
492 setupUpdateWithInvalidBody(); // we expect a failure
493 // Submit the updated resource to the service and store the response.
494 PoxPayloadOut output = new PoxPayloadOut(OrgAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
495 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(),
497 res = client.updateItem(knownResourceId, knownItemResourceId, output);
499 assertStatusCode(res, testName);
502 res.releaseConnection();
510 * @param testName the test name
511 * @throws Exception the exception
513 @Test(dataProvider = "testName", groups = {"readItem"},
514 dependsOnMethods = {"readItem"})
515 public void readContact(String testName) throws Exception {
519 // Submit the request to the service and store the response.
520 OrgAuthorityClient client = new OrgAuthorityClient();
521 ClientResponse<String> res =
522 client.readContact(knownResourceId, knownItemResourceId,
523 knownContactResourceId);
525 assertStatusCode(res, testName);
526 // Check whether we've received a contact.
527 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
528 ContactsCommon contact = (ContactsCommon) extractPart(input,
529 new ContactClient().getCommonPartName(), ContactsCommon.class);
530 Assert.assertNotNull(contact);
531 boolean showFull = true;
532 if (showFull && logger.isDebugEnabled()) {
533 logger.debug(testName + ": returned payload:");
534 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
536 Assert.assertEquals(contact.getInAuthority(), knownResourceId);
537 Assert.assertEquals(contact.getInItem(), knownItemResourceId);
540 res.releaseConnection();
546 * Read contact non existent.
548 * @param testName the test name
550 @Test(dataProvider = "testName", groups = {"readItem"},
551 dependsOnMethods = {"readContact"})
552 public void readContactNonExistent(String testName) {
554 setupReadNonExistent();
556 // Submit the request to the service and store the response.
557 OrgAuthorityClient client = new OrgAuthorityClient();
558 ClientResponse<String> res =
559 client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
561 int statusCode = res.getStatus();
563 // Check the status code of the response: does it match
564 // the expected response(s)?
565 if (logger.isDebugEnabled()) {
566 logger.debug(testName + ": status = " + statusCode);
568 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
569 invalidStatusCodeMessage(testRequestType, statusCode));
570 Assert.assertEquals(statusCode, testExpectedStatusCode);
573 res.releaseConnection();
582 // @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
583 public void readItemList(String testName) {
584 readItemList(knownAuthorityWithItems, null);
588 * Read item list by authority name.
591 // @Test(dataProvider = "testName",
592 // dependsOnMethods = {"readItem"})
593 public void readItemListByName(String testName) {
594 readItemList(null, READITEMS_SHORT_IDENTIFIER);
600 * @param vcsid the vcsid
601 * @param name the name
603 private void readItemList(String vcsid, String name) {
605 final String testName = "readItemList";
609 // Submit the request to the service and store the response.
610 OrgAuthorityClient client = new OrgAuthorityClient();
611 ClientResponse<AbstractCommonList> res = null;
613 res = client.readItemList(vcsid, null, null);
614 } else if (name != null) {
615 res = client.readItemListForNamedAuthority(name, null, null);
617 Assert.fail("readItemList passed null csid and name!");
620 AbstractCommonList list = null;
622 assertStatusCode(res, testName);
623 list = res.getEntity();
626 res.releaseConnection();
630 List<AbstractCommonList.ListItem> items = list.getListItem();
631 int nItemsReturned = items.size();
632 // There will be 'nItemsToCreateInList'
633 // items created by the createItemList test,
634 // all associated with the same parent resource.
635 int nExpectedItems = nItemsToCreateInList;
636 if (logger.isDebugEnabled()) {
637 logger.debug(testName + ": Expected "
638 + nExpectedItems + " items; got: " + nItemsReturned);
640 Assert.assertEquals(nItemsReturned, nExpectedItems);
642 for (AbstractCommonList.ListItem item : items) {
644 AbstractCommonListUtils.ListItemGetElementValue(item, REFNAME);
645 Assert.assertTrue((null != value), "Item refName is null!");
647 AbstractCommonListUtils.ListItemGetElementValue(item, DISPLAYNAME);
648 Assert.assertTrue((null != value), "Item displayName is null!");
650 if (logger.isTraceEnabled()) {
651 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
658 @Test(groups = {"readList"},
659 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.readItemList"})
660 public void readContactList() {
661 readContactList(knownResourceId, knownItemResourceId);
667 * @param parentcsid the parentcsid
668 * @param itemcsid the itemcsid
670 private void readContactList(String parentcsid, String itemcsid) {
671 final String testName = "readContactList";
675 // Submit the request to the service and store the response.
676 OrgAuthorityClient client = new OrgAuthorityClient();
677 ClientResponse<AbstractCommonList> res =
678 client.readContactList(parentcsid, itemcsid);
679 AbstractCommonList list = null;
681 assertStatusCode(res, testName);
682 list = res.getEntity();
685 res.releaseConnection();
689 List<AbstractCommonList.ListItem> listitems =
691 int nItemsReturned = listitems.size();
692 // There will be one item created, associated with a
693 // known parent resource, by the createItem test.
695 // In addition, there will be 'nItemsToCreateInList'
696 // additional items created by the createItemList test,
697 // all associated with the same parent resource.
698 int nExpectedItems = nItemsToCreateInList + 1;
699 if (logger.isDebugEnabled()) {
700 logger.debug(testName + ": Expected "
701 + nExpectedItems + " items; got: " + nItemsReturned);
703 Assert.assertEquals(nItemsReturned, nExpectedItems);
705 // Optionally output additional data about list members for debugging.
706 boolean iterateThroughList = false;
707 if (iterateThroughList && logger.isDebugEnabled()) {
708 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
715 * @param testName the test name
716 * @throws Exception the exception
718 @Test(dataProvider = "testName", groups = {"update"},
719 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractAuthorityServiceTest.updateItem"})
720 public void updateContact(String testName) throws Exception {
724 // Retrieve the contents of a resource to update.
725 OrgAuthorityClient client = new OrgAuthorityClient();
726 ClientResponse<String> res =
727 client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
728 ContactsCommon contact = null;
730 assertStatusCode(res, testName);
731 if (logger.isDebugEnabled()) {
732 logger.debug("got Contact to update with ID: "
733 + knownContactResourceId
734 + " in item: " + knownItemResourceId
735 + " in parent: " + knownResourceId);
737 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
738 contact = (ContactsCommon) extractPart(input,
739 new ContactClient().getCommonPartName(), ContactsCommon.class);
740 Assert.assertNotNull(contact);
743 res.releaseConnection();
747 // Verify the contents of this resource
748 AddressGroupList addressGroupList = contact.getAddressGroupList();
749 Assert.assertNotNull(addressGroupList);
750 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
751 Assert.assertNotNull(addressGroups);
752 Assert.assertTrue(addressGroups.size() > 0);
753 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
754 Assert.assertNotNull(addressPlace1);
756 // Update the contents of this resource.
757 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
758 contact.setAddressGroupList(addressGroupList);
759 if (logger.isDebugEnabled()) {
760 logger.debug("to be updated Contact");
761 logger.debug(objectAsXmlString(contact,
762 ContactsCommon.class));
765 // Submit the updated resource to the service and store the response.
766 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
767 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
768 commonPart.setLabel(new ContactClient().getCommonPartName());
770 res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
772 assertStatusCode(res, testName);
773 // Retrieve the updated resource and verify that its contents exist.
774 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
775 ContactsCommon updatedContact =
776 (ContactsCommon) extractPart(input,
777 new ContactClient().getCommonPartName(), ContactsCommon.class);
778 Assert.assertNotNull(updatedContact);
780 // Verify that the updated resource received the correct data.
781 Assert.assertEquals(updatedContact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
782 contact.getAddressGroupList().getAddressGroup().get(0).getAddressPlace1(),
783 "Data in updated object did not match submitted data.");
786 res.releaseConnection();
792 * Update non existent contact.
794 * @param testName the test name
795 * @throws Exception the exception
797 @Test(dataProvider = "testName", groups = {"update"},
798 dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
799 public void updateNonExistentContact(String testName) throws Exception {
800 // Currently a no-op test
803 // ---------------------------------------------------------------
804 // CRUD tests : DELETE tests
805 // ---------------------------------------------------------------
807 // Note: delete sub-resources in ascending hierarchical order,
808 // before deleting their parents.
812 * @param testName the test name
813 * @throws Exception the exception
815 @Test(dataProvider = "testName", groups = {"delete"},
816 dependsOnMethods = {"updateContact"})
817 public void deleteContact(String testName) throws Exception {
821 if (logger.isDebugEnabled()) {
822 logger.debug("parentcsid =" + knownResourceId
823 + " itemcsid = " + knownItemResourceId
824 + " csid = " + knownContactResourceId);
827 // Submit the request to the service and store the response.
828 OrgAuthorityClient client = new OrgAuthorityClient();
829 ClientResponse<Response> res =
830 client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
832 assertStatusCode(res, testName);
835 res.releaseConnection();
841 public void delete(String testName) throws Exception {
842 // Do nothing. See localDelete(). This ensure proper test order.
845 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
846 public void localDelete(String testName) throws Exception {
847 super.delete(testName);
851 public void deleteItem(String testName) throws Exception {
852 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
853 // its dependencies will get run first and then we can call the base class' delete method.
856 @Test(dataProvider = "testName", groups = {"delete"},
857 dependsOnMethods = {"verifyIllegalItemDisplayName", "testContactSubmitRequest", "deleteContact"})
858 public void localDeleteItem(String testName) throws Exception {
859 super.deleteItem(testName);
863 * Delete non existent contact.
865 * @param testName the test name
867 @Test(dataProvider = "testName", groups = {"delete"},
868 dependsOnMethods = {"deleteContact"})
869 public void deleteNonExistentContact(String testName) {
871 setupDeleteNonExistent();
873 // Submit the request to the service and store the response.
874 OrgAuthorityClient client = new OrgAuthorityClient();
875 ClientResponse<Response> res =
876 client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
878 assertStatusCode(res, testName);
881 res.releaseConnection();
887 * Test contact submit request.
889 @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
890 public void testContactSubmitRequest() {
892 // Expected status code: 200 OK
893 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
895 // Submit the request to the service and store the response.
896 String method = ServiceRequestType.READ.httpMethodName();
897 String url = getContactResourceURL(knownResourceId,
898 knownItemResourceId, knownContactResourceId);
899 int statusCode = submitRequest(method, url);
901 // Check the status code of the response: does it match
902 // the expected response(s)?
903 if (logger.isDebugEnabled()) {
904 logger.debug("testContactSubmitRequest: url=" + url
905 + " status=" + statusCode);
907 Assert.assertEquals(statusCode, EXPECTED_STATUS);
911 // ---------------------------------------------------------------
912 // Cleanup of resources created during testing
913 // ---------------------------------------------------------------
915 * Deletes all resources created by tests, after all tests have been run.
917 * This cleanup method will always be run, even if one or more tests fail.
918 * For this reason, it attempts to remove all resources created
919 * at any point during testing, even if some of those resources
920 * may be expected to be deleted by certain tests.
922 @AfterClass(alwaysRun = true)
924 public void cleanUp() {
925 String noTest = System.getProperty("noTestCleanup");
926 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
927 if (logger.isDebugEnabled()) {
928 logger.debug("Skipping Cleanup phase ...");
932 if (logger.isDebugEnabled()) {
933 logger.debug("Cleaning up temporary resources created for testing ...");
936 String parentResourceId;
937 String itemResourceId;
938 String contactResourceId;
939 // Clean up contact resources.
940 parentResourceId = knownResourceId;
941 OrgAuthorityClient client = new OrgAuthorityClient();
942 for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
943 contactResourceId = entry.getKey();
944 itemResourceId = entry.getValue();
945 // Note: Any non-success responses from the delete operation
946 // below are ignored and not reported.
947 ClientResponse<Response> res =
948 client.deleteContact(parentResourceId, itemResourceId, contactResourceId);
949 res.releaseConnection();
951 // Clean up item resources.
952 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
953 itemResourceId = entry.getKey();
954 parentResourceId = entry.getValue();
955 // Note: Any non-success responses from the delete operation
956 // below are ignored and not reported.
957 ClientResponse<Response> res =
958 client.deleteItem(parentResourceId, itemResourceId);
959 res.releaseConnection();
961 // Clean up parent resources.
966 // ---------------------------------------------------------------
967 // Utility methods used by tests above
968 // ---------------------------------------------------------------
970 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
973 * Gets the item service path component.
975 * @return the item service path component
977 public String getItemServicePathComponent() {
978 return AuthorityClient.ITEMS;
982 * Gets the contact service path component.
984 * @return the contact service path component
986 public String getContactServicePathComponent() {
987 return ContactClient.SERVICE_PATH_COMPONENT;
991 * Returns the root URL for the item service.
993 * This URL consists of a base URL for all services, followed by
994 * a path component for the owning parent, followed by the
995 * path component for the items.
997 * @param parentResourceIdentifier An identifier (such as a UUID) for the
998 * parent authority resource of the relevant item resource.
1000 * @return The root URL for the item service.
1002 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1003 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1007 * Returns the URL of a specific item resource managed by a service, and
1008 * designated by an identifier (such as a universally unique ID, or UUID).
1010 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1011 * parent authority resource of the relevant item resource.
1013 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1016 * @return The URL of a specific item resource managed by a service.
1018 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1019 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1023 * Returns the root URL for the contact service.
1025 * This URL consists of a base URL for all services, followed by
1026 * a path component for the owning authority, followed by the
1027 * path component for the owning item, followed by the path component
1028 * for the contact service.
1030 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1031 * parent authority resource of the relevant item resource.
1033 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1036 * @return The root URL for the contact service.
1038 protected String getContactServiceRootURL(String parentResourceIdentifier,
1039 String itemResourceIdentifier) {
1040 return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/"
1041 + getContactServicePathComponent();
1045 * Returns the URL of a specific contact resource managed by a service, and
1046 * designated by an identifier (such as a universally unique ID, or UUID).
1048 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1049 * parent resource of the relevant item resource.
1051 * @param resourceIdentifier An identifier (such as a UUID) for an
1054 * @return The URL of a specific resource managed by a service.
1056 protected String getContactResourceURL(String parentResourceIdentifier,
1057 String itemResourceIdentifier, String contactResourceIdentifier) {
1058 return getContactServiceRootURL(parentResourceIdentifier,
1059 itemResourceIdentifier) + "/" + contactResourceIdentifier;
1063 public void authorityTests(String testName) {
1064 // TODO Auto-generated method stub
1069 protected String createItemInAuthority(String authorityId) {
1070 return createItemInAuthority(authorityId, null /*refname*/);
1074 protected OrganizationsCommon updateItemInstance(OrganizationsCommon organizationsCommon) {
1076 OrganizationsCommon result = organizationsCommon;
1078 OrgTermGroupList termList = result.getOrgTermGroupList();
1079 Assert.assertNotNull(termList);
1080 List<OrgTermGroup> terms = termList.getOrgTermGroup();
1081 Assert.assertNotNull(terms);
1082 Assert.assertTrue(terms.size() > 0);
1083 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
1084 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
1090 protected void compareUpdatedItemInstances(OrganizationsCommon original,
1091 OrganizationsCommon updated) throws Exception {
1093 OrgTermGroupList originalTermList = original.getOrgTermGroupList();
1094 Assert.assertNotNull(originalTermList);
1095 List<OrgTermGroup> originalTerms = originalTermList.getOrgTermGroup();
1096 Assert.assertNotNull(originalTerms);
1097 Assert.assertTrue(originalTerms.size() > 0);
1099 OrgTermGroupList updatedTermList = updated.getOrgTermGroupList();
1100 Assert.assertNotNull(updatedTermList);
1101 List<OrgTermGroup> updatedTerms = updatedTermList.getOrgTermGroup();
1102 Assert.assertNotNull(updatedTerms);
1103 Assert.assertTrue(updatedTerms.size() > 0);
1105 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
1106 originalTerms.get(0).getTermDisplayName(),
1107 "Value in updated record did not match submitted data.");
1111 protected PoxPayloadOut createInstance(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);
1122 protected PoxPayloadOut createNonExistenceInstance(String commonPartName,
1123 String identifier) {
1124 String shortId = identifier;
1125 String displayName = "displayName-" + shortId;
1126 //String baseRefName = OrgAuthorityClientUtils.createOrgAuthRefName(shortId, null);
1127 PoxPayloadOut result = OrgAuthorityClientUtils.createOrgAuthorityInstance(
1128 displayName, shortId, commonPartName);
1132 protected PoxPayloadOut createNonExistenceItemInstance(String commonPartName,
1133 String identifier) {
1134 Map<String, String> nonexOrgMap = new HashMap<String, String>();
1135 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
1136 PoxPayloadOut result =
1137 OrgAuthorityClientUtils.createOrganizationInstance(
1138 knownResourceRefName,
1139 nonexOrgMap, OrgAuthorityClientUtils.getTermGroupInstance(TEST_ORG_NAME), commonPartName);
1144 protected OrgauthoritiesCommon updateInstance(OrgauthoritiesCommon orgauthoritiesCommon) {
1145 OrgauthoritiesCommon result = new OrgauthoritiesCommon();
1147 result.setDisplayName("updated-" + orgauthoritiesCommon.getDisplayName());
1148 result.setVocabType("updated-" + orgauthoritiesCommon.getVocabType());
1154 protected void compareUpdatedInstances(OrgauthoritiesCommon original,
1155 OrgauthoritiesCommon updated) throws Exception {
1156 // Verify that the updated resource received the correct data.
1157 Assert.assertEquals(updated.getDisplayName(),
1158 original.getDisplayName(),
1159 "Display name in updated object did not match submitted data.");