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