1 package org.collectionspace.services.client.test;
\r
3 import java.util.List;
\r
5 import javax.ws.rs.core.Response;
\r
6 import org.jboss.resteasy.client.ClientResponse;
\r
8 import org.collectionspace.services.client.AbstractCommonListUtils;
\r
9 import org.collectionspace.services.client.AuthorityClient;
\r
10 import org.collectionspace.services.client.AuthorityClientImpl;
\r
11 import org.collectionspace.services.client.AuthorityProxy;
\r
12 import org.collectionspace.services.client.PayloadInputPart;
\r
13 import org.collectionspace.services.client.PayloadOutputPart;
\r
14 import org.collectionspace.services.client.PoxPayloadOut;
\r
15 import org.collectionspace.services.jaxb.AbstractCommonList;
\r
17 import org.slf4j.Logger;
\r
18 import org.slf4j.LoggerFactory;
\r
19 import org.testng.Assert;
\r
20 import org.testng.annotations.Test;
\r
26 * @param <AUTHORITY_COMMON_TYPE>
\r
27 * @param <AUTHORITY_ITEM_TYPE>
\r
29 * All CRUD related authority test classes should extend this class.
\r
32 public abstract class AbstractAuthorityServiceTest<AUTHORITY_COMMON_TYPE, AUTHORITY_ITEM_TYPE>
\r
33 extends AbstractPoxServiceTestImpl<AbstractCommonList, AUTHORITY_COMMON_TYPE> {
\r
35 private final Logger logger = LoggerFactory.getLogger(AbstractAuthorityServiceTest.class);
\r
37 protected String knownResourceShortIdentifer = null;
\r
38 protected static final String READITEMS_SHORT_IDENTIFIER = "resourceWithItems";
\r
39 protected String knownAuthorityWithItems = null;
\r
41 protected String knownResourceRefName = null;
\r
42 protected String knownItemResourceId = null;
\r
43 protected String knownItemResourceShortIdentifer = null;
\r
44 protected int nItemsToCreateInList = 5;
\r
46 public abstract void authorityTests(String testName);
\r
47 protected abstract String createItemInAuthority(String authorityId);
\r
49 protected abstract AUTHORITY_ITEM_TYPE updateItemInstance(final AUTHORITY_ITEM_TYPE authorityItem);
\r
50 protected abstract void compareUpdatedItemInstances(AUTHORITY_ITEM_TYPE original, AUTHORITY_ITEM_TYPE updated) throws Exception;
\r
52 protected void setKnownItemResource(String id, String shortIdentifer ) {
\r
53 knownItemResourceId = id;
\r
54 knownItemResourceShortIdentifer = shortIdentifer;
\r
57 protected void setKnownResource(String id, String shortIdentifer,
\r
59 knownResourceId = id;
\r
60 knownResourceShortIdentifer = shortIdentifer;
\r
61 knownResourceRefName = refName;
\r
65 * Returns the root URL for a service.
\r
67 * This URL consists of a base URL for all services, followed by
\r
68 * a path component for the owning vocabulary, followed by the
\r
69 * path component for the items.
\r
71 * @return The root URL for a service.
\r
73 protected String getItemServiceRootURL(String parentResourceIdentifier) {
\r
74 return getResourceURL(parentResourceIdentifier) + "/" + getServicePathItemsComponent();
\r
78 * Returns the URL of a specific resource managed by a service, and
\r
79 * designated by an identifier (such as a universally unique ID, or UUID).
\r
81 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
\r
83 * @return The URL of a specific resource managed by a service.
\r
85 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
\r
86 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
\r
90 * For authorities we override this method so we can save the shortid.
\r
93 protected String createWithIdentifier(String testName, String identifier) throws Exception {
\r
94 String csid = createResource(testName, identifier);
\r
95 // Store the ID returned from the first resource created
\r
96 // for additional tests below.
\r
97 if (getKnowResourceId() == null) {
\r
98 setKnownResource(csid, identifier /*shortId*/, null /*refname*/ );
\r
99 if (logger.isDebugEnabled()) {
\r
100 logger.debug(testName + ": Setting knownResourceId=" + getKnowResourceId());
\r
107 @Test(dependsOnMethods = {"readItem", "CRUDTests"})
\r
108 public void testItemSubmitRequest() {
\r
110 // Expected status code: 200 OK
\r
111 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
\r
113 // Submit the request to the service and store the response.
\r
114 String method = ServiceRequestType.READ.httpMethodName();
\r
115 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
\r
116 int statusCode = submitRequest(method, url);
\r
118 // Check the status code of the response: does it match
\r
119 // the expected response(s)?
\r
120 if (logger.isDebugEnabled()) {
\r
121 logger.debug("testItemSubmitRequest: url=" + url
\r
122 + " status=" + statusCode);
\r
124 Assert.assertEquals(statusCode, EXPECTED_STATUS);
\r
128 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
\r
129 dependsOnMethods = {"readItem"})
\r
130 public void verifyIgnoredUpdateWithInAuthority(String testName) throws Exception {
\r
134 // Submit the request to the service and store the response.
\r
135 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
\r
136 (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
137 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
\r
138 int statusCode = res.getStatus();
\r
140 // Check the status code of the response: does it match
\r
141 // the expected response(s)?
\r
142 if (logger.isDebugEnabled()) {
\r
143 logger.debug(testName + " read authority:" + knownResourceId + "/Item:"
\r
144 + knownItemResourceId + " status = " + statusCode);
\r
146 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
147 invalidStatusCodeMessage(testRequestType, statusCode));
\r
148 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
\r
150 AUTHORITY_ITEM_TYPE vitem = extractItemCommonPartValue(res);
\r
151 Assert.assertNotNull(vitem);
\r
152 // Try to Update with new parent vocab (use self, for test).
\r
153 Assert.assertEquals(client.getInAuthority(vitem), knownResourceId,
\r
154 "VocabularyItem inAuthority does not match knownResourceId.");
\r
155 client.setInAuthority(vitem, knownItemResourceId);
\r
157 // Submit the updated resource to the service and store the response.
\r
158 PoxPayloadOut output = this.createItemRequestTypeInstance(vitem);
\r
159 res = client.updateItem(knownResourceId, knownItemResourceId, output);
\r
160 statusCode = res.getStatus();
\r
162 // Check the status code of the response: does it match the expected response(s)?
\r
163 if (logger.isDebugEnabled()) {
\r
164 logger.debug(testName + ": status = " + statusCode);
\r
166 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
167 invalidStatusCodeMessage(testRequestType, statusCode));
\r
168 Assert.assertEquals(statusCode, testExpectedStatusCode);
\r
170 // Retrieve the updated resource and verify that the parent did not change
\r
171 res = client.readItem(knownResourceId, knownItemResourceId);
\r
172 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
\r
173 Assert.assertNotNull(updatedVocabularyItem);
\r
175 // Verify that the updated resource received the correct data.
\r
176 Assert.assertEquals(client.getInAuthority(updatedVocabularyItem),
\r
178 "VocabularyItem allowed update to the parent (inAuthority).");
\r
181 @Test(dataProvider = "testName",
\r
182 dependsOnMethods = {"CRUDTests"})
\r
183 public void createItem(String testName) {
\r
187 String newID = createItemInAuthority(knownResourceId);
\r
189 // Store the ID returned from the first item resource created
\r
190 // for additional tests below.
\r
191 if (knownItemResourceId == null) {
\r
192 knownItemResourceId = newID;
\r
193 if (null != testName && logger.isDebugEnabled()) {
\r
194 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
\r
199 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
\r
200 dependsOnMethods = {"createItem"})
\r
201 public void createItemList(String testName) throws Exception {
\r
202 knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
\r
203 for (int j = 0; j < nItemsToCreateInList; j++) {
\r
204 createItemInAuthority(knownAuthorityWithItems);
\r
211 * @param testName the test name
\r
212 * @throws Exception the exception
\r
214 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
\r
215 dependsOnMethods = {"CRUDTests"})
\r
216 public void readByName(String testName) throws Exception {
\r
220 // Submit the request to the service and store the response.
\r
221 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
222 ClientResponse<String> res = client.readByName(getKnowResourceIdentifier());
\r
223 int statusCode = res.getStatus();
\r
225 // Check the status code of the response: does it match
\r
226 // the expected response(s)?
\r
227 if (logger.isDebugEnabled()) {
\r
228 logger.debug(testName + ": status = " + statusCode);
\r
230 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
231 invalidStatusCodeMessage(testRequestType, statusCode));
\r
232 Assert.assertEquals(statusCode, testExpectedStatusCode);
\r
234 AUTHORITY_COMMON_TYPE commonPart = extractCommonPartValue(res);
\r
235 Assert.assertNotNull(commonPart);
\r
239 * Extracts the common part item from a service's item payload.
\r
243 * @throws Exception
\r
245 public AUTHORITY_ITEM_TYPE extractItemCommonPartValue(ClientResponse<String> res) throws Exception {
\r
246 AUTHORITY_ITEM_TYPE result = null;
\r
248 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
249 PayloadInputPart payloadInputPart = extractPart(res, client.getItemCommonPartName());
\r
250 if (payloadInputPart != null) {
\r
251 result = (AUTHORITY_ITEM_TYPE) payloadInputPart.getBody();
\r
253 Assert.assertNotNull(result,
\r
254 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
\r
259 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
\r
260 dependsOnMethods = {"readItem"})
\r
261 public void readItemNonExistent(String testName) {
\r
263 setupReadNonExistent();
\r
265 // Submit the request to the service and store the response.
\r
266 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
267 ClientResponse<String> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
\r
268 int statusCode = res.getStatus();
\r
270 // Check the status code of the response: does it match
\r
271 // the expected response(s)?
\r
272 if (logger.isDebugEnabled()) {
\r
273 logger.debug(testName + ": status = " + statusCode);
\r
275 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
276 invalidStatusCodeMessage(testRequestType, statusCode));
\r
277 Assert.assertEquals(statusCode, testExpectedStatusCode);
\r
280 @Test(dataProvider = "testName",
\r
281 dependsOnMethods = {"createItem"})
\r
282 public void readItem(String testName) throws Exception {
\r
286 // Submit the request to the service and store the response.
\r
287 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
288 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
\r
289 int statusCode = res.getStatus();
\r
291 // Check the status code of the response: does it match
\r
292 // the expected response(s)?
\r
293 if (logger.isDebugEnabled()) {
\r
294 logger.debug(testName + ": status = " + statusCode);
\r
296 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
297 invalidStatusCodeMessage(testRequestType, statusCode));
\r
298 Assert.assertEquals(statusCode, testExpectedStatusCode);
\r
300 AUTHORITY_ITEM_TYPE itemCommonPart = extractItemCommonPartValue(res);
\r
301 Assert.assertNotNull(itemCommonPart);
\r
302 Assert.assertEquals(client.getInAuthority(itemCommonPart), knownResourceId);
\r
303 verifyReadItemInstance(itemCommonPart);
\r
306 protected abstract void verifyReadItemInstance(AUTHORITY_ITEM_TYPE item) throws Exception;
\r
308 @Test(dataProvider = "testName",
\r
309 dependsOnMethods = {"testItemSubmitRequest", "updateItem", "verifyIgnoredUpdateWithInAuthority"})
\r
310 public void deleteItem(String testName) throws Exception {
\r
314 // Submit the request to the service and store the response.
\r
315 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
316 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
\r
317 int statusCode = res.getStatus();
\r
319 // Check the status code of the response: does it match
\r
320 // the expected response(s)?
\r
321 if (logger.isDebugEnabled()) {
\r
322 logger.debug("delete: status = " + statusCode);
\r
324 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
325 invalidStatusCodeMessage(testRequestType, statusCode));
\r
326 Assert.assertEquals(statusCode, testExpectedStatusCode);
\r
329 protected void readItemListInt(String vcsid, String shortId, String testName) {
\r
333 // Submit the request to the service and store the response.
\r
334 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
335 ClientResponse<AbstractCommonList> res = null;
\r
336 if (vcsid != null) {
\r
337 res = client.readItemList(vcsid, null, null);
\r
338 } else if (shortId != null) {
\r
339 res = client.readItemListForNamedAuthority(shortId, null, null);
\r
341 Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
\r
343 int statusCode = res.getStatus();
\r
345 // Check the status code of the response: does it match
\r
346 // the expected response(s)?
\r
347 if (logger.isDebugEnabled()) {
\r
348 logger.debug(" " + testName + ": status = " + statusCode);
\r
350 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
351 invalidStatusCodeMessage(testRequestType, statusCode));
\r
352 Assert.assertEquals(statusCode, testExpectedStatusCode);
\r
354 AbstractCommonList list = res.getEntity();
\r
355 List<AbstractCommonList.ListItem> items = list.getListItem();
\r
356 int nItemsReturned = items.size();
\r
357 long nItemsTotal = list.getTotalItems();
\r
358 if (logger.isDebugEnabled()) {
\r
359 logger.debug(" " + testName + ": Expected "
\r
360 + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
\r
362 Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
\r
364 if(logger.isTraceEnabled()){
\r
365 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
\r
369 @Test(dataProvider = "testName",
\r
370 dependsOnMethods = {"createItemList"})
\r
371 public void readItemList(String testName) {
\r
372 readItemListInt(knownAuthorityWithItems, null, testName);
\r
375 @Test(dataProvider = "testName",
\r
376 dependsOnMethods = {"readItem"})
\r
377 public void readItemListByName(String testName) {
\r
378 readItemListInt(null, READITEMS_SHORT_IDENTIFIER, testName);
\r
381 @Test(dataProvider = "testName",
\r
382 dependsOnMethods = {"deleteItem"})
\r
383 public void deleteNonExistentItem(String testName) {
\r
385 setupDeleteNonExistent();
\r
387 // Submit the request to the service and store the response.
\r
388 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
389 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
\r
390 int statusCode = res.getStatus();
\r
392 // Check the status code of the response: does it match
\r
393 // the expected response(s)?
\r
394 if (logger.isDebugEnabled()) {
\r
395 logger.debug(testName + ": status = " + statusCode);
\r
397 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
398 invalidStatusCodeMessage(testRequestType, statusCode));
\r
399 Assert.assertEquals(statusCode, testExpectedStatusCode);
\r
402 protected String getServicePathItemsComponent() {
\r
403 return AuthorityClient.ITEMS;
\r
406 public PoxPayloadOut createItemRequestTypeInstance(AUTHORITY_ITEM_TYPE itemTypeInstance) {
\r
407 PoxPayloadOut result = null;
\r
409 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
410 PoxPayloadOut payloadOut = new PoxPayloadOut(this.getServicePathItemsComponent());
\r
411 PayloadOutputPart part = payloadOut.addPart(client.getItemCommonPartName(), itemTypeInstance);
\r
412 result = payloadOut;
\r
418 * Update an Authority item.
\r
421 * @throws Exception
\r
423 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
\r
424 dependsOnMethods = {"readItem", "CRUDTests", "verifyIgnoredUpdateWithInAuthority"})
\r
425 public void updateItem(String testName) throws Exception {
\r
428 AUTHORITY_ITEM_TYPE theUpdate = null;
\r
430 // Retrieve the contents of a resource to update.
\r
431 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
\r
432 (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
433 ClientResponse<String> res =
\r
434 client.readItem(knownResourceId, knownItemResourceId);
\r
436 if (logger.isDebugEnabled()) {
\r
437 logger.debug(testName + ": read status = " + res.getStatus());
\r
439 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
\r
441 if (logger.isDebugEnabled()) {
\r
442 logger.debug("got Authority item to update with ID: "
\r
443 + knownItemResourceId
\r
444 + " in authority: " + knownResourceId);
\r
446 AUTHORITY_ITEM_TYPE authorityItem = extractItemCommonPartValue(res);
\r
447 Assert.assertNotNull(authorityItem);
\r
449 // Update the contents of this resource.
\r
450 theUpdate = updateItemInstance(authorityItem);
\r
451 if (logger.isDebugEnabled()) {
\r
452 logger.debug("\n\nTo be updated fields: CSID = " + knownItemResourceId + "\n"
\r
453 + objectAsXmlString(theUpdate));
\r
456 res.releaseConnection();
\r
459 // Submit the updated resource to the service and store the response.
\r
460 PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
\r
461 res = client.updateItem(knownResourceId, knownItemResourceId, output);
\r
463 int statusCode = res.getStatus();
\r
465 // Check the status code of the response: does it match the expected response(s)?
\r
466 if (logger.isDebugEnabled()) {
\r
467 logger.debug("updateItem: status = " + statusCode);
\r
469 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
470 invalidStatusCodeMessage(testRequestType, statusCode));
\r
471 Assert.assertEquals(statusCode, testExpectedStatusCode);
\r
473 // Retrieve the updated resource and verify that its contents exist.
\r
474 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
\r
475 Assert.assertNotNull(updatedVocabularyItem);
\r
477 compareUpdatedItemInstances(theUpdate, updatedVocabularyItem);
\r
479 res.releaseConnection();
\r
483 protected abstract PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier);
\r
486 * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
\r
488 @Test(dataProvider = "testName",
\r
489 dependsOnMethods = {"create", "update", "updateNonExistent"})
\r
490 public void updateNonExistentItem(String testName) throws Exception {
\r
492 setupUpdateNonExistent();
\r
494 // Submit the request to the service and store the response.
\r
495 // Note: The ID used in this 'create' call may be arbitrary.
\r
496 // The only relevant ID may be the one used in update(), below.
\r
497 AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
\r
498 (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
\r
499 PoxPayloadOut multipart = createNonExistenceItemInstance(client.getItemCommonPartName(), NON_EXISTENT_ID);
\r
500 ClientResponse<String> res =
\r
501 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
\r
503 int statusCode = res.getStatus();
\r
505 // Check the status code of the response: does it match
\r
506 // the expected response(s)?
\r
507 if (logger.isDebugEnabled()) {
\r
508 logger.debug(testName + ": status = " + statusCode);
\r
510 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
\r
511 invalidStatusCodeMessage(testRequestType, statusCode));
\r
512 Assert.assertEquals(statusCode, testExpectedStatusCode);
\r
514 res.releaseConnection();
\r
519 // Methods to persuade TestNG to follow the correct test dependency path
\r
522 @Test(dataProvider = "testName",
\r
523 dependsOnMethods = {"createItem"})
\r
524 public void baseAuthorityTests(String testName) {
\r
525 // Do nothing. Here just to setup a test dependency chain.
\r
529 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
\r
530 * refer to this method in their @Test annotation declarations.
\r
533 @Test(dataProvider = "testName",
\r
534 dependsOnMethods = {
\r
535 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
\r
536 public void CRUDTests(String testName) {
\r
537 // TODO Auto-generated method stub
\r