]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
83bb131b4d25615f90e5a44e0fb0c36f99da06e3
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client.test;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import javax.ws.rs.core.Response;
8
9 import org.collectionspace.services.client.AbstractCommonListUtils;
10 import org.collectionspace.services.client.AuthorityClient;
11 import org.collectionspace.services.client.AuthorityClientImpl;
12 import org.collectionspace.services.client.AuthorityProxy;
13 import org.collectionspace.services.client.CollectionSpaceClient;
14 import org.collectionspace.services.client.PayloadInputPart;
15 import org.collectionspace.services.client.PayloadOutputPart;
16 import org.collectionspace.services.client.PoxPayloadOut;
17 import org.collectionspace.services.jaxb.AbstractCommonList;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.testng.Assert;
21 import org.testng.annotations.Test;
22
23 /**
24  * 
25  * @author remillet
26  *
27  * @param <AUTHORITY_COMMON_TYPE>
28  * @param <AUTHORITY_ITEM_TYPE>
29  * 
30  * All CRUD related authority test classes should extend this class.
31  * 
32  */
33 public abstract class AbstractAuthorityServiceTest<AUTHORITY_COMMON_TYPE, AUTHORITY_ITEM_TYPE> 
34         extends AbstractPoxServiceTestImpl<AbstractCommonList, AUTHORITY_COMMON_TYPE> {
35
36     private final Logger logger = LoggerFactory.getLogger(AbstractAuthorityServiceTest.class);
37         
38     protected String knownResourceShortIdentifer = null;
39         protected static final String READITEMS_SHORT_IDENTIFIER = "resourceWithItems" + random.nextInt(1000); 
40         protected String knownAuthorityWithItems = null;
41         
42         protected static final String SAS_IDENTIFIER = "SAS"; 
43         protected String knownSASAuthorityResourceId = null;
44         protected String knownSASItemResourceId = null;
45         protected HashMap<String, String> allSASResourceItemIdsCreated = new HashMap<String, String>(); /* itemURN, parentURN */;
46
47         protected String knownResourceRefName = null;
48     protected String knownItemResourceId = null;
49     protected String knownItemResourceShortIdentifer = null;    
50     protected int nItemsToCreateInList = 5;
51     protected String TEST_SHORTID = "johnWayneActor";
52
53     /*
54      * Abstract methods that subclasses must override/implement
55      */
56     
57     /**
58      * 
59      * @param testName
60      */
61     public abstract void authorityTests(String testName);
62         
63         /**
64          * 
65          * @param client
66          * @param vcsid
67          * @return
68          */
69         abstract protected String createItemInAuthority(AuthorityClient client, String vcsid, String shortId);
70         
71     
72     /**
73      * 
74      * @param authorityItem
75      * @return
76      */
77     protected abstract AUTHORITY_ITEM_TYPE updateItemInstance(final AUTHORITY_ITEM_TYPE authorityItem);    
78     
79     /**
80      * 
81      * @param original
82      * @param updated
83      * @throws Exception
84      */
85     protected abstract void compareUpdatedItemInstances(AUTHORITY_ITEM_TYPE original, AUTHORITY_ITEM_TYPE updated) throws Exception;
86     
87     /**
88      * 
89      * @param id
90      * @param shortIdentifer
91      */
92     protected void setKnownItemResource(String id, String shortIdentifer ) {
93         knownItemResourceId = id;
94         knownItemResourceShortIdentifer = shortIdentifer;
95     }
96
97     /**
98      * 
99      * @param id
100      * @param shortIdentifer
101      * @param refName
102      */
103     protected void setKnownResource(String id, String shortIdentifer,
104             String refName) {
105         knownResourceId = id;
106         knownResourceShortIdentifer = shortIdentifer;
107         knownResourceRefName = refName;
108     }
109
110     /**
111      * 
112      * @return
113      */
114         protected String getSASAuthorityIdentifier() {
115                 // TODO Auto-generated method stub
116                 return this.getKnowResourceIdentifier() + SAS_IDENTIFIER;
117         }
118     
119         /**
120          * 
121          * @param shortId
122          * @return
123          */
124         protected String getUrnIdentifier(String shortId) {
125                 return String.format("urn:cspace:name(%s)", shortId);
126         }
127         
128     /**
129      * Sets up create tests.
130      */
131     protected void setupSync() {
132         testExpectedStatusCode = this.STATUS_OK;
133         testRequestType = ServiceRequestType.SYNC;
134         testSetup(testExpectedStatusCode, testRequestType);
135     }
136     
137     /**
138      * Gets a client to the SAS (Shared Authority Server)
139      *
140      * @return the client
141      */
142     protected AuthorityClient getSASClientInstance() {
143         return (AuthorityClient) this.getClientInstance(CollectionSpaceClient.SAS_CLIENT_PROPERTIES_FILENAME);
144     }
145
146     /**
147      * Returns the root URL for a service.
148      *
149      * This URL consists of a base URL for all services, followed by
150      * a path component for the owning vocabulary, followed by the 
151      * path component for the items.
152      *
153      * @return The root URL for a service.
154      */
155     protected String getItemServiceRootURL(String parentResourceIdentifier) {
156         return getResourceURL(parentResourceIdentifier) + "/" + getServicePathItemsComponent();
157     }
158
159     /**
160      * Returns the URL of a specific resource managed by a service, and
161      * designated by an identifier (such as a universally unique ID, or UUID).
162      *
163      * @param  resourceIdentifier  An identifier (such as a UUID) for a resource.
164      *
165      * @return The URL of a specific resource managed by a service.
166      */
167     protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
168         return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
169     }
170         
171     /**
172      * For authorities we override this method so we can save the shortid.
173      */
174     @Override
175     protected String createWithIdentifier(String testName, String identifier) throws Exception {
176         String csid = createResource(testName, identifier);
177         // Store the ID returned from the first resource created
178         // for additional tests below.
179         if (getKnowResourceId() == null) {
180                 setKnownResource(csid, identifier /*shortId*/, null /*refname*/ );
181             if (logger.isDebugEnabled()) {
182                 logger.debug(testName + ": Setting knownResourceId=" + getKnowResourceId());
183             }
184         }
185         
186         return identifier;
187     }    
188     
189     @Test(dependsOnMethods = {"readItem", "CRUDTests"})
190     public void testItemSubmitRequest() {
191
192         // Expected status code: 200 OK
193         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
194
195         // Submit the request to the service and store the response.
196         String method = ServiceRequestType.READ.httpMethodName();
197         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
198         int statusCode = submitRequest(method, url);
199
200         // Check the status code of the response: does it match
201         // the expected response(s)?
202         if (logger.isDebugEnabled()) {
203             logger.debug("testItemSubmitRequest: url=" + url
204                     + " status=" + statusCode);
205         }
206         Assert.assertEquals(statusCode, EXPECTED_STATUS);
207     }    
208
209     
210     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
211         dependsOnMethods = {"readItem"})
212     public void verifyIgnoredUpdateWithInAuthority(String testName) throws Exception {
213         // Perform setup.
214         setupUpdate();
215
216         // Submit the request to the service and store the response.
217         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client = 
218                         (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
219         Response res = client.readItem(knownResourceId, knownItemResourceId);
220         AUTHORITY_ITEM_TYPE vitem = null;
221         try {
222                 int statusCode = res.getStatus();
223         
224                 // Check the status code of the response: does it match
225                 // the expected response(s)?
226                 if (logger.isDebugEnabled()) {
227                         logger.debug(testName + " read authority:" + knownResourceId + "/Item:"
228                                         + knownItemResourceId + " status = " + statusCode);
229                 }
230                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
231                                 invalidStatusCodeMessage(testRequestType, statusCode));
232                 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
233         
234                 vitem = extractItemCommonPartValue(res);
235                 Assert.assertNotNull(vitem);
236                 // Try to Update with new parent vocab (use self, for test).
237                 Assert.assertEquals(client.getInAuthority(vitem), knownResourceId,
238                                 "VocabularyItem inAuthority does not match knownResourceId.");
239                 client.setInAuthority(vitem, knownItemResourceId);
240
241         } finally {
242                 res.close();
243         }
244         
245         // Submit the updated resource to the service and store the response.
246         PoxPayloadOut output = this.createItemRequestTypeInstance(vitem);
247         res = client.updateItem(knownResourceId, knownItemResourceId, output);
248         try {
249                 int statusCode = res.getStatus();
250         
251                 // Check the status code of the response: does it match the expected response(s)?
252                 if (logger.isDebugEnabled()) {
253                         logger.debug(testName + ": status = " + statusCode);
254                 }
255                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
256                                 invalidStatusCodeMessage(testRequestType, statusCode));
257                 Assert.assertEquals(statusCode, testExpectedStatusCode);
258         } finally {
259                 res.close();
260         }
261         
262         res = client.readItem(knownResourceId, knownItemResourceId);
263         try {
264                 // Retrieve the updated resource and verify that the parent did not change
265                 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
266                 Assert.assertNotNull(updatedVocabularyItem);
267         
268                 // Verify that the updated resource received the correct data.
269                 Assert.assertEquals(client.getInAuthority(updatedVocabularyItem),
270                                 knownResourceId,
271                                 "VocabularyItem allowed update to the parent (inAuthority).");
272         } finally {
273                 res.close();
274         }
275     }
276     
277     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
278     public void createItem(String testName) {
279         // Perform setup.
280         setupCreate();
281
282         String newID = createItemInAuthority((AuthorityClient) getClientInstance(), knownResourceId, getTestAuthorityItemShortId());
283
284         // Store the ID returned from the first item resource created
285         // for additional tests below.
286         if (knownItemResourceId == null) {
287             knownItemResourceId = newID;
288             if (null != testName && logger.isDebugEnabled()) {
289                 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
290             }
291         }
292     }
293     
294     /**
295      * Sync the local with the SAS
296      */
297     @Test(dataProvider = "testName", dependsOnMethods = {"createSASItem", "CRUDTests"})
298     public void syncWithSAS(String testName) {
299         //
300         // First check to see if the authority supports synchronization.
301         //
302         AuthorityClient client = (AuthorityClient) this.getClientInstance();
303         if (client.supportsSync() == false) {
304                 return; // Exit the test since this authority doesn't support synchronization
305         }
306         
307         //
308         // Create an empty instance of the authority, so we can sync items with it.  We're
309         // using the short ID of the SAS authority.  The short ID of the local and the SAS will (must) be the same.
310         //
311         String localAuthorityId = null;
312         try {
313                         localAuthorityId = createResource(client, testName, getSASAuthorityIdentifier());
314                 } catch (Exception e) {
315                         Assert.assertNotNull(localAuthorityId);
316                 }
317
318         //
319         // Now we can try to sync the SAS authority with the local one we just created.
320         //
321         setupSync();
322         Response response = client.syncByName(getSASAuthorityIdentifier()); // Notice we're using the Short ID (short ID is the same on the local and SAS)
323         try {
324                 int statusCode = response.getStatus();
325                 if (logger.isDebugEnabled()) {
326                     logger.debug(testName + ": HTTP status = " + statusCode);
327                 }
328                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
329                         invalidStatusCodeMessage(testRequestType, statusCode));
330                 Assert.assertEquals(statusCode, testExpectedStatusCode);
331         } finally {
332                 response.close();
333         }
334     }
335     
336     /**
337      * SAS - Create a new authority on the SAS server.
338      * @param testName
339      */    
340     @Test(dataProvider = "testName", dependsOnMethods = {"createItem", "CRUDTests"})
341     public void createSASAuthority(String testName) {
342         //
343         // First check to see if the authority supports synchronization.
344         //
345         AuthorityClient client = (AuthorityClient) this.getClientInstance();
346         if (client.supportsSync() == false) {
347                 return; // Exit the test since this authority doesn't support synchronization
348         }
349         
350         // Perform setup.
351         setupCreate();
352
353         try {
354                 String newID = createResource(getSASClientInstance(), testName, getSASAuthorityIdentifier());
355                 knownSASAuthorityResourceId = newID;
356             if (logger.isDebugEnabled()) {
357                 String.format("Created SAS authority '%s' with CSID=%s.", getSASAuthorityIdentifier(), newID);
358             }
359         } catch (Exception e) {
360                 logger.info(String.format("Failed to create SAS authority '%s'.", getSASAuthorityIdentifier()));
361         }
362     }
363
364     /**
365      * SAS - Create an item in the SAS authority on the SAS server.
366      * @param testName
367      */
368     @Test(dataProvider = "testName", dependsOnMethods = {"createSASAuthority", "CRUDTests"})
369     public void createSASItem(String testName) {
370         //
371         // First check to see if the authority supports synchronization.
372         //
373         AuthorityClient client = (AuthorityClient) this.getClientInstance();
374         if (client.supportsSync() == false) {
375                 return; // Exit the test since this authority doesn't support synchronization
376         }
377         
378         // Perform setup.
379         setupCreate();
380
381         String shortId = "SassyActor" + System.currentTimeMillis() + Math.abs(random.nextInt()); // short ID needs to be unique
382         String newID = createItemInAuthority(getSASClientInstance(), knownSASAuthorityResourceId, shortId);
383
384                 // Store the ID returned from the first item resource created
385         // for additional tests below.
386         if (knownSASItemResourceId == null) {
387                 knownSASItemResourceId = newID;
388             if (null != testName && logger.isDebugEnabled()) {
389                 logger.debug(testName + ": knownSASItemResourceId=" + knownSASItemResourceId);
390             }
391         }
392         //
393         // Keep track of the SAS authority items we create, so we can delete them from
394         // the *local* authority after we perform a sync operation.  We need to keep track
395         // of the URN (not the CSID) since the CSIDs will differ on the SAS vs local.
396         //
397         this.allSASResourceItemIdsCreated.put(this.getUrnIdentifier(shortId), getUrnIdentifier(getSASAuthorityIdentifier()));
398     }
399     
400     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
401                 dependsOnMethods = {"createItem"})
402     public void createItemList(String testName) throws Exception {
403         knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
404         for (int j = 0; j < nItemsToCreateInList; j++) {
405                 createItemInAuthority((AuthorityClient) getClientInstance(), knownAuthorityWithItems, this.getTestAuthorityItemShortId(true));
406         }
407     }
408
409     /**
410      * Read by name.
411      *
412      * @param testName the test name
413      * @throws Exception the exception
414      */
415     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
416                 dependsOnMethods = {"CRUDTests"})
417     public void readByName(String testName) throws Exception {
418         // Perform setup.
419         setupRead();
420
421         // Submit the request to the service and store the response.
422         AuthorityClient client = (AuthorityClient) this.getClientInstance();
423         Response res = client.readByName(getKnowResourceIdentifier());
424         try {
425                 int statusCode = res.getStatus();
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                 AUTHORITY_COMMON_TYPE commonPart = extractCommonPartValue(res);
437                 Assert.assertNotNull(commonPart);
438         } finally {
439                 res.close();
440         }
441     }
442     
443     /**
444      * Extracts the common part item from a service's item payload.
445      * 
446      * @param res
447      * @return
448      * @throws Exception
449      */
450         public AUTHORITY_ITEM_TYPE extractItemCommonPartValue(Response res) throws Exception {
451                 AUTHORITY_ITEM_TYPE result = null;
452                 
453         AuthorityClient client = (AuthorityClient) getClientInstance();
454                 PayloadInputPart payloadInputPart = extractPart(res, client.getItemCommonPartName());
455                 if (payloadInputPart != null) {
456                         result = (AUTHORITY_ITEM_TYPE) payloadInputPart.getBody();
457                 }
458                 Assert.assertNotNull(result,
459                                 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
460                 
461                 return result;
462         }
463     
464     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
465                 dependsOnMethods = {"readItem"})
466     public void readItemNonExistent(String testName) {
467         // Perform setup.
468         setupReadNonExistent();
469
470         // Submit the request to the service and store the response.
471         AuthorityClient client = (AuthorityClient) getClientInstance();
472         Response res = client.readItem(knownResourceId, NON_EXISTENT_ID);
473         try {
474                 int statusCode = res.getStatus();
475         
476                 // Check the status code of the response: does it match
477                 // the expected response(s)?
478                 if (logger.isDebugEnabled()) {
479                     logger.debug(testName + ": status = " + statusCode);
480                 }
481                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
482                         invalidStatusCodeMessage(testRequestType, statusCode));
483                 Assert.assertEquals(statusCode, testExpectedStatusCode);
484         } finally {
485                 res.close();
486         }
487     }
488         
489     @Test(dataProvider = "testName",
490                 dependsOnMethods = {"createItem"})
491     public void readItem(String testName) throws Exception {
492         // Perform setup.
493         setupRead();
494
495         // Submit the request to the service and store the response.
496         AuthorityClient client = (AuthorityClient) getClientInstance();
497         Response res = client.readItem(knownResourceId, knownItemResourceId);
498         try {
499                 int statusCode = res.getStatus();
500         
501                 // Check the status code of the response: does it match
502                 // the expected response(s)?
503                 if (logger.isDebugEnabled()) {
504                     logger.debug(testName + ": status = " + statusCode);
505                 }
506                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
507                         invalidStatusCodeMessage(testRequestType, statusCode));
508                 Assert.assertEquals(statusCode, testExpectedStatusCode);
509         
510                 AUTHORITY_ITEM_TYPE itemCommonPart = extractItemCommonPartValue(res);
511                 Assert.assertNotNull(itemCommonPart);
512                 Assert.assertEquals(client.getInAuthority(itemCommonPart), knownResourceId);
513                 verifyReadItemInstance(itemCommonPart);
514         } finally {
515                 res.close();
516         }
517     }
518     
519     protected abstract void verifyReadItemInstance(AUTHORITY_ITEM_TYPE item) throws Exception;
520         
521     @Test(dataProvider = "testName",
522                 dependsOnMethods = {"testItemSubmitRequest", "updateItem", "verifyIgnoredUpdateWithInAuthority"})    
523     public void deleteItem(String testName) throws Exception {
524         // Perform setup.
525         setupDelete();
526
527         // Submit the request to the service and store the response.
528         AuthorityClient client = (AuthorityClient) getClientInstance();
529         Response res = client.deleteItem(knownResourceId, knownItemResourceId);
530         int statusCode;
531         try {
532                 statusCode = res.getStatus();
533         } finally {
534                 res.close();
535         }
536
537         // Check the status code of the response: does it match
538         // the expected response(s)?
539         if (logger.isDebugEnabled()) {
540             logger.debug("delete: status = " + statusCode);
541         }
542         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
543                 invalidStatusCodeMessage(testRequestType, statusCode));
544         Assert.assertEquals(statusCode, testExpectedStatusCode);
545     }
546     
547     protected void readItemListInt(String vcsid, String shortId, String testName) {
548         // Perform setup.
549         setupReadList();
550
551         // Submit the request to the service and store the response.
552         AuthorityClient client = (AuthorityClient) getClientInstance();
553         Response res = null;
554         if (vcsid != null) {
555             res = client.readItemList(vcsid, null, null);
556         } else if (shortId != null) {
557             res = client.readItemListForNamedAuthority(shortId, null, null);
558         } else {
559             Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
560         }
561         try {
562                 int statusCode = res.getStatus();
563         
564                 // Check the status code of the response: does it match
565                 // the expected response(s)?
566                 if (logger.isDebugEnabled()) {
567                     logger.debug("  " + testName + ": status = " + statusCode);
568                 }
569                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
570                         invalidStatusCodeMessage(testRequestType, statusCode));
571                 Assert.assertEquals(statusCode, testExpectedStatusCode);
572         
573                 AbstractCommonList list = res.readEntity(AbstractCommonList.class);
574                 List<AbstractCommonList.ListItem> items = list.getListItem();
575                 int nItemsReturned = items.size();
576                 long nItemsTotal = list.getTotalItems();
577                 if (logger.isDebugEnabled()) {
578                     logger.debug("  " + testName + ": Expected "
579                             + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
580                 }
581                 Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
582         
583                 if(logger.isTraceEnabled()){
584                         AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
585                 }
586         } finally {
587                 res.close();
588         }
589     }
590     
591     @Test(dataProvider = "testName",
592                 dependsOnMethods = {"createItemList"})
593     public void readItemList(String testName) {
594         readItemListInt(knownAuthorityWithItems, null, testName);
595     }
596
597     @Test(dataProvider = "testName",
598                 dependsOnMethods = {"readItem"})
599     public void readItemListByName(String testName) {
600         readItemListInt(null, READITEMS_SHORT_IDENTIFIER, testName);
601     }
602
603     @Test(dataProvider = "testName",
604                 dependsOnMethods = {"deleteItem"})
605     public void deleteNonExistentItem(String testName) {
606         // Perform setup.
607         setupDeleteNonExistent();
608
609         // Submit the request to the service and store the response.
610         AuthorityClient client = (AuthorityClient) getClientInstance();
611         Response res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
612         int statusCode;
613         try {
614                 statusCode = res.getStatus();
615         } finally {
616                 res.close();
617         }
618
619         // Check the status code of the response: does it match
620         // the expected response(s)?
621         if (logger.isDebugEnabled()) {
622             logger.debug(testName + ": status = " + statusCode);
623         }
624         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
625                 invalidStatusCodeMessage(testRequestType, statusCode));
626         Assert.assertEquals(statusCode, testExpectedStatusCode);
627     }
628     
629     protected String getServicePathItemsComponent() {
630         return AuthorityClient.ITEMS;
631     }
632     
633         public PoxPayloadOut createItemRequestTypeInstance(AUTHORITY_ITEM_TYPE itemTypeInstance) {
634                 PoxPayloadOut result = null;
635                 
636         AuthorityClient client = (AuthorityClient) getClientInstance();
637         PoxPayloadOut payloadOut = new PoxPayloadOut(this.getServicePathItemsComponent());
638         PayloadOutputPart part = payloadOut.addPart(client.getItemCommonPartName(), itemTypeInstance);
639         result = payloadOut;
640                 
641                 return result;
642         }
643
644         /**
645          * Update an Authority item.
646          * 
647          * @param testName
648          * @throws Exception
649          */
650     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
651                 dependsOnMethods = {"readItem", "CRUDTests", "verifyIgnoredUpdateWithInAuthority"})
652     public void updateItem(String testName) throws Exception {
653         // Perform setup.
654         setupUpdate();
655         AUTHORITY_ITEM_TYPE theUpdate = null;
656
657         // Retrieve the contents of a resource to update.
658         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
659                         (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
660         Response res = client.readItem(knownResourceId, knownItemResourceId);
661         try {
662                 if (logger.isDebugEnabled()) {
663                     logger.debug(testName + ": read status = " + res.getStatus());
664                 }
665                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
666         
667                 if (logger.isDebugEnabled()) {
668                     logger.debug("got Authority item to update with ID: "
669                             + knownItemResourceId
670                             + " in authority: " + knownResourceId);
671                 }
672                 AUTHORITY_ITEM_TYPE authorityItem = extractItemCommonPartValue(res);
673                 Assert.assertNotNull(authorityItem);
674
675                 // Update the contents of this resource.
676                 theUpdate = updateItemInstance(authorityItem);
677                 if (logger.isDebugEnabled()) {
678                     logger.debug("\n\nTo be updated fields: CSID = "  + knownItemResourceId + "\n"
679                                 + objectAsXmlString(theUpdate));
680                 }
681         } finally {
682                 res.close();
683         }
684
685         // Submit the updated resource to the service and store the response.
686         PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
687         res = client.updateItem(knownResourceId, knownItemResourceId, output);
688         try {
689                 int statusCode = res.getStatus();
690         
691                 // Check the status code of the response: does it match the expected response(s)?
692                 if (logger.isDebugEnabled()) {
693                     logger.debug("updateItem: status = " + statusCode);
694                 }
695                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
696                         invalidStatusCodeMessage(testRequestType, statusCode));
697                 Assert.assertEquals(statusCode, testExpectedStatusCode);
698         
699                 // Retrieve the updated resource and verify that its contents exist.
700                 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
701                 Assert.assertNotNull(updatedVocabularyItem);
702
703                 compareUpdatedItemInstances(theUpdate, updatedVocabularyItem);
704         } finally {
705                 res.close();
706         }
707     }
708     
709     protected abstract PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier);
710     
711     /* (non-Javadoc)
712      * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
713      */
714     @Test(dataProvider = "testName",
715         dependsOnMethods = {"create", "update", "updateNonExistent"})
716     public void updateNonExistentItem(String testName) throws Exception {
717         // Perform setup.
718         setupUpdateNonExistent();
719
720         // Submit the request to the service and store the response.
721         // Note: The ID used in this 'create' call may be arbitrary.
722         // The only relevant ID may be the one used in update(), below.
723         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
724                         (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
725         PoxPayloadOut multipart = createNonExistenceItemInstance(client.getItemCommonPartName(), NON_EXISTENT_ID);
726         Response res = client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
727         try {
728                 int statusCode = res.getStatus();
729         
730                 // Check the status code of the response: does it match
731                 // the expected response(s)?
732                 if (logger.isDebugEnabled()) {
733                         logger.debug(testName + ": status = " + statusCode);
734                 }
735                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
736                                 invalidStatusCodeMessage(testRequestType, statusCode));
737                 Assert.assertEquals(statusCode, testExpectedStatusCode);
738         } finally {
739                 res.close();
740         }
741     }
742         
743     //
744     // Methods to persuade TestNG to follow the correct test dependency path
745     //
746     
747     @Test(dataProvider = "testName",
748                 dependsOnMethods = {"createItem"})
749     public void baseAuthorityTests(String testName) {
750         // Do nothing.  Here just to setup a test dependency chain.
751     }
752     
753     /*
754      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
755      * refer to this method in their @Test annotation declarations.
756      */
757     @Override
758     @Test(dataProvider = "testName",
759                 dependsOnMethods = {
760                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
761         public void CRUDTests(String testName) {
762                 // TODO Auto-generated method stub
763         }
764         
765     @Override
766     public void cleanUp() {
767         String noTest = System.getProperty("noTestCleanup");
768         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
769             if (logger.isDebugEnabled()) {
770                 logger.debug("Skipping Cleanup phase ...");
771             }
772             return;
773         }
774         
775         AuthorityClient client = (AuthorityClient) this.getClientInstance();
776         String parentResourceId;
777         String itemResourceId;
778         //
779         // Clean up all authority item resources.
780         //
781         for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
782             itemResourceId = entry.getKey();
783             parentResourceId = entry.getValue();
784             Response response = client.deleteItem(parentResourceId, itemResourceId);
785             try {
786                 int status = response.getStatus();
787                 if (status != Response.Status.OK.getStatusCode()) {
788                         logger.debug(String.format("Could not deleted authority item '%s' in authority '%s'.",
789                                         itemResourceId, parentResourceId));
790                 }
791             } finally {
792                 response.close();
793             }
794         }
795         //
796         // Clean up authority items that were the result of a sync with the SAS
797         // all the IDs are URN (not CSIDs).  The URNs work for the local items as well
798         // as the SAS items.
799         //
800         for (Map.Entry<String, String> entry : allSASResourceItemIdsCreated.entrySet()) {
801             itemResourceId = entry.getKey();
802             parentResourceId = entry.getValue();
803             // Note: Any non-success responses from the delete operation
804             // below are ignored and not reported.
805             client.deleteItem(parentResourceId, itemResourceId).close();
806         }
807         //
808         // Clean up authority items on the SAS using the SAS client.
809         //
810         client = (AuthorityClient) this.getSASClientInstance();
811         for (Map.Entry<String, String> entry : allSASResourceItemIdsCreated.entrySet()) {
812             itemResourceId = entry.getKey();
813             parentResourceId = entry.getValue();
814             client.deleteItem(parentResourceId, itemResourceId).close();
815         }
816         //
817         // Finally, call out superclass's cleanUp method to deleted the local authorities
818         //
819         super.cleanUp();
820         //
821         // Call out superclass's cleanUp method to delete the SAS authorities
822         //
823         super.cleanUp(client);        
824     }
825     
826         protected String getTestAuthorityItemShortId() {
827                 return getTestAuthorityItemShortId(false);
828         }
829
830         protected String getTestAuthorityItemShortId(boolean makeUnique) {
831                 String result = TEST_SHORTID;
832                 
833                 if (makeUnique == true) {
834                         result = result + System.currentTimeMillis() + Math.abs(random.nextInt());
835                 }
836                 
837                 return result;
838         }
839 }