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