]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7bd15d44c8957150b8a25b90b841b8a79b172ac4
[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         Response res = client.readItem(knownResourceId, knownItemResourceId);
138         AUTHORITY_ITEM_TYPE vitem = null;
139         try {
140                 int statusCode = res.getStatus();
141         
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);
147                 }
148                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
149                                 invalidStatusCodeMessage(testRequestType, statusCode));
150                 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
151         
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);
158
159         } finally {
160                 res.close();
161         }
162         
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);
166         try {
167                 int statusCode = res.getStatus();
168         
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);
172                 }
173                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
174                                 invalidStatusCodeMessage(testRequestType, statusCode));
175                 Assert.assertEquals(statusCode, testExpectedStatusCode);
176         
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);
181         
182                 // Verify that the updated resource received the correct data.
183                 Assert.assertEquals(client.getInAuthority(updatedVocabularyItem),
184                                 knownResourceId,
185                                 "VocabularyItem allowed update to the parent (inAuthority).");
186         } finally {
187                 res.close();
188         }
189     }
190     
191     @Test(dataProvider = "testName",
192                 dependsOnMethods = {"CRUDTests"})
193     public void createItem(String testName) {
194         // Perform setup.
195         setupCreate();
196
197         String newID = createItemInAuthority(knownResourceId);
198
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);
205             }
206         }
207     }
208     
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);
215         }
216     }
217
218     /**
219      * Read by name.
220      *
221      * @param testName the test name
222      * @throws Exception the exception
223      */
224     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
225                 dependsOnMethods = {"CRUDTests"})
226     public void readByName(String testName) throws Exception {
227         // Perform setup.
228         setupRead();
229
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());
233         try {
234                 int statusCode = res.getStatus();
235         
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);
240                 }
241                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
242                         invalidStatusCodeMessage(testRequestType, statusCode));
243                 Assert.assertEquals(statusCode, testExpectedStatusCode);
244                 
245                 AUTHORITY_COMMON_TYPE commonPart = extractCommonPartValue(res);
246                 Assert.assertNotNull(commonPart);
247         } finally {
248                 res.close();
249         }
250     }
251     
252     /**
253      * Extracts the common part item from a service's item payload.
254      * 
255      * @param res
256      * @return
257      * @throws Exception
258      */
259         public AUTHORITY_ITEM_TYPE extractItemCommonPartValue(Response res) throws Exception {
260                 AUTHORITY_ITEM_TYPE result = null;
261                 
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();
267                 }
268                 Assert.assertNotNull(result,
269                                 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
270                 
271                 return result;
272         }
273     
274     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
275                 dependsOnMethods = {"readItem"})
276     public void readItemNonExistent(String testName) {
277         // Perform setup.
278         setupReadNonExistent();
279
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);
284         try {
285                 int statusCode = res.getStatus();
286         
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);
291                 }
292                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
293                         invalidStatusCodeMessage(testRequestType, statusCode));
294                 Assert.assertEquals(statusCode, testExpectedStatusCode);
295         } finally {
296                 res.close();
297         }
298     }
299         
300     @Test(dataProvider = "testName",
301                 dependsOnMethods = {"createItem"})
302     public void readItem(String testName) throws Exception {
303         // Perform setup.
304         setupRead();
305
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);
309         try {
310                 int statusCode = res.getStatus();
311         
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);
316                 }
317                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
318                         invalidStatusCodeMessage(testRequestType, statusCode));
319                 Assert.assertEquals(statusCode, testExpectedStatusCode);
320         
321                 AUTHORITY_ITEM_TYPE itemCommonPart = extractItemCommonPartValue(res);
322                 Assert.assertNotNull(itemCommonPart);
323                 Assert.assertEquals(client.getInAuthority(itemCommonPart), knownResourceId);
324                 verifyReadItemInstance(itemCommonPart);
325         } finally {
326                 res.close();
327         }
328     }
329     
330     protected abstract void verifyReadItemInstance(AUTHORITY_ITEM_TYPE item) throws Exception;
331         
332     @Test(dataProvider = "testName",
333                 dependsOnMethods = {"testItemSubmitRequest", "updateItem", "verifyIgnoredUpdateWithInAuthority"})    
334     public void deleteItem(String testName) throws Exception {
335         // Perform setup.
336         setupDelete();
337
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);
342         int statusCode;
343         try {
344                 statusCode = res.getStatus();
345         } finally {
346                 res.close();
347         }
348
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);
353         }
354         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
355                 invalidStatusCodeMessage(testRequestType, statusCode));
356         Assert.assertEquals(statusCode, testExpectedStatusCode);
357     }
358     
359     protected void readItemListInt(String vcsid, String shortId, String testName) {
360         // Perform setup.
361         setupReadList();
362
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;
366         if (vcsid != null) {
367             res = client.readItemList(vcsid, null, null);
368         } else if (shortId != null) {
369             res = client.readItemListForNamedAuthority(shortId, null, null);
370         } else {
371             Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
372         }
373         int statusCode = res.getStatus();
374
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);
379         }
380         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
381                 invalidStatusCodeMessage(testRequestType, statusCode));
382         Assert.assertEquals(statusCode, testExpectedStatusCode);
383
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);
391         }
392         Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
393
394         if(logger.isTraceEnabled()){
395                 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
396         }
397     }
398     
399     @Test(dataProvider = "testName",
400                 dependsOnMethods = {"createItemList"})
401     public void readItemList(String testName) {
402         readItemListInt(knownAuthorityWithItems, null, testName);
403     }
404
405     @Test(dataProvider = "testName",
406                 dependsOnMethods = {"readItem"})
407     public void readItemListByName(String testName) {
408         readItemListInt(null, READITEMS_SHORT_IDENTIFIER, testName);
409     }
410
411     @Test(dataProvider = "testName",
412                 dependsOnMethods = {"deleteItem"})
413     public void deleteNonExistentItem(String testName) {
414         // Perform setup.
415         setupDeleteNonExistent();
416
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);
420         int statusCode;
421         try {
422                 statusCode = res.getStatus();
423         } finally {
424                 res.close();
425         }
426
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);
431         }
432         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
433                 invalidStatusCodeMessage(testRequestType, statusCode));
434         Assert.assertEquals(statusCode, testExpectedStatusCode);
435     }
436     
437     protected String getServicePathItemsComponent() {
438         return AuthorityClient.ITEMS;
439     }
440     
441         public PoxPayloadOut createItemRequestTypeInstance(AUTHORITY_ITEM_TYPE itemTypeInstance) {
442                 PoxPayloadOut result = null;
443                 
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);
447         result = payloadOut;
448                 
449                 return result;
450         }
451
452         /**
453          * Update an Authority item.
454          * 
455          * @param testName
456          * @throws Exception
457          */
458     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
459                 dependsOnMethods = {"readItem", "CRUDTests", "verifyIgnoredUpdateWithInAuthority"})
460     public void updateItem(String testName) throws Exception {
461         // Perform setup.
462         setupUpdate();
463         AUTHORITY_ITEM_TYPE theUpdate = null;
464
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);
469         try {
470                 if (logger.isDebugEnabled()) {
471                     logger.debug(testName + ": read status = " + res.getStatus());
472                 }
473                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
474         
475                 if (logger.isDebugEnabled()) {
476                     logger.debug("got Authority item to update with ID: "
477                             + knownItemResourceId
478                             + " in authority: " + knownResourceId);
479                 }
480                 AUTHORITY_ITEM_TYPE authorityItem = extractItemCommonPartValue(res);
481                 Assert.assertNotNull(authorityItem);
482
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));
488                 }
489         } finally {
490                 res.close();
491         }
492
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);
496         try {
497                 int statusCode = res.getStatus();
498         
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);
502                 }
503                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
504                         invalidStatusCodeMessage(testRequestType, statusCode));
505                 Assert.assertEquals(statusCode, testExpectedStatusCode);
506         
507                 // Retrieve the updated resource and verify that its contents exist.
508                 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
509                 Assert.assertNotNull(updatedVocabularyItem);
510
511                 compareUpdatedItemInstances(theUpdate, updatedVocabularyItem);
512         } finally {
513                 res.close();
514         }
515     }
516     
517     protected abstract PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier);
518     
519     /* (non-Javadoc)
520      * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
521      */
522     @Test(dataProvider = "testName",
523         dependsOnMethods = {"create", "update", "updateNonExistent"})
524     public void updateNonExistentItem(String testName) throws Exception {
525         // Perform setup.
526         setupUpdateNonExistent();
527
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);
536         try {
537                 int statusCode = res.getStatus();
538         
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);
543                 }
544                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
545                                 invalidStatusCodeMessage(testRequestType, statusCode));
546                 Assert.assertEquals(statusCode, testExpectedStatusCode);
547         } finally {
548                 res.releaseConnection();
549         }
550     }
551         
552     //
553     // Methods to persuade TestNG to follow the correct test dependency path
554     //
555     
556     @Test(dataProvider = "testName",
557                 dependsOnMethods = {"createItem"})
558     public void baseAuthorityTests(String testName) {
559         // Do nothing.  Here just to setup a test dependency chain.
560     }
561     
562     /*
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.
565      */
566     @Override
567     @Test(dataProvider = "testName",
568                 dependsOnMethods = {
569                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
570         public void CRUDTests(String testName) {
571                 // TODO Auto-generated method stub
572         }
573     
574 }