1 package org.collectionspace.services.client.test;
3 import java.lang.reflect.Array;
4 import java.util.ArrayList;
5 import java.util.HashMap;
9 import javax.ws.rs.core.Response;
11 import org.collectionspace.services.client.AbstractCommonListUtils;
12 import org.collectionspace.services.client.AuthorityClient;
13 import org.collectionspace.services.client.AuthorityClientImpl;
14 import org.collectionspace.services.client.AuthorityProxy;
15 import org.collectionspace.services.client.CollectionSpaceClient;
16 import org.collectionspace.services.client.PayloadInputPart;
17 import org.collectionspace.services.client.PayloadOutputPart;
18 import org.collectionspace.services.client.PoxPayloadIn;
19 import org.collectionspace.services.client.PoxPayloadOut;
20 import org.collectionspace.services.client.XmlTools;
21 import org.collectionspace.services.client.workflow.WorkflowClient;
22 import org.collectionspace.services.jaxb.AbstractCommonList;
23 import org.dom4j.Document;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.testng.Assert;
27 import org.testng.annotations.Test;
33 * @param <AUTHORITY_COMMON_TYPE>
34 * @param <AUTHORITY_ITEM_TYPE>
36 * All CRUD related authority test classes should extend this class.
39 public abstract class AbstractAuthorityServiceTest<AUTHORITY_COMMON_TYPE, AUTHORITY_ITEM_TYPE>
40 extends AbstractPoxServiceTestImpl<AbstractCommonList, AUTHORITY_COMMON_TYPE> {
42 private final Logger logger = LoggerFactory.getLogger(AbstractAuthorityServiceTest.class);
44 protected String knownResourceShortIdentifer = null;
45 protected static final String READITEMS_SHORT_IDENTIFIER = "resourceWithItems" + random.nextInt(1000);
46 protected String knownAuthorityWithItems = null;
47 protected String knownAuthorityWithItemsIdentifier = null;
49 protected static final String SAS_IDENTIFIER = "SAS";
51 private static final int SAS_ITEMLIST_SIZE = 5;
52 protected String knownSASAuthorityResourceId = null;
53 protected String knownSASAuthorityResourceIdentifier = null;
54 protected List<String> knownSASItemIdentifiersList = new ArrayList<String>();
55 protected String knownSASItemResourceId = null;
56 protected HashMap<String, String> allSASResourceItemIdsCreated = new HashMap<String, String>(); /* itemURN, parentURN */;
58 protected String knownResourceRefName = null;
59 protected String knownItemResourceId = null;
60 protected String knownItemResourceShortIdentifer = null;
61 protected int nItemsToCreateInList = 5;
62 protected String TEST_SHORTID = "johnWayneActor";
65 * Abstract methods that subclasses must override/implement
72 public abstract void authorityTests(String testName);
80 abstract protected String createItemInAuthority(AuthorityClient client, String vcsid, String shortId);
85 * @param authorityItem
88 protected abstract AUTHORITY_ITEM_TYPE updateItemInstance(final AUTHORITY_ITEM_TYPE authorityItem);
96 protected abstract void compareUpdatedItemInstances(AUTHORITY_ITEM_TYPE original,
97 AUTHORITY_ITEM_TYPE updated,
98 boolean compareRevNumbers) throws Exception;
106 protected void compareUpdatedItemInstances(AUTHORITY_ITEM_TYPE original,
107 AUTHORITY_ITEM_TYPE updated) throws Exception {
108 compareUpdatedItemInstances(original, updated, false);
114 * @param shortIdentifer
116 protected void setKnownItemResource(String id, String shortIdentifer ) {
117 knownItemResourceId = id;
118 knownItemResourceShortIdentifer = shortIdentifer;
124 * @param shortIdentifer
127 protected void setKnownResource(String id, String shortIdentifer,
129 knownResourceId = id;
130 knownResourceShortIdentifer = shortIdentifer;
131 knownResourceRefName = refName;
138 protected String getSASAuthorityIdentifier() {
139 // TODO Auto-generated method stub
140 return this.getKnowResourceIdentifier() + SAS_IDENTIFIER;
148 protected String getUrnIdentifier(String shortId) {
149 return String.format("urn:cspace:name(%s)", shortId);
153 * Sets up create tests.
155 protected void setupSync() {
156 testExpectedStatusCode = this.STATUS_OK;
157 testRequestType = ServiceRequestType.SYNC;
158 testSetup(testExpectedStatusCode, testRequestType);
162 * Gets a client to the SAS (Shared Authority Server)
166 protected AuthorityClient getSASClientInstance() {
167 return (AuthorityClient) this.getClientInstance(CollectionSpaceClient.SAS_CLIENT_PROPERTIES_FILENAME);
171 * Returns the root URL for a service.
173 * This URL consists of a base URL for all services, followed by
174 * a path component for the owning vocabulary, followed by the
175 * path component for the items.
177 * @return The root URL for a service.
179 protected String getItemServiceRootURL(String parentResourceIdentifier) {
180 return getResourceURL(parentResourceIdentifier) + "/" + getServicePathItemsComponent();
184 * Returns the URL of a specific resource managed by a service, and
185 * designated by an identifier (such as a universally unique ID, or UUID).
187 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
189 * @return The URL of a specific resource managed by a service.
191 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
192 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
196 * For authorities we override this method so we can save the shortid.
199 protected String createWithIdentifier(String testName, String identifier) throws Exception {
200 String csid = createResource(testName, identifier);
201 // Store the ID returned from the first resource created
202 // for additional tests below.
203 if (getKnowResourceId() == null) {
204 setKnownResource(csid, identifier /*shortId*/, null /*refname*/ );
205 if (logger.isDebugEnabled()) {
206 logger.debug(testName + ": Setting knownResourceId=" + getKnowResourceId());
213 @Test(dependsOnMethods = {"readItem", "CRUDTests"})
214 public void testItemSubmitRequest() {
216 // Expected status code: 200 OK
217 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
219 // Submit the request to the service and store the response.
220 String method = ServiceRequestType.READ.httpMethodName();
221 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
222 int statusCode = submitRequest(method, url);
224 // Check the status code of the response: does it match
225 // the expected response(s)?
226 if (logger.isDebugEnabled()) {
227 logger.debug("testItemSubmitRequest: url=" + url
228 + " status=" + statusCode);
230 Assert.assertEquals(statusCode, EXPECTED_STATUS);
234 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
235 dependsOnMethods = {"readItem"})
236 public void verifyIgnoredUpdateWithInAuthority(String testName) throws Exception {
240 // Submit the request to the service and store the response.
241 AuthorityClient client = (AuthorityClient)this.getClientInstance();
242 Response res = client.readItem(knownResourceId, knownItemResourceId);
243 AUTHORITY_ITEM_TYPE vitem = null;
245 int statusCode = res.getStatus();
247 // Check the status code of the response: does it match
248 // the expected response(s)?
249 if (logger.isDebugEnabled()) {
250 logger.debug(testName + " read authority:" + knownResourceId + "/Item:"
251 + knownItemResourceId + " status = " + statusCode);
253 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
254 invalidStatusCodeMessage(testRequestType, statusCode));
255 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
257 vitem = extractItemCommonPartValue(res);
258 Assert.assertNotNull(vitem);
259 // Try to Update with new parent vocab (use self, for test).
260 Assert.assertEquals(client.getInAuthority(vitem), knownResourceId,
261 "VocabularyItem inAuthority does not match knownResourceId.");
262 client.setInAuthority(vitem, knownItemResourceId);
268 // Submit the updated resource to the service and store the response.
269 PoxPayloadOut output = this.createItemRequestTypeInstance(vitem);
270 res = client.updateItem(knownResourceId, knownItemResourceId, output);
272 int statusCode = res.getStatus();
274 // Check the status code of the response: does it match the expected response(s)?
275 if (logger.isDebugEnabled()) {
276 logger.debug(testName + ": status = " + statusCode);
278 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
279 invalidStatusCodeMessage(testRequestType, statusCode));
280 Assert.assertEquals(statusCode, testExpectedStatusCode);
285 res = client.readItem(knownResourceId, knownItemResourceId);
287 // Retrieve the updated resource and verify that the parent did not change
288 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
289 Assert.assertNotNull(updatedVocabularyItem);
291 // Verify that the updated resource received the correct data.
292 Assert.assertEquals(client.getInAuthority(updatedVocabularyItem),
294 "VocabularyItem allowed update to the parent (inAuthority).");
300 @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
301 public void createItem(String testName) {
305 String newID = createItemInAuthority((AuthorityClient) getClientInstance(), knownResourceId, getTestAuthorityItemShortId());
307 // Store the ID returned from the first item resource created
308 // for additional tests below.
309 if (knownItemResourceId == null) {
310 knownItemResourceId = newID;
311 if (null != testName && logger.isDebugEnabled()) {
312 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
318 * Sync the local with the SAS
321 @Test(dataProvider = "testName", dependsOnMethods = {"createSASItemList", "CRUDTests"})
322 public void syncWithSAS(String testName) throws Exception {
324 // First check to see if the authority supports synchronization.
326 AuthorityClient client = (AuthorityClient) this.getClientInstance();
327 if (client.supportsSync() == false) {
328 return; // Exit the test since this authority doesn't support synchronization
332 // Create an empty instance of the authority, so we can sync items with it. We're
333 // using the short ID of the SAS authority. The short ID of the local and the SAS will (must) be the same.
335 String localAuthorityId = null;
337 localAuthorityId = createResource(client, testName, knownSASAuthorityResourceIdentifier, false);
338 } catch (Exception e) {
339 Assert.assertNotNull(localAuthorityId);
343 // Now we can try to sync the SAS authority with the local one we just created.
346 Response response = client.syncByName(knownSASAuthorityResourceIdentifier); // Notice we're using the Short ID (short ID is the same on the local and SAS)
348 int statusCode = response.getStatus();
349 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
350 invalidStatusCodeMessage(testRequestType, statusCode));
351 Assert.assertEquals(statusCode, testExpectedStatusCode);
357 // Check to see if the parent authority (local) is in the "Replicated" state.
360 response = client.readByName(knownSASAuthorityResourceIdentifier); // Notice we're using the Short ID (short ID is the same on the local and SAS)
362 int statusCode = response.getStatus();
363 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
364 invalidStatusCodeMessage(testRequestType, statusCode));
365 Assert.assertEquals(statusCode, testExpectedStatusCode);
366 String workflowState = this.extractAuthorityWorkflowState(response);
367 Assert.assertTrue(workflowState.contains(WorkflowClient.WORKFLOWSTATE_REPLICATED)); // After a sync, the parent should be in the "Replicated" state
375 * Check to make sure the sync with the SAS returned the correct number of items as well as items
376 * with the correct short IDs.
379 @Test(dataProvider = "testName", dependsOnMethods = {"syncWithSAS", "CRUDTests"})
380 public void veryifySyncWithSAS(String testName) {
384 // Submit the request to the service and store the response.
385 AuthorityClient client = (AuthorityClient) getClientInstance();
387 res = client.readItemListForNamedAuthority(knownSASAuthorityResourceIdentifier, null, null);
390 int statusCode = res.getStatus();
392 // Check the status code of the response: does it match
393 // the expected response(s)?
394 if (logger.isDebugEnabled()) {
395 logger.debug(" " + testName + ": status = " + statusCode);
397 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
398 invalidStatusCodeMessage(testRequestType, statusCode));
399 Assert.assertEquals(statusCode, testExpectedStatusCode);
401 AbstractCommonList list = res.readEntity(AbstractCommonList.class);
402 List<AbstractCommonList.ListItem> items = list.getListItem();
403 int nItemsReturned = items.size();
404 long nItemsTotal = list.getTotalItems();
405 if (logger.isDebugEnabled()) {
406 logger.debug(" " + testName + ": Expected "
407 + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
409 Assert.assertEquals(nItemsTotal, SAS_ITEMLIST_SIZE);
411 for (AbstractCommonList.ListItem item : items) {
412 String shortId = AbstractCommonListUtils.ListItemGetElementValue(
413 item, AuthorityClient.SHORT_IDENTIFIER);
414 Assert.assertTrue(knownSASItemIdentifiersList.contains(shortId)); // The local short ID should be in SAS list
416 String workflowState = AbstractCommonListUtils.ListItemGetElementValue(
417 item, CollectionSpaceClient.COLLECTIONSPACE_CORE_WORKFLOWSTATE);
418 Assert.assertTrue(workflowState.contains(WorkflowClient.WORKFLOWSTATE_REPLICATED)); // Workflow state must show up in a "replicated" state
420 String refName = AbstractCommonListUtils.ListItemGetElementValue(
421 item, AuthorityClient.REF_NAME);
422 Assert.assertTrue((refName != null), "Item refName is null or not set in the item list!");
423 String termDisplayName = AbstractCommonListUtils.ListItemGetElementValue(item,
424 AuthorityClient.TERM_DISPLAY_NAME);
425 String vocabDisplayName = AbstractCommonListUtils.ListItemGetElementValue(item,
426 AuthorityClient.VOCAB_DISPLAY_NAME);
427 // One of these names needs to be set.
428 Assert.assertTrue(!(termDisplayName == null && vocabDisplayName == null), "The item's display name is null or not set in the item list!");
431 if(logger.isTraceEnabled()){
432 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
440 * SAS - Create a new authority on the SAS server.
443 @Test(dataProvider = "testName", dependsOnMethods = {"createItem", "CRUDTests"})
444 public void createSASAuthority(String testName) {
446 // First check to see if the authority supports synchronization.
448 AuthorityClient client = (AuthorityClient) this.getClientInstance();
449 if (client.supportsSync() == false) {
450 return; // Exit the test since this authority doesn't support synchronization
457 String newID = createResource(getSASClientInstance(), testName, getSASAuthorityIdentifier(), true);
458 knownSASAuthorityResourceId = newID;
459 knownSASAuthorityResourceIdentifier = getShortId(getSASClientInstance(), knownSASAuthorityResourceId);
460 if (logger.isDebugEnabled()) {
461 String.format("Created SAS authority '%s' with CSID=%s.", getSASAuthorityIdentifier(), newID);
463 } catch (Exception e) {
464 logger.info(String.format("Failed to create SAS authority '%s'.", getSASAuthorityIdentifier()));
469 * SAS - Create an item in the SAS authority on the SAS server.
472 @Test(dataProvider = "testName", dependsOnMethods = {"createSASAuthority", "CRUDTests"})
473 public void createSASItemList(String testName) {
475 // First check to see if the authority supports synchronization.
477 AuthorityClient client = (AuthorityClient) this.getClientInstance();
478 if (client.supportsSync() == false) {
479 return; // Exit the test since this authority doesn't support synchronization
485 for (int i = 0; i < SAS_ITEMLIST_SIZE; i++) {
486 String shortId = "SassyActor" + System.currentTimeMillis() + Math.abs(random.nextInt()); // short ID needs to be unique
487 String newID = createItemInAuthority(getSASClientInstance(), knownSASAuthorityResourceId, shortId);
489 // Store the ID returned from the first item resource created
490 // for additional tests below.
491 if (knownSASItemResourceId == null) {
492 knownSASItemResourceId = newID;
493 if (null != testName && logger.isDebugEnabled()) {
494 logger.debug(testName + ": knownSASItemResourceId=" + knownSASItemResourceId);
498 knownSASItemIdentifiersList.add(shortId);
501 // Keep track of the SAS authority items we create, so we can delete them from
502 // the *local* authority after we perform a sync operation. We need to keep track
503 // of the URN (not the CSID) since the CSIDs will differ on the SAS vs local.
505 this.allSASResourceItemIdsCreated.put(this.getUrnIdentifier(shortId), getUrnIdentifier(getSASAuthorityIdentifier()));
510 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
511 dependsOnMethods = {"createItem"})
512 public void createItemList(String testName) throws Exception {
513 knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
514 knownAuthorityWithItemsIdentifier = getShortId(knownAuthorityWithItems);
515 for (int j = 0; j < nItemsToCreateInList; j++) {
516 createItemInAuthority((AuthorityClient) getClientInstance(), knownAuthorityWithItems, this.getTestAuthorityItemShortId(true));
520 private String getShortId(AuthorityClient client, String authorityCsid) throws Exception {
521 String result = null;
523 // Submit the request to the service and store the response.
524 Response res = client.read(authorityCsid);
526 int statusCode = res.getStatus();
527 result = this.extractAuthorityShortId(res);
535 private String getShortId(String authorityCsid) throws Exception {
536 AuthorityClient client = (AuthorityClient) getClientInstance();
537 return getShortId(client, authorityCsid);
543 * @param testName the test name
544 * @throws Exception the exception
546 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
547 dependsOnMethods = {"CRUDTests"})
548 public void readByName(String testName) throws Exception {
552 // Submit the request to the service and store the response.
553 AuthorityClient client = (AuthorityClient) this.getClientInstance();
554 Response res = client.readByName(getKnowResourceIdentifier());
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);
567 AUTHORITY_COMMON_TYPE commonPart = extractCommonPartValue(res);
568 Assert.assertNotNull(commonPart);
575 * Extracts the common part item from a service's item payload.
581 public AUTHORITY_ITEM_TYPE extractItemCommonPartValue(Response res) throws Exception {
582 AUTHORITY_ITEM_TYPE result = null;
584 AuthorityClient client = (AuthorityClient) getClientInstance();
585 PayloadInputPart payloadInputPart = extractPart(res, client.getItemCommonPartName());
586 if (payloadInputPart != null) {
587 result = (AUTHORITY_ITEM_TYPE) payloadInputPart.getBody();
589 Assert.assertNotNull(result,
590 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
597 * Extracts the short ID from a service request payload
603 protected String extractAuthorityShortId(Response res) throws Exception {
604 String result = null;
606 PoxPayloadIn input = new PoxPayloadIn((String)res.readEntity(getEntityResponseType()));
607 Document document = input.getDOMDocument();
608 result = XmlTools.getElementValue(document, "//" + AuthorityClient.SHORT_IDENTIFIER);
614 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
615 dependsOnMethods = {"readItem"})
616 public void readItemNonExistent(String testName) {
618 setupReadNonExistent();
620 // Submit the request to the service and store the response.
621 AuthorityClient client = (AuthorityClient) getClientInstance();
622 Response res = client.readItem(knownResourceId, NON_EXISTENT_ID);
624 int statusCode = res.getStatus();
626 // Check the status code of the response: does it match
627 // the expected response(s)?
628 if (logger.isDebugEnabled()) {
629 logger.debug(testName + ": status = " + statusCode);
631 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
632 invalidStatusCodeMessage(testRequestType, statusCode));
633 Assert.assertEquals(statusCode, testExpectedStatusCode);
639 @Test(dataProvider = "testName",
640 dependsOnMethods = {"createItem"})
641 public void readItem(String testName) throws Exception {
645 // Submit the request to the service and store the response.
646 AuthorityClient client = (AuthorityClient) getClientInstance();
647 Response res = client.readItem(knownResourceId, knownItemResourceId);
649 int statusCode = res.getStatus();
651 // Check the status code of the response: does it match
652 // the expected response(s)?
653 if (logger.isDebugEnabled()) {
654 logger.debug(testName + ": status = " + statusCode);
656 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
657 invalidStatusCodeMessage(testRequestType, statusCode));
658 Assert.assertEquals(statusCode, testExpectedStatusCode);
660 AUTHORITY_ITEM_TYPE itemCommonPart = extractItemCommonPartValue(res);
661 Assert.assertNotNull(itemCommonPart);
662 Assert.assertEquals(client.getInAuthority(itemCommonPart), knownResourceId);
663 verifyReadItemInstance(itemCommonPart);
669 protected abstract void verifyReadItemInstance(AUTHORITY_ITEM_TYPE item) throws Exception;
671 @Test(dataProvider = "testName",
672 dependsOnMethods = {"testItemSubmitRequest", "updateItem", "verifyIgnoredUpdateWithInAuthority"})
673 public void deleteItem(String testName) throws Exception {
677 // Submit the request to the service and store the response.
678 AuthorityClient client = (AuthorityClient) getClientInstance();
679 Response res = client.deleteItem(knownResourceId, knownItemResourceId);
682 statusCode = res.getStatus();
687 // Check the status code of the response: does it match
688 // the expected response(s)?
689 if (logger.isDebugEnabled()) {
690 logger.debug("delete: status = " + statusCode);
692 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
693 invalidStatusCodeMessage(testRequestType, statusCode));
694 Assert.assertEquals(statusCode, testExpectedStatusCode);
697 protected void readItemListInt(String vcsid, String shortId, String testName) {
701 // Submit the request to the service and store the response.
702 AuthorityClient client = (AuthorityClient) getClientInstance();
705 res = client.readItemList(vcsid, null, null);
706 } else if (shortId != null) {
707 res = client.readItemListForNamedAuthority(shortId, null, null);
709 Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
712 int statusCode = res.getStatus();
714 // Check the status code of the response: does it match
715 // the expected response(s)?
716 if (logger.isDebugEnabled()) {
717 logger.debug(" " + testName + ": status = " + statusCode);
719 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
720 invalidStatusCodeMessage(testRequestType, statusCode));
721 Assert.assertEquals(statusCode, testExpectedStatusCode);
723 AbstractCommonList list = res.readEntity(AbstractCommonList.class);
724 List<AbstractCommonList.ListItem> items = list.getListItem();
725 int nItemsReturned = items.size();
726 long nItemsTotal = list.getTotalItems();
727 if (logger.isDebugEnabled()) {
728 logger.debug(" " + testName + ": Expected "
729 + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
731 Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
733 for (AbstractCommonList.ListItem item : items) {
734 String refName = AbstractCommonListUtils.ListItemGetElementValue(
735 item, AuthorityClient.REF_NAME);
736 Assert.assertTrue((refName != null), "Item refName is null or not set in the item list!");
737 String termDisplayName = AbstractCommonListUtils.ListItemGetElementValue(item,
738 AuthorityClient.TERM_DISPLAY_NAME);
739 String vocabDisplayName = AbstractCommonListUtils.ListItemGetElementValue(item,
740 AuthorityClient.VOCAB_DISPLAY_NAME);
741 // One of these names needs to be set.
742 Assert.assertTrue(!(termDisplayName == null && vocabDisplayName == null), "The item's display name is null or not set in the item list!");
745 if(logger.isTraceEnabled()){
746 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
753 @Test(dataProvider = "testName", dependsOnMethods = {"createItemList"})
754 public void readItemList(String testName) {
755 readItemListInt(knownAuthorityWithItems, null, testName);
758 @Test(dataProvider = "testName", dependsOnMethods = {"readItemList"})
759 public void readItemListByName(String testName) {
760 readItemListInt(null, knownAuthorityWithItemsIdentifier, testName);
763 @Test(dataProvider = "testName",
764 dependsOnMethods = {"deleteItem"})
765 public void deleteNonExistentItem(String testName) {
767 setupDeleteNonExistent();
769 // Submit the request to the service and store the response.
770 AuthorityClient client = (AuthorityClient) getClientInstance();
771 Response res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
774 statusCode = res.getStatus();
779 // Check the status code of the response: does it match
780 // the expected response(s)?
781 if (logger.isDebugEnabled()) {
782 logger.debug(testName + ": status = " + statusCode);
784 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
785 invalidStatusCodeMessage(testRequestType, statusCode));
786 Assert.assertEquals(statusCode, testExpectedStatusCode);
789 protected String getServicePathItemsComponent() {
790 return AuthorityClient.ITEMS;
793 public PoxPayloadOut createItemRequestTypeInstance(AUTHORITY_ITEM_TYPE itemTypeInstance) {
794 PoxPayloadOut result = null;
796 AuthorityClient client = (AuthorityClient) getClientInstance();
797 PoxPayloadOut payloadOut = new PoxPayloadOut(this.getServicePathItemsComponent());
798 PayloadOutputPart part = payloadOut.addPart(client.getItemCommonPartName(), itemTypeInstance);
805 * Update an Authority item.
810 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
811 dependsOnMethods = {"readItem", "CRUDTests", "verifyIgnoredUpdateWithInAuthority"})
812 public void updateItem(String testName) throws Exception {
815 AUTHORITY_ITEM_TYPE theUpdate = null;
817 // Retrieve the contents of a resource to update.
818 AuthorityClient client = (AuthorityClient)this.getClientInstance();
819 Response res = client.readItem(knownResourceId, knownItemResourceId);
821 if (logger.isDebugEnabled()) {
822 logger.debug(testName + ": read status = " + res.getStatus());
824 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
826 if (logger.isDebugEnabled()) {
827 logger.debug("got Authority item to update with ID: "
828 + knownItemResourceId
829 + " in authority: " + knownResourceId);
831 AUTHORITY_ITEM_TYPE authorityItem = extractItemCommonPartValue(res);
832 Assert.assertNotNull(authorityItem);
834 // Update the contents of this resource.
835 theUpdate = updateItemInstance(authorityItem);
836 if (logger.isDebugEnabled()) {
837 logger.debug("\n\nTo be updated fields: CSID = " + knownItemResourceId + "\n"
838 + objectAsXmlString(theUpdate));
844 // Submit the updated resource to the service and store the response.
845 PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
846 res = client.updateItem(knownResourceId, knownItemResourceId, output);
848 int statusCode = res.getStatus();
850 // Check the status code of the response: does it match the expected response(s)?
851 if (logger.isDebugEnabled()) {
852 logger.debug("updateItem: status = " + statusCode);
854 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
855 invalidStatusCodeMessage(testRequestType, statusCode));
856 Assert.assertEquals(statusCode, testExpectedStatusCode);
858 // Retrieve the updated resource and verify that its contents exist.
859 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
860 Assert.assertNotNull(updatedVocabularyItem);
862 compareUpdatedItemInstances(theUpdate, updatedVocabularyItem);
868 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
869 dependsOnMethods = {"readItem", "CRUDTests", "veryifySyncWithSAS"})
870 public void updateLocalItemWithSync(String testName) throws Exception {
873 AUTHORITY_ITEM_TYPE theUpdate = null;
875 // Retrieve the contents of a shared authority item that we're going to update.
876 AUTHORITY_ITEM_TYPE sasAuthorityItem = null;
877 AuthorityClient sasClient = (AuthorityClient) this.getSASClientInstance();
878 Response res = sasClient.readNamedItemInNamedAuthority(knownSASAuthorityResourceIdentifier, knownSASItemIdentifiersList.get(0));
880 if (logger.isDebugEnabled()) {
881 logger.debug(testName + ": read status = " + res.getStatus());
883 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
885 sasAuthorityItem = extractItemCommonPartValue(res); // This is a SAS authority item
886 Assert.assertNotNull(sasAuthorityItem);
891 // Update the contents of this authority item so we can post an UPDATE to the server
892 theUpdate = updateItemInstance(sasAuthorityItem);
894 // Submit the updated authority item and check the response.
895 PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
896 res = sasClient.updateNamedItemInNamedAuthority(knownSASAuthorityResourceIdentifier, knownSASItemIdentifiersList.get(0), output);
897 AUTHORITY_ITEM_TYPE updatedSASAuthorityItem = null;
899 int statusCode = res.getStatus();
901 // Check the status code of the response: does it match the expected response(s)?
902 if (logger.isDebugEnabled()) {
903 logger.debug("updateItem: status = " + statusCode);
905 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
906 invalidStatusCodeMessage(testRequestType, statusCode));
907 Assert.assertEquals(statusCode, testExpectedStatusCode);
909 // Retrieve the updated authority item and verify that its contents exist.
910 updatedSASAuthorityItem = extractItemCommonPartValue(res);
911 Assert.assertNotNull(updatedSASAuthorityItem);
913 compareUpdatedItemInstances(theUpdate, updatedSASAuthorityItem);
918 // Synchronize the local item's parent authority and verify the update we just made
919 // to the SAS item appears locally after the sync
921 AuthorityClient localClient = (AuthorityClient) this.getClientInstance();
922 Response response = localClient.syncByName(knownSASAuthorityResourceIdentifier); // Notice we're using the Short ID (short ID is the same on the local and SAS)
924 int statusCode = response.getStatus();
925 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode), invalidStatusCodeMessage(testRequestType, statusCode));
926 Assert.assertEquals(statusCode, testExpectedStatusCode);
932 res = localClient.readNamedItemInNamedAuthority(knownSASAuthorityResourceIdentifier, knownSASItemIdentifiersList.get(0));
934 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
935 AUTHORITY_ITEM_TYPE syncedAuthorityItem = extractItemCommonPartValue(res);
936 Assert.assertNotNull(syncedAuthorityItem);
937 compareUpdatedItemInstances(syncedAuthorityItem, updatedSASAuthorityItem);
943 protected abstract PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier);
946 * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
948 @Test(dataProvider = "testName",
949 dependsOnMethods = {"create", "update", "updateNonExistent"})
950 public void updateNonExistentItem(String testName) throws Exception {
952 setupUpdateNonExistent();
954 // Submit the request to the service and store the response.
955 // Note: The ID used in this 'create' call may be arbitrary.
956 // The only relevant ID may be the one used in update(), below.
957 AuthorityClient client = (AuthorityClient)this.getClientInstance();
958 PoxPayloadOut multipart = createNonExistenceItemInstance(client.getItemCommonPartName(), NON_EXISTENT_ID);
959 Response res = client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
961 int statusCode = res.getStatus();
963 // Check the status code of the response: does it match
964 // the expected response(s)?
965 if (logger.isDebugEnabled()) {
966 logger.debug(testName + ": status = " + statusCode);
968 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
969 invalidStatusCodeMessage(testRequestType, statusCode));
970 Assert.assertEquals(statusCode, testExpectedStatusCode);
977 // Methods to persuade TestNG to follow the correct test dependency path
980 @Test(dataProvider = "testName",
981 dependsOnMethods = {"createItem"})
982 public void baseAuthorityTests(String testName) {
983 // Do nothing. Here just to setup a test dependency chain.
987 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
988 * refer to this method in their @Test annotation declarations.
991 @Test(dataProvider = "testName",
993 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
994 public void CRUDTests(String testName) {
995 // TODO Auto-generated method stub
999 public void cleanUp() {
1000 String noTest = System.getProperty("noTestCleanup");
1001 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1002 if (logger.isDebugEnabled()) {
1003 logger.debug("Skipping Cleanup phase ...");
1008 AuthorityClient client = (AuthorityClient) this.getClientInstance();
1009 String parentResourceId;
1010 String itemResourceId;
1012 // Clean up all authority item resources.
1014 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1015 itemResourceId = entry.getKey();
1016 parentResourceId = entry.getValue();
1017 Response response = client.deleteItem(parentResourceId, itemResourceId);
1019 int status = response.getStatus();
1020 if (status != Response.Status.OK.getStatusCode()) {
1021 logger.debug(String.format("Could not deleted authority item '%s' in authority '%s'.",
1022 itemResourceId, parentResourceId));
1029 // Clean up authority items that were the result of a sync with the SAS
1030 // all the IDs are URN (not CSIDs). The URNs work for the local items as well
1031 // as the SAS items.
1033 for (Map.Entry<String, String> entry : allSASResourceItemIdsCreated.entrySet()) {
1034 itemResourceId = entry.getKey();
1035 parentResourceId = entry.getValue();
1036 // Note: Any non-success responses from the delete operation
1037 // below are ignored and not reported.
1038 client.deleteItem(parentResourceId, itemResourceId).close();
1041 // Clean up authority items on the SAS using the SAS client.
1043 client = (AuthorityClient) this.getSASClientInstance();
1044 for (Map.Entry<String, String> entry : allSASResourceItemIdsCreated.entrySet()) {
1045 itemResourceId = entry.getKey();
1046 parentResourceId = entry.getValue();
1047 client.deleteItem(parentResourceId, itemResourceId).close();
1050 // Finally, call out superclass's cleanUp method to deleted the local authorities
1054 // Call out superclass's cleanUp method to delete the SAS authorities
1056 super.cleanUp(client);
1059 protected String getTestAuthorityItemShortId() {
1060 return getTestAuthorityItemShortId(false);
1063 protected String getTestAuthorityItemShortId(boolean makeUnique) {
1064 String result = TEST_SHORTID;
1066 if (makeUnique == true) {
1067 result = result + System.currentTimeMillis() + Math.abs(random.nextInt());