]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
76d3cf95d9ab5f178ff50c3fc1411e598fac1b36
[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                 } catch (Exception e) {
398                     throw new RuntimeException(e);
399                 }
400         } finally {
401                 res.releaseConnection();
402         }
403     }
404
405     /**
406      * Read by name.
407      *
408      * @param testName the test name
409      * @throws Exception the exception
410      */
411     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
412             groups = {"read"}, dependsOnGroups = {"create"})
413         public void readByName(String testName) throws Exception {
414
415         if (logger.isDebugEnabled()) {
416             logger.debug(testBanner(testName+"("+knownResourceShortIdentifer+")", CLASS_NAME));
417         }
418         // Perform setup.
419         setupRead();
420
421         // Submit the request to the service and store the response.
422         LocationAuthorityClient client = new LocationAuthorityClient();
423         ClientResponse<String> res = client.readByName(knownResourceShortIdentifer);
424         try {
425                 int statusCode = res.getStatus();
426         
427                 // Check the status code of the response: does it match
428                 // the expected response(s)?
429                 if(logger.isDebugEnabled()){
430                     logger.debug(testName + ": status = " + statusCode);
431                 }
432                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
433                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
434                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
435                 //FIXME: remove the following try catch once Aron fixes signatures
436                 try {
437                     PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
438                     LocationauthoritiesCommon locationAuthority = (LocationauthoritiesCommon) extractPart(input,
439                             client.getCommonPartName(), LocationauthoritiesCommon.class);
440                     Assert.assertNotNull(locationAuthority);
441                 } catch (Exception e) {
442                     throw new RuntimeException(e);
443                 }
444         } finally {
445                 res.releaseConnection();
446         }
447     }
448
449
450     /**
451          * Read item.
452          *
453          * @param testName the test name
454          * @throws Exception the exception
455          */
456     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
457         groups = {"read"}, dependsOnMethods = {"read"})
458     public void readItem(String testName) throws Exception {
459
460         if (logger.isDebugEnabled()) {
461             logger.debug(testBanner(testName, CLASS_NAME));
462         }
463         // Perform setup.
464         setupRead();
465
466         // Submit the request to the service and store the response.
467         LocationAuthorityClient client = new LocationAuthorityClient();
468         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
469         try {
470                 int statusCode = res.getStatus();
471         
472                 // Check the status code of the response: does it match
473                 // the expected response(s)?
474                 if(logger.isDebugEnabled()){
475                     logger.debug(testName + ": status = " + statusCode);
476                 }
477                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
478                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
479                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
480         
481                 // Check whether we've received a location.
482                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
483                 LocationsCommon location = (LocationsCommon) extractPart(input,
484                         client.getItemCommonPartName(), LocationsCommon.class);
485                 Assert.assertNotNull(location);
486                 boolean showFull = true;
487                 if(showFull && logger.isDebugEnabled()){
488                     logger.debug(testName + ": returned payload:");
489                     logger.debug(objectAsXmlString(location, LocationsCommon.class));
490                 }
491                 Assert.assertEquals(location.getInAuthority(), knownResourceId);
492             } finally {
493                 res.releaseConnection();
494             }
495
496     }
497
498     /**
499      * Verify item display name.
500      *
501      * @param testName the test name
502      * @throws Exception the exception
503      */
504     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
505         dependsOnMethods = {"readItem", "updateItem"})
506     public void verifyItemDisplayName(String testName) throws Exception {
507
508         if (logger.isDebugEnabled()) {
509             logger.debug(testBanner(testName, CLASS_NAME));
510         }
511         // Perform setup.
512         setupUpdate();
513
514         // Submit the request to the service and store the response.
515         LocationAuthorityClient client = new LocationAuthorityClient();
516         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
517         try {
518                 int statusCode = res.getStatus();
519         
520                 // Check the status code of the response: does it match
521                 // the expected response(s)?
522                 if(logger.isDebugEnabled()){
523                     logger.debug(testName + ": status = " + statusCode);
524                 }
525                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
526                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
527                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
528         
529                 // Check whether location has expected displayName.
530                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
531                 LocationsCommon location = (LocationsCommon) extractPart(input,
532                         client.getItemCommonPartName(), LocationsCommon.class);
533                 Assert.assertNotNull(location);
534                 String displayName = location.getDisplayName();
535                 // Make sure displayName matches computed form
536                 String expectedDisplayName = 
537                     LocationAuthorityClientUtils.prepareDefaultDisplayName(TEST_NAME);
538                 Assert.assertNotNull(displayName, expectedDisplayName);
539                 
540                 // Update the shortName and verify the computed name is updated.
541                 location.setCsid(null);
542                 location.setDisplayNameComputed(true);
543                 location.setName("updated-" + TEST_NAME);
544                 expectedDisplayName = 
545                     LocationAuthorityClientUtils.prepareDefaultDisplayName("updated-" + TEST_NAME);
546         
547                 // Submit the updated resource to the service and store the response.
548                 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
549                 PayloadOutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
550                 commonPart.setLabel(client.getItemCommonPartName());
551                 res.releaseConnection();
552                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
553                 statusCode = res.getStatus();
554         
555                 // Check the status code of the response: does it match the expected response(s)?
556                 if(logger.isDebugEnabled()){
557                     logger.debug("updateItem: status = " + statusCode);
558                 }
559                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
560                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
561                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
562         
563                 // Retrieve the updated resource and verify that its contents exist.
564                 input = new PoxPayloadIn(res.getEntity());
565                 LocationsCommon updatedLocation =
566                         (LocationsCommon) extractPart(input,
567                                 client.getItemCommonPartName(), LocationsCommon.class);
568                 Assert.assertNotNull(updatedLocation);
569         
570                 // Verify that the updated resource received the correct data.
571                 Assert.assertEquals(updatedLocation.getName(), location.getName(),
572                     "Updated ForeName in Location did not match submitted data.");
573                 // Verify that the updated resource computes the right displayName.
574                 Assert.assertEquals(updatedLocation.getDisplayName(), expectedDisplayName,
575                     "Updated ForeName in Location not reflected in computed DisplayName.");
576         
577                 // Now Update the displayName, not computed and verify the computed name is overriden.
578                 location.setDisplayNameComputed(false);
579                 expectedDisplayName = "TestName";
580                 location.setDisplayName(expectedDisplayName);
581         
582                 // Submit the updated resource to the service and store the response.
583                 output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
584                 commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
585                 commonPart.setLabel(client.getItemCommonPartName());
586                 res.releaseConnection();
587                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
588                 statusCode = res.getStatus();
589         
590                 // Check the status code of the response: does it match the expected response(s)?
591                 if(logger.isDebugEnabled()){
592                     logger.debug("updateItem: status = " + statusCode);
593                 }
594                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
595                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
596                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
597         
598                 // Retrieve the updated resource and verify that its contents exist.
599                 input = new PoxPayloadIn(res.getEntity());
600                 updatedLocation =
601                         (LocationsCommon) extractPart(input,
602                                 client.getItemCommonPartName(), LocationsCommon.class);
603                 Assert.assertNotNull(updatedLocation);
604         
605                 // Verify that the updated resource received the correct data.
606                 Assert.assertEquals(updatedLocation.isDisplayNameComputed(), false,
607                         "Updated displayNameComputed in Location did not match submitted data.");
608                 // Verify that the updated resource computes the right displayName.
609                 Assert.assertEquals(updatedLocation.getDisplayName(),
610                                 expectedDisplayName,
611                         "Updated DisplayName (not computed) in Location not stored.");
612             } finally {
613                 res.releaseConnection();
614             }
615     }
616
617     /**
618      * Verify illegal item display name.
619      *
620      * @param testName the test name
621      * @throws Exception the exception
622      */
623     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
624             dependsOnMethods = {"verifyItemDisplayName"})
625     public void verifyIllegalItemDisplayName(String testName) throws Exception {
626
627         if (logger.isDebugEnabled()) {
628             logger.debug(testBanner(testName, CLASS_NAME));
629         }
630         // Perform setup.
631         // FIXME: create a setup configuration for this operation.
632         setupUpdateWithWrongXmlSchema();
633
634         // Submit the request to the service and store the response.
635         LocationAuthorityClient client = new LocationAuthorityClient();
636         ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
637         try {
638                 int statusCode = res.getStatus();
639         
640                 // Check the status code of the response: does it match
641                 // the expected response(s)?
642                 if(logger.isDebugEnabled()){
643                     logger.debug(testName + ": status = " + statusCode);
644                 }
645                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
646                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
647                 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
648         
649                 // Check whether Location has expected displayName.
650                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
651                 LocationsCommon location = (LocationsCommon) extractPart(input,
652                         client.getItemCommonPartName(), LocationsCommon.class);
653                 Assert.assertNotNull(location);
654                 // Try to Update with computed false and no displayName
655                 location.setDisplayNameComputed(false);
656                 location.setDisplayName(null);
657         
658                 // Submit the updated resource to the service and store the response.
659                 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
660                 PayloadOutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
661                 commonPart.setLabel(client.getItemCommonPartName());
662                 res.releaseConnection();
663                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
664                 statusCode = res.getStatus();
665         
666                 // Check the status code of the response: does it match the expected response(s)?
667                 if(logger.isDebugEnabled()){
668                     logger.debug("updateItem: status = " + statusCode);
669                 }
670                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
671                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
672                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
673             } finally {
674                 res.releaseConnection();
675             }
676     }
677    
678
679     // Failure outcomes
680     /* (non-Javadoc)
681      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
682      */
683     @Override
684     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
685         groups = {"read"}, dependsOnMethods = {"read"})
686     public void readNonExistent(String testName) {
687
688         if (logger.isDebugEnabled()) {
689             logger.debug(testBanner(testName, CLASS_NAME));
690         }
691         // Perform setup.
692         setupReadNonExistent();
693
694         // Submit the request to the service and store the response.
695         LocationAuthorityClient client = new LocationAuthorityClient();
696         ClientResponse<String> res = client.read(NON_EXISTENT_ID);
697         try {
698                 int statusCode = res.getStatus();
699         
700                 // Check the status code of the response: does it match
701                 // the expected response(s)?
702                 if(logger.isDebugEnabled()){
703                     logger.debug(testName + ": status = " + statusCode);
704                 }
705                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
706                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
707                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
708             } finally {
709                 res.releaseConnection();
710             }
711     }
712
713     /**
714      * Read item non existent.
715      *
716      * @param testName the test name
717      */
718     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
719         groups = {"read"}, dependsOnMethods = {"readItem"})
720     public void readItemNonExistent(String testName) {
721
722         if (logger.isDebugEnabled()) {
723             logger.debug(testBanner(testName, CLASS_NAME));
724         }
725         // Perform setup.
726         setupReadNonExistent();
727
728         // Submit the request to the service and store the response.
729         LocationAuthorityClient client = new LocationAuthorityClient();
730         ClientResponse<String> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
731         try {
732                 int statusCode = res.getStatus();
733         
734                 // Check the status code of the response: does it match
735                 // the expected response(s)?
736                 if(logger.isDebugEnabled()){
737                     logger.debug(testName + ": status = " + statusCode);
738                 }
739                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
740                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
741                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
742             } finally {
743                 res.releaseConnection();
744             }
745     }
746
747
748     // ---------------------------------------------------------------
749     // CRUD tests : READ_LIST tests
750     // ---------------------------------------------------------------
751     // Success outcomes
752
753     /* (non-Javadoc)
754      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
755      */
756     @Override
757     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
758         groups = {"readList"}, dependsOnGroups = {"createList", "read"})
759     public void readList(String testName) throws Exception {
760
761         if (logger.isDebugEnabled()) {
762             logger.debug(testBanner(testName, CLASS_NAME));
763         }
764         // Perform setup.
765         setupReadList();
766
767         // Submit the request to the service and store the response.
768         LocationAuthorityClient client = new LocationAuthorityClient();
769         ClientResponse<LocationauthoritiesCommonList> res = client.readList();
770         try {
771                 LocationauthoritiesCommonList list = res.getEntity();
772                 int statusCode = res.getStatus();
773         
774                 // Check the status code of the response: does it match
775                 // the expected response(s)?
776                 if(logger.isDebugEnabled()){
777                     logger.debug(testName + ": status = " + statusCode);
778                 }
779                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
780                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
781                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
782         
783                 // Optionally output additional data about list members for debugging.
784                 boolean iterateThroughList = false;
785                 if (iterateThroughList && logger.isDebugEnabled()) {
786                     List<LocationauthoritiesCommonList.LocationauthorityListItem> items =
787                             list.getLocationauthorityListItem();
788                     int i = 0;
789                     for (LocationauthoritiesCommonList.LocationauthorityListItem item : items) {
790                         String csid = item.getCsid();
791                         logger.debug(testName + ": list-item[" + i + "] csid=" +
792                                 csid);
793                         logger.debug(testName + ": list-item[" + i + "] displayName=" +
794                                 item.getDisplayName());
795                         logger.debug(testName + ": list-item[" + i + "] URI=" +
796                                 item.getUri());
797                         readItemList(csid, null);
798                         i++;
799                     }
800                 }
801             } finally {
802                 res.releaseConnection();
803             }
804     }
805
806     /**
807      * Read item list.
808      */
809     @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
810     public void readItemList() {
811         String testName = "readItemList";
812         if (logger.isDebugEnabled()) {
813             logger.debug(testBanner(testName, CLASS_NAME));
814         }
815         readItemList(knownResourceId, null);
816     }
817
818     /**
819      * Read item list by authority name.
820      */
821     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
822     public void readItemListByAuthorityName() {
823         String testName = "readItemListByAuthorityName";
824         if (logger.isDebugEnabled()) {
825             logger.debug(testBanner(testName, CLASS_NAME));
826         }
827         readItemList(null, knownResourceShortIdentifer);
828     }
829     
830     /**
831      * Read item list.
832      *
833      * @param vcsid the vcsid
834      * @param name the name
835      */
836     private void readItemList(String vcsid, String shortId) {
837
838         String testName = "readItemList";
839
840         // Perform setup.
841         setupReadList();
842         
843         // Submit the request to the service and store the response.
844         LocationAuthorityClient client = new LocationAuthorityClient();
845         ClientResponse<LocationsCommonList> res = null;
846         if(vcsid!= null) {
847                 res = client.readItemList(vcsid, null, null);
848         } else if(shortId!= null) {
849                 res = client.readItemListForNamedAuthority(shortId, null, null);
850         } else {
851                 Assert.fail("readItemList passed null csid and name!");
852         }
853         try {
854                 LocationsCommonList list = res.getEntity();
855                 int statusCode = res.getStatus();
856         
857                 // Check the status code of the response: does it match
858                 // the expected response(s)?
859                 if(logger.isDebugEnabled()){
860                     logger.debug(testName + ": status = " + statusCode);
861                 }
862                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
863                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
864                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
865         
866                 List<LocationsCommonList.LocationListItem> items =
867                     list.getLocationListItem();
868                 int nItemsReturned = items.size();
869                 // There will be one item created, associated with a
870                 // known parent resource, by the createItem test.
871                 //
872                 // In addition, there will be 'nItemsToCreateInList'
873                 // additional items created by the createItemList test,
874                 // all associated with the same parent resource.
875                 int nExpectedItems = nItemsToCreateInList + 1;
876                 if(logger.isDebugEnabled()){
877                     logger.debug(testName + ": Expected "
878                                 + nExpectedItems +" items; got: "+nItemsReturned);
879                 }
880                 Assert.assertEquals(nItemsReturned, nExpectedItems);
881         
882                 int i = 0;
883                 for (LocationsCommonList.LocationListItem item : items) {
884                         Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
885                         Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
886                         // Optionally output additional data about list members for debugging.
887                         boolean showDetails = true;
888                         if (showDetails && logger.isDebugEnabled()) {
889                         logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
890                                 item.getCsid());
891                         logger.debug("  " + testName + ": list-item[" + i + "] refName=" +
892                                 item.getRefName());
893                         logger.debug("  " + testName + ": list-item[" + i + "] displayName=" +
894                                 item.getDisplayName());
895                         logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
896                                 item.getUri());
897                     }
898                     i++;
899                 }
900             } finally {
901                 res.releaseConnection();
902             }
903     }
904
905
906     // Failure outcomes
907     // None at present.
908
909     // ---------------------------------------------------------------
910     // CRUD tests : UPDATE tests
911     // ---------------------------------------------------------------
912     // Success outcomes
913     /* (non-Javadoc)
914      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
915      */
916     @Override
917     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
918         groups = {"update"}, dependsOnGroups = {"read", "readList"})
919     public void update(String testName) throws Exception {
920
921         if (logger.isDebugEnabled()) {
922             logger.debug(testBanner(testName, CLASS_NAME));
923         }
924         // Perform setup.
925         setupUpdate();
926
927         // Retrieve the contents of a resource to update.
928         LocationAuthorityClient client = new LocationAuthorityClient();
929         ClientResponse<String> res = client.read(knownResourceId);
930         try {
931                 if(logger.isDebugEnabled()){
932                     logger.debug(testName + ": read status = " + res.getStatus());
933                 }
934                 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
935         
936                 if(logger.isDebugEnabled()){
937                     logger.debug("got LocationAuthority to update with ID: " + knownResourceId);
938                 }
939                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
940                 LocationauthoritiesCommon locationAuthority = (LocationauthoritiesCommon) extractPart(input,
941                         client.getCommonPartName(), LocationauthoritiesCommon.class);
942                 Assert.assertNotNull(locationAuthority);
943         
944                 // Update the contents of this resource.
945                 locationAuthority.setDisplayName("updated-" + locationAuthority.getDisplayName());
946                 locationAuthority.setVocabType("updated-" + locationAuthority.getVocabType());
947                 if(logger.isDebugEnabled()){
948                     logger.debug("to be updated LocationAuthority");
949                     logger.debug(objectAsXmlString(locationAuthority, LocationauthoritiesCommon.class));
950                 }
951         
952                 // Submit the updated resource to the service and store the response.
953                 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_PAYLOAD_NAME);
954                 PayloadOutputPart commonPart = output.addPart(locationAuthority, MediaType.APPLICATION_XML_TYPE);
955                 commonPart.setLabel(client.getCommonPartName());
956                 res.releaseConnection();
957                 res = client.update(knownResourceId, output);
958                 int statusCode = res.getStatus();
959         
960                 // Check the status code of the response: does it match the expected response(s)?
961                 if(logger.isDebugEnabled()){
962                     logger.debug(testName + ": status = " + statusCode);
963                 }
964                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
965                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
966                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
967         
968                 // Retrieve the updated resource and verify that its contents exist.
969                 input = new PoxPayloadIn(res.getEntity());
970                 LocationauthoritiesCommon updatedLocationAuthority =
971                         (LocationauthoritiesCommon) extractPart(input,
972                                 client.getCommonPartName(), LocationauthoritiesCommon.class);
973                 Assert.assertNotNull(updatedLocationAuthority);
974         
975                 // Verify that the updated resource received the correct data.
976                 Assert.assertEquals(updatedLocationAuthority.getDisplayName(),
977                         locationAuthority.getDisplayName(),
978                         "Data in updated object did not match submitted data.");
979             } finally {
980                 res.releaseConnection();
981             }
982     }
983
984     /**
985      * Update item.
986      *
987      * @param testName the test name
988      * @throws Exception the exception
989      */
990     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
991         groups = {"update"}, dependsOnMethods = {"update"})
992     public void updateItem(String testName) throws Exception {
993
994         if (logger.isDebugEnabled()) {
995             logger.debug(testBanner(testName, CLASS_NAME));
996         }
997         // Perform setup.
998         setupUpdate();
999
1000         // Retrieve the contents of a resource to update.
1001         LocationAuthorityClient client = new LocationAuthorityClient();
1002         ClientResponse<String> res =
1003                 client.readItem(knownResourceId, knownItemResourceId);
1004         try {
1005                 if(logger.isDebugEnabled()){
1006                     logger.debug(testName + ": read status = " + res.getStatus());
1007                 }
1008                 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1009         
1010                 if(logger.isDebugEnabled()){
1011                     logger.debug("got Location to update with ID: " +
1012                         knownItemResourceId +
1013                         " in LocationAuthority: " + knownResourceId );
1014                 }
1015                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
1016                 LocationsCommon location = (LocationsCommon) extractPart(input,
1017                         client.getItemCommonPartName(), LocationsCommon.class);
1018                 Assert.assertNotNull(location);
1019         
1020                 // Update the contents of this resource.
1021                 location.setCsid(null);
1022                 location.setName("updated-" + location.getName());
1023                 if(logger.isDebugEnabled()){
1024                     logger.debug("to be updated Location");
1025                     logger.debug(objectAsXmlString(location,
1026                         LocationsCommon.class));
1027                 }        
1028         
1029                 // Submit the updated resource to the service and store the response.
1030                 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
1031                 PayloadOutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
1032                 commonPart.setLabel(client.getItemCommonPartName());
1033                 res.releaseConnection();
1034                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
1035                 int statusCode = res.getStatus();
1036         
1037                 // Check the status code of the response: does it match the expected response(s)?
1038                 if(logger.isDebugEnabled()){
1039                     logger.debug(testName + ": status = " + statusCode);
1040                 }
1041                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1042                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1043                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1044         
1045                 // Retrieve the updated resource and verify that its contents exist.
1046                 input = new PoxPayloadIn(res.getEntity());
1047                 LocationsCommon updatedLocation =
1048                         (LocationsCommon) extractPart(input,
1049                                 client.getItemCommonPartName(), LocationsCommon.class);
1050                 Assert.assertNotNull(updatedLocation);
1051         
1052                 // Verify that the updated resource received the correct data.
1053                 Assert.assertEquals(updatedLocation.getName(), location.getName(),
1054                         "Data in updated Location did not match submitted data.");
1055             } finally {
1056                 res.releaseConnection();
1057             }
1058     }
1059
1060     // Failure outcomes
1061     // Placeholders until the three tests below can be uncommented.
1062     // See Issue CSPACE-401.
1063     /* (non-Javadoc)
1064      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
1065      */
1066     @Override
1067     public void updateWithEmptyEntityBody(String testName) throws Exception {
1068     }
1069
1070     /* (non-Javadoc)
1071      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
1072      */
1073     @Override
1074     public void updateWithMalformedXml(String testName) throws Exception {
1075     }
1076
1077     /* (non-Javadoc)
1078      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
1079      */
1080     @Override
1081     public void updateWithWrongXmlSchema(String testName) throws Exception {
1082     }
1083
1084
1085     /* (non-Javadoc)
1086          * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
1087          */
1088 @Override
1089     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1090         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1091     public void updateNonExistent(String testName) throws Exception {
1092
1093         if (logger.isDebugEnabled()) {
1094             logger.debug(testBanner(testName, CLASS_NAME));
1095         }
1096         // Perform setup.
1097         setupUpdateNonExistent();
1098
1099         // Submit the request to the service and store the response.
1100         // Note: The ID(s) used when creating the request payload may be arbitrary.
1101         // The only relevant ID may be the one used in update(), below.
1102         LocationAuthorityClient client = new LocationAuthorityClient();
1103         String displayName = "displayName-NON_EXISTENT_ID";
1104         PoxPayloadOut multipart = LocationAuthorityClientUtils.createLocationAuthorityInstance(
1105                                 displayName, "nonEx", client.getCommonPartName());
1106         ClientResponse<String> res =
1107                 client.update(NON_EXISTENT_ID, multipart);
1108         try {
1109                 int statusCode = res.getStatus();
1110         
1111                 // Check the status code of the response: does it match
1112                 // the expected response(s)?
1113                 if(logger.isDebugEnabled()){
1114                     logger.debug(testName + ": status = " + statusCode);
1115                 }
1116                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1117                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1118                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1119             } finally {
1120                 res.releaseConnection();
1121             }
1122     }
1123
1124     /**
1125      * Update non existent item.
1126      *
1127      * @param testName the test name
1128      * @throws Exception the exception
1129      */
1130     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1131         groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1132     public void updateNonExistentItem(String testName) throws Exception {
1133
1134         if (logger.isDebugEnabled()) {
1135             logger.debug(testBanner(testName, CLASS_NAME));
1136         }
1137         // Perform setup.
1138         setupUpdateNonExistent();
1139
1140         // Submit the request to the service and store the response.
1141         // Note: The ID used in this 'create' call may be arbitrary.
1142         // The only relevant ID may be the one used in update(), below.
1143         LocationAuthorityClient client = new LocationAuthorityClient();
1144         Map<String, String> nonexMap = new HashMap<String,String>();
1145         nonexMap.put(LocationJAXBSchema.NAME, TEST_NAME);
1146         nonexMap.put(LocationJAXBSchema.SHORT_IDENTIFIER, "nonEx");
1147         nonexMap.put(LocationJAXBSchema.LOCATION_TYPE, TEST_LOCATION_TYPE);
1148         nonexMap.put(LocationJAXBSchema.TERM_STATUS, TEST_STATUS);
1149         PoxPayloadOut multipart = 
1150         LocationAuthorityClientUtils.createLocationInstance(
1151                         LocationAuthorityClientUtils.createLocationRefName(knownResourceRefName, "nonEx", "Non Existent"), 
1152                         nonexMap, client.getItemCommonPartName() );
1153         ClientResponse<String> res =
1154                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1155         try {
1156                 int statusCode = res.getStatus();
1157         
1158                 // Check the status code of the response: does it match
1159                 // the expected response(s)?
1160                 if(logger.isDebugEnabled()){
1161                     logger.debug(testName + ": status = " + statusCode);
1162                 }
1163                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1164                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1165                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1166             } finally {
1167                 res.releaseConnection();
1168             }
1169     }
1170
1171     // ---------------------------------------------------------------
1172     // CRUD tests : DELETE tests
1173     // ---------------------------------------------------------------
1174     // Success outcomes
1175
1176     // Note: delete sub-resources in ascending hierarchical order,
1177     // before deleting their parents.
1178
1179    /**
1180     * Delete item.
1181     *
1182     * @param testName the test name
1183     * @throws Exception the exception
1184     */
1185    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1186         //groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "readListByPartialTerm", "update"})
1187         groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "update"})
1188     public void deleteItem(String testName) throws Exception {
1189
1190         if (logger.isDebugEnabled()) {
1191             logger.debug(testBanner(testName, CLASS_NAME));
1192         }
1193         // Perform setup.
1194         setupDelete();
1195
1196         if(logger.isDebugEnabled()){
1197             logger.debug("parentcsid =" + knownResourceId +
1198                 " itemcsid = " + knownItemResourceId);
1199         }
1200
1201         // Submit the request to the service and store the response.
1202         LocationAuthorityClient client = new LocationAuthorityClient();
1203         ClientResponse<Response> res = 
1204                 client.deleteItem(knownResourceId, knownItemResourceId);
1205         try {
1206                 int statusCode = res.getStatus();
1207         
1208                 // Check the status code of the response: does it match
1209                 // the expected response(s)?
1210                 if(logger.isDebugEnabled()){
1211                     logger.debug(testName + ": status = " + statusCode);
1212                 }
1213                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1214                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1215                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1216             } finally {
1217                 res.releaseConnection();
1218             }
1219     }
1220
1221     /* (non-Javadoc)
1222      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
1223      */
1224     @Override
1225     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1226         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1227     public void delete(String testName) throws Exception {
1228
1229         if (logger.isDebugEnabled()) {
1230             logger.debug(testBanner(testName, CLASS_NAME));
1231         }
1232         // Perform setup.
1233         setupDelete();
1234
1235         if(logger.isDebugEnabled()){
1236             logger.debug("parentcsid =" + knownResourceId);
1237         }
1238
1239         // Submit the request to the service and store the response.
1240         LocationAuthorityClient client = new LocationAuthorityClient();
1241         ClientResponse<Response> res = client.delete(knownResourceId);
1242         try {
1243                 int statusCode = res.getStatus();
1244         
1245                 // Check the status code of the response: does it match
1246                 // the expected response(s)?
1247                 if(logger.isDebugEnabled()){
1248                     logger.debug(testName + ": status = " + statusCode);
1249                 }
1250                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1251                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1252                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1253             } finally {
1254                 res.releaseConnection();
1255             }
1256     }
1257
1258     // Failure outcomes
1259     /* (non-Javadoc)
1260      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
1261      */
1262     @Override
1263     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1264         groups = {"delete"}, dependsOnMethods = {"delete"})
1265     public void deleteNonExistent(String testName) throws Exception {
1266
1267         if (logger.isDebugEnabled()) {
1268             logger.debug(testBanner(testName, CLASS_NAME));
1269         }
1270         // Perform setup.
1271         setupDeleteNonExistent();
1272
1273         // Submit the request to the service and store the response.
1274         LocationAuthorityClient client = new LocationAuthorityClient();
1275         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1276         try {
1277                 int statusCode = res.getStatus();
1278         
1279                 // Check the status code of the response: does it match
1280                 // the expected response(s)?
1281                 if(logger.isDebugEnabled()){
1282                     logger.debug(testName + ": status = " + statusCode);
1283                 }
1284                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1285                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1286                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1287             } finally {
1288                 res.releaseConnection();
1289             }
1290     }
1291
1292     /**
1293      * Delete non existent item.
1294      *
1295      * @param testName the test name
1296      */
1297     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1298         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1299     public void deleteNonExistentItem(String testName) {
1300
1301         if (logger.isDebugEnabled()) {
1302             logger.debug(testBanner(testName, CLASS_NAME));
1303         }
1304         // Perform setup.
1305         setupDeleteNonExistent();
1306
1307         // Submit the request to the service and store the response.
1308         LocationAuthorityClient client = new LocationAuthorityClient();
1309         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1310         try {
1311                 int statusCode = res.getStatus();
1312         
1313                 // Check the status code of the response: does it match
1314                 // the expected response(s)?
1315                 if(logger.isDebugEnabled()){
1316                     logger.debug(testName + ": status = " + statusCode);
1317                 }
1318                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1319                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1320                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1321             } finally {
1322                 res.releaseConnection();
1323             }
1324     }
1325
1326     // ---------------------------------------------------------------
1327     // Utility tests : tests of code used in tests above
1328     // ---------------------------------------------------------------
1329     /**
1330      * Tests the code for manually submitting data that is used by several
1331      * of the methods above.
1332      */
1333     @Test(dependsOnMethods = {"create", "read"})
1334     public void testSubmitRequest() {
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 = getResourceURL(knownResourceId);
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("testSubmitRequest: url=" + url +
1348                 " status=" + statusCode);
1349         }
1350         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1351
1352     }
1353
1354     /**
1355      * Test item submit request.
1356      */
1357     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1358     public void testItemSubmitRequest() {
1359
1360         // Expected status code: 200 OK
1361         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1362
1363         // Submit the request to the service and store the response.
1364         String method = ServiceRequestType.READ.httpMethodName();
1365         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1366         int statusCode = submitRequest(method, url);
1367
1368         // Check the status code of the response: does it match
1369         // the expected response(s)?
1370         if(logger.isDebugEnabled()){
1371             logger.debug("testItemSubmitRequest: url=" + url +
1372                 " status=" + statusCode);
1373         }
1374         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1375
1376     }
1377
1378     // ---------------------------------------------------------------
1379     // Cleanup of resources created during testing
1380     // ---------------------------------------------------------------
1381     
1382     /**
1383      * Deletes all resources created by tests, after all tests have been run.
1384      *
1385      * This cleanup method will always be run, even if one or more tests fail.
1386      * For this reason, it attempts to remove all resources created
1387      * at any point during testing, even if some of those resources
1388      * may be expected to be deleted by certain tests.
1389      */
1390
1391     @AfterClass(alwaysRun=true)
1392     public void cleanUp() {
1393         String noTest = System.getProperty("noTestCleanup");
1394         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1395             if (logger.isDebugEnabled()) {
1396                 logger.debug("Skipping Cleanup phase ...");
1397             }
1398             return;
1399         }
1400         if (logger.isDebugEnabled()) {
1401             logger.debug("Cleaning up temporary resources created for testing ...");
1402         }
1403         String parentResourceId;
1404         String itemResourceId;
1405         // Clean up contact resources.
1406         LocationAuthorityClient client = new LocationAuthorityClient();
1407         parentResourceId = knownResourceId;
1408         // Clean up item resources.
1409         for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
1410             itemResourceId = entry.getKey();
1411             parentResourceId = entry.getValue();
1412             // Note: Any non-success responses from the delete operation
1413             // below are ignored and not reported.
1414             ClientResponse<Response> res =
1415                 client.deleteItem(parentResourceId, itemResourceId);
1416                 res.releaseConnection();
1417         }
1418         // Clean up parent resources.
1419         for (String resourceId : allResourceIdsCreated) {
1420             // Note: Any non-success responses from the delete operation
1421             // below are ignored and not reported.
1422             ClientResponse<Response> res = client.delete(resourceId);
1423                 res.releaseConnection();
1424         }
1425     }
1426
1427     // ---------------------------------------------------------------
1428     // Utility methods used by tests above
1429     // ---------------------------------------------------------------
1430     /* (non-Javadoc)
1431      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
1432      */
1433
1434     /**
1435      * Returns the root URL for the item service.
1436      *
1437      * This URL consists of a base URL for all services, followed by
1438      * a path component for the owning parent, followed by the
1439      * path component for the items.
1440      *
1441      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1442      * parent authority resource of the relevant item resource.
1443      *
1444      * @return The root URL for the item service.
1445      */
1446     protected String getItemServiceRootURL(String parentResourceIdentifier) {
1447         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1448     }
1449
1450     /**
1451      * Returns the URL of a specific item resource managed by a service, and
1452      * designated by an identifier (such as a universally unique ID, or UUID).
1453      *
1454      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1455      * parent authority resource of the relevant item resource.
1456      *
1457      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1458      * item resource.
1459      *
1460      * @return The URL of a specific item resource managed by a service.
1461      */
1462     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1463         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1464     }
1465
1466
1467 }