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