]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0e6cb273caac4e0d00230118d0c8c702b24dabb3
[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"; 
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() + this.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 create an empty instance of the authority, so we can sync items with it.  We're
301         // using the short ID of the SAS authority.  The short ID of the local and the SAS will (must) be the same.
302         //
303         AuthorityClient client = (AuthorityClient) this.getClientInstance();
304         String localAuthorityId = null;
305         try {
306                         localAuthorityId = createResource(client, testName, getSASAuthorityIdentifier());
307                 } catch (Exception e) {
308                         Assert.assertNotNull(localAuthorityId);
309                 }
310
311         //
312         // Now we can try to sync the SAS authority with the local one we just created.
313         //
314         setupSync();
315         Response response = client.syncByName(getSASAuthorityIdentifier()); // Notice we're using the Short ID (short ID is the same on the local and SAS)
316         try {
317                 int statusCode = response.getStatus();
318                 if (logger.isDebugEnabled()) {
319                     logger.debug(testName + ": HTTP status = " + statusCode);
320                 }
321                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
322                         invalidStatusCodeMessage(testRequestType, statusCode));
323                 Assert.assertEquals(statusCode, testExpectedStatusCode);
324         } finally {
325                 response.close();
326         }
327     }
328     
329     /**
330      * SAS - Create a new authority on the SAS server.
331      * @param testName
332      */    
333     @Test(dataProvider = "testName", dependsOnMethods = {"createItem", "CRUDTests"})
334     public void createSASAuthority(String testName) {
335         // Perform setup.
336         setupCreate();
337
338         try {
339                 String newID = createResource(getSASClientInstance(), testName, getSASAuthorityIdentifier());
340                 knownSASAuthorityResourceId = newID;
341             if (logger.isDebugEnabled()) {
342                 String.format("Created SAS authority '%s' with CSID=%s.", getSASAuthorityIdentifier(), newID);
343             }
344         } catch (Exception e) {
345                 logger.info(String.format("Failed to create SAS authority '%s'.", getSASAuthorityIdentifier()));
346         }
347     }
348
349     /**
350      * SAS - Create an item in the SAS authority on the SAS server.
351      * @param testName
352      */
353     @Test(dataProvider = "testName", dependsOnMethods = {"createSASAuthority", "CRUDTests"})
354     public void createSASItem(String testName) {
355         // Perform setup.
356         setupCreate();
357
358         String shortId = "SassyActor" + System.currentTimeMillis() + Math.abs(random.nextInt()); // short ID needs to be unique
359         String newID = createItemInAuthority(getSASClientInstance(), knownSASAuthorityResourceId, shortId);
360
361                 // Store the ID returned from the first item resource created
362         // for additional tests below.
363         if (knownSASItemResourceId == null) {
364                 knownSASItemResourceId = newID;
365             if (null != testName && logger.isDebugEnabled()) {
366                 logger.debug(testName + ": knownSASItemResourceId=" + knownSASItemResourceId);
367             }
368         }
369         //
370         // Keep track of the SAS authority items we create, so we can delete them from
371         // the *local* authority after we perform a sync operation.  We need to keep track
372         // of the URN (not the CSID) since the CSIDs will differ on the SAS vs local.
373         //
374         this.allSASResourceItemIdsCreated.put(this.getUrnIdentifier(shortId), getUrnIdentifier(getSASAuthorityIdentifier()));
375     }
376     
377     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
378                 dependsOnMethods = {"createItem"})
379     public void createItemList(String testName) throws Exception {
380         knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
381         for (int j = 0; j < nItemsToCreateInList; j++) {
382                 createItemInAuthority((AuthorityClient) getClientInstance(), knownAuthorityWithItems, this.getTestAuthorityItemShortId(true));
383         }
384     }
385
386     /**
387      * Read by name.
388      *
389      * @param testName the test name
390      * @throws Exception the exception
391      */
392     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
393                 dependsOnMethods = {"CRUDTests"})
394     public void readByName(String testName) throws Exception {
395         // Perform setup.
396         setupRead();
397
398         // Submit the request to the service and store the response.
399         AuthorityClient client = (AuthorityClient) this.getClientInstance();
400         Response res = client.readByName(getKnowResourceIdentifier());
401         try {
402                 int statusCode = res.getStatus();
403         
404                 // Check the status code of the response: does it match
405                 // the expected response(s)?
406                 if (logger.isDebugEnabled()) {
407                     logger.debug(testName + ": status = " + statusCode);
408                 }
409                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
410                         invalidStatusCodeMessage(testRequestType, statusCode));
411                 Assert.assertEquals(statusCode, testExpectedStatusCode);
412                 
413                 AUTHORITY_COMMON_TYPE commonPart = extractCommonPartValue(res);
414                 Assert.assertNotNull(commonPart);
415         } finally {
416                 res.close();
417         }
418     }
419     
420     /**
421      * Extracts the common part item from a service's item payload.
422      * 
423      * @param res
424      * @return
425      * @throws Exception
426      */
427         public AUTHORITY_ITEM_TYPE extractItemCommonPartValue(Response res) throws Exception {
428                 AUTHORITY_ITEM_TYPE result = null;
429                 
430         AuthorityClient client = (AuthorityClient) getClientInstance();
431                 PayloadInputPart payloadInputPart = extractPart(res, client.getItemCommonPartName());
432                 if (payloadInputPart != null) {
433                         result = (AUTHORITY_ITEM_TYPE) payloadInputPart.getBody();
434                 }
435                 Assert.assertNotNull(result,
436                                 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
437                 
438                 return result;
439         }
440     
441     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
442                 dependsOnMethods = {"readItem"})
443     public void readItemNonExistent(String testName) {
444         // Perform setup.
445         setupReadNonExistent();
446
447         // Submit the request to the service and store the response.
448         AuthorityClient client = (AuthorityClient) getClientInstance();
449         Response res = client.readItem(knownResourceId, NON_EXISTENT_ID);
450         try {
451                 int statusCode = res.getStatus();
452         
453                 // Check the status code of the response: does it match
454                 // the expected response(s)?
455                 if (logger.isDebugEnabled()) {
456                     logger.debug(testName + ": status = " + statusCode);
457                 }
458                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
459                         invalidStatusCodeMessage(testRequestType, statusCode));
460                 Assert.assertEquals(statusCode, testExpectedStatusCode);
461         } finally {
462                 res.close();
463         }
464     }
465         
466     @Test(dataProvider = "testName",
467                 dependsOnMethods = {"createItem"})
468     public void readItem(String testName) throws Exception {
469         // Perform setup.
470         setupRead();
471
472         // Submit the request to the service and store the response.
473         AuthorityClient client = (AuthorityClient) getClientInstance();
474         Response res = client.readItem(knownResourceId, knownItemResourceId);
475         try {
476                 int statusCode = res.getStatus();
477         
478                 // Check the status code of the response: does it match
479                 // the expected response(s)?
480                 if (logger.isDebugEnabled()) {
481                     logger.debug(testName + ": status = " + statusCode);
482                 }
483                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
484                         invalidStatusCodeMessage(testRequestType, statusCode));
485                 Assert.assertEquals(statusCode, testExpectedStatusCode);
486         
487                 AUTHORITY_ITEM_TYPE itemCommonPart = extractItemCommonPartValue(res);
488                 Assert.assertNotNull(itemCommonPart);
489                 Assert.assertEquals(client.getInAuthority(itemCommonPart), knownResourceId);
490                 verifyReadItemInstance(itemCommonPart);
491         } finally {
492                 res.close();
493         }
494     }
495     
496     protected abstract void verifyReadItemInstance(AUTHORITY_ITEM_TYPE item) throws Exception;
497         
498     @Test(dataProvider = "testName",
499                 dependsOnMethods = {"testItemSubmitRequest", "updateItem", "verifyIgnoredUpdateWithInAuthority"})    
500     public void deleteItem(String testName) throws Exception {
501         // Perform setup.
502         setupDelete();
503
504         // Submit the request to the service and store the response.
505         AuthorityClient client = (AuthorityClient) getClientInstance();
506         Response res = client.deleteItem(knownResourceId, knownItemResourceId);
507         int statusCode;
508         try {
509                 statusCode = res.getStatus();
510         } finally {
511                 res.close();
512         }
513
514         // Check the status code of the response: does it match
515         // the expected response(s)?
516         if (logger.isDebugEnabled()) {
517             logger.debug("delete: status = " + statusCode);
518         }
519         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
520                 invalidStatusCodeMessage(testRequestType, statusCode));
521         Assert.assertEquals(statusCode, testExpectedStatusCode);
522     }
523     
524     protected void readItemListInt(String vcsid, String shortId, String testName) {
525         // Perform setup.
526         setupReadList();
527
528         // Submit the request to the service and store the response.
529         AuthorityClient client = (AuthorityClient) getClientInstance();
530         Response res = null;
531         if (vcsid != null) {
532             res = client.readItemList(vcsid, null, null);
533         } else if (shortId != null) {
534             res = client.readItemListForNamedAuthority(shortId, null, null);
535         } else {
536             Assert.fail("Internal Error: readItemList both vcsid and shortId are null!");
537         }
538         try {
539                 int statusCode = res.getStatus();
540         
541                 // Check the status code of the response: does it match
542                 // the expected response(s)?
543                 if (logger.isDebugEnabled()) {
544                     logger.debug("  " + testName + ": status = " + statusCode);
545                 }
546                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
547                         invalidStatusCodeMessage(testRequestType, statusCode));
548                 Assert.assertEquals(statusCode, testExpectedStatusCode);
549         
550                 AbstractCommonList list = res.readEntity(AbstractCommonList.class);
551                 List<AbstractCommonList.ListItem> items = list.getListItem();
552                 int nItemsReturned = items.size();
553                 long nItemsTotal = list.getTotalItems();
554                 if (logger.isDebugEnabled()) {
555                     logger.debug("  " + testName + ": Expected "
556                             + nItemsToCreateInList + " items; got: " + nItemsReturned + " of: " + nItemsTotal);
557                 }
558                 Assert.assertEquals(nItemsTotal, nItemsToCreateInList);
559         
560                 if(logger.isTraceEnabled()){
561                         AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
562                 }
563         } finally {
564                 res.close();
565         }
566     }
567     
568     @Test(dataProvider = "testName",
569                 dependsOnMethods = {"createItemList"})
570     public void readItemList(String testName) {
571         readItemListInt(knownAuthorityWithItems, null, testName);
572     }
573
574     @Test(dataProvider = "testName",
575                 dependsOnMethods = {"readItem"})
576     public void readItemListByName(String testName) {
577         readItemListInt(null, READITEMS_SHORT_IDENTIFIER, testName);
578     }
579
580     @Test(dataProvider = "testName",
581                 dependsOnMethods = {"deleteItem"})
582     public void deleteNonExistentItem(String testName) {
583         // Perform setup.
584         setupDeleteNonExistent();
585
586         // Submit the request to the service and store the response.
587         AuthorityClient client = (AuthorityClient) getClientInstance();
588         Response res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
589         int statusCode;
590         try {
591                 statusCode = res.getStatus();
592         } finally {
593                 res.close();
594         }
595
596         // Check the status code of the response: does it match
597         // the expected response(s)?
598         if (logger.isDebugEnabled()) {
599             logger.debug(testName + ": status = " + statusCode);
600         }
601         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
602                 invalidStatusCodeMessage(testRequestType, statusCode));
603         Assert.assertEquals(statusCode, testExpectedStatusCode);
604     }
605     
606     protected String getServicePathItemsComponent() {
607         return AuthorityClient.ITEMS;
608     }
609     
610         public PoxPayloadOut createItemRequestTypeInstance(AUTHORITY_ITEM_TYPE itemTypeInstance) {
611                 PoxPayloadOut result = null;
612                 
613         AuthorityClient client = (AuthorityClient) getClientInstance();
614         PoxPayloadOut payloadOut = new PoxPayloadOut(this.getServicePathItemsComponent());
615         PayloadOutputPart part = payloadOut.addPart(client.getItemCommonPartName(), itemTypeInstance);
616         result = payloadOut;
617                 
618                 return result;
619         }
620
621         /**
622          * Update an Authority item.
623          * 
624          * @param testName
625          * @throws Exception
626          */
627     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
628                 dependsOnMethods = {"readItem", "CRUDTests", "verifyIgnoredUpdateWithInAuthority"})
629     public void updateItem(String testName) throws Exception {
630         // Perform setup.
631         setupUpdate();
632         AUTHORITY_ITEM_TYPE theUpdate = null;
633
634         // Retrieve the contents of a resource to update.
635         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
636                         (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
637         Response res = client.readItem(knownResourceId, knownItemResourceId);
638         try {
639                 if (logger.isDebugEnabled()) {
640                     logger.debug(testName + ": read status = " + res.getStatus());
641                 }
642                 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
643         
644                 if (logger.isDebugEnabled()) {
645                     logger.debug("got Authority item to update with ID: "
646                             + knownItemResourceId
647                             + " in authority: " + knownResourceId);
648                 }
649                 AUTHORITY_ITEM_TYPE authorityItem = extractItemCommonPartValue(res);
650                 Assert.assertNotNull(authorityItem);
651
652                 // Update the contents of this resource.
653                 theUpdate = updateItemInstance(authorityItem);
654                 if (logger.isDebugEnabled()) {
655                     logger.debug("\n\nTo be updated fields: CSID = "  + knownItemResourceId + "\n"
656                                 + objectAsXmlString(theUpdate));
657                 }
658         } finally {
659                 res.close();
660         }
661
662         // Submit the updated resource to the service and store the response.
663         PoxPayloadOut output = this.createItemRequestTypeInstance(theUpdate);
664         res = client.updateItem(knownResourceId, knownItemResourceId, output);
665         try {
666                 int statusCode = res.getStatus();
667         
668                 // Check the status code of the response: does it match the expected response(s)?
669                 if (logger.isDebugEnabled()) {
670                     logger.debug("updateItem: status = " + statusCode);
671                 }
672                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
673                         invalidStatusCodeMessage(testRequestType, statusCode));
674                 Assert.assertEquals(statusCode, testExpectedStatusCode);
675         
676                 // Retrieve the updated resource and verify that its contents exist.
677                 AUTHORITY_ITEM_TYPE updatedVocabularyItem = extractItemCommonPartValue(res);
678                 Assert.assertNotNull(updatedVocabularyItem);
679
680                 compareUpdatedItemInstances(theUpdate, updatedVocabularyItem);
681         } finally {
682                 res.close();
683         }
684     }
685     
686     protected abstract PoxPayloadOut createNonExistenceItemInstance(String commonPartName, String identifier);
687     
688     /* (non-Javadoc)
689      * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
690      */
691     @Test(dataProvider = "testName",
692         dependsOnMethods = {"create", "update", "updateNonExistent"})
693     public void updateNonExistentItem(String testName) throws Exception {
694         // Perform setup.
695         setupUpdateNonExistent();
696
697         // Submit the request to the service and store the response.
698         // Note: The ID used in this 'create' call may be arbitrary.
699         // The only relevant ID may be the one used in update(), below.
700         AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy> client =
701                         (AuthorityClientImpl<AUTHORITY_ITEM_TYPE, AuthorityProxy>)this.getClientInstance();
702         PoxPayloadOut multipart = createNonExistenceItemInstance(client.getItemCommonPartName(), NON_EXISTENT_ID);
703         Response res = client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
704         try {
705                 int statusCode = res.getStatus();
706         
707                 // Check the status code of the response: does it match
708                 // the expected response(s)?
709                 if (logger.isDebugEnabled()) {
710                         logger.debug(testName + ": status = " + statusCode);
711                 }
712                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
713                                 invalidStatusCodeMessage(testRequestType, statusCode));
714                 Assert.assertEquals(statusCode, testExpectedStatusCode);
715         } finally {
716                 res.close();
717         }
718     }
719         
720     //
721     // Methods to persuade TestNG to follow the correct test dependency path
722     //
723     
724     @Test(dataProvider = "testName",
725                 dependsOnMethods = {"createItem"})
726     public void baseAuthorityTests(String testName) {
727         // Do nothing.  Here just to setup a test dependency chain.
728     }
729     
730     /*
731      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
732      * refer to this method in their @Test annotation declarations.
733      */
734     @Override
735     @Test(dataProvider = "testName",
736                 dependsOnMethods = {
737                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
738         public void CRUDTests(String testName) {
739                 // TODO Auto-generated method stub
740         }
741         
742     @Override
743     public void cleanUp() {
744         String noTest = System.getProperty("noTestCleanup");
745         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
746             if (logger.isDebugEnabled()) {
747                 logger.debug("Skipping Cleanup phase ...");
748             }
749             return;
750         }
751         
752         AuthorityClient client = (AuthorityClient) this.getClientInstance();
753         String parentResourceId;
754         String itemResourceId;
755         //
756         // Clean up all authority item resources.
757         //
758         for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
759             itemResourceId = entry.getKey();
760             parentResourceId = entry.getValue();
761             Response response = client.deleteItem(parentResourceId, itemResourceId);
762             try {
763                 int status = response.getStatus();
764                 if (status != Response.Status.OK.getStatusCode()) {
765                         logger.debug(String.format("Could not deleted authority item '%s' in authority '%s'.",
766                                         itemResourceId, parentResourceId));
767                 }
768             } finally {
769                 response.close();
770             }
771         }
772         //
773         // Clean up authority items that were the result of a sync with the SAS
774         // all the IDs are URN (not CSIDs).  The URNs work for the local items as well
775         // as the SAS items.
776         //
777         for (Map.Entry<String, String> entry : allSASResourceItemIdsCreated.entrySet()) {
778             itemResourceId = entry.getKey();
779             parentResourceId = entry.getValue();
780             // Note: Any non-success responses from the delete operation
781             // below are ignored and not reported.
782             client.deleteItem(parentResourceId, itemResourceId).close();
783         }
784         //
785         // Clean up authority items on the SAS using the SAS client.
786         //
787         client = (AuthorityClient) this.getSASClientInstance();
788         for (Map.Entry<String, String> entry : allSASResourceItemIdsCreated.entrySet()) {
789             itemResourceId = entry.getKey();
790             parentResourceId = entry.getValue();
791             client.deleteItem(parentResourceId, itemResourceId).close();
792         }
793         //
794         // Finally, call out superclass's cleanUp method to deleted the local authorities
795         //
796         super.cleanUp();
797         //
798         // Call out superclass's cleanUp method to delete the SAS authorities
799         //
800         super.cleanUp(client);        
801     }
802     
803         protected String getTestAuthorityItemShortId() {
804                 return getTestAuthorityItemShortId(false);
805         }
806
807         protected String getTestAuthorityItemShortId(boolean makeUnique) {
808                 String result = TEST_SHORTID;
809                 
810                 if (makeUnique == true) {
811                         result = result + System.currentTimeMillis() + Math.abs(random.nextInt());
812                 }
813                 
814                 return result;
815         }
816 }