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, AUTHORITY_ITEM_TYPE updated) throws Exception;
101 * @param shortIdentifer
103 protected void setKnownItemResource(String id, String shortIdentifer ) {
104 knownItemResourceId = id;
105 knownItemResourceShortIdentifer = shortIdentifer;
111 * @param shortIdentifer
114 protected void setKnownResource(String id, String shortIdentifer,
116 knownResourceId = id;
117 knownResourceShortIdentifer = shortIdentifer;
118 knownResourceRefName = refName;
125 protected String getSASAuthorityIdentifier() {
126 // TODO Auto-generated method stub
127 return this.getKnowResourceIdentifier() + SAS_IDENTIFIER;
135 protected String getUrnIdentifier(String shortId) {
136 return String.format("urn:cspace:name(%s)", shortId);
140 * Sets up create tests.
142 protected void setupSync() {
143 testExpectedStatusCode = this.STATUS_OK;
144 testRequestType = ServiceRequestType.SYNC;
145 testSetup(testExpectedStatusCode, testRequestType);
149 * Gets a client to the SAS (Shared Authority Server)
153 protected AuthorityClient getSASClientInstance() {
154 return (AuthorityClient) this.getClientInstance(CollectionSpaceClient.SAS_CLIENT_PROPERTIES_FILENAME);
158 * Returns the root URL for a service.
160 * This URL consists of a base URL for all services, followed by
161 * a path component for the owning vocabulary, followed by the
162 * path component for the items.
164 * @return The root URL for a service.
166 protected String getItemServiceRootURL(String parentResourceIdentifier) {
167 return getResourceURL(parentResourceIdentifier) + "/" + getServicePathItemsComponent();
171 * Returns the URL of a specific resource managed by a service, and
172 * designated by an identifier (such as a universally unique ID, or UUID).
174 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
176 * @return The URL of a specific resource managed by a service.
178 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
179 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
183 * For authorities we override this method so we can save the shortid.
186 protected String createWithIdentifier(String testName, String identifier) throws Exception {
187 String csid = createResource(testName, identifier);
188 // Store the ID returned from the first resource created
189 // for additional tests below.
190 if (getKnowResourceId() == null) {
191 setKnownResource(csid, identifier /*shortId*/, null /*refname*/ );
192 if (logger.isDebugEnabled()) {
193 logger.debug(testName + ": Setting knownResourceId=" + getKnowResourceId());
200 @Test(dependsOnMethods = {"readItem", "CRUDTests"})
201 public void testItemSubmitRequest() {
203 // Expected status code: 200 OK
204 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
206 // Submit the request to the service and store the response.
207 String method = ServiceRequestType.READ.httpMethodName();
208 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
209 int statusCode = submitRequest(method, url);
211 // Check the status code of the response: does it match
212 // the expected response(s)?
213 if (logger.isDebugEnabled()) {
214 logger.debug("testItemSubmitRequest: url=" + url
215 + " status=" + statusCode);
217 Assert.assertEquals(statusCode, EXPECTED_STATUS);
221 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
222 dependsOnMethods = {"readItem"})
223 public void verifyIgnoredUpdateWithInAuthority(String testName) throws Exception {
227 // Submit the request to the service and store the response.
228 AuthorityClient client = (AuthorityClient)this.getClientInstance();
229 Response res = client.readItem(knownResourceId, knownItemResourceId);
230 AUTHORITY_ITEM_TYPE vitem = null;
232 int statusCode = res.getStatus();
234 // Check the status code of the response: does it match
235 // the expected response(s)?
236 if (logger.isDebugEnabled()) {
237 logger.debug(testName + " read authority:" + knownResourceId + "/Item:"
238 + knownItemResourceId + " status = " + statusCode);
240 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
241 invalidStatusCodeMessage(testRequestType, statusCode));
242 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
244 vitem = extractItemCommonPartValue(res);
245 Assert.assertNotNull(vitem);
246 // Try to Update with new parent vocab (use self, for test).
247 Assert.assertEquals(client.getInAuthority(vitem), knownResourceId,
248 "VocabularyItem inAuthority does not match knownResourceId.");
249 client.setInAuthority(vitem, knownItemResourceId);
255 // Submit the updated resource to the service and store the response.
256 PoxPayloadOut output = this.createItemRequestTypeInstance(vitem);
257 res = client.updateItem(knownResourceId, knownItemResourceId, output);
259 int statusCode = res.getStatus();
261 // Check the status code of the response: does it match the expected response(s)?
262 if (logger.isDebugEnabled()) {
263 logger.debug(testName + ": status = " + statusCode);
265 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
266 invalidStatusCodeMessage(testRequestType, statusCode));
267 Assert.assertEquals(statusCode, testExpectedStatusCode);
272 res = client.readItem(knownResourceId, knownItemResourceId);
274 // Retrieve the updated resource and verify that the parent did not change
275 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
276 Assert.assertNotNull(updatedVocabularyItem);
278 // Verify that the updated resource received the correct data.
279 Assert.assertEquals(client.getInAuthority(updatedVocabularyItem),
281 "VocabularyItem allowed update to the parent (inAuthority).");
287 @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
288 public void createItem(String testName) {
292 String newID = createItemInAuthority((AuthorityClient) getClientInstance(), knownResourceId, getTestAuthorityItemShortId());
294 // Store the ID returned from the first item resource created
295 // for additional tests below.
296 if (knownItemResourceId == null) {
297 knownItemResourceId = newID;
298 if (null != testName && logger.isDebugEnabled()) {
299 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
305 * Sync the local with the SAS
308 @Test(dataProvider = "testName", dependsOnMethods = {"createSASItemList", "CRUDTests"})
309 public void syncWithSAS(String testName) throws Exception {
311 // First check to see if the authority supports synchronization.
313 AuthorityClient client = (AuthorityClient) this.getClientInstance();
314 if (client.supportsSync() == false) {
315 return; // Exit the test since this authority doesn't support synchronization
319 // Create an empty instance of the authority, so we can sync items with it. We're
320 // using the short ID of the SAS authority. The short ID of the local and the SAS will (must) be the same.
322 String localAuthorityId = null;
324 localAuthorityId = createResource(client, testName, knownSASAuthorityResourceIdentifier, false);
325 } catch (Exception e) {
326 Assert.assertNotNull(localAuthorityId);
330 // Now we can try to sync the SAS authority with the local one we just created.
333 Response response = client.syncByName(knownSASAuthorityResourceIdentifier); // Notice we're using the Short ID (short ID is the same on the local and SAS)
335 int statusCode = response.getStatus();
336 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
337 invalidStatusCodeMessage(testRequestType, statusCode));
338 Assert.assertEquals(statusCode, testExpectedStatusCode);
344 // Check to see if the parent authority (local) is in the "Replicated" state.
347 response = client.readByName(knownSASAuthorityResourceIdentifier); // Notice we're using the Short ID (short ID is the same on the local and SAS)
349 int statusCode = response.getStatus();
350 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
351 invalidStatusCodeMessage(testRequestType, statusCode));
352 Assert.assertEquals(statusCode, testExpectedStatusCode);
353 String workflowState = this.extractAuthorityWorkflowState(response);
354 Assert.assertTrue(workflowState.contains(WorkflowClient.WORKFLOWSTATE_REPLICATED)); // After a sync, the parent should be in the "Replicated" state
362 * Check to make sure the sync with the SAS returned the correct number of items as well as items
363 * with the correct short IDs.
366 @Test(dataProvider = "testName", dependsOnMethods = {"syncWithSAS", "CRUDTests"})
367 public void veryifySyncWithSAS(String testName) {
371 // Submit the request to the service and store the response.
372 AuthorityClient client = (AuthorityClient) getClientInstance();
374 res = client.readItemListForNamedAuthority(knownSASAuthorityResourceIdentifier, null, null);
377 int statusCode = res.getStatus();
379 // Check the status code of the response: does it match
380 // the expected response(s)?
381 if (logger.isDebugEnabled()) {
382 logger.debug(" " + testName + ": status = " + statusCode);
384 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
385 invalidStatusCodeMessage(testRequestType, statusCode));
386 Assert.assertEquals(statusCode, testExpectedStatusCode);
388 AbstractCommonList list = res.readEntity(AbstractCommonList.class);
389 List<AbstractCommonList.ListItem> items = list.getListItem();
390 int nItemsReturned = items.size();
391 long nItemsTotal = list.getTotalItems();
392 if (logger.isDebugEnabled()) {
393 logger.debug(" " + testName + ": Expected "
394 + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
396 Assert.assertEquals(nItemsTotal, SAS_ITEMLIST_SIZE);
398 for (AbstractCommonList.ListItem item : items) {
399 String shortId = AbstractCommonListUtils.ListItemGetElementValue(
400 item, AuthorityClient.SHORT_IDENTIFIER);
401 Assert.assertTrue(knownSASItemIdentifiersList.contains(shortId)); // The local short ID should be in SAS list
403 String workflowState = AbstractCommonListUtils.ListItemGetElementValue(
404 item, CollectionSpaceClient.COLLECTIONSPACE_CORE_WORKFLOWSTATE);
405 Assert.assertTrue(workflowState.contains(WorkflowClient.WORKFLOWSTATE_REPLICATED)); // Workflow state must show up in a "replicated" state
407 String refName = AbstractCommonListUtils.ListItemGetElementValue(
408 item, AuthorityClient.REF_NAME);
409 Assert.assertTrue((refName != null), "Item refName is null or not set in the item list!");
410 String termDisplayName = AbstractCommonListUtils.ListItemGetElementValue(item,
411 AuthorityClient.TERM_DISPLAY_NAME);
412 String vocabDisplayName = AbstractCommonListUtils.ListItemGetElementValue(item,
413 AuthorityClient.VOCAB_DISPLAY_NAME);
414 // One of these names needs to be set.
415 Assert.assertTrue(!(termDisplayName == null && vocabDisplayName == null), "The item's display name is null or not set in the item list!");
418 if(logger.isTraceEnabled()){
419 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
427 * SAS - Create a new authority on the SAS server.
430 @Test(dataProvider = "testName", dependsOnMethods = {"createItem", "CRUDTests"})
431 public void createSASAuthority(String testName) {
433 // First check to see if the authority supports synchronization.
435 AuthorityClient client = (AuthorityClient) this.getClientInstance();
436 if (client.supportsSync() == false) {
437 return; // Exit the test since this authority doesn't support synchronization
444 String newID = createResource(getSASClientInstance(), testName, getSASAuthorityIdentifier(), true);
445 knownSASAuthorityResourceId = newID;
446 knownSASAuthorityResourceIdentifier = getShortId(getSASClientInstance(), knownSASAuthorityResourceId);
447 if (logger.isDebugEnabled()) {
448 String.format("Created SAS authority '%s' with CSID=%s.", getSASAuthorityIdentifier(), newID);
450 } catch (Exception e) {
451 logger.info(String.format("Failed to create SAS authority '%s'.", getSASAuthorityIdentifier()));
456 * SAS - Create an item in the SAS authority on the SAS server.
459 @Test(dataProvider = "testName", dependsOnMethods = {"createSASAuthority", "CRUDTests"})
460 public void createSASItemList(String testName) {
462 // First check to see if the authority supports synchronization.
464 AuthorityClient client = (AuthorityClient) this.getClientInstance();
465 if (client.supportsSync() == false) {
466 return; // Exit the test since this authority doesn't support synchronization
472 for (int i = 0; i < SAS_ITEMLIST_SIZE; i++) {
473 String shortId = "SassyActor" + System.currentTimeMillis() + Math.abs(random.nextInt()); // short ID needs to be unique
474 String newID = createItemInAuthority(getSASClientInstance(), knownSASAuthorityResourceId, shortId);
476 // Store the ID returned from the first item resource created
477 // for additional tests below.
478 if (knownSASItemResourceId == null) {
479 knownSASItemResourceId = newID;
480 if (null != testName && logger.isDebugEnabled()) {
481 logger.debug(testName + ": knownSASItemResourceId=" + knownSASItemResourceId);
485 knownSASItemIdentifiersList.add(shortId);
488 // Keep track of the SAS authority items we create, so we can delete them from
489 // the *local* authority after we perform a sync operation. We need to keep track
490 // of the URN (not the CSID) since the CSIDs will differ on the SAS vs local.
492 this.allSASResourceItemIdsCreated.put(this.getUrnIdentifier(shortId), getUrnIdentifier(getSASAuthorityIdentifier()));
497 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
498 dependsOnMethods = {"createItem"})
499 public void createItemList(String testName) throws Exception {
500 knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
501 knownAuthorityWithItemsIdentifier = getShortId(knownAuthorityWithItems);
502 for (int j = 0; j < nItemsToCreateInList; j++) {
503 createItemInAuthority((AuthorityClient) getClientInstance(), knownAuthorityWithItems, this.getTestAuthorityItemShortId(true));
507 private String getShortId(AuthorityClient client, String authorityCsid) throws Exception {
508 String result = null;
510 // Submit the request to the service and store the response.
511 Response res = client.read(authorityCsid);
513 int statusCode = res.getStatus();
514 result = this.extractAuthorityShortId(res);
522 private String getShortId(String authorityCsid) throws Exception {
523 AuthorityClient client = (AuthorityClient) getClientInstance();
524 return getShortId(client, authorityCsid);
530 * @param testName the test name
531 * @throws Exception the exception
533 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
534 dependsOnMethods = {"CRUDTests"})
535 public void readByName(String testName) throws Exception {
539 // Submit the request to the service and store the response.
540 AuthorityClient client = (AuthorityClient) this.getClientInstance();
541 Response res = client.readByName(getKnowResourceIdentifier());
543 int statusCode = res.getStatus();
545 // Check the status code of the response: does it match
546 // the expected response(s)?
547 if (logger.isDebugEnabled()) {
548 logger.debug(testName + ": status = " + statusCode);
550 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
551 invalidStatusCodeMessage(testRequestType, statusCode));
552 Assert.assertEquals(statusCode, testExpectedStatusCode);
554 AUTHORITY_COMMON_TYPE commonPart = extractCommonPartValue(res);
555 Assert.assertNotNull(commonPart);
562 * Extracts the common part item from a service's item payload.
568 public AUTHORITY_ITEM_TYPE extractItemCommonPartValue(Response res) throws Exception {
569 AUTHORITY_ITEM_TYPE result = null;
571 AuthorityClient client = (AuthorityClient) getClientInstance();
572 PayloadInputPart payloadInputPart = extractPart(res, client.getItemCommonPartName());
573 if (payloadInputPart != null) {
574 result = (AUTHORITY_ITEM_TYPE) payloadInputPart.getBody();
576 Assert.assertNotNull(result,
577 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
584 * Extracts the short ID from a service request payload
590 protected String extractAuthorityShortId(Response res) throws Exception {
591 String result = null;
593 PoxPayloadIn input = new PoxPayloadIn((String)res.readEntity(getEntityResponseType()));
594 Document document = input.getDOMDocument();
595 result = XmlTools.getElementValue(document, "//" + AuthorityClient.SHORT_IDENTIFIER);
601 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
602 dependsOnMethods = {"readItem"})
603 public void readItemNonExistent(String testName) {
605 setupReadNonExistent();
607 // Submit the request to the service and store the response.
608 AuthorityClient client = (AuthorityClient) getClientInstance();
609 Response res = client.readItem(knownResourceId, NON_EXISTENT_ID);
611 int statusCode = res.getStatus();
613 // Check the status code of the response: does it match
614 // the expected response(s)?
615 if (logger.isDebugEnabled()) {
616 logger.debug(testName + ": status = " + statusCode);
618 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
619 invalidStatusCodeMessage(testRequestType, statusCode));
620 Assert.assertEquals(statusCode, testExpectedStatusCode);
626 @Test(dataProvider = "testName",
627 dependsOnMethods = {"createItem"})
628 public void readItem(String testName) throws Exception {
632 // Submit the request to the service and store the response.
633 AuthorityClient client = (AuthorityClient) getClientInstance();
634 Response res = client.readItem(knownResourceId, knownItemResourceId);
636 int statusCode = res.getStatus();
638 // Check the status code of the response: does it match
639 // the expected response(s)?
640 if (logger.isDebugEnabled()) {
641 logger.debug(testName + ": status = " + statusCode);
643 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
644 invalidStatusCodeMessage(testRequestType, statusCode));
645 Assert.assertEquals(statusCode, testExpectedStatusCode);
647 AUTHORITY_ITEM_TYPE itemCommonPart = extractItemCommonPartValue(res);
648 Assert.assertNotNull(itemCommonPart);
649 Assert.assertEquals(client.getInAuthority(itemCommonPart), knownResourceId);
650 verifyReadItemInstance(itemCommonPart);
656 protected abstract void verifyReadItemInstance(AUTHORITY_ITEM_TYPE item) throws Exception;
658 @Test(dataProvider = "testName",
659 dependsOnMethods = {"testItemSubmitRequest", "updateItem", "verifyIgnoredUpdateWithInAuthority"})
660 public void deleteItem(String testName) throws Exception {
664 // Submit the request to the service and store the response.
665 AuthorityClient client = (AuthorityClient) getClientInstance();
666 Response res = client.deleteItem(knownResourceId, knownItemResourceId);
669 statusCode = res.getStatus();
674 // Check the status code of the response: does it match
675 // the expected response(s)?
676 if (logger.isDebugEnabled()) {
677 logger.debug("delete: status = " + statusCode);
679 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
680 invalidStatusCodeMessage(testRequestType, statusCode));
681 Assert.assertEquals(statusCode, testExpectedStatusCode);
684 protected void readItemListInt(String vcsid, String shortId, String testName) {
688 // Submit the request to the service and store the response.
689 AuthorityClient client = (AuthorityClient) getClientInstance();
692 res = client.readItemList(vcsid, null, null);
693 } else if (shortId != null) {
694 res = client.readItemListForNamedAuthority(shortId, null, null);
696 Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
699 int statusCode = res.getStatus();
701 // Check the status code of the response: does it match
702 // the expected response(s)?
703 if (logger.isDebugEnabled()) {
704 logger.debug(" " + testName + ": status = " + statusCode);
706 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
707 invalidStatusCodeMessage(testRequestType, statusCode));
708 Assert.assertEquals(statusCode, testExpectedStatusCode);
710 AbstractCommonList list = res.readEntity(AbstractCommonList.class);
711 List<AbstractCommonList.ListItem> items = list.getListItem();
712 int nItemsReturned = items.size();
713 long nItemsTotal = list.getTotalItems();
714 if (logger.isDebugEnabled()) {
715 logger.debug(" " + testName + ": Expected "
716 + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
718 Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
720 for (AbstractCommonList.ListItem item : items) {
721 String refName = AbstractCommonListUtils.ListItemGetElementValue(
722 item, AuthorityClient.REF_NAME);
723 Assert.assertTrue((refName != null), "Item refName is null or not set in the item list!");
724 String termDisplayName = AbstractCommonListUtils.ListItemGetElementValue(item,
725 AuthorityClient.TERM_DISPLAY_NAME);
726 String vocabDisplayName = AbstractCommonListUtils.ListItemGetElementValue(item,
727 AuthorityClient.VOCAB_DISPLAY_NAME);
728 // One of these names needs to be set.
729 Assert.assertTrue(!(termDisplayName == null && vocabDisplayName == null), "The item's display name is null or not set in the item list!");
732 if(logger.isTraceEnabled()){
733 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
740 @Test(dataProvider = "testName", dependsOnMethods = {"createItemList"})
741 public void readItemList(String testName) {
742 readItemListInt(knownAuthorityWithItems, null, testName);
745 @Test(dataProvider = "testName", dependsOnMethods = {"readItemList"})
746 public void readItemListByName(String testName) {
747 readItemListInt(null, knownAuthorityWithItemsIdentifier, testName);
750 @Test(dataProvider = "testName",
751 dependsOnMethods = {"deleteItem"})
752 public void deleteNonExistentItem(String testName) {
754 setupDeleteNonExistent();
756 // Submit the request to the service and store the response.
757 AuthorityClient client = (AuthorityClient) getClientInstance();
758 Response res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
761 statusCode = res.getStatus();
766 // Check the status code of the response: does it match
767 // the expected response(s)?
768 if (logger.isDebugEnabled()) {
769 logger.debug(testName + ": status = " + statusCode);
771 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
772 invalidStatusCodeMessage(testRequestType, statusCode));
773 Assert.assertEquals(statusCode, testExpectedStatusCode);
776 protected String getServicePathItemsComponent() {
777 return AuthorityClient.ITEMS;
780 public PoxPayloadOut createItemRequestTypeInstance(AUTHORITY_ITEM_TYPE itemTypeInstance) {
781 PoxPayloadOut result = null;
783 AuthorityClient client = (AuthorityClient) getClientInstance();
784 PoxPayloadOut payloadOut = new PoxPayloadOut(this.getServicePathItemsComponent());
785 PayloadOutputPart part = payloadOut.addPart(client.getItemCommonPartName(), itemTypeInstance);
792 * Update an Authority item.
797 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
798 dependsOnMethods = {"readItem", "CRUDTests", "verifyIgnoredUpdateWithInAuthority"})
799 public void updateItem(String testName) throws Exception {
802 AUTHORITY_ITEM_TYPE theUpdate = null;
804 // Retrieve the contents of a resource to update.
805 AuthorityClient client = (AuthorityClient)this.getClientInstance();
806 Response res = client.readItem(knownResourceId, knownItemResourceId);
808 if (logger.isDebugEnabled()) {
809 logger.debug(testName + ": read status = " + res.getStatus());
811 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
813 if (logger.isDebugEnabled()) {
814 logger.debug("got Authority item to update with ID: "
815 + knownItemResourceId
816 + " in authority: " + knownResourceId);
818 AUTHORITY_ITEM_TYPE authorityItem = extractItemCommonPartValue(res);
819 Assert.assertNotNull(authorityItem);
821 // Update the contents of this resource.
822 theUpdate = updateItemInstance(authorityItem);
823 if (logger.isDebugEnabled()) {
824 logger.debug("\n\nTo be updated fields: CSID = " + knownItemResourceId + "\n"
825 + objectAsXmlString(theUpdate));
831 // Submit the updated resource to the service and store the response.
832 PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
833 res = client.updateItem(knownResourceId, knownItemResourceId, output);
835 int statusCode = res.getStatus();
837 // Check the status code of the response: does it match the expected response(s)?
838 if (logger.isDebugEnabled()) {
839 logger.debug("updateItem: status = " + statusCode);
841 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
842 invalidStatusCodeMessage(testRequestType, statusCode));
843 Assert.assertEquals(statusCode, testExpectedStatusCode);
845 // Retrieve the updated resource and verify that its contents exist.
846 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
847 Assert.assertNotNull(updatedVocabularyItem);
849 compareUpdatedItemInstances(theUpdate, updatedVocabularyItem);
855 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
856 dependsOnMethods = {"readItem", "CRUDTests", "veryifySyncWithSAS"})
857 public void updateLocalItemWithSync(String testName) throws Exception {
860 AUTHORITY_ITEM_TYPE theUpdate = null;
862 // Retrieve the contents of a shared authority item that we're going to update.
863 AUTHORITY_ITEM_TYPE sasAuthorityItem = null;
864 AuthorityClient sasClient = (AuthorityClient) this.getSASClientInstance();
865 Response res = sasClient.readNamedItemInNamedAuthority(knownSASAuthorityResourceIdentifier, knownSASItemIdentifiersList.get(0));
867 if (logger.isDebugEnabled()) {
868 logger.debug(testName + ": read status = " + res.getStatus());
870 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
872 sasAuthorityItem = extractItemCommonPartValue(res); // This is a SAS authority item
873 Assert.assertNotNull(sasAuthorityItem);
878 // Update the contents of this authority item so we can post an UPDATE to the server
879 theUpdate = updateItemInstance(sasAuthorityItem);
881 // Submit the updated authority item and check the response.
882 PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
883 res = sasClient.updateNamedItemInNamedAuthority(knownSASAuthorityResourceIdentifier, knownSASItemIdentifiersList.get(0), output);
884 AUTHORITY_ITEM_TYPE updatedAuthorityItem = null;
886 int statusCode = res.getStatus();
888 // Check the status code of the response: does it match the expected response(s)?
889 if (logger.isDebugEnabled()) {
890 logger.debug("updateItem: status = " + statusCode);
892 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
893 invalidStatusCodeMessage(testRequestType, statusCode));
894 Assert.assertEquals(statusCode, testExpectedStatusCode);
896 // Retrieve the updated authority item and verify that its contents exist.
897 updatedAuthorityItem = extractItemCommonPartValue(res);
898 Assert.assertNotNull(updatedAuthorityItem);
900 compareUpdatedItemInstances(theUpdate, updatedAuthorityItem);
905 // Synchronize the item's parent authority and verify the update we just made
906 // to the SAS appears locally
908 AuthorityClient localClient = (AuthorityClient) this.getClientInstance();
909 Response response = localClient.syncByName(knownSASAuthorityResourceIdentifier); // Notice we're using the Short ID (short ID is the same on the local and SAS)
911 int statusCode = response.getStatus();
912 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode), invalidStatusCodeMessage(testRequestType, statusCode));
913 Assert.assertEquals(statusCode, testExpectedStatusCode);
919 res = localClient.readNamedItemInNamedAuthority(knownSASAuthorityResourceIdentifier, knownSASItemIdentifiersList.get(0));
921 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
922 AUTHORITY_ITEM_TYPE syncedAuthorityItem = extractItemCommonPartValue(res);
923 Assert.assertNotNull(syncedAuthorityItem);
924 compareUpdatedItemInstances(sasAuthorityItem, updatedAuthorityItem);
930 protected abstract PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier);
933 * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
935 @Test(dataProvider = "testName",
936 dependsOnMethods = {"create", "update", "updateNonExistent"})
937 public void updateNonExistentItem(String testName) throws Exception {
939 setupUpdateNonExistent();
941 // Submit the request to the service and store the response.
942 // Note: The ID used in this 'create' call may be arbitrary.
943 // The only relevant ID may be the one used in update(), below.
944 AuthorityClient client = (AuthorityClient)this.getClientInstance();
945 PoxPayloadOut multipart = createNonExistenceItemInstance(client.getItemCommonPartName(), NON_EXISTENT_ID);
946 Response res = client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
948 int statusCode = res.getStatus();
950 // Check the status code of the response: does it match
951 // the expected response(s)?
952 if (logger.isDebugEnabled()) {
953 logger.debug(testName + ": status = " + statusCode);
955 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
956 invalidStatusCodeMessage(testRequestType, statusCode));
957 Assert.assertEquals(statusCode, testExpectedStatusCode);
964 // Methods to persuade TestNG to follow the correct test dependency path
967 @Test(dataProvider = "testName",
968 dependsOnMethods = {"createItem"})
969 public void baseAuthorityTests(String testName) {
970 // Do nothing. Here just to setup a test dependency chain.
974 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
975 * refer to this method in their @Test annotation declarations.
978 @Test(dataProvider = "testName",
980 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
981 public void CRUDTests(String testName) {
982 // TODO Auto-generated method stub
986 public void cleanUp() {
987 String noTest = System.getProperty("noTestCleanup");
988 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
989 if (logger.isDebugEnabled()) {
990 logger.debug("Skipping Cleanup phase ...");
995 AuthorityClient client = (AuthorityClient) this.getClientInstance();
996 String parentResourceId;
997 String itemResourceId;
999 // Clean up all authority item resources.
1001 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1002 itemResourceId = entry.getKey();
1003 parentResourceId = entry.getValue();
1004 Response response = client.deleteItem(parentResourceId, itemResourceId);
1006 int status = response.getStatus();
1007 if (status != Response.Status.OK.getStatusCode()) {
1008 logger.debug(String.format("Could not deleted authority item '%s' in authority '%s'.",
1009 itemResourceId, parentResourceId));
1016 // Clean up authority items that were the result of a sync with the SAS
1017 // all the IDs are URN (not CSIDs). The URNs work for the local items as well
1018 // as the SAS items.
1020 for (Map.Entry<String, String> entry : allSASResourceItemIdsCreated.entrySet()) {
1021 itemResourceId = entry.getKey();
1022 parentResourceId = entry.getValue();
1023 // Note: Any non-success responses from the delete operation
1024 // below are ignored and not reported.
1025 client.deleteItem(parentResourceId, itemResourceId).close();
1028 // Clean up authority items on the SAS using the SAS client.
1030 client = (AuthorityClient) this.getSASClientInstance();
1031 for (Map.Entry<String, String> entry : allSASResourceItemIdsCreated.entrySet()) {
1032 itemResourceId = entry.getKey();
1033 parentResourceId = entry.getValue();
1034 client.deleteItem(parentResourceId, itemResourceId).close();
1037 // Finally, call out superclass's cleanUp method to deleted the local authorities
1041 // Call out superclass's cleanUp method to delete the SAS authorities
1043 super.cleanUp(client);
1046 protected String getTestAuthorityItemShortId() {
1047 return getTestAuthorityItemShortId(false);
1050 protected String getTestAuthorityItemShortId(boolean makeUnique) {
1051 String result = TEST_SHORTID;
1053 if (makeUnique == true) {
1054 result = result + System.currentTimeMillis() + Math.abs(random.nextInt());