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