]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f878f897e2084d21d1fa2a170fd16d0dbd2500ce
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client.test;
2
3 import java.util.List;
4
5 import javax.ws.rs.core.Response;
6 import org.jboss.resteasy.client.ClientResponse;
7
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;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.testng.Assert;
20 import org.testng.annotations.Test;
21
22 /**
23  * 
24  * @author remillet
25  *
26  * @param <AUTHORITY_COMMON_TYPE>
27  * @param <AUTHORITY_ITEM_TYPE>
28  * 
29  * All CRUD related authority test classes should extend this class.
30  * 
31  */
32 public abstract class AbstractAuthorityServiceTest<AUTHORITY_COMMON_TYPE, AUTHORITY_ITEM_TYPE> 
33         extends AbstractPoxServiceTestImpl<AbstractCommonList, AUTHORITY_COMMON_TYPE> {
34
35     private final Logger logger = LoggerFactory.getLogger(AbstractAuthorityServiceTest.class);
36         
37     protected String knownResourceShortIdentifer = null;
38         protected static final String READITEMS_SHORT_IDENTIFIER = "resourceWithItems"; 
39         protected String knownAuthorityWithItems = null;
40         
41         protected String knownResourceRefName = null;
42     protected String knownItemResourceId = null;
43     protected String knownItemResourceShortIdentifer = null;    
44     protected int nItemsToCreateInList = 5;
45                 
46         public abstract void authorityTests(String testName);
47     protected abstract String createItemInAuthority(String authorityId);
48  
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;
51     
52     protected void setKnownItemResource(String id, String shortIdentifer ) {
53         knownItemResourceId = id;
54         knownItemResourceShortIdentifer = shortIdentifer;
55     }
56
57     protected void setKnownResource(String id, String shortIdentifer,
58             String refName) {
59         knownResourceId = id;
60         knownResourceShortIdentifer = shortIdentifer;
61         knownResourceRefName = refName;
62     }
63
64     /**
65      * Returns the root URL for a service.
66      *
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.
70      *
71      * @return The root URL for a service.
72      */
73     protected String getItemServiceRootURL(String parentResourceIdentifier) {
74         return getResourceURL(parentResourceIdentifier) + "/" + getServicePathItemsComponent();
75     }
76
77     /**
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).
80      *
81      * @param  resourceIdentifier  An identifier (such as a UUID) for a resource.
82      *
83      * @return The URL of a specific resource managed by a service.
84      */
85     protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
86         return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
87     }
88         
89     /**
90      * For authorities we override this method so we can save the shortid.
91      */
92     @Override
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());
101             }
102         }
103         
104         return identifier;
105     }    
106     
107     @Test(dependsOnMethods = {"readItem", "CRUDTests"})
108     public void testItemSubmitRequest() {
109
110         // Expected status code: 200 OK
111         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
112
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);
117
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);
123         }
124         Assert.assertEquals(statusCode, EXPECTED_STATUS);
125     }    
126
127     
128     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
129         dependsOnMethods = {"readItem"})
130     public void verifyIgnoredUpdateWithInAuthority(String testName) throws Exception {
131         // Perform setup.
132         setupUpdate();
133
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         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
138         int statusCode = res.getStatus();
139
140         // Check the status code of the response: does it match
141         // the expected response(s)?
142         if (logger.isDebugEnabled()) {
143                 logger.debug(testName + " read authority:" + knownResourceId + "/Item:"
144                                 + knownItemResourceId + " status = " + statusCode);
145         }
146         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
147                         invalidStatusCodeMessage(testRequestType, statusCode));
148         Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
149
150         AUTHORITY_ITEM_TYPE vitem = extractItemCommonPartValue(res);
151         Assert.assertNotNull(vitem);
152         // Try to Update with new parent vocab (use self, for test).
153         Assert.assertEquals(client.getInAuthority(vitem), knownResourceId,
154                         "VocabularyItem inAuthority does not match knownResourceId.");
155         client.setInAuthority(vitem, knownItemResourceId);
156
157         // Submit the updated resource to the service and store the response.
158         PoxPayloadOut output = this.createItemRequestTypeInstance(vitem);
159         res = client.updateItem(knownResourceId, knownItemResourceId, output);
160         statusCode = res.getStatus();
161
162         // Check the status code of the response: does it match the expected response(s)?
163         if (logger.isDebugEnabled()) {
164                 logger.debug(testName + ": status = " + statusCode);
165         }
166         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
167                         invalidStatusCodeMessage(testRequestType, statusCode));
168         Assert.assertEquals(statusCode, testExpectedStatusCode);
169
170         // Retrieve the updated resource and verify that the parent did not change
171         res = client.readItem(knownResourceId, knownItemResourceId);
172         AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
173         Assert.assertNotNull(updatedVocabularyItem);
174
175         // Verify that the updated resource received the correct data.
176         Assert.assertEquals(client.getInAuthority(updatedVocabularyItem),
177                         knownResourceId,
178                         "VocabularyItem allowed update to the parent (inAuthority).");
179     }
180     
181     @Test(dataProvider = "testName",
182                 dependsOnMethods = {"CRUDTests"})
183     public void createItem(String testName) {
184         // Perform setup.
185         setupCreate();
186
187         String newID = createItemInAuthority(knownResourceId);
188
189         // Store the ID returned from the first item resource created
190         // for additional tests below.
191         if (knownItemResourceId == null) {
192             knownItemResourceId = newID;
193             if (null != testName && logger.isDebugEnabled()) {
194                 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
195             }
196         }
197     }
198     
199     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
200                 dependsOnMethods = {"createItem"})
201     public void createItemList(String testName) throws Exception {
202         knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
203         for (int j = 0; j < nItemsToCreateInList; j++) {
204                 createItemInAuthority(knownAuthorityWithItems);
205         }
206     }
207
208     /**
209      * Read by name.
210      *
211      * @param testName the test name
212      * @throws Exception the exception
213      */
214     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
215                 dependsOnMethods = {"CRUDTests"})
216     public void readByName(String testName) throws Exception {
217         // Perform setup.
218         setupRead();
219
220         // Submit the request to the service and store the response.
221         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
222         ClientResponse<String> res = client.readByName(getKnowResourceIdentifier());
223         int statusCode = res.getStatus();
224
225         // Check the status code of the response: does it match
226         // the expected response(s)?
227         if (logger.isDebugEnabled()) {
228             logger.debug(testName + ": status = " + statusCode);
229         }
230         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
231                 invalidStatusCodeMessage(testRequestType, statusCode));
232         Assert.assertEquals(statusCode, testExpectedStatusCode);
233         
234         AUTHORITY_COMMON_TYPE commonPart = extractCommonPartValue(res);
235         Assert.assertNotNull(commonPart);
236     }
237     
238     /**
239      * Extracts the common part item from a service's item payload.
240      * 
241      * @param res
242      * @return
243      * @throws Exception
244      */
245         public AUTHORITY_ITEM_TYPE extractItemCommonPartValue(ClientResponse<String> res) throws Exception {
246                 AUTHORITY_ITEM_TYPE result = null;
247                 
248         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
249                 PayloadInputPart payloadInputPart = extractPart(res, client.getItemCommonPartName());
250                 if (payloadInputPart != null) {
251                         result = (AUTHORITY_ITEM_TYPE) payloadInputPart.getBody();
252                 }
253                 Assert.assertNotNull(result,
254                                 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
255                 
256                 return result;
257         }
258     
259     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
260                 dependsOnMethods = {"readItem"})
261     public void readItemNonExistent(String testName) {
262         // Perform setup.
263         setupReadNonExistent();
264
265         // Submit the request to the service and store the response.
266         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
267         ClientResponse<String> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
268         int statusCode = res.getStatus();
269
270         // Check the status code of the response: does it match
271         // the expected response(s)?
272         if (logger.isDebugEnabled()) {
273             logger.debug(testName + ": status = " + statusCode);
274         }
275         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
276                 invalidStatusCodeMessage(testRequestType, statusCode));
277         Assert.assertEquals(statusCode, testExpectedStatusCode);
278     }
279         
280     @Test(dataProvider = "testName",
281                 dependsOnMethods = {"createItem"})
282     public void readItem(String testName) throws Exception {
283         // Perform setup.
284         setupRead();
285
286         // Submit the request to the service and store the response.
287         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
288         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
289         int statusCode = res.getStatus();
290
291         // Check the status code of the response: does it match
292         // the expected response(s)?
293         if (logger.isDebugEnabled()) {
294             logger.debug(testName + ": status = " + statusCode);
295         }
296         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
297                 invalidStatusCodeMessage(testRequestType, statusCode));
298         Assert.assertEquals(statusCode, testExpectedStatusCode);
299
300         AUTHORITY_ITEM_TYPE itemCommonPart = extractItemCommonPartValue(res);
301         Assert.assertNotNull(itemCommonPart);
302         Assert.assertEquals(client.getInAuthority(itemCommonPart), knownResourceId);
303         verifyReadItemInstance(itemCommonPart);
304     }
305     
306     protected abstract void verifyReadItemInstance(AUTHORITY_ITEM_TYPE item) throws Exception;
307         
308     @Test(dataProvider = "testName",
309                 dependsOnMethods = {"testItemSubmitRequest", "updateItem", "verifyIgnoredUpdateWithInAuthority"})    
310     public void deleteItem(String testName) throws Exception {
311         // Perform setup.
312         setupDelete();
313
314         // Submit the request to the service and store the response.
315         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
316         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
317         int statusCode = res.getStatus();
318
319         // Check the status code of the response: does it match
320         // the expected response(s)?
321         if (logger.isDebugEnabled()) {
322             logger.debug("delete: status = " + statusCode);
323         }
324         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
325                 invalidStatusCodeMessage(testRequestType, statusCode));
326         Assert.assertEquals(statusCode, testExpectedStatusCode);
327     }
328     
329     protected void readItemListInt(String vcsid, String shortId, String testName) {
330         // Perform setup.
331         setupReadList();
332
333         // Submit the request to the service and store the response.
334         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
335         ClientResponse<AbstractCommonList> res = null;
336         if (vcsid != null) {
337             res = client.readItemList(vcsid, null, null);
338         } else if (shortId != null) {
339             res = client.readItemListForNamedAuthority(shortId, null, null);
340         } else {
341             Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
342         }
343         int statusCode = res.getStatus();
344
345         // Check the status code of the response: does it match
346         // the expected response(s)?
347         if (logger.isDebugEnabled()) {
348             logger.debug("  " + testName + ": status = " + statusCode);
349         }
350         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
351                 invalidStatusCodeMessage(testRequestType, statusCode));
352         Assert.assertEquals(statusCode, testExpectedStatusCode);
353
354         AbstractCommonList list = res.getEntity();
355         List<AbstractCommonList.ListItem> items = list.getListItem();
356         int nItemsReturned = items.size();
357         long nItemsTotal = list.getTotalItems();
358         if (logger.isDebugEnabled()) {
359             logger.debug("  " + testName + ": Expected "
360                     + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
361         }
362         Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
363
364         if(logger.isTraceEnabled()){
365                 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
366         }
367     }
368     
369     @Test(dataProvider = "testName",
370                 dependsOnMethods = {"createItemList"})
371     public void readItemList(String testName) {
372         readItemListInt(knownAuthorityWithItems, null, testName);
373     }
374
375     @Test(dataProvider = "testName",
376                 dependsOnMethods = {"readItem"})
377     public void readItemListByName(String testName) {
378         readItemListInt(null, READITEMS_SHORT_IDENTIFIER, testName);
379     }
380
381     @Test(dataProvider = "testName",
382                 dependsOnMethods = {"deleteItem"})
383     public void deleteNonExistentItem(String testName) {
384         // Perform setup.
385         setupDeleteNonExistent();
386
387         // Submit the request to the service and store the response.
388         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
389         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
390         int statusCode = res.getStatus();
391
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);
396         }
397         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
398                 invalidStatusCodeMessage(testRequestType, statusCode));
399         Assert.assertEquals(statusCode, testExpectedStatusCode);
400     }
401     
402     protected String getServicePathItemsComponent() {
403         return AuthorityClient.ITEMS;
404     }
405     
406         public PoxPayloadOut createItemRequestTypeInstance(AUTHORITY_ITEM_TYPE itemTypeInstance) {
407                 PoxPayloadOut result = null;
408                 
409         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
410         PoxPayloadOut payloadOut = new PoxPayloadOut(this.getServicePathItemsComponent());
411         PayloadOutputPart part = payloadOut.addPart(client.getItemCommonPartName(), itemTypeInstance);
412         result = payloadOut;
413                 
414                 return result;
415         }
416
417         /**
418          * Update an Authority item.
419          * 
420          * @param testName
421          * @throws Exception
422          */
423     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
424                 dependsOnMethods = {"readItem", "CRUDTests", "verifyIgnoredUpdateWithInAuthority"})
425     public void updateItem(String testName) throws Exception {
426         // Perform setup.
427         setupUpdate();
428         AUTHORITY_ITEM_TYPE theUpdate = null;
429
430         // Retrieve the contents of a resource to update.
431         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
432                         (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
433         ClientResponse<String> res =
434                 client.readItem(knownResourceId, knownItemResourceId);
435         try {
436                 if (logger.isDebugEnabled()) {
437                     logger.debug(testName + ": read status = " + res.getStatus());
438                 }
439                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
440         
441                 if (logger.isDebugEnabled()) {
442                     logger.debug("got Authority item to update with ID: "
443                             + knownItemResourceId
444                             + " in authority: " + knownResourceId);
445                 }
446                 AUTHORITY_ITEM_TYPE authorityItem = extractItemCommonPartValue(res);
447                 Assert.assertNotNull(authorityItem);
448
449                 // Update the contents of this resource.
450                 theUpdate = updateItemInstance(authorityItem);
451                 if (logger.isDebugEnabled()) {
452                     logger.debug("\n\nTo be updated fields: CSID = "  + knownItemResourceId + "\n"
453                                 + objectAsXmlString(theUpdate));
454                 }
455         } finally {
456                 res.releaseConnection();
457         }
458
459         // Submit the updated resource to the service and store the response.
460         PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
461         res = client.updateItem(knownResourceId, knownItemResourceId, output);
462         try {
463                 int statusCode = res.getStatus();
464         
465                 // Check the status code of the response: does it match the expected response(s)?
466                 if (logger.isDebugEnabled()) {
467                     logger.debug("updateItem: status = " + statusCode);
468                 }
469                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
470                         invalidStatusCodeMessage(testRequestType, statusCode));
471                 Assert.assertEquals(statusCode, testExpectedStatusCode);
472         
473                 // Retrieve the updated resource and verify that its contents exist.
474                 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
475                 Assert.assertNotNull(updatedVocabularyItem);
476
477                 compareUpdatedItemInstances(theUpdate, updatedVocabularyItem);
478         } finally {
479                 res.releaseConnection();
480         }
481     }
482     
483     protected abstract PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier);
484     
485     /* (non-Javadoc)
486      * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
487      */
488     @Test(dataProvider = "testName",
489         dependsOnMethods = {"create", "update", "updateNonExistent"})
490     public void updateNonExistentItem(String testName) throws Exception {
491         // Perform setup.
492         setupUpdateNonExistent();
493
494         // Submit the request to the service and store the response.
495         // Note: The ID used in this 'create' call may be arbitrary.
496         // The only relevant ID may be the one used in update(), below.
497         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
498                         (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
499         PoxPayloadOut multipart = createNonExistenceItemInstance(client.getItemCommonPartName(), NON_EXISTENT_ID);
500         ClientResponse<String> res =
501                         client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
502         try {
503                 int statusCode = res.getStatus();
504         
505                 // Check the status code of the response: does it match
506                 // the expected response(s)?
507                 if (logger.isDebugEnabled()) {
508                         logger.debug(testName + ": status = " + statusCode);
509                 }
510                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
511                                 invalidStatusCodeMessage(testRequestType, statusCode));
512                 Assert.assertEquals(statusCode, testExpectedStatusCode);
513         } finally {
514                 res.releaseConnection();
515         }
516     }
517         
518     //
519     // Methods to persuade TestNG to follow the correct test dependency path
520     //
521     
522     @Test(dataProvider = "testName",
523                 dependsOnMethods = {"createItem"})
524     public void baseAuthorityTests(String testName) {
525         // Do nothing.  Here just to setup a test dependency chain.
526     }
527     
528     /*
529      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
530      * refer to this method in their @Test annotation declarations.
531      */
532     @Override
533     @Test(dataProvider = "testName",
534                 dependsOnMethods = {
535                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
536         public void CRUDTests(String testName) {
537                 // TODO Auto-generated method stub
538         }
539     
540 }