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