1 package org.collectionspace.services.client.test;
5 import javax.ws.rs.core.Response;
6 import org.jboss.resteasy.client.ClientResponse;
8 import org.collectionspace.services.client.AbstractCommonListUtils;
9 import org.collectionspace.services.client.AuthorityClient;
10 import org.collectionspace.services.client.AuthorityClientImpl;
11 import org.collectionspace.services.client.AuthorityProxy;
12 import org.collectionspace.services.client.PayloadInputPart;
13 import org.collectionspace.services.client.PayloadOutputPart;
14 import org.collectionspace.services.client.PoxPayloadOut;
15 import org.collectionspace.services.jaxb.AbstractCommonList;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.testng.Assert;
20 import org.testng.annotations.Test;
26 * @param <AUTHORITY_COMMON_TYPE>
27 * @param <AUTHORITY_ITEM_TYPE>
29 * All CRUD related authority test classes should extend this class.
32 public abstract class AbstractAuthorityServiceTest<AUTHORITY_COMMON_TYPE, AUTHORITY_ITEM_TYPE>
33 extends AbstractPoxServiceTestImpl<AbstractCommonList, AUTHORITY_COMMON_TYPE> {
35 private final Logger logger = LoggerFactory.getLogger(AbstractAuthorityServiceTest.class);
37 protected String knownResourceShortIdentifer = null;
38 protected static final String READITEMS_SHORT_IDENTIFIER = "resourceWithItems";
39 protected String knownAuthorityWithItems = null;
41 protected String knownResourceRefName = null;
42 protected String knownItemResourceId = null;
43 protected String knownItemResourceShortIdentifer = null;
44 protected int nItemsToCreateInList = 5;
46 public abstract void authorityTests(String testName);
47 protected abstract String createItemInAuthority(String authorityId);
49 protected abstract AUTHORITY_ITEM_TYPE updateItemInstance(final AUTHORITY_ITEM_TYPE authorityItem);
50 protected abstract void compareUpdatedItemInstances(AUTHORITY_ITEM_TYPE original, AUTHORITY_ITEM_TYPE updated) throws Exception;
52 protected void setKnownItemResource(String id, String shortIdentifer ) {
53 knownItemResourceId = id;
54 knownItemResourceShortIdentifer = shortIdentifer;
57 protected void setKnownResource(String id, String shortIdentifer,
60 knownResourceShortIdentifer = shortIdentifer;
61 knownResourceRefName = refName;
65 * Returns the root URL for a service.
67 * This URL consists of a base URL for all services, followed by
68 * a path component for the owning vocabulary, followed by the
69 * path component for the items.
71 * @return The root URL for a service.
73 protected String getItemServiceRootURL(String parentResourceIdentifier) {
74 return getResourceURL(parentResourceIdentifier) + "/" + getServicePathItemsComponent();
78 * Returns the URL of a specific resource managed by a service, and
79 * designated by an identifier (such as a universally unique ID, or UUID).
81 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
83 * @return The URL of a specific resource managed by a service.
85 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
86 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
90 * For authorities we override this method so we can save the shortid.
93 protected String createWithIdentifier(String testName, String identifier) throws Exception {
94 String csid = createResource(testName, identifier);
95 // Store the ID returned from the first resource created
96 // for additional tests below.
97 if (getKnowResourceId() == null) {
98 setKnownResource(csid, identifier /*shortId*/, null /*refname*/ );
99 if (logger.isDebugEnabled()) {
100 logger.debug(testName + ": Setting knownResourceId=" + getKnowResourceId());
107 @Test(dependsOnMethods = {"readItem", "CRUDTests"})
108 public void testItemSubmitRequest() {
110 // Expected status code: 200 OK
111 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
113 // Submit the request to the service and store the response.
114 String method = ServiceRequestType.READ.httpMethodName();
115 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
116 int statusCode = submitRequest(method, url);
118 // Check the status code of the response: does it match
119 // the expected response(s)?
120 if (logger.isDebugEnabled()) {
121 logger.debug("testItemSubmitRequest: url=" + url
122 + " status=" + statusCode);
124 Assert.assertEquals(statusCode, EXPECTED_STATUS);
128 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
129 dependsOnMethods = {"readItem"})
130 public void verifyIgnoredUpdateWithInAuthority(String testName) throws Exception {
134 // Submit the request to the service and store the response.
135 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
136 (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
137 Response res = client.readItem(knownResourceId, knownItemResourceId);
138 AUTHORITY_ITEM_TYPE vitem = null;
140 int statusCode = res.getStatus();
142 // Check the status code of the response: does it match
143 // the expected response(s)?
144 if (logger.isDebugEnabled()) {
145 logger.debug(testName + " read authority:" + knownResourceId + "/Item:"
146 + knownItemResourceId + " status = " + statusCode);
148 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
149 invalidStatusCodeMessage(testRequestType, statusCode));
150 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
152 vitem = extractItemCommonPartValue(res);
153 Assert.assertNotNull(vitem);
154 // Try to Update with new parent vocab (use self, for test).
155 Assert.assertEquals(client.getInAuthority(vitem), knownResourceId,
156 "VocabularyItem inAuthority does not match knownResourceId.");
157 client.setInAuthority(vitem, knownItemResourceId);
163 // Submit the updated resource to the service and store the response.
164 PoxPayloadOut output = this.createItemRequestTypeInstance(vitem);
165 res = client.updateItem(knownResourceId, knownItemResourceId, output);
167 int statusCode = res.getStatus();
169 // Check the status code of the response: does it match the expected response(s)?
170 if (logger.isDebugEnabled()) {
171 logger.debug(testName + ": status = " + statusCode);
173 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(testRequestType, statusCode));
175 Assert.assertEquals(statusCode, testExpectedStatusCode);
177 // Retrieve the updated resource and verify that the parent did not change
178 res = client.readItem(knownResourceId, knownItemResourceId);
179 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
180 Assert.assertNotNull(updatedVocabularyItem);
182 // Verify that the updated resource received the correct data.
183 Assert.assertEquals(client.getInAuthority(updatedVocabularyItem),
185 "VocabularyItem allowed update to the parent (inAuthority).");
191 @Test(dataProvider = "testName",
192 dependsOnMethods = {"CRUDTests"})
193 public void createItem(String testName) {
197 String newID = createItemInAuthority(knownResourceId);
199 // Store the ID returned from the first item resource created
200 // for additional tests below.
201 if (knownItemResourceId == null) {
202 knownItemResourceId = newID;
203 if (null != testName && logger.isDebugEnabled()) {
204 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
209 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
210 dependsOnMethods = {"createItem"})
211 public void createItemList(String testName) throws Exception {
212 knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
213 for (int j = 0; j < nItemsToCreateInList; j++) {
214 createItemInAuthority(knownAuthorityWithItems);
221 * @param testName the test name
222 * @throws Exception the exception
224 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
225 dependsOnMethods = {"CRUDTests"})
226 public void readByName(String testName) throws Exception {
230 // Submit the request to the service and store the response.
231 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
232 ClientResponse<String> res = client.readByName(getKnowResourceIdentifier());
234 int statusCode = res.getStatus();
236 // Check the status code of the response: does it match
237 // the expected response(s)?
238 if (logger.isDebugEnabled()) {
239 logger.debug(testName + ": status = " + statusCode);
241 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
242 invalidStatusCodeMessage(testRequestType, statusCode));
243 Assert.assertEquals(statusCode, testExpectedStatusCode);
245 AUTHORITY_COMMON_TYPE commonPart = extractCommonPartValue(res);
246 Assert.assertNotNull(commonPart);
253 * Extracts the common part item from a service's item payload.
259 public AUTHORITY_ITEM_TYPE extractItemCommonPartValue(Response res) throws Exception {
260 AUTHORITY_ITEM_TYPE result = null;
262 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)
263 this.getClientInstance();
264 PayloadInputPart payloadInputPart = extractPart(res, client.getItemCommonPartName());
265 if (payloadInputPart != null) {
266 result = (AUTHORITY_ITEM_TYPE) payloadInputPart.getBody();
268 Assert.assertNotNull(result,
269 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
274 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
275 dependsOnMethods = {"readItem"})
276 public void readItemNonExistent(String testName) {
278 setupReadNonExistent();
280 // Submit the request to the service and store the response.
281 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)
282 this.getClientInstance();
283 Response res = client.readItem(knownResourceId, NON_EXISTENT_ID);
285 int statusCode = res.getStatus();
287 // Check the status code of the response: does it match
288 // the expected response(s)?
289 if (logger.isDebugEnabled()) {
290 logger.debug(testName + ": status = " + statusCode);
292 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
293 invalidStatusCodeMessage(testRequestType, statusCode));
294 Assert.assertEquals(statusCode, testExpectedStatusCode);
300 @Test(dataProvider = "testName",
301 dependsOnMethods = {"createItem"})
302 public void readItem(String testName) throws Exception {
306 // Submit the request to the service and store the response.
307 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
308 Response res = client.readItem(knownResourceId, knownItemResourceId);
310 int statusCode = res.getStatus();
312 // Check the status code of the response: does it match
313 // the expected response(s)?
314 if (logger.isDebugEnabled()) {
315 logger.debug(testName + ": status = " + statusCode);
317 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
318 invalidStatusCodeMessage(testRequestType, statusCode));
319 Assert.assertEquals(statusCode, testExpectedStatusCode);
321 AUTHORITY_ITEM_TYPE itemCommonPart = extractItemCommonPartValue(res);
322 Assert.assertNotNull(itemCommonPart);
323 Assert.assertEquals(client.getInAuthority(itemCommonPart), knownResourceId);
324 verifyReadItemInstance(itemCommonPart);
330 protected abstract void verifyReadItemInstance(AUTHORITY_ITEM_TYPE item) throws Exception;
332 @Test(dataProvider = "testName",
333 dependsOnMethods = {"testItemSubmitRequest", "updateItem", "verifyIgnoredUpdateWithInAuthority"})
334 public void deleteItem(String testName) throws Exception {
338 // Submit the request to the service and store the response.
339 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)
340 this.getClientInstance();
341 Response res = client.deleteItem(knownResourceId, knownItemResourceId);
344 statusCode = res.getStatus();
349 // Check the status code of the response: does it match
350 // the expected response(s)?
351 if (logger.isDebugEnabled()) {
352 logger.debug("delete: status = " + statusCode);
354 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
355 invalidStatusCodeMessage(testRequestType, statusCode));
356 Assert.assertEquals(statusCode, testExpectedStatusCode);
359 protected void readItemListInt(String vcsid, String shortId, String testName) {
363 // Submit the request to the service and store the response.
364 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
365 ClientResponse<AbstractCommonList> res = null;
367 res = client.readItemList(vcsid, null, null);
368 } else if (shortId != null) {
369 res = client.readItemListForNamedAuthority(shortId, null, null);
371 Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
373 int statusCode = res.getStatus();
375 // Check the status code of the response: does it match
376 // the expected response(s)?
377 if (logger.isDebugEnabled()) {
378 logger.debug(" " + testName + ": status = " + statusCode);
380 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
381 invalidStatusCodeMessage(testRequestType, statusCode));
382 Assert.assertEquals(statusCode, testExpectedStatusCode);
384 AbstractCommonList list = res.getEntity();
385 List<AbstractCommonList.ListItem> items = list.getListItem();
386 int nItemsReturned = items.size();
387 long nItemsTotal = list.getTotalItems();
388 if (logger.isDebugEnabled()) {
389 logger.debug(" " + testName + ": Expected "
390 + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
392 Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
394 if(logger.isTraceEnabled()){
395 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
399 @Test(dataProvider = "testName",
400 dependsOnMethods = {"createItemList"})
401 public void readItemList(String testName) {
402 readItemListInt(knownAuthorityWithItems, null, testName);
405 @Test(dataProvider = "testName",
406 dependsOnMethods = {"readItem"})
407 public void readItemListByName(String testName) {
408 readItemListInt(null, READITEMS_SHORT_IDENTIFIER, testName);
411 @Test(dataProvider = "testName",
412 dependsOnMethods = {"deleteItem"})
413 public void deleteNonExistentItem(String testName) {
415 setupDeleteNonExistent();
417 // Submit the request to the service and store the response.
418 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
419 Response res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
422 statusCode = res.getStatus();
427 // Check the status code of the response: does it match
428 // the expected response(s)?
429 if (logger.isDebugEnabled()) {
430 logger.debug(testName + ": status = " + statusCode);
432 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
433 invalidStatusCodeMessage(testRequestType, statusCode));
434 Assert.assertEquals(statusCode, testExpectedStatusCode);
437 protected String getServicePathItemsComponent() {
438 return AuthorityClient.ITEMS;
441 public PoxPayloadOut createItemRequestTypeInstance(AUTHORITY_ITEM_TYPE itemTypeInstance) {
442 PoxPayloadOut result = null;
444 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
445 PoxPayloadOut payloadOut = new PoxPayloadOut(this.getServicePathItemsComponent());
446 PayloadOutputPart part = payloadOut.addPart(client.getItemCommonPartName(), itemTypeInstance);
453 * Update an Authority item.
458 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
459 dependsOnMethods = {"readItem", "CRUDTests", "verifyIgnoredUpdateWithInAuthority"})
460 public void updateItem(String testName) throws Exception {
463 AUTHORITY_ITEM_TYPE theUpdate = null;
465 // Retrieve the contents of a resource to update.
466 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
467 (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
468 Response res = client.readItem(knownResourceId, knownItemResourceId);
470 if (logger.isDebugEnabled()) {
471 logger.debug(testName + ": read status = " + res.getStatus());
473 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
475 if (logger.isDebugEnabled()) {
476 logger.debug("got Authority item to update with ID: "
477 + knownItemResourceId
478 + " in authority: " + knownResourceId);
480 AUTHORITY_ITEM_TYPE authorityItem = extractItemCommonPartValue(res);
481 Assert.assertNotNull(authorityItem);
483 // Update the contents of this resource.
484 theUpdate = updateItemInstance(authorityItem);
485 if (logger.isDebugEnabled()) {
486 logger.debug("\n\nTo be updated fields: CSID = " + knownItemResourceId + "\n"
487 + objectAsXmlString(theUpdate));
493 // Submit the updated resource to the service and store the response.
494 PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
495 res = client.updateItem(knownResourceId, knownItemResourceId, output);
497 int statusCode = res.getStatus();
499 // Check the status code of the response: does it match the expected response(s)?
500 if (logger.isDebugEnabled()) {
501 logger.debug("updateItem: status = " + statusCode);
503 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
504 invalidStatusCodeMessage(testRequestType, statusCode));
505 Assert.assertEquals(statusCode, testExpectedStatusCode);
507 // Retrieve the updated resource and verify that its contents exist.
508 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
509 Assert.assertNotNull(updatedVocabularyItem);
511 compareUpdatedItemInstances(theUpdate, updatedVocabularyItem);
517 protected abstract PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier);
520 * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
522 @Test(dataProvider = "testName",
523 dependsOnMethods = {"create", "update", "updateNonExistent"})
524 public void updateNonExistentItem(String testName) throws Exception {
526 setupUpdateNonExistent();
528 // Submit the request to the service and store the response.
529 // Note: The ID used in this 'create' call may be arbitrary.
530 // The only relevant ID may be the one used in update(), below.
531 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
532 (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
533 PoxPayloadOut multipart = createNonExistenceItemInstance(client.getItemCommonPartName(), NON_EXISTENT_ID);
534 ClientResponse<String> res =
535 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
537 int statusCode = res.getStatus();
539 // Check the status code of the response: does it match
540 // the expected response(s)?
541 if (logger.isDebugEnabled()) {
542 logger.debug(testName + ": status = " + statusCode);
544 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
545 invalidStatusCodeMessage(testRequestType, statusCode));
546 Assert.assertEquals(statusCode, testExpectedStatusCode);
548 res.releaseConnection();
553 // Methods to persuade TestNG to follow the correct test dependency path
556 @Test(dataProvider = "testName",
557 dependsOnMethods = {"createItem"})
558 public void baseAuthorityTests(String testName) {
559 // Do nothing. Here just to setup a test dependency chain.
563 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
564 * refer to this method in their @Test annotation declarations.
567 @Test(dataProvider = "testName",
569 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
570 public void CRUDTests(String testName) {
571 // TODO Auto-generated method stub