]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c2e8b68b2d5405514ce0d5c522181015b206d7dd
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c)) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.collectionspace.services.LocationJAXBSchema;
31 import org.collectionspace.services.client.AuthorityClient;
32 import org.collectionspace.services.client.CollectionSpaceClient;
33 import org.collectionspace.services.client.PayloadOutputPart;
34 import org.collectionspace.services.client.PoxPayloadIn;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.common.AbstractCommonListUtils;
37 import org.collectionspace.services.client.LocationAuthorityClient;
38 import org.collectionspace.services.client.LocationAuthorityClientUtils;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.collectionspace.services.location.LocationauthoritiesCommon;
41 import org.collectionspace.services.location.LocationsCommon;
42
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import org.jboss.resteasy.client.ClientResponse;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import org.testng.Assert;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.Test;
53
54 /**
55  * LocationAuthorityServiceTest, carries out tests against a
56  * deployed and running LocationAuthority Service.
57  *
58  * $LastChangedRevision: 753 $
59  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
60  */
61 public class LocationAuthorityServiceTest extends AbstractServiceTestImpl { //FIXME: Test classes for Vocab, Person, Org, and Location should have a base class!
62
63     /** The logger. */
64     private final String CLASS_NAME = LocationAuthorityServiceTest.class.getName();
65     private final Logger logger = LoggerFactory.getLogger(LocationAuthorityServiceTest.class);
66     private final String REFNAME = "refName";
67     private final String DISPLAYNAME = "displayName";
68
69         @Override
70         public String getServicePathComponent() {
71                 return LocationAuthorityClient.SERVICE_PATH_COMPONENT;
72         }
73
74         @Override
75         protected String getServiceName() {
76                 return LocationAuthorityClient.SERVICE_NAME;
77         }
78     
79     public String getItemServicePathComponent() {
80         return AuthorityClient.ITEMS;
81     }   
82     
83     // Instance variables specific to this test.
84     
85 //    /** The SERVICE path component. */
86 //    final String SERVICE_PATH_COMPONENT = "locationauthorities";
87 //    
88 //    /** The ITEM service path component. */
89 //    final String ITEM_SERVICE_PATH_COMPONENT = "items";
90 //    
91 //    /** The CONTACT service path component. */
92 //    final String CONTACT_SERVICE_PATH_COMPONENT = "contacts";
93     
94     final String TEST_NAME = "Shelf 1";
95     final String TEST_SHORTID = "shelf1";
96     final String TEST_CONDITION_NOTE = "Basically clean";
97     final String TEST_CONDITION_NOTE_DATE = "June 11, 1979";
98     final String TEST_SECURITY_NOTE = "Kind of safe";
99     final String TEST_ACCESS_NOTE = "Only right-thinkers may see";
100     final String TEST_ADDRESS = "123 Main Street, Anytown USA";
101     // TODO Make loc type be a controlled vocab term.
102     final String TEST_LOCATION_TYPE = "Shelf";
103     // TODO Make status type be a controlled vocab term.
104     final String TEST_STATUS = "Approved";
105     
106     /** The known resource id. */
107     private String knownResourceId = null;
108     private String knownResourceShortIdentifer = null;
109     private String knownResourceRefName = null;
110     private String knownLocationTypeRefName = null;
111     private String knownItemResourceId = null;
112     private String knownItemResourceShortIdentifer = null;
113     private String knownContactResourceId = null;
114     
115     /** The n items to create in list. */
116     private int nItemsToCreateInList = 3;
117     
118     /** The all resource ids created. */
119     private List<String> allResourceIdsCreated = new ArrayList<String>();
120     
121     /** The all item resource ids created. */
122     private Map<String, String> allItemResourceIdsCreated =
123         new HashMap<String, String>();
124     
125     protected void setKnownResource( String id, String shortIdentifer,
126                 String refName ) {
127         knownResourceId = id;
128         knownResourceShortIdentifer = shortIdentifer;
129         knownResourceRefName = refName;
130     }
131
132     protected void setKnownItemResource( String id, String shortIdentifer ) {
133         knownItemResourceId = id;
134         knownItemResourceShortIdentifer = shortIdentifer;
135     }
136
137     /* (non-Javadoc)
138      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
139      */
140     @Override
141     protected CollectionSpaceClient getClientInstance() {
142         return new LocationAuthorityClient();
143     }
144     
145     @Override
146     protected PoxPayloadOut createInstance(String identifier) {
147         LocationAuthorityClient client = new LocationAuthorityClient();
148         String shortId = identifier;
149         String displayName = "displayName-" + shortId;
150         String baseRefName = 
151                 LocationAuthorityClientUtils.createLocationAuthRefName(shortId, null);
152         PoxPayloadOut multipart = 
153             LocationAuthorityClientUtils.createLocationAuthorityInstance(
154             displayName, shortId, client.getCommonPartName());
155         return multipart;
156     }
157         
158     // ---------------------------------------------------------------
159     // CRUD tests : CREATE tests
160     // ---------------------------------------------------------------
161     // Success outcomes
162     /* (non-Javadoc)
163      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
164      */
165     @Override
166     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
167         groups = {"create"})
168     public void create(String testName) throws Exception {
169
170         if (logger.isDebugEnabled()) {
171             logger.debug(testBanner(testName, CLASS_NAME));
172         }
173         // Perform setup, such as initializing the type of service request
174         // (e.g. CREATE, DELETE), its valid and expected status codes, and
175         // its associated HTTP method name (e.g. POST, DELETE).
176         setupCreate();
177
178         // Submit the request to the service and store the response.
179         LocationAuthorityClient client = new LocationAuthorityClient();
180         String shortId = createIdentifier();
181         String displayName = "displayName-" + shortId;
182         String baseRefName = 
183                 LocationAuthorityClientUtils.createLocationAuthRefName(shortId, null);
184         
185         PoxPayloadOut multipart = 
186             LocationAuthorityClientUtils.createLocationAuthorityInstance(
187             displayName, shortId, client.getCommonPartName());
188         String newID = null;
189         ClientResponse<Response> res = client.create(multipart);
190         try {
191                 int statusCode = res.getStatus();
192         
193                 // Check the status code of the response: does it match
194                 // the expected response(s)?
195                 //
196                 // Specifically:
197                 // Does it fall within the set of valid status codes?
198                 // Does it exactly match the expected status code?
199                 if(logger.isDebugEnabled()){
200                     logger.debug(testName + ": status = " + statusCode);
201                 }
202                 Assert.assertTrue(this.REQUEST_TYPE.isValidStatusCode(statusCode),
203                         invalidStatusCodeMessage(this.REQUEST_TYPE, statusCode));
204                 Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
205         
206                 newID = LocationAuthorityClientUtils.extractId(res);
207         } finally {
208                 res.releaseConnection();
209         }
210         // Store the ID returned from the first resource created
211         // for additional tests below.
212         if (knownResourceId == null){
213                 setKnownResource( newID, shortId, baseRefName );
214             if (logger.isDebugEnabled()) {
215                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
216             }
217         }
218         // Store the IDs from every resource created by tests,
219         // so they can be deleted after tests have been run.
220         allResourceIdsCreated.add(newID);
221     }
222
223     /**
224      * Creates the item.
225      *
226      * @param testName the test name
227      */
228     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
229         groups = {"create"}, dependsOnMethods = {"create"})
230     public void createItem(String testName) {
231         if(logger.isDebugEnabled()){
232             logger.debug(testBanner(testName, CLASS_NAME));
233         }
234         setupCreate();
235         createItemInAuthority(knownResourceId, knownResourceRefName);
236     }
237
238     /**
239      * Creates the item in authority.
240      *
241      * @param vcsid the vcsid
242      * @param authRefName the auth ref name
243      * @return the string
244      */
245     private String createItemInAuthority(String vcsid, String authRefName) {
246
247         final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
248         if(logger.isDebugEnabled()){
249             logger.debug(testBanner(testName, CLASS_NAME));
250         }
251
252         // Submit the request to the service and store the response.
253         LocationAuthorityClient client = new LocationAuthorityClient();
254         Map<String, String> shelf1Map = new HashMap<String,String>();
255         // TODO Make loc type and status be controlled vocabs.
256         shelf1Map.put(LocationJAXBSchema.NAME, TEST_NAME);
257         shelf1Map.put(LocationJAXBSchema.SHORT_IDENTIFIER, TEST_SHORTID);
258         shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE, TEST_CONDITION_NOTE);
259         shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE_DATE, TEST_CONDITION_NOTE_DATE);
260         shelf1Map.put(LocationJAXBSchema.SECURITY_NOTE, TEST_SECURITY_NOTE);
261         shelf1Map.put(LocationJAXBSchema.ACCESS_NOTE, TEST_ACCESS_NOTE);
262         shelf1Map.put(LocationJAXBSchema.ADDRESS, TEST_ADDRESS);
263         shelf1Map.put(LocationJAXBSchema.LOCATION_TYPE, TEST_LOCATION_TYPE);
264         shelf1Map.put(LocationJAXBSchema.TERM_STATUS, TEST_STATUS);
265         
266         String newID = LocationAuthorityClientUtils.createItemInAuthority(vcsid,
267                         authRefName, shelf1Map, client );
268
269         // Store the ID returned from the first item resource created
270         // for additional tests below.
271         if (knownItemResourceId == null){
272                 setKnownItemResource(newID, TEST_SHORTID);
273             if (logger.isDebugEnabled()) {
274                 logger.debug(testName + ": knownItemResourceId=" + newID);
275             }
276         }
277
278         // Store the IDs from any item resources created
279         // by tests, along with the IDs of their parents, so these items
280         // can be deleted after all tests have been run.
281         allItemResourceIdsCreated.put(newID, vcsid);
282
283         return newID;
284     }
285
286
287
288     // Failure outcomes
289
290     // Placeholders until the three tests below can be uncommented.
291     // See Issue CSPACE-401.
292     /* (non-Javadoc)
293      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
294      */
295     @Override
296     public void createWithEmptyEntityBody(String testName) throws Exception {
297     }
298
299     /* (non-Javadoc)
300      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
301      */
302     @Override
303     public void createWithMalformedXml(String testName) throws Exception {
304     }
305
306     /* (non-Javadoc)
307      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
308      */
309     @Override
310     public void createWithWrongXmlSchema(String testName) throws Exception {
311     }
312
313
314     // ---------------------------------------------------------------
315     // CRUD tests : CREATE LIST tests
316     // ---------------------------------------------------------------
317     // Success outcomes
318     /* (non-Javadoc)
319          * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
320          */
321     @Override
322     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
323         groups = {"createList"}, dependsOnGroups = {"create"})
324     public void createList(String testName) throws Exception {
325         for (int i = 0; i < nItemsToCreateInList; i++) {
326             create(testName);
327         }
328     }
329
330     /**
331      * Creates the item list.
332      *
333      * @param testName the test name
334      * @throws Exception the exception
335      */
336     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
337         groups = {"createList"}, dependsOnMethods = {"createList"})
338     public void createItemList(String testName) throws Exception {
339         // Add items to the initially-created, known parent record.
340         for (int j = 0; j < nItemsToCreateInList; j++) {
341             createItem(testName);
342         }
343     }
344
345     // ---------------------------------------------------------------
346     // CRUD tests : READ tests
347     // ---------------------------------------------------------------
348     // Success outcomes
349     /* (non-Javadoc)
350      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
351      */
352     @Override
353     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
354         groups = {"read"}, dependsOnGroups = {"create"})
355     public void read(String testName) throws Exception {
356
357         if (logger.isDebugEnabled()) {
358             logger.debug(testBanner(testName, CLASS_NAME));
359         }
360         // Perform setup.
361         setupRead();
362         
363         // Submit the request to the service and store the response.
364         LocationAuthorityClient client = new LocationAuthorityClient();
365         String newID = null;
366         ClientResponse<String> res = client.read(knownResourceId);
367         try {
368                 int statusCode = res.getStatus();
369         
370                 // Check the status code of the response: does it match
371                 // the expected response(s)?
372                 if(logger.isDebugEnabled()){
373                     logger.debug(testName + ": status = " + statusCode);
374                 }
375                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
376                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
377                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
378                 //FIXME: remove the following try catch once Aron fixes signatures
379                 try {
380                     PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
381                     LocationauthoritiesCommon locationAuthority = 
382                         (LocationauthoritiesCommon) extractPart(input,
383                             client.getCommonPartName(), LocationauthoritiesCommon.class);
384                     Assert.assertNotNull(locationAuthority);
385                     Assert.assertNotNull(locationAuthority.getDisplayName());
386                     Assert.assertNotNull(locationAuthority.getShortIdentifier());
387                     Assert.assertNotNull(locationAuthority.getRefName());
388                 } catch (Exception e) {
389                     throw new RuntimeException(e);
390                 }
391         } finally {
392                 res.releaseConnection();
393         }
394     }
395
396     /**
397      * Read by name.
398      *
399      * @param testName the test name
400      * @throws Exception the exception
401      */
402     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
403             groups = {"read"}, dependsOnGroups = {"create"})
404         public void readByName(String testName) throws Exception {
405
406         if (logger.isDebugEnabled()) {
407             logger.debug(testBanner(testName+"("+knownResourceShortIdentifer+")", CLASS_NAME));
408         }
409         // Perform setup.
410         setupRead();
411
412         // Submit the request to the service and store the response.
413         LocationAuthorityClient client = new LocationAuthorityClient();
414         ClientResponse<String> res = client.readByName(knownResourceShortIdentifer);
415         try {
416                 int statusCode = res.getStatus();
417         
418                 // Check the status code of the response: does it match
419                 // the expected response(s)?
420                 if(logger.isDebugEnabled()){
421                     logger.debug(testName + ": status = " + statusCode);
422                 }
423                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
426                 //FIXME: remove the following try catch once Aron fixes signatures
427                 try {
428                     PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
429                     LocationauthoritiesCommon locationAuthority = (LocationauthoritiesCommon) extractPart(input,
430                             client.getCommonPartName(), LocationauthoritiesCommon.class);
431                     Assert.assertNotNull(locationAuthority);
432                 } catch (Exception e) {
433                     throw new RuntimeException(e);
434                 }
435         } finally {
436                 res.releaseConnection();
437         }
438     }
439
440
441     /**
442          * Read item.
443          *
444          * @param testName the test name
445          * @throws Exception the exception
446          */
447     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
448         groups = {"read"}, dependsOnMethods = {"read"})
449     public void readItem(String testName) throws Exception {
450
451         if (logger.isDebugEnabled()) {
452             logger.debug(testBanner(testName, CLASS_NAME));
453         }
454         // Perform setup.
455         setupRead();
456
457         // Submit the request to the service and store the response.
458         LocationAuthorityClient client = new LocationAuthorityClient();
459         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
460         try {
461                 int statusCode = res.getStatus();
462         
463                 // Check the status code of the response: does it match
464                 // the expected response(s)?
465                 if(logger.isDebugEnabled()){
466                     logger.debug(testName + ": status = " + statusCode);
467                 }
468                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
469                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
470                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
471         
472                 // Check whether we've received a location.
473                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
474                 LocationsCommon location = (LocationsCommon) extractPart(input,
475                         client.getItemCommonPartName(), LocationsCommon.class);
476                 Assert.assertNotNull(location);
477                 boolean showFull = true;
478                 if(showFull && logger.isDebugEnabled()){
479                     logger.debug(testName + ": returned payload:");
480                     logger.debug(objectAsXmlString(location, LocationsCommon.class));
481                 }
482                 Assert.assertEquals(location.getInAuthority(), knownResourceId);
483             } finally {
484                 res.releaseConnection();
485             }
486
487     }
488
489     /**
490      * Verify item display name.
491      *
492      * @param testName the test name
493      * @throws Exception the exception
494      */
495     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
496         dependsOnMethods = {"readItem", "updateItem"})
497     public void verifyItemDisplayName(String testName) throws Exception {
498
499         if (logger.isDebugEnabled()) {
500             logger.debug(testBanner(testName, CLASS_NAME));
501         }
502         // Perform setup.
503         setupUpdate();
504
505         // Submit the request to the service and store the response.
506         LocationAuthorityClient client = new LocationAuthorityClient();
507         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
508         try {
509                 int statusCode = res.getStatus();
510         
511                 // Check the status code of the response: does it match
512                 // the expected response(s)?
513                 if(logger.isDebugEnabled()){
514                     logger.debug(testName + ": status = " + statusCode);
515                 }
516                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
517                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
518                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
519         
520                 // Check whether location has expected displayName.
521                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
522                 LocationsCommon location = (LocationsCommon) extractPart(input,
523                         client.getItemCommonPartName(), LocationsCommon.class);
524                 Assert.assertNotNull(location);
525                 String displayName = location.getDisplayName();
526                 // Make sure displayName matches computed form
527                 String expectedDisplayName = 
528                     LocationAuthorityClientUtils.prepareDefaultDisplayName(TEST_NAME);
529                 Assert.assertNotNull(displayName, expectedDisplayName);
530                 
531                 // Update the shortName and verify the computed name is updated.
532                 location.setCsid(null);
533                 location.setDisplayNameComputed(true);
534                 location.setName("updated-" + TEST_NAME);
535                 expectedDisplayName = 
536                     LocationAuthorityClientUtils.prepareDefaultDisplayName("updated-" + TEST_NAME);
537         
538                 // Submit the updated resource to the service and store the response.
539                 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
540                 PayloadOutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
541                 commonPart.setLabel(client.getItemCommonPartName());
542                 res.releaseConnection();
543                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
544                 statusCode = res.getStatus();
545         
546                 // Check the status code of the response: does it match the expected response(s)?
547                 if(logger.isDebugEnabled()){
548                     logger.debug("updateItem: status = " + statusCode);
549                 }
550                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
551                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
552                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
553         
554                 // Retrieve the updated resource and verify that its contents exist.
555                 input = new PoxPayloadIn(res.getEntity());
556                 LocationsCommon updatedLocation =
557                         (LocationsCommon) extractPart(input,
558                                 client.getItemCommonPartName(), LocationsCommon.class);
559                 Assert.assertNotNull(updatedLocation);
560         
561                 // Verify that the updated resource received the correct data.
562                 Assert.assertEquals(updatedLocation.getName(), location.getName(),
563                     "Updated ForeName in Location did not match submitted data.");
564                 // Verify that the updated resource computes the right displayName.
565                 Assert.assertEquals(updatedLocation.getDisplayName(), expectedDisplayName,
566                     "Updated ForeName in Location not reflected in computed DisplayName.");
567         
568                 // Now Update the displayName, not computed and verify the computed name is overriden.
569                 location.setDisplayNameComputed(false);
570                 expectedDisplayName = "TestName";
571                 location.setDisplayName(expectedDisplayName);
572         
573                 // Submit the updated resource to the service and store the response.
574                 output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
575                 commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
576                 commonPart.setLabel(client.getItemCommonPartName());
577                 res.releaseConnection();
578                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
579                 statusCode = res.getStatus();
580         
581                 // Check the status code of the response: does it match the expected response(s)?
582                 if(logger.isDebugEnabled()){
583                     logger.debug("updateItem: status = " + statusCode);
584                 }
585                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
586                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
587                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
588         
589                 // Retrieve the updated resource and verify that its contents exist.
590                 input = new PoxPayloadIn(res.getEntity());
591                 updatedLocation =
592                         (LocationsCommon) extractPart(input,
593                                 client.getItemCommonPartName(), LocationsCommon.class);
594                 Assert.assertNotNull(updatedLocation);
595         
596                 // Verify that the updated resource received the correct data.
597                 Assert.assertEquals(updatedLocation.isDisplayNameComputed(), false,
598                         "Updated displayNameComputed in Location did not match submitted data.");
599                 // Verify that the updated resource computes the right displayName.
600                 Assert.assertEquals(updatedLocation.getDisplayName(),
601                                 expectedDisplayName,
602                         "Updated DisplayName (not computed) in Location not stored.");
603             } finally {
604                 res.releaseConnection();
605             }
606     }
607
608     /**
609      * Verify illegal item display name.
610      *
611      * @param testName the test name
612      * @throws Exception the exception
613      */
614     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
615             dependsOnMethods = {"verifyItemDisplayName"})
616     public void verifyIllegalItemDisplayName(String testName) throws Exception {
617
618         if (logger.isDebugEnabled()) {
619             logger.debug(testBanner(testName, CLASS_NAME));
620         }
621         // Perform setup.
622         // FIXME: create a setup configuration for this operation.
623         setupUpdateWithWrongXmlSchema();
624
625         // Submit the request to the service and store the response.
626         LocationAuthorityClient client = new LocationAuthorityClient();
627         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
628         try {
629                 int statusCode = res.getStatus();
630         
631                 // Check the status code of the response: does it match
632                 // the expected response(s)?
633                 if(logger.isDebugEnabled()){
634                     logger.debug(testName + ": status = " + statusCode);
635                 }
636                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
637                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
638                 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
639         
640                 // Check whether Location has expected displayName.
641                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
642                 LocationsCommon location = (LocationsCommon) extractPart(input,
643                         client.getItemCommonPartName(), LocationsCommon.class);
644                 Assert.assertNotNull(location);
645                 // Try to Update with computed false and no displayName
646                 location.setDisplayNameComputed(false);
647                 location.setDisplayName(null);
648         
649                 // Submit the updated resource to the service and store the response.
650                 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
651                 PayloadOutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
652                 commonPart.setLabel(client.getItemCommonPartName());
653                 res.releaseConnection();
654                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
655                 statusCode = res.getStatus();
656         
657                 // Check the status code of the response: does it match the expected response(s)?
658                 if(logger.isDebugEnabled()){
659                     logger.debug("updateItem: status = " + statusCode);
660                 }
661                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
662                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
663                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
664             } finally {
665                 res.releaseConnection();
666             }
667     }
668    
669
670     // Failure outcomes
671     /* (non-Javadoc)
672      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
673      */
674     @Override
675     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
676         groups = {"read"}, dependsOnMethods = {"read"})
677     public void readNonExistent(String testName) {
678
679         if (logger.isDebugEnabled()) {
680             logger.debug(testBanner(testName, CLASS_NAME));
681         }
682         // Perform setup.
683         setupReadNonExistent();
684
685         // Submit the request to the service and store the response.
686         LocationAuthorityClient client = new LocationAuthorityClient();
687         ClientResponse<String> res = client.read(NON_EXISTENT_ID);
688         try {
689                 int statusCode = res.getStatus();
690         
691                 // Check the status code of the response: does it match
692                 // the expected response(s)?
693                 if(logger.isDebugEnabled()){
694                     logger.debug(testName + ": status = " + statusCode);
695                 }
696                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
697                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
698                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
699             } finally {
700                 res.releaseConnection();
701             }
702     }
703
704     /**
705      * Read item non existent.
706      *
707      * @param testName the test name
708      */
709     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
710         groups = {"read"}, dependsOnMethods = {"readItem"})
711     public void readItemNonExistent(String testName) {
712
713         if (logger.isDebugEnabled()) {
714             logger.debug(testBanner(testName, CLASS_NAME));
715         }
716         // Perform setup.
717         setupReadNonExistent();
718
719         // Submit the request to the service and store the response.
720         LocationAuthorityClient client = new LocationAuthorityClient();
721         ClientResponse<String> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
722         try {
723                 int statusCode = res.getStatus();
724         
725                 // Check the status code of the response: does it match
726                 // the expected response(s)?
727                 if(logger.isDebugEnabled()){
728                     logger.debug(testName + ": status = " + statusCode);
729                 }
730                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
731                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
732                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
733             } finally {
734                 res.releaseConnection();
735             }
736     }
737
738
739     // ---------------------------------------------------------------
740     // CRUD tests : READ_LIST tests
741     // ---------------------------------------------------------------
742     // Success outcomes
743
744     /* (non-Javadoc)
745      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
746      */
747     @Override
748     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
749         groups = {"readList"}, dependsOnGroups = {"createList", "read"})
750     public void readList(String testName) throws Exception {
751
752         if (logger.isDebugEnabled()) {
753             logger.debug(testBanner(testName, CLASS_NAME));
754         }
755         // Perform setup.
756         setupReadList();
757
758         // Submit the request to the service and store the response.
759         LocationAuthorityClient client = new LocationAuthorityClient();
760         ClientResponse<AbstractCommonList> res = client.readList();
761         try {
762                 AbstractCommonList list = res.getEntity();
763                 int statusCode = res.getStatus();
764         
765                 // Check the status code of the response: does it match
766                 // the expected response(s)?
767                 if(logger.isDebugEnabled()){
768                     logger.debug(testName + ": status = " + statusCode);
769                 }
770                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
771                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
772                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
773         
774                 // Optionally output additional data about list members for debugging.
775                 if(logger.isTraceEnabled()){
776                         AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
777                 }
778             } finally {
779                 res.releaseConnection();
780             }
781     }
782
783     /**
784      * Read item list.
785      */
786     @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
787     public void readItemList() {
788         String testName = "readItemList";
789         if (logger.isDebugEnabled()) {
790             logger.debug(testBanner(testName, CLASS_NAME));
791         }
792         readItemList(knownResourceId, null);
793     }
794
795     /**
796      * Read item list by authority name.
797      */
798     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
799     public void readItemListByAuthorityName() {
800         String testName = "readItemListByAuthorityName";
801         if (logger.isDebugEnabled()) {
802             logger.debug(testBanner(testName, CLASS_NAME));
803         }
804         readItemList(null, knownResourceShortIdentifer);
805     }
806     
807     /**
808      * Read item list.
809      *
810      * @param vcsid the vcsid
811      * @param name the name
812      */
813     private void readItemList(String vcsid, String shortId) {
814
815         String testName = "readItemList";
816
817         // Perform setup.
818         setupReadList();
819         
820         // Submit the request to the service and store the response.
821         LocationAuthorityClient client = new LocationAuthorityClient();
822         ClientResponse<AbstractCommonList> res = null;
823         if(vcsid!= null) {
824                 res = client.readItemList(vcsid, null, null);
825         } else if(shortId!= null) {
826                 res = client.readItemListForNamedAuthority(shortId, null, null);
827         } else {
828                 Assert.fail("readItemList passed null csid and name!");
829         }
830         try {
831                 AbstractCommonList list = res.getEntity();
832                 int statusCode = res.getStatus();
833         
834                 // Check the status code of the response: does it match
835                 // the expected response(s)?
836                 if(logger.isDebugEnabled()){
837                     logger.debug(testName + ": status = " + statusCode);
838                 }
839                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
840                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
841                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
842         
843                 List<AbstractCommonList.ListItem> items =
844                     list.getListItem();
845                 int nItemsReturned = items.size();
846                 // There will be one item created, associated with a
847                 // known parent resource, by the createItem test.
848                 //
849                 // In addition, there will be 'nItemsToCreateInList'
850                 // additional items created by the createItemList test,
851                 // all associated with the same parent resource.
852                 int nExpectedItems = nItemsToCreateInList + 1;
853                 if(logger.isDebugEnabled()){
854                     logger.debug(testName + ": Expected "
855                                 + nExpectedItems +" items; got: "+nItemsReturned);
856                 }
857                 Assert.assertEquals(nItemsReturned, nExpectedItems);
858         
859             for (AbstractCommonList.ListItem item : items) {
860                 String value = 
861                         AbstractCommonListUtils.ListItemGetElementValue(item, REFNAME);
862                 Assert.assertTrue((null != value), "Item refName is null!");
863                 value = 
864                         AbstractCommonListUtils.ListItemGetElementValue(item, DISPLAYNAME);
865                 Assert.assertTrue((null != value), "Item displayName is null!");
866             }
867             if(logger.isTraceEnabled()){
868                 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
869             }
870             } finally {
871                 res.releaseConnection();
872             }
873     }
874
875
876     // Failure outcomes
877     // None at present.
878
879     // ---------------------------------------------------------------
880     // CRUD tests : UPDATE tests
881     // ---------------------------------------------------------------
882     // Success outcomes
883     /* (non-Javadoc)
884      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
885      */
886     @Override
887     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
888         groups = {"update"}, dependsOnGroups = {"read", "readList"})
889     public void update(String testName) throws Exception {
890
891         if (logger.isDebugEnabled()) {
892             logger.debug(testBanner(testName, CLASS_NAME));
893         }
894         // Perform setup.
895         setupUpdate();
896
897         // Retrieve the contents of a resource to update.
898         LocationAuthorityClient client = new LocationAuthorityClient();
899         ClientResponse<String> res = client.read(knownResourceId);
900         try {
901                 if(logger.isDebugEnabled()){
902                     logger.debug(testName + ": read status = " + res.getStatus());
903                 }
904                 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
905         
906                 if(logger.isDebugEnabled()){
907                     logger.debug("got LocationAuthority to update with ID: " + knownResourceId);
908                 }
909                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
910                 LocationauthoritiesCommon locationAuthority = (LocationauthoritiesCommon) extractPart(input,
911                         client.getCommonPartName(), LocationauthoritiesCommon.class);
912                 Assert.assertNotNull(locationAuthority);
913         
914                 // Update the contents of this resource.
915                 locationAuthority.setDisplayName("updated-" + locationAuthority.getDisplayName());
916                 locationAuthority.setVocabType("updated-" + locationAuthority.getVocabType());
917                 if(logger.isDebugEnabled()){
918                     logger.debug("to be updated LocationAuthority");
919                     logger.debug(objectAsXmlString(locationAuthority, LocationauthoritiesCommon.class));
920                 }
921         
922                 // Submit the updated resource to the service and store the response.
923                 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_PAYLOAD_NAME);
924                 PayloadOutputPart commonPart = output.addPart(locationAuthority, MediaType.APPLICATION_XML_TYPE);
925                 commonPart.setLabel(client.getCommonPartName());
926                 res.releaseConnection();
927                 res = client.update(knownResourceId, output);
928                 int statusCode = res.getStatus();
929         
930                 // Check the status code of the response: does it match the expected response(s)?
931                 if(logger.isDebugEnabled()){
932                     logger.debug(testName + ": status = " + statusCode);
933                 }
934                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
935                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
936                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
937         
938                 // Retrieve the updated resource and verify that its contents exist.
939                 input = new PoxPayloadIn(res.getEntity());
940                 LocationauthoritiesCommon updatedLocationAuthority =
941                         (LocationauthoritiesCommon) extractPart(input,
942                                 client.getCommonPartName(), LocationauthoritiesCommon.class);
943                 Assert.assertNotNull(updatedLocationAuthority);
944         
945                 // Verify that the updated resource received the correct data.
946                 Assert.assertEquals(updatedLocationAuthority.getDisplayName(),
947                         locationAuthority.getDisplayName(),
948                         "Data in updated object did not match submitted data.");
949             } finally {
950                 res.releaseConnection();
951             }
952     }
953
954     /**
955      * Update item.
956      *
957      * @param testName the test name
958      * @throws Exception the exception
959      */
960     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
961         groups = {"update"}, dependsOnMethods = {"update"})
962     public void updateItem(String testName) throws Exception {
963
964         if (logger.isDebugEnabled()) {
965             logger.debug(testBanner(testName, CLASS_NAME));
966         }
967         // Perform setup.
968         setupUpdate();
969
970         // Retrieve the contents of a resource to update.
971         LocationAuthorityClient client = new LocationAuthorityClient();
972         ClientResponse<String> res =
973                 client.readItem(knownResourceId, knownItemResourceId);
974         try {
975                 if(logger.isDebugEnabled()){
976                     logger.debug(testName + ": read status = " + res.getStatus());
977                 }
978                 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
979         
980                 if(logger.isDebugEnabled()){
981                     logger.debug("got Location to update with ID: " +
982                         knownItemResourceId +
983                         " in LocationAuthority: " + knownResourceId );
984                 }
985                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
986                 LocationsCommon location = (LocationsCommon) extractPart(input,
987                         client.getItemCommonPartName(), LocationsCommon.class);
988                 Assert.assertNotNull(location);
989         
990                 // Update the contents of this resource.
991                 location.setCsid(null);
992                 location.setName("updated-" + location.getName());
993                 if(logger.isDebugEnabled()){
994                     logger.debug("to be updated Location");
995                     logger.debug(objectAsXmlString(location,
996                         LocationsCommon.class));
997                 }        
998         
999                 // Submit the updated resource to the service and store the response.
1000                 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
1001                 PayloadOutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
1002                 commonPart.setLabel(client.getItemCommonPartName());
1003                 res.releaseConnection();
1004                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
1005                 int statusCode = res.getStatus();
1006         
1007                 // Check the status code of the response: does it match the expected response(s)?
1008                 if(logger.isDebugEnabled()){
1009                     logger.debug(testName + ": status = " + statusCode);
1010                 }
1011                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1012                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1013                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1014         
1015                 // Retrieve the updated resource and verify that its contents exist.
1016                 input = new PoxPayloadIn(res.getEntity());
1017                 LocationsCommon updatedLocation =
1018                         (LocationsCommon) extractPart(input,
1019                                 client.getItemCommonPartName(), LocationsCommon.class);
1020                 Assert.assertNotNull(updatedLocation);
1021         
1022                 // Verify that the updated resource received the correct data.
1023                 Assert.assertEquals(updatedLocation.getName(), location.getName(),
1024                         "Data in updated Location did not match submitted data.");
1025             } finally {
1026                 res.releaseConnection();
1027             }
1028     }
1029
1030     // Failure outcomes
1031     // Placeholders until the three tests below can be uncommented.
1032     // See Issue CSPACE-401.
1033     /* (non-Javadoc)
1034      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
1035      */
1036     @Override
1037     public void updateWithEmptyEntityBody(String testName) throws Exception {
1038     }
1039
1040     /* (non-Javadoc)
1041      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
1042      */
1043     @Override
1044     public void updateWithMalformedXml(String testName) throws Exception {
1045     }
1046
1047     /* (non-Javadoc)
1048      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
1049      */
1050     @Override
1051     public void updateWithWrongXmlSchema(String testName) throws Exception {
1052     }
1053
1054
1055     /* (non-Javadoc)
1056          * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
1057          */
1058 @Override
1059     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1060         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1061     public void updateNonExistent(String testName) throws Exception {
1062
1063         if (logger.isDebugEnabled()) {
1064             logger.debug(testBanner(testName, CLASS_NAME));
1065         }
1066         // Perform setup.
1067         setupUpdateNonExistent();
1068
1069         // Submit the request to the service and store the response.
1070         // Note: The ID(s) used when creating the request payload may be arbitrary.
1071         // The only relevant ID may be the one used in update(), below.
1072         LocationAuthorityClient client = new LocationAuthorityClient();
1073         String displayName = "displayName-NON_EXISTENT_ID";
1074         PoxPayloadOut multipart = LocationAuthorityClientUtils.createLocationAuthorityInstance(
1075                                 displayName, "nonEx", client.getCommonPartName());
1076         ClientResponse<String> res =
1077                 client.update(NON_EXISTENT_ID, multipart);
1078         try {
1079                 int statusCode = res.getStatus();
1080         
1081                 // Check the status code of the response: does it match
1082                 // the expected response(s)?
1083                 if(logger.isDebugEnabled()){
1084                     logger.debug(testName + ": status = " + statusCode);
1085                 }
1086                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1087                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1088                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1089             } finally {
1090                 res.releaseConnection();
1091             }
1092     }
1093
1094     /**
1095      * Update non existent item.
1096      *
1097      * @param testName the test name
1098      * @throws Exception the exception
1099      */
1100     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1101         groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1102     public void updateNonExistentItem(String testName) throws Exception {
1103
1104         if (logger.isDebugEnabled()) {
1105             logger.debug(testBanner(testName, CLASS_NAME));
1106         }
1107         // Perform setup.
1108         setupUpdateNonExistent();
1109
1110         // Submit the request to the service and store the response.
1111         // Note: The ID used in this 'create' call may be arbitrary.
1112         // The only relevant ID may be the one used in update(), below.
1113         LocationAuthorityClient client = new LocationAuthorityClient();
1114         Map<String, String> nonexMap = new HashMap<String,String>();
1115         nonexMap.put(LocationJAXBSchema.NAME, TEST_NAME);
1116         nonexMap.put(LocationJAXBSchema.SHORT_IDENTIFIER, "nonEx");
1117         nonexMap.put(LocationJAXBSchema.LOCATION_TYPE, TEST_LOCATION_TYPE);
1118         nonexMap.put(LocationJAXBSchema.TERM_STATUS, TEST_STATUS);
1119         PoxPayloadOut multipart = 
1120         LocationAuthorityClientUtils.createLocationInstance(
1121                         LocationAuthorityClientUtils.createLocationRefName(knownResourceRefName, "nonEx", "Non Existent"), 
1122                         nonexMap, client.getItemCommonPartName() );
1123         ClientResponse<String> res =
1124                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1125         try {
1126                 int statusCode = res.getStatus();
1127         
1128                 // Check the status code of the response: does it match
1129                 // the expected response(s)?
1130                 if(logger.isDebugEnabled()){
1131                     logger.debug(testName + ": status = " + statusCode);
1132                 }
1133                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1134                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1135                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1136             } finally {
1137                 res.releaseConnection();
1138             }
1139     }
1140
1141     // ---------------------------------------------------------------
1142     // CRUD tests : DELETE tests
1143     // ---------------------------------------------------------------
1144     // Success outcomes
1145
1146     // Note: delete sub-resources in ascending hierarchical order,
1147     // before deleting their parents.
1148
1149    /**
1150     * Delete item.
1151     *
1152     * @param testName the test name
1153     * @throws Exception the exception
1154     */
1155    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1156         //groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "readListByPartialTerm", "update"})
1157         groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "update"})
1158     public void deleteItem(String testName) throws Exception {
1159
1160         if (logger.isDebugEnabled()) {
1161             logger.debug(testBanner(testName, CLASS_NAME));
1162         }
1163         // Perform setup.
1164         setupDelete();
1165
1166         if(logger.isDebugEnabled()){
1167             logger.debug("parentcsid =" + knownResourceId +
1168                 " itemcsid = " + knownItemResourceId);
1169         }
1170
1171         // Submit the request to the service and store the response.
1172         LocationAuthorityClient client = new LocationAuthorityClient();
1173         ClientResponse<Response> res = 
1174                 client.deleteItem(knownResourceId, knownItemResourceId);
1175         try {
1176                 int statusCode = res.getStatus();
1177         
1178                 // Check the status code of the response: does it match
1179                 // the expected response(s)?
1180                 if(logger.isDebugEnabled()){
1181                     logger.debug(testName + ": status = " + statusCode);
1182                 }
1183                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1184                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1185                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1186             } finally {
1187                 res.releaseConnection();
1188             }
1189     }
1190
1191     /* (non-Javadoc)
1192      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
1193      */
1194     @Override
1195     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1196         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1197     public void delete(String testName) throws Exception {
1198
1199         if (logger.isDebugEnabled()) {
1200             logger.debug(testBanner(testName, CLASS_NAME));
1201         }
1202         // Perform setup.
1203         setupDelete();
1204
1205         if(logger.isDebugEnabled()){
1206             logger.debug("parentcsid =" + knownResourceId);
1207         }
1208
1209         // Submit the request to the service and store the response.
1210         LocationAuthorityClient client = new LocationAuthorityClient();
1211         ClientResponse<Response> res = client.delete(knownResourceId);
1212         try {
1213                 int statusCode = res.getStatus();
1214         
1215                 // Check the status code of the response: does it match
1216                 // the expected response(s)?
1217                 if(logger.isDebugEnabled()){
1218                     logger.debug(testName + ": status = " + statusCode);
1219                 }
1220                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1221                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1222                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1223             } finally {
1224                 res.releaseConnection();
1225             }
1226     }
1227
1228     // Failure outcomes
1229     /* (non-Javadoc)
1230      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
1231      */
1232     @Override
1233     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1234         groups = {"delete"}, dependsOnMethods = {"delete"})
1235     public void deleteNonExistent(String testName) throws Exception {
1236
1237         if (logger.isDebugEnabled()) {
1238             logger.debug(testBanner(testName, CLASS_NAME));
1239         }
1240         // Perform setup.
1241         setupDeleteNonExistent();
1242
1243         // Submit the request to the service and store the response.
1244         LocationAuthorityClient client = new LocationAuthorityClient();
1245         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1246         try {
1247                 int statusCode = res.getStatus();
1248         
1249                 // Check the status code of the response: does it match
1250                 // the expected response(s)?
1251                 if(logger.isDebugEnabled()){
1252                     logger.debug(testName + ": status = " + statusCode);
1253                 }
1254                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1255                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1256                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1257             } finally {
1258                 res.releaseConnection();
1259             }
1260     }
1261
1262     /**
1263      * Delete non existent item.
1264      *
1265      * @param testName the test name
1266      */
1267     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1268         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1269     public void deleteNonExistentItem(String testName) {
1270
1271         if (logger.isDebugEnabled()) {
1272             logger.debug(testBanner(testName, CLASS_NAME));
1273         }
1274         // Perform setup.
1275         setupDeleteNonExistent();
1276
1277         // Submit the request to the service and store the response.
1278         LocationAuthorityClient client = new LocationAuthorityClient();
1279         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1280         try {
1281                 int statusCode = res.getStatus();
1282         
1283                 // Check the status code of the response: does it match
1284                 // the expected response(s)?
1285                 if(logger.isDebugEnabled()){
1286                     logger.debug(testName + ": status = " + statusCode);
1287                 }
1288                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1289                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1290                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1291             } finally {
1292                 res.releaseConnection();
1293             }
1294     }
1295
1296     // ---------------------------------------------------------------
1297     // Utility tests : tests of code used in tests above
1298     // ---------------------------------------------------------------
1299     /**
1300      * Tests the code for manually submitting data that is used by several
1301      * of the methods above.
1302      */
1303     @Test(dependsOnMethods = {"create", "read"})
1304     public void testSubmitRequest() {
1305
1306         // Expected status code: 200 OK
1307         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1308
1309         // Submit the request to the service and store the response.
1310         String method = ServiceRequestType.READ.httpMethodName();
1311         String url = getResourceURL(knownResourceId);
1312         int statusCode = submitRequest(method, url);
1313
1314         // Check the status code of the response: does it match
1315         // the expected response(s)?
1316         if(logger.isDebugEnabled()){
1317             logger.debug("testSubmitRequest: url=" + url +
1318                 " status=" + statusCode);
1319         }
1320         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1321
1322     }
1323
1324     /**
1325      * Test item submit request.
1326      */
1327     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1328     public void testItemSubmitRequest() {
1329
1330         // Expected status code: 200 OK
1331         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1332
1333         // Submit the request to the service and store the response.
1334         String method = ServiceRequestType.READ.httpMethodName();
1335         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1336         int statusCode = submitRequest(method, url);
1337
1338         // Check the status code of the response: does it match
1339         // the expected response(s)?
1340         if(logger.isDebugEnabled()){
1341             logger.debug("testItemSubmitRequest: url=" + url +
1342                 " status=" + statusCode);
1343         }
1344         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1345
1346     }
1347
1348     // ---------------------------------------------------------------
1349     // Cleanup of resources created during testing
1350     // ---------------------------------------------------------------
1351     
1352     /**
1353      * Deletes all resources created by tests, after all tests have been run.
1354      *
1355      * This cleanup method will always be run, even if one or more tests fail.
1356      * For this reason, it attempts to remove all resources created
1357      * at any point during testing, even if some of those resources
1358      * may be expected to be deleted by certain tests.
1359      */
1360
1361     @AfterClass(alwaysRun=true)
1362     public void cleanUp() {
1363         String noTest = System.getProperty("noTestCleanup");
1364         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1365             if (logger.isDebugEnabled()) {
1366                 logger.debug("Skipping Cleanup phase ...");
1367             }
1368             return;
1369         }
1370         if (logger.isDebugEnabled()) {
1371             logger.debug("Cleaning up temporary resources created for testing ...");
1372         }
1373         String parentResourceId;
1374         String itemResourceId;
1375         // Clean up contact resources.
1376         LocationAuthorityClient client = new LocationAuthorityClient();
1377         parentResourceId = knownResourceId;
1378         // Clean up item resources.
1379         for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
1380             itemResourceId = entry.getKey();
1381             parentResourceId = entry.getValue();
1382             // Note: Any non-success responses from the delete operation
1383             // below are ignored and not reported.
1384             ClientResponse<Response> res =
1385                 client.deleteItem(parentResourceId, itemResourceId);
1386                 res.releaseConnection();
1387         }
1388         // Clean up parent resources.
1389         for (String resourceId : allResourceIdsCreated) {
1390             // Note: Any non-success responses from the delete operation
1391             // below are ignored and not reported.
1392             ClientResponse<Response> res = client.delete(resourceId);
1393                 res.releaseConnection();
1394         }
1395     }
1396
1397     // ---------------------------------------------------------------
1398     // Utility methods used by tests above
1399     // ---------------------------------------------------------------
1400     /* (non-Javadoc)
1401      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
1402      */
1403
1404     /**
1405      * Returns the root URL for the item service.
1406      *
1407      * This URL consists of a base URL for all services, followed by
1408      * a path component for the owning parent, followed by the
1409      * path component for the items.
1410      *
1411      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1412      * parent authority resource of the relevant item resource.
1413      *
1414      * @return The root URL for the item service.
1415      */
1416     protected String getItemServiceRootURL(String parentResourceIdentifier) {
1417         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1418     }
1419
1420     /**
1421      * Returns the URL of a specific item resource managed by a service, and
1422      * designated by an identifier (such as a universally unique ID, or UUID).
1423      *
1424      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1425      * parent authority resource of the relevant item resource.
1426      *
1427      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1428      * item resource.
1429      *
1430      * @return The URL of a specific item resource managed by a service.
1431      */
1432     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1433         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1434     }
1435
1436
1437 }