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