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