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