]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
08578f359f70b8a49ae8fe8e874d8f25ce529419
[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         Response res = client.deleteItem(knownResourceId, knownItemResourceId);
317         int statusCode;
318         try {
319                 statusCode = res.getStatus();
320         } finally {
321                 res.close();
322         }
323
324         // Check the status code of the response: does it match
325         // the expected response(s)?
326         if (logger.isDebugEnabled()) {
327             logger.debug("delete: status = " + statusCode);
328         }
329         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
330                 invalidStatusCodeMessage(testRequestType, statusCode));
331         Assert.assertEquals(statusCode, testExpectedStatusCode);
332     }
333     
334     protected void readItemListInt(String vcsid, String shortId, String testName) {
335         // Perform setup.
336         setupReadList();
337
338         // Submit the request to the service and store the response.
339         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
340         ClientResponse<AbstractCommonList> res = null;
341         if (vcsid != null) {
342             res = client.readItemList(vcsid, null, null);
343         } else if (shortId != null) {
344             res = client.readItemListForNamedAuthority(shortId, null, null);
345         } else {
346             Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
347         }
348         int statusCode = res.getStatus();
349
350         // Check the status code of the response: does it match
351         // the expected response(s)?
352         if (logger.isDebugEnabled()) {
353             logger.debug("  " + testName + ": status = " + statusCode);
354         }
355         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
356                 invalidStatusCodeMessage(testRequestType, statusCode));
357         Assert.assertEquals(statusCode, testExpectedStatusCode);
358
359         AbstractCommonList list = res.getEntity();
360         List<AbstractCommonList.ListItem> items = list.getListItem();
361         int nItemsReturned = items.size();
362         long nItemsTotal = list.getTotalItems();
363         if (logger.isDebugEnabled()) {
364             logger.debug("  " + testName + ": Expected "
365                     + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
366         }
367         Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
368
369         if(logger.isTraceEnabled()){
370                 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
371         }
372     }
373     
374     @Test(dataProvider = "testName",
375                 dependsOnMethods = {"createItemList"})
376     public void readItemList(String testName) {
377         readItemListInt(knownAuthorityWithItems, null, testName);
378     }
379
380     @Test(dataProvider = "testName",
381                 dependsOnMethods = {"readItem"})
382     public void readItemListByName(String testName) {
383         readItemListInt(null, READITEMS_SHORT_IDENTIFIER, testName);
384     }
385
386     @Test(dataProvider = "testName",
387                 dependsOnMethods = {"deleteItem"})
388     public void deleteNonExistentItem(String testName) {
389         // Perform setup.
390         setupDeleteNonExistent();
391
392         // Submit the request to the service and store the response.
393         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
394         Response res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
395         int statusCode;
396         try {
397                 statusCode = res.getStatus();
398         } finally {
399                 res.close();
400         }
401
402         // Check the status code of the response: does it match
403         // the expected response(s)?
404         if (logger.isDebugEnabled()) {
405             logger.debug(testName + ": status = " + statusCode);
406         }
407         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
408                 invalidStatusCodeMessage(testRequestType, statusCode));
409         Assert.assertEquals(statusCode, testExpectedStatusCode);
410     }
411     
412     protected String getServicePathItemsComponent() {
413         return AuthorityClient.ITEMS;
414     }
415     
416         public PoxPayloadOut createItemRequestTypeInstance(AUTHORITY_ITEM_TYPE itemTypeInstance) {
417                 PoxPayloadOut result = null;
418                 
419         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
420         PoxPayloadOut payloadOut = new PoxPayloadOut(this.getServicePathItemsComponent());
421         PayloadOutputPart part = payloadOut.addPart(client.getItemCommonPartName(), itemTypeInstance);
422         result = payloadOut;
423                 
424                 return result;
425         }
426
427         /**
428          * Update an Authority item.
429          * 
430          * @param testName
431          * @throws Exception
432          */
433     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
434                 dependsOnMethods = {"readItem", "CRUDTests", "verifyIgnoredUpdateWithInAuthority"})
435     public void updateItem(String testName) throws Exception {
436         // Perform setup.
437         setupUpdate();
438         AUTHORITY_ITEM_TYPE theUpdate = null;
439
440         // Retrieve the contents of a resource to update.
441         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
442                         (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
443         ClientResponse<String> res =
444                 client.readItem(knownResourceId, knownItemResourceId);
445         try {
446                 if (logger.isDebugEnabled()) {
447                     logger.debug(testName + ": read status = " + res.getStatus());
448                 }
449                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
450         
451                 if (logger.isDebugEnabled()) {
452                     logger.debug("got Authority item to update with ID: "
453                             + knownItemResourceId
454                             + " in authority: " + knownResourceId);
455                 }
456                 AUTHORITY_ITEM_TYPE authorityItem = extractItemCommonPartValue(res);
457                 Assert.assertNotNull(authorityItem);
458
459                 // Update the contents of this resource.
460                 theUpdate = updateItemInstance(authorityItem);
461                 if (logger.isDebugEnabled()) {
462                     logger.debug("\n\nTo be updated fields: CSID = "  + knownItemResourceId + "\n"
463                                 + objectAsXmlString(theUpdate));
464                 }
465         } finally {
466                 res.releaseConnection();
467         }
468
469         // Submit the updated resource to the service and store the response.
470         PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
471         res = client.updateItem(knownResourceId, knownItemResourceId, output);
472         try {
473                 int statusCode = res.getStatus();
474         
475                 // Check the status code of the response: does it match the expected response(s)?
476                 if (logger.isDebugEnabled()) {
477                     logger.debug("updateItem: status = " + statusCode);
478                 }
479                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
480                         invalidStatusCodeMessage(testRequestType, statusCode));
481                 Assert.assertEquals(statusCode, testExpectedStatusCode);
482         
483                 // Retrieve the updated resource and verify that its contents exist.
484                 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
485                 Assert.assertNotNull(updatedVocabularyItem);
486
487                 compareUpdatedItemInstances(theUpdate, updatedVocabularyItem);
488         } finally {
489                 res.releaseConnection();
490         }
491     }
492     
493     protected abstract PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier);
494     
495     /* (non-Javadoc)
496      * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
497      */
498     @Test(dataProvider = "testName",
499         dependsOnMethods = {"create", "update", "updateNonExistent"})
500     public void updateNonExistentItem(String testName) throws Exception {
501         // Perform setup.
502         setupUpdateNonExistent();
503
504         // Submit the request to the service and store the response.
505         // Note: The ID used in this 'create' call may be arbitrary.
506         // The only relevant ID may be the one used in update(), below.
507         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
508                         (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
509         PoxPayloadOut multipart = createNonExistenceItemInstance(client.getItemCommonPartName(), NON_EXISTENT_ID);
510         ClientResponse<String> res =
511                         client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
512         try {
513                 int statusCode = res.getStatus();
514         
515                 // Check the status code of the response: does it match
516                 // the expected response(s)?
517                 if (logger.isDebugEnabled()) {
518                         logger.debug(testName + ": status = " + statusCode);
519                 }
520                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
521                                 invalidStatusCodeMessage(testRequestType, statusCode));
522                 Assert.assertEquals(statusCode, testExpectedStatusCode);
523         } finally {
524                 res.releaseConnection();
525         }
526     }
527         
528     //
529     // Methods to persuade TestNG to follow the correct test dependency path
530     //
531     
532     @Test(dataProvider = "testName",
533                 dependsOnMethods = {"createItem"})
534     public void baseAuthorityTests(String testName) {
535         // Do nothing.  Here just to setup a test dependency chain.
536     }
537     
538     /*
539      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
540      * refer to this method in their @Test annotation declarations.
541      */
542     @Override
543     @Test(dataProvider = "testName",
544                 dependsOnMethods = {
545                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
546         public void CRUDTests(String testName) {
547                 // TODO Auto-generated method stub
548         }
549     
550 }