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