]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
088ea75205efda8d9016d21d00b95dc5a0853f18
[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.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30
31 import org.collectionspace.services.PersonJAXBSchema;
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.contact.ContactsCommon;
36 import org.collectionspace.services.contact.ContactsCommonList;
37 import org.collectionspace.services.client.PersonAuthorityClient;
38 import org.collectionspace.services.client.PersonAuthorityClientUtils;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.collectionspace.services.person.PersonauthoritiesCommon;
41 import org.collectionspace.services.person.PersonauthoritiesCommonList;
42 import org.collectionspace.services.person.PersonsCommon;
43 import org.collectionspace.services.person.PersonsCommonList;
44 import org.jboss.resteasy.client.ClientResponse;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
47 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.testng.Assert;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.Test;
53
54 /**
55  * PersonAuthorityServiceTest, carries out tests against a
56  * deployed and running PersonAuthority Service.
57  *
58  * $LastChangedRevision: 753 $
59  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
60  */
61 public class PersonAuthorityServiceTest extends AbstractServiceTestImpl {
62
63     /** The logger. */
64     private final Logger logger =
65         LoggerFactory.getLogger(PersonAuthorityServiceTest.class);
66
67     // Instance variables specific to this test.
68     /** The service path component. */
69     final String SERVICE_PATH_COMPONENT = "personauthorities";
70     
71     /** The item service path component. */
72     final String ITEM_SERVICE_PATH_COMPONENT = "items";
73     
74     /** The contact service path component. */
75     final String CONTACT_SERVICE_PATH_COMPONENT = "contacts";
76     
77     /** The test forename. */
78     final String TEST_FORE_NAME = "John";
79     
80     /** The test middle name. */
81     final String TEST_MIDDLE_NAME = null;
82     
83     /** The test surname. */
84     final String TEST_SUR_NAME = "Wayne";
85     
86     /** The test birthdate. */
87     final String TEST_BIRTH_DATE = "May 26, 1907";
88     
89     /** The test death date. */
90     final String TEST_DEATH_DATE = "June 11, 1979";
91
92     // Test name for partial term matching: Lech Walensa
93     final String TEST_PARTIAL_TERM_FORE_NAME = "Lech";
94     // Contains two non-USASCII range Unicode UTF-8 characters
95     final String TEST_PARTIAL_TERM_SUR_NAME = "Wa" + "\u0142" + "\u0119" + "sa";
96     final String TEST_PARTIAL_TERM_DISPLAY_NAME =
97             TEST_PARTIAL_TERM_FORE_NAME + " " + TEST_PARTIAL_TERM_SUR_NAME;
98  
99     /** The known resource id. */
100     private String knownResourceId = null;
101     
102     /** The known resource display name. */
103     private String knownResourceDisplayName = null;
104     
105     /** The known resource ref name. */
106     private String knownResourceRefName = null;
107     
108     /** The known item resource id. */
109     private String knownItemResourceId = null;
110
111     // The resource ID of an item resource used for partial term matching tests.
112     private String knownItemPartialTermResourceId = null;
113     
114     /** The known contact resource id. */
115     private String knownContactResourceId = null;
116     
117     /** The n items to create in list. */
118     private int nItemsToCreateInList = 3;
119     
120     /** The all item resource ids created. */
121     private Map<String, String> allItemResourceIdsCreated =
122         new HashMap<String, String>();
123     
124     /** The all contact resource ids created. */
125     private Map<String, String> allContactResourceIdsCreated =
126         new HashMap<String, String>();
127
128     /* (non-Javadoc)
129      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
130      */
131     @Override
132     protected CollectionSpaceClient getClientInstance() {
133         return new PersonAuthorityClient();
134     }
135     
136     /* (non-Javadoc)
137      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
138      */
139     @Override
140         protected AbstractCommonList getAbstractCommonList(
141                         ClientResponse<AbstractCommonList> response) {
142         return response.getEntity(PersonsCommonList.class);
143     }
144  
145     // ---------------------------------------------------------------
146     // CRUD tests : CREATE tests
147     // ---------------------------------------------------------------
148     // Success outcomes
149     /* (non-Javadoc)
150      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
151      */
152     @Override
153     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
154         groups = {"create"})
155     public void create(String testName) throws Exception {
156
157         // Perform setup, such as initializing the type of service request
158         // (e.g. CREATE, DELETE), its valid and expected status codes, and
159         // its associated HTTP method name (e.g. POST, DELETE).
160         setupCreate(testName);
161
162         // Submit the request to the service and store the response.
163         PersonAuthorityClient client = new PersonAuthorityClient();
164         String identifier = createIdentifier();
165         String displayName = "displayName-" + identifier;
166         String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(displayName, false);
167         String fullRefName = PersonAuthorityClientUtils.createPersonAuthRefName(displayName, true);
168         MultipartOutput multipart = 
169             PersonAuthorityClientUtils.createPersonAuthorityInstance(
170             displayName, fullRefName, client.getCommonPartName());
171         
172         String newID = null;
173         ClientResponse<Response> res = client.create(multipart);
174         try {
175                 int statusCode = res.getStatus();
176         
177                 // Check the status code of the response: does it match
178                 // the expected response(s)?
179                 //
180                 // Specifically:
181                 // Does it fall within the set of valid status codes?
182                 // Does it exactly match the expected status code?
183                 if(logger.isDebugEnabled()){
184                     logger.debug(testName + ": status = " + statusCode);
185                 }
186                 Assert.assertTrue(this.REQUEST_TYPE.isValidStatusCode(statusCode),
187                         invalidStatusCodeMessage(this.REQUEST_TYPE, statusCode));
188                 Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
189         
190                 newID = PersonAuthorityClientUtils.extractId(res);
191         } finally {
192                 res.releaseConnection();
193         }
194         // Store the refname from the first resource created
195         // for additional tests below.
196         knownResourceRefName = baseRefName;
197         // Store the ID returned from the first resource created
198         // for additional tests below.
199         if (knownResourceId == null){
200             knownResourceId = newID;
201             knownResourceDisplayName = displayName;
202             if (logger.isDebugEnabled()) {
203                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
204             }
205         }
206         // Store the IDs from every resource created by tests,
207         // so they can be deleted after tests have been run.
208         allResourceIdsCreated.add(newID);
209     }
210
211     /**
212      * Creates the item.
213      *
214      * @param testName the test name
215      */
216     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
217         groups = {"create"}, dependsOnMethods = {"create"})
218     public void createItem(String testName) {
219         setupCreate(testName);
220         String newID = createItemInAuthority(knownResourceId, knownResourceRefName);
221     }
222
223     /**
224      * Creates the item in authority.
225      *
226      * @param vcsid the vcsid
227      * @param authRefName the auth ref name
228      * @return the string
229      */
230     private String createItemInAuthority(String vcsid, String authRefName) {
231
232         final String testName = "createItemInAuthority";
233         if(logger.isDebugEnabled()){
234             logger.debug(testName + ":...");
235         }
236
237         // Submit the request to the service and store the response.
238         PersonAuthorityClient client = new PersonAuthorityClient();
239         String refName = PersonAuthorityClientUtils.createPersonRefName(authRefName, "John Wayne", true);
240         Map<String, String> johnWayneMap = new HashMap<String,String>();
241         //
242         // Fill the property map
243         //
244         johnWayneMap.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "false");
245         johnWayneMap.put(PersonJAXBSchema.DISPLAY_NAME, "John Wayne");
246         
247         johnWayneMap.put(PersonJAXBSchema.FORE_NAME, TEST_FORE_NAME);
248         johnWayneMap.put(PersonJAXBSchema.SUR_NAME, TEST_SUR_NAME);
249         johnWayneMap.put(PersonJAXBSchema.GENDER, "male");
250         johnWayneMap.put(PersonJAXBSchema.BIRTH_DATE, TEST_BIRTH_DATE);
251         johnWayneMap.put(PersonJAXBSchema.BIRTH_PLACE, "Winterset, Iowa");
252         johnWayneMap.put(PersonJAXBSchema.DEATH_DATE, TEST_DEATH_DATE);
253         johnWayneMap.put(PersonJAXBSchema.BIO_NOTE, "born Marion Robert Morrison and better" +
254             "known by his stage name John Wayne, was an American film actor, director " +
255             "and producer. He epitomized rugged masculinity and has become an enduring " +
256             "American icon. He is famous for his distinctive voice, walk and height. " +
257             "He was also known for his conservative political views and his support in " +
258             "the 1950s for anti-communist positions.");
259         MultipartOutput multipart = 
260             PersonAuthorityClientUtils.createPersonInstance(vcsid, refName, johnWayneMap,
261                 client.getItemCommonPartName() );
262
263         String newID = null;
264         ClientResponse<Response> res = client.createItem(vcsid, multipart);
265         try {
266                 int statusCode = res.getStatus();
267                 // Check the status code of the response: does it match
268                 // the expected response(s)?
269                 if(logger.isDebugEnabled()){
270                     logger.debug(testName + ": status = " + statusCode);
271                 }
272                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
273                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
274                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
275
276                 newID = PersonAuthorityClientUtils.extractId(res);
277         } finally {
278                 res.releaseConnection();
279         }
280
281         // Store the ID returned from the first item resource created
282         // for additional tests below.
283         if (knownItemResourceId == null){
284             knownItemResourceId = newID;
285             if (logger.isDebugEnabled()) {
286                 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
287             }
288         }
289
290         // Store the IDs from any item resources created
291         // by tests, along with the IDs of their parents, so these items
292         // can be deleted after all tests have been run.
293         allItemResourceIdsCreated.put(newID, vcsid);
294
295         return newID;
296     }
297     
298     /**
299      * Creates an item in the authority, used for partial term matching tests.
300      *
301      * @param vcsid the vcsid
302      * @param authRefName the auth ref name
303      * @return the string
304      */
305     private String createItemInAuthorityForPartialTermMatch(String vcsid, String authRefName) {
306         
307         final String testName = "createItemInAuthorityForPartialTermMatch";
308         if(logger.isDebugEnabled()){
309             logger.debug(testName + ":...");
310         }
311
312         setupCreate(testName);
313
314         // Submit the request to the service and store the response.
315         PersonAuthorityClient client = new PersonAuthorityClient();
316         String refName = PersonAuthorityClientUtils.createPersonRefName(authRefName,
317                 TEST_PARTIAL_TERM_DISPLAY_NAME, true);
318         Map<String, String> partialTermPersonMap = new HashMap<String,String>();
319         //
320         // Fill the property map
321         //
322         partialTermPersonMap.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "false");
323         partialTermPersonMap.put(PersonJAXBSchema.DISPLAY_NAME, TEST_PARTIAL_TERM_DISPLAY_NAME);
324         partialTermPersonMap.put(PersonJAXBSchema.FORE_NAME, TEST_PARTIAL_TERM_FORE_NAME);
325         partialTermPersonMap.put(PersonJAXBSchema.SUR_NAME, TEST_PARTIAL_TERM_SUR_NAME);
326         partialTermPersonMap.put(PersonJAXBSchema.GENDER, "male");
327         MultipartOutput multipart = 
328             PersonAuthorityClientUtils.createPersonInstance(vcsid, refName, partialTermPersonMap,
329                 client.getItemCommonPartName() );
330
331         String newID = null;
332         ClientResponse<Response> res = client.createItem(vcsid, multipart);
333         try {
334                 int statusCode = res.getStatus();
335                 // Check the status code of the response: does it match
336                 // the expected response(s)?
337                 if(logger.isDebugEnabled()){
338                     logger.debug(testName + ": status = " + statusCode);
339                 }
340                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
341                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
342                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
343
344                 newID = PersonAuthorityClientUtils.extractId(res);
345         } finally {
346                 res.releaseConnection();
347         }
348
349         // Store the ID returned from the first item resource created
350         // for additional tests below.
351         if (knownItemResourceId == null){
352             knownItemResourceId = newID;
353             if (logger.isDebugEnabled()) {
354                 logger.debug(testName + ": knownItemPartialTermResourceId=" + knownItemPartialTermResourceId);
355             }
356         }
357
358         // Store the IDs from any item resources created
359         // by tests, along with the IDs of their parents, so these items
360         // can be deleted after all tests have been run.
361         allItemResourceIdsCreated.put(newID, vcsid);
362
363         return newID;
364     }
365
366     /**
367      * Creates the contact.
368      *
369      * @param testName the test name
370      */
371     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
372         groups = {"create"}, dependsOnMethods = {"createItem"})
373     public void createContact(String testName) {
374         setupCreate(testName);
375         String newID = createContactInItem(knownResourceId, knownItemResourceId);
376     }
377
378    /**
379     * Creates the contact in item.
380     *
381     * @param parentcsid the parentcsid
382     * @param itemcsid the itemcsid
383     * @return the string
384     */
385    private String createContactInItem(String parentcsid, String itemcsid) {
386
387         final String testName = "createContactInItem";
388         setupCreate(testName);
389         if(logger.isDebugEnabled()){
390             logger.debug(testName + ":...");
391         }
392
393         // Submit the request to the service and store the response.
394         PersonAuthorityClient client = new PersonAuthorityClient();
395         String identifier = createIdentifier();
396         MultipartOutput multipart =
397             ContactClientUtils.createContactInstance(parentcsid,
398             itemcsid, identifier, new ContactClient().getCommonPartName());
399         
400         String newID = null;
401         ClientResponse<Response> res =
402              client.createContact(parentcsid, itemcsid, multipart);
403         try {
404                 int statusCode = res.getStatus();
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
414                 newID = PersonAuthorityClientUtils.extractId(res);
415         } finally {
416                 res.releaseConnection();
417         }
418
419         // Store the ID returned from the first contact resource created
420         // for additional tests below.
421         if (knownContactResourceId == null){
422             knownContactResourceId = newID;
423             if (logger.isDebugEnabled()) {
424                 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
425             }
426         }
427
428         // Store the IDs from any contact resources created
429         // by tests, along with the IDs of their parent items,
430         // so these items can be deleted after all tests have been run.
431         allContactResourceIdsCreated.put(newID, itemcsid);
432
433         return newID;
434     }
435
436     // Failure outcomes
437
438     // Placeholders until the three tests below can be uncommented.
439     // See Issue CSPACE-401.
440     /* (non-Javadoc)
441      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
442      */
443     @Override
444     public void createWithEmptyEntityBody(String testName) throws Exception {
445         //Should this really be empty?
446     }
447
448     /* (non-Javadoc)
449      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
450      */
451     @Override
452     public void createWithMalformedXml(String testName) throws Exception {
453         //Should this really be empty?
454     }
455
456     /* (non-Javadoc)
457      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
458      */
459     @Override
460     public void createWithWrongXmlSchema(String testName) throws Exception {
461         //Should this really be empty?
462     }
463
464 /*
465     @Override
466     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
467         groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
468     public void createWithEmptyEntityBody(String testName) throws Exception {
469
470     // Perform setup.
471     setupCreateWithEmptyEntityBody(testName);
472
473     // Submit the request to the service and store the response.
474     String method = REQUEST_TYPE.httpMethodName();
475     String url = getServiceRootURL();
476     String mediaType = MediaType.APPLICATION_XML;
477     final String entity = "";
478     int statusCode = submitRequest(method, url, mediaType, entity);
479
480     // Check the status code of the response: does it match
481     // the expected response(s)?
482     if(logger.isDebugEnabled()) {
483         logger.debug(testName + ": url=" + url +
484             " status=" + statusCode);
485      }
486     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
487     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
488     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
489     }
490
491     @Override
492     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
493         groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
494     public void createWithMalformedXml(String testName) throws Exception {
495
496     // Perform setup.
497     setupCreateWithMalformedXml(testName);
498
499     // Submit the request to the service and store the response.
500     String method = REQUEST_TYPE.httpMethodName();
501     String url = getServiceRootURL();
502     String mediaType = MediaType.APPLICATION_XML;
503     final String entity = MALFORMED_XML_DATA; // Constant from base class.
504     int statusCode = submitRequest(method, url, mediaType, entity);
505
506     // Check the status code of the response: does it match
507     // the expected response(s)?
508     if(logger.isDebugEnabled()){
509         logger.debug(testName + ": url=" + url +
510             " status=" + statusCode);
511      }
512     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
513     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
514     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
515     }
516
517     @Override
518     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
519         groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
520     public void createWithWrongXmlSchema(String testName) throws Exception {
521
522     // Perform setup.
523     setupCreateWithWrongXmlSchema(testName);
524
525     // Submit the request to the service and store the response.
526     String method = REQUEST_TYPE.httpMethodName();
527     String url = getServiceRootURL();
528     String mediaType = MediaType.APPLICATION_XML;
529     final String entity = WRONG_XML_SCHEMA_DATA;
530     int statusCode = submitRequest(method, url, mediaType, entity);
531
532     // Check the status code of the response: does it match
533     // the expected response(s)?
534     if(logger.isDebugEnabled()){
535         logger.debug(testName + ": url=" + url +
536             " status=" + statusCode);
537      }
538     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
539     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
540     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
541     }
542 */
543
544     // ---------------------------------------------------------------
545     // CRUD tests : CREATE LIST tests
546     // ---------------------------------------------------------------
547     // Success outcomes
548     /* (non-Javadoc)
549  * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
550  */
551     @Override
552     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
553         groups = {"createList"}, dependsOnGroups = {"create"})
554     public void createList(String testName) throws Exception {
555         for (int i = 0; i < nItemsToCreateInList; i++) {
556             create(testName);
557         }
558     }
559
560     /**
561      * Creates the item list.
562      *
563      * @param testName the test name
564      * @throws Exception the exception
565      */
566     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
567         groups = {"createList"}, dependsOnMethods = {"createList"})
568     public void createItemList(String testName) throws Exception {
569         // Add items to the initially-created, known parent record.
570         for (int j = 0; j < nItemsToCreateInList; j++) {
571             createItem(testName);
572         }
573     }
574
575     /**
576      * Creates the contact list.
577      *
578      * @param testName the test name
579      * @throws Exception the exception
580      */
581     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
582         groups = {"createList"}, dependsOnMethods = {"createItemList"})
583     public void createContactList(String testName) throws Exception {
584         // Add contacts to the initially-created, known item record.
585         for (int j = 0; j < nItemsToCreateInList; j++) {
586             createContact(testName);
587         }
588     }
589
590     // ---------------------------------------------------------------
591     // CRUD tests : READ tests
592     // ---------------------------------------------------------------
593     // Success outcomes
594     /* (non-Javadoc)
595      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
596      */
597     @Override
598     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
599         groups = {"read"}, dependsOnGroups = {"create"})
600     public void read(String testName) throws Exception {
601
602         // Perform setup.
603         setupRead();
604         
605         // Submit the request to the service and store the response.
606         PersonAuthorityClient client = new PersonAuthorityClient();
607         ClientResponse<MultipartInput> res = client.read(knownResourceId);
608         try {
609                 int statusCode = res.getStatus();
610                 // Check the status code of the response: does it match
611                 // the expected response(s)?
612                 if(logger.isDebugEnabled()){
613                     logger.debug(testName + ": status = " + statusCode);
614                 }
615                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
616                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
617                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
618                 //FIXME: remove the following try catch once Aron fixes signatures
619                 try {
620                     MultipartInput input = (MultipartInput) res.getEntity();
621                     PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
622                             client.getCommonPartName(), PersonauthoritiesCommon.class);
623                     Assert.assertNotNull(personAuthority);
624                 } catch (Exception e) {
625                     throw new RuntimeException(e);
626                 }
627         } finally {
628                 res.releaseConnection();
629         }
630     }
631
632     /**
633      * Read by name.
634      *
635      * @param testName the test name
636      * @throws Exception the exception
637      */
638     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
639             groups = {"read"}, dependsOnGroups = {"create"})
640         public void readByName(String testName) throws Exception {
641
642         // Perform setup.
643         setupRead();
644
645         // Submit the request to the service and store the response.
646         PersonAuthorityClient client = new PersonAuthorityClient();
647         ClientResponse<MultipartInput> res = client.readByName(knownResourceDisplayName);
648         try {
649                 int statusCode = res.getStatus();
650                 // Check the status code of the response: does it match
651                 // the expected response(s)?
652                 if(logger.isDebugEnabled()){
653                     logger.debug(testName + ": status = " + statusCode);
654                 }
655                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
656                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
657                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
658                 //FIXME: remove the following try catch once Aron fixes signatures
659                 try {
660                     MultipartInput input = (MultipartInput) res.getEntity();
661                     PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
662                             client.getCommonPartName(), PersonauthoritiesCommon.class);
663                     Assert.assertNotNull(personAuthority);
664                 } catch (Exception e) {
665                     throw new RuntimeException(e);
666                 }
667         } finally {
668                 res.releaseConnection();
669         }
670     }
671
672 /*
673     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
674         groups = {"read"}, dependsOnMethods = {"read"})
675     public void readByName(String testName) throws Exception {
676
677         // Perform setup.
678         setupRead();
679
680         // Submit the request to the service and store the response.
681         ClientResponse<MultipartInput> res = client.read(knownResourceId);
682         int statusCode = res.getStatus();
683
684         // Check the status code of the response: does it match
685         // the expected response(s)?
686         if(logger.isDebugEnabled()){
687             logger.debug(testName + ": status = " + statusCode);
688         }
689         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
690                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
691         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
692         //FIXME: remove the following try catch once Aron fixes signatures
693         try {
694             MultipartInput input = (MultipartInput) res.getEntity();
695             PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
696                     client.getCommonPartName(), PersonauthoritiesCommon.class);
697             Assert.assertNotNull(personAuthority);
698         } catch (Exception e) {
699             throw new RuntimeException(e);
700         }
701     }
702 */
703
704     /**
705  * Read item.
706  *
707  * @param testName the test name
708  * @throws Exception the exception
709  */
710 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
711         groups = {"read"}, dependsOnMethods = {"read"})
712     public void readItem(String testName) throws Exception {
713         // Perform setup.
714         setupRead(testName);
715
716         // Submit the request to the service and store the response.
717         PersonAuthorityClient client = new PersonAuthorityClient();
718         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
719         try {
720                 int statusCode = res.getStatus();
721         
722                 // Check the status code of the response: does it match
723                 // the expected response(s)?
724                 if(logger.isDebugEnabled()){
725                     logger.debug(testName + ": status = " + statusCode);
726                 }
727                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
728                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
729                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
730         
731                 // Check whether we've received a person.
732                 MultipartInput input = (MultipartInput) res.getEntity();
733                 PersonsCommon person = (PersonsCommon) extractPart(input,
734                         client.getItemCommonPartName(), PersonsCommon.class);
735                 Assert.assertNotNull(person);
736                 boolean showFull = true;
737                 if(showFull && logger.isDebugEnabled()){
738                     logger.debug(testName + ": returned payload:");
739                     logger.debug(objectAsXmlString(person, PersonsCommon.class));
740                 }
741                 Assert.assertEquals(person.getInAuthority(), knownResourceId);
742         } finally {
743                 res.releaseConnection();
744         }
745     }
746
747     /**
748      * Verify item display name.
749      *
750      * @param testName the test name
751      * @throws Exception the exception
752      */
753     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
754         dependsOnMethods = {"readItem", "updateItem"})
755     public void verifyItemDisplayName(String testName) throws Exception {
756         // Perform setup.
757         setupUpdate(testName);
758
759         // Submit the request to the service and store the response.
760         PersonAuthorityClient client = new PersonAuthorityClient();
761         MultipartInput input =null;
762         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
763         try {
764                 int statusCode = res.getStatus();
765         
766                 // Check the status code of the response: does it match
767                 // the expected response(s)?
768                 if(logger.isDebugEnabled()){
769                     logger.debug(testName + ": status = " + statusCode);
770                 }
771                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
772                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
773                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
774         
775                 // Check whether person has expected displayName.
776                 input = (MultipartInput) res.getEntity();
777         } finally {
778                 res.releaseConnection();
779         }
780                 
781         PersonsCommon person = (PersonsCommon) extractPart(input,
782                 client.getItemCommonPartName(), PersonsCommon.class);
783         Assert.assertNotNull(person);
784         String displayName = person.getDisplayName();
785         // Make sure displayName matches computed form
786         String expectedDisplayName = 
787             PersonAuthorityClientUtils.prepareDefaultDisplayName(
788                 TEST_FORE_NAME, null, TEST_SUR_NAME,
789                 TEST_BIRTH_DATE, TEST_DEATH_DATE);
790         Assert.assertNotNull(displayName, expectedDisplayName);
791     
792         // Update the shortName and verify the computed name is updated.
793         person.setCsid(null);
794         person.setDisplayNameComputed(true);
795         person.setForeName("updated-" + TEST_FORE_NAME);
796         expectedDisplayName = 
797             PersonAuthorityClientUtils.prepareDefaultDisplayName(
798                 "updated-" + TEST_FORE_NAME, null, TEST_SUR_NAME, 
799                 TEST_BIRTH_DATE, TEST_DEATH_DATE);
800
801         // Submit the updated resource to the service and store the response.
802         MultipartOutput output = new MultipartOutput();
803         OutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
804         commonPart.getHeaders().add("label", client.getItemCommonPartName());
805         res = client.updateItem(knownResourceId, knownItemResourceId, output);
806         try {
807                 int statusCode = res.getStatus();
808         
809                 // Check the status code of the response: does it match the expected response(s)?
810                 if(logger.isDebugEnabled()){
811                     logger.debug("updateItem: status = " + statusCode);
812                 }
813                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
814                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
815                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
816         
817                 // Retrieve the updated resource and verify that its contents exist.
818                 input = (MultipartInput) res.getEntity();
819         } finally {
820                 res.releaseConnection();
821         }
822         
823         PersonsCommon updatedPerson =
824                 (PersonsCommon) extractPart(input,
825                         client.getItemCommonPartName(), PersonsCommon.class);
826         Assert.assertNotNull(updatedPerson);
827
828         // Verify that the updated resource received the correct data.
829         Assert.assertEquals(updatedPerson.getForeName(), person.getForeName(),
830             "Updated ForeName in Person did not match submitted data.");
831         // Verify that the updated resource computes the right displayName.
832         Assert.assertEquals(updatedPerson.getDisplayName(), expectedDisplayName,
833             "Updated ForeName in Person not reflected in computed DisplayName.");
834
835         // Now Update the displayName, not computed and verify the computed name is overriden.
836         person.setDisplayNameComputed(false);
837         expectedDisplayName = "TestName";
838         person.setDisplayName(expectedDisplayName);
839
840         // Submit the updated resource to the service and store the response.
841         output = new MultipartOutput();
842         commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
843         commonPart.getHeaders().add("label", client.getItemCommonPartName());
844         res = client.updateItem(knownResourceId, knownItemResourceId, output);
845         try {
846                 int statusCode = res.getStatus();
847         
848                 // Check the status code of the response: does it match the expected response(s)?
849                 if(logger.isDebugEnabled()){
850                     logger.debug("updateItem: status = " + statusCode);
851                 }
852                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
853                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
854                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
855         
856                 // Retrieve the updated resource and verify that its contents exist.
857                 input = (MultipartInput) res.getEntity();
858         } finally {
859                 res.releaseConnection();
860         }
861         
862         updatedPerson =
863                 (PersonsCommon) extractPart(input,
864                         client.getItemCommonPartName(), PersonsCommon.class);
865         Assert.assertNotNull(updatedPerson);
866
867         // Verify that the updated resource received the correct data.
868         Assert.assertEquals(updatedPerson.isDisplayNameComputed(), false,
869                 "Updated displayNameComputed in Person did not match submitted data.");
870         // Verify that the updated resource computes the right displayName.
871         Assert.assertEquals(updatedPerson.getDisplayName(),
872                         expectedDisplayName,
873                 "Updated DisplayName (not computed) in Person not stored.");
874     }
875
876     /**
877      * Verify illegal item display name.
878      *
879      * @param testName the test name
880      * @throws Exception the exception
881      */
882     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
883             dependsOnMethods = {"verifyItemDisplayName"})
884     public void verifyIllegalItemDisplayName(String testName) throws Exception {
885
886         // Perform setup.
887         setupUpdateWithWrongXmlSchema(testName);
888
889         // Submit the request to the service and store the response.
890         PersonAuthorityClient client = new PersonAuthorityClient();
891         MultipartInput input = null;
892         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
893         try {
894                 int statusCode = res.getStatus();
895         
896                 // Check the status code of the response: does it match
897                 // the expected response(s)?
898                 if(logger.isDebugEnabled()){
899                     logger.debug(testName + ": status = " + statusCode);
900                 }
901                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
902                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
903                 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
904         
905                 // Check whether Person has expected displayName.
906                 input = (MultipartInput) res.getEntity();
907         } finally {
908                 res.releaseConnection();
909         }
910         
911         PersonsCommon person = (PersonsCommon) extractPart(input,
912                 client.getItemCommonPartName(), PersonsCommon.class);
913         Assert.assertNotNull(person);
914         // Try to Update with computed false and no displayName
915         person.setDisplayNameComputed(false);
916         person.setDisplayName(null);
917
918         // Submit the updated resource to the service and store the response.
919         MultipartOutput output = new MultipartOutput();
920         OutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
921         commonPart.getHeaders().add("label", client.getItemCommonPartName());
922         res = client.updateItem(knownResourceId, knownItemResourceId, output);
923         try {
924                 int statusCode = res.getStatus();
925         
926                 // Check the status code of the response: does it match the expected response(s)?
927                 if(logger.isDebugEnabled()){
928                     logger.debug("updateItem: status = " + statusCode);
929                 }
930                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
931                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
932                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
933         } finally {
934                 res.releaseConnection();
935         }
936     }
937     
938     /**
939      * Read contact.
940      *
941      * @param testName the test name
942      * @throws Exception the exception
943      */
944     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
945         groups = {"read"}, dependsOnMethods = {"readItem"})
946     public void readContact(String testName) throws Exception {
947         // Perform setup.
948         setupRead(testName);
949
950         // Submit the request to the service and store the response.
951         PersonAuthorityClient client = new PersonAuthorityClient();
952         MultipartInput input = null;
953         ClientResponse<MultipartInput> res =
954             client.readContact(knownResourceId, knownItemResourceId,
955             knownContactResourceId);
956         try {
957                 int statusCode = res.getStatus();
958         
959                 // Check the status code of the response: does it match
960                 // the expected response(s)?
961                 if(logger.isDebugEnabled()){
962                     logger.debug(testName + ": status = " + statusCode);
963                 }
964                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
965                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
966                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
967         
968                 // Check whether we've received a contact.
969                 input = (MultipartInput) res.getEntity();
970         } finally {
971                 res.releaseConnection();
972         }
973         
974         ContactsCommon contact = (ContactsCommon) extractPart(input,
975                 new ContactClient().getCommonPartName(), ContactsCommon.class);
976         Assert.assertNotNull(contact);
977         boolean showFull = true;
978         if(showFull && logger.isDebugEnabled()){
979             logger.debug(testName + ": returned payload:");
980             logger.debug(objectAsXmlString(contact, ContactsCommon.class));
981         }
982         Assert.assertEquals(contact.getInAuthority(), knownResourceId);
983         Assert.assertEquals(contact.getInItem(), knownItemResourceId);
984
985     }
986
987     // Failure outcomes
988     /* (non-Javadoc)
989      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
990      */
991     @Override
992     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
993         groups = {"read"}, dependsOnMethods = {"read"})
994     public void readNonExistent(String testName) {
995
996         // Perform setup.
997         setupReadNonExistent(testName);
998
999         // Submit the request to the service and store the response.
1000         PersonAuthorityClient client = new PersonAuthorityClient();
1001         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
1002         try {
1003                 int statusCode = res.getStatus();
1004                 // Check the status code of the response: does it match
1005                 // the expected response(s)?
1006                 if(logger.isDebugEnabled()){
1007                     logger.debug(testName + ": status = " + statusCode);
1008                 }
1009                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1010                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1011                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1012         } finally {
1013                 res.releaseConnection();
1014         }
1015     }
1016
1017     /**
1018      * Read item non existent.
1019      *
1020      * @param testName the test name
1021      */
1022     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1023         groups = {"read"}, dependsOnMethods = {"readItem"})
1024     public void readItemNonExistent(String testName) {
1025
1026         // Perform setup.
1027         setupReadNonExistent(testName);
1028
1029         // Submit the request to the service and store the response.
1030         PersonAuthorityClient client = new PersonAuthorityClient();
1031         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
1032         try {
1033                 int statusCode = res.getStatus();
1034         
1035                 // Check the status code of the response: does it match
1036                 // the expected response(s)?
1037                 if(logger.isDebugEnabled()){
1038                     logger.debug(testName + ": status = " + statusCode);
1039                 }
1040                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1041                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1042                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1043         } finally {
1044                 res.releaseConnection();
1045         }
1046     }
1047
1048     /**
1049      * Read contact non existent.
1050      *
1051      * @param testName the test name
1052      */
1053     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1054         groups = {"read"}, dependsOnMethods = {"readContact"})
1055     public void readContactNonExistent(String testName) {
1056
1057         // Perform setup.
1058         setupReadNonExistent(testName);
1059
1060         // Submit the request to the service and store the response.
1061         PersonAuthorityClient client = new PersonAuthorityClient();
1062         ClientResponse<MultipartInput> res =
1063             client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1064         try {
1065                 int statusCode = res.getStatus();
1066         
1067                 // Check the status code of the response: does it match
1068                 // the expected response(s)?
1069                 if(logger.isDebugEnabled()){
1070                     logger.debug(testName + ": status = " + statusCode);
1071                 }
1072                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1073                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1074                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1075         } finally {
1076                 res.releaseConnection();
1077         }
1078     }
1079
1080     // ---------------------------------------------------------------
1081     // CRUD tests : READ_LIST tests
1082     // ---------------------------------------------------------------
1083     // Success outcomes
1084
1085     /* (non-Javadoc)
1086      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
1087      */
1088     @Override
1089     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1090         groups = {"readList"}, dependsOnGroups = {"createList", "read"})
1091     public void readList(String testName) throws Exception {
1092
1093         // Perform setup.
1094         setupReadList(testName);
1095
1096         // Submit the request to the service and store the response.
1097         PersonAuthorityClient client = new PersonAuthorityClient();
1098         PersonauthoritiesCommonList list = null;
1099         ClientResponse<PersonauthoritiesCommonList> res = client.readList();
1100         try {
1101                 int statusCode = res.getStatus();
1102         
1103                 // Check the status code of the response: does it match
1104                 // the expected response(s)?
1105                 if(logger.isDebugEnabled()){
1106                     logger.debug(testName + ": status = " + statusCode);
1107                 }
1108                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1109                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1110                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1111         
1112                 list = res.getEntity();
1113         } finally {
1114                 res.releaseConnection();
1115         }
1116
1117         // Optionally output additional data about list members for debugging.
1118         boolean iterateThroughList = false;
1119         if (iterateThroughList && logger.isDebugEnabled()) {
1120             List<PersonauthoritiesCommonList.PersonauthorityListItem> items =
1121                     list.getPersonauthorityListItem();
1122             int i = 0;
1123             for (PersonauthoritiesCommonList.PersonauthorityListItem item : items) {
1124                 String csid = item.getCsid();
1125                 logger.debug(testName + ": list-item[" + i + "] csid=" +
1126                         csid);
1127                 logger.debug(testName + ": list-item[" + i + "] displayName=" +
1128                         item.getDisplayName());
1129                 logger.debug(testName + ": list-item[" + i + "] URI=" +
1130                         item.getUri());
1131                 readItemList(csid, null);
1132                 i++;
1133             }
1134         }
1135     }
1136
1137     /**
1138      * Read item list.
1139      */
1140     @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
1141     public void readItemList() {
1142         readItemList(knownResourceId, null);
1143     }
1144
1145     /**
1146      * Read item list by authority name.
1147      */
1148     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
1149     public void readItemListByAuthorityName() {
1150         readItemList(null, knownResourceDisplayName);
1151     }
1152     
1153     /**
1154      * Read item list.
1155      *
1156      * @param vcsid the vcsid
1157      * @param name the name
1158      */
1159     private void readItemList(String vcsid, String name) {
1160
1161         final String testName = "readItemList";
1162
1163         // Perform setup.
1164         setupReadList(testName);
1165         
1166         // Submit the request to the service and store the response.
1167         PersonAuthorityClient client = new PersonAuthorityClient();
1168         ClientResponse<PersonsCommonList> res = null;
1169         if (vcsid!= null) {
1170                 res = client.readItemList(vcsid);
1171         } else if (name!= null) {
1172                 res = client.readItemListForNamedAuthority(name);
1173         } else {
1174                 Assert.fail("readItemList passed null csid and name!");
1175         }
1176         PersonsCommonList list = null;
1177         try {
1178                 int statusCode = res.getStatus();
1179         
1180                 // Check the status code of the response: does it match
1181                 // the expected response(s)?
1182                 if(logger.isDebugEnabled()){
1183                     logger.debug(testName + ": status = " + statusCode);
1184                 }
1185                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1186                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1187                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1188
1189                 list = res.getEntity();
1190         } finally {
1191                 res.releaseConnection();
1192         }
1193
1194         List<PersonsCommonList.PersonListItem> items =
1195             list.getPersonListItem();
1196         int nItemsReturned = items.size();
1197         // There will be one item created, associated with a
1198         // known parent resource, by the createItem test.
1199         //
1200         // In addition, there will be 'nItemsToCreateInList'
1201         // additional items created by the createItemList test,
1202         // all associated with the same parent resource.
1203         int nExpectedItems = nItemsToCreateInList + 1;
1204         if(logger.isDebugEnabled()){
1205             logger.debug(testName + ": Expected "
1206                         + nExpectedItems +" items; got: "+nItemsReturned);
1207         }
1208         Assert.assertEquals(nItemsReturned, nExpectedItems);
1209
1210         int i = 0;
1211         for (PersonsCommonList.PersonListItem item : items) {
1212                 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
1213                 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
1214                 // Optionally output additional data about list members for debugging.
1215                 boolean showDetails = true;
1216                 if (showDetails && logger.isDebugEnabled()) {
1217                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
1218                         item.getCsid());
1219                 logger.debug("  " + testName + ": list-item[" + i + "] refName=" +
1220                         item.getRefName());
1221                 logger.debug("  " + testName + ": list-item[" + i + "] displayName=" +
1222                         item.getDisplayName());
1223                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
1224                         item.getUri());
1225             }
1226             i++;
1227         }
1228     }
1229
1230     /**
1231      * Read contact list.
1232      */
1233     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
1234     public void readContactList() {
1235         readContactList(knownResourceId, knownItemResourceId);
1236     }
1237
1238     /**
1239      * Read contact list.
1240      *
1241      * @param parentcsid the parentcsid
1242      * @param itemcsid the itemcsid
1243      */
1244     private void readContactList(String parentcsid, String itemcsid) {
1245         final String testName = "readContactList";
1246
1247         // Perform setup.
1248         setupReadList(testName);
1249
1250         // Submit the request to the service and store the response.
1251         PersonAuthorityClient client = new PersonAuthorityClient();
1252         ContactsCommonList list = null;
1253         ClientResponse<ContactsCommonList> res =
1254                 client.readContactList(parentcsid, itemcsid);
1255         try {
1256                 int statusCode = res.getStatus();
1257         
1258                 // Check the status code of the response: does it match
1259                 // the expected response(s)?
1260                 if(logger.isDebugEnabled()){
1261                     logger.debug(testName + ": status = " + statusCode);
1262                 }
1263                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1264                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1265                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1266         
1267                 list = res.getEntity();
1268         } finally {
1269                 res.releaseConnection();
1270         }
1271         
1272         List<ContactsCommonList.ContactListItem> listitems =
1273             list.getContactListItem();
1274         int nItemsReturned = listitems.size();
1275         // There will be one item created, associated with a
1276         // known parent resource, by the createItem test.
1277         //
1278         // In addition, there will be 'nItemsToCreateInList'
1279         // additional items created by the createItemList test,
1280         // all associated with the same parent resource.
1281         int nExpectedItems = nItemsToCreateInList + 1;
1282         if(logger.isDebugEnabled()){
1283             logger.debug(testName + ": Expected "
1284                         + nExpectedItems +" items; got: "+nItemsReturned);
1285         }
1286         Assert.assertEquals(nItemsReturned, nExpectedItems);
1287
1288         int i = 0;
1289         for (ContactsCommonList.ContactListItem listitem : listitems) {
1290                 // Optionally output additional data about list members for debugging.
1291                 boolean showDetails = false;
1292                 if (showDetails && logger.isDebugEnabled()) {
1293                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
1294                         listitem.getCsid());
1295                 logger.debug("  " + testName + ": list-item[" + i + "] addressPlace=" +
1296                         listitem.getAddressPlace());
1297                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
1298                         listitem.getUri());
1299             }
1300             i++;
1301         }
1302     }
1303
1304     // Failure outcomes
1305
1306     // There are no failure outcome tests at present.
1307
1308     // ---------------------------------------------------------------
1309     // CRUD tests : READ_LIST tests by partial term match.
1310     // ---------------------------------------------------------------
1311
1312     // Success outcomes
1313
1314     /**
1315      * Read item list by partial term.
1316      */
1317     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1318         groups = {"readListByPartialTerm"}, dependsOnGroups = {"readList"})
1319     public void readItemListByPartialTerm(String testName) {
1320         int numMatchesFound = 0;
1321         final String PARTIAL_TERM = TEST_PARTIAL_TERM_FORE_NAME;
1322         // setupCreate(testName);
1323         String newID = createItemInAuthorityForPartialTermMatch(knownResourceId, knownResourceRefName);
1324         numMatchesFound =
1325             readItemListByPartialTerm(testName, knownResourceId, PARTIAL_TERM);
1326         final int NUM_MATCHES_EXPECTED = 1;
1327         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED);
1328     }
1329
1330     /**
1331      * Read item list by partial term, where the case of at
1332      * least one character of the partial term doesn't match that
1333      * of the full term expected to be matched.
1334      */
1335     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1336         groups = {"readListByPartialTerm"}, dependsOnMethods = {"readItemListByPartialTerm"})
1337     public void readItemListByPartialTermNonMatchingCase(String testName) {
1338
1339         setupReadList(testName);
1340         int numMatchesFound = 0;
1341         final int NUM_MATCHES_EXPECTED = 1;
1342
1343         final String PARTIAL_TERM_LOWERCASE = TEST_PARTIAL_TERM_FORE_NAME.toLowerCase();
1344         numMatchesFound =
1345             readItemListByPartialTerm(testName, knownResourceId, PARTIAL_TERM_LOWERCASE);
1346         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED);
1347
1348         final String PARTIAL_TERM_UPPERCASE = TEST_PARTIAL_TERM_FORE_NAME.toUpperCase();
1349         numMatchesFound =
1350             readItemListByPartialTerm(testName, knownResourceId, PARTIAL_TERM_UPPERCASE);
1351         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED);
1352
1353     }
1354
1355     /**
1356      * Read item list by partial term, with at least one Unicode UTF-8 character
1357      * (outside the USASCII range) in the partial term.
1358      */
1359     // FIXME: Test currently fails with a true UTF-8 String - need to investigate why.
1360     // Will be commented out for now until we get this working ...
1361 /*
1362     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1363         groups = {"readListByPartialTerm"}, dependsOnMethods = {"readItemListByPartialTerm"})
1364     public void readItemListByPartialTermUTF8(String testName) {
1365         int numMatchesFound = 0;
1366         final String PARTIAL_TERM_UTF8 = TEST_PARTIAL_TERM_SUR_NAME;
1367         if (logger.isDebugEnabled()) {
1368             logger.debug("Attempting match on partial term '" + PARTIAL_TERM_UTF8 + "' ...");
1369         }
1370         numMatchesFound =
1371             readItemListByPartialTerm(testName, knownResourceId, PARTIAL_TERM_UTF8);
1372         if (logger.isDebugEnabled()) {
1373             logger.debug("Found " + numMatchesFound + " match(es).");
1374         }
1375         final int NUM_MATCHES_EXPECTED = 1;
1376         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED);
1377     }
1378 */
1379
1380     // Failure outcomes
1381
1382     /**
1383      * Read item list by partial term, where the partial term is not
1384      * expected to be matched by any term in any resource.
1385      */
1386     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1387         groups = {"readListByPartialTerm"}, dependsOnMethods = {"readItemListByPartialTerm"})
1388     public void readItemListByNonexistentPartialTerm(String testName) {
1389         int numMatchesFound = 0;
1390         final String NON_EXISTENT_PARTIAL_TERM = "jlmbsoq";
1391         numMatchesFound =
1392             readItemListByPartialTerm(testName, knownResourceId, NON_EXISTENT_PARTIAL_TERM);
1393         final int NUM_MATCHES_EXPECTED = 0;
1394         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED);
1395
1396     }
1397
1398
1399     /**
1400      * Read item list by partial term.
1401      *
1402      * @param testName The name of the test which has invoked this method.
1403      * @param vcsid The CSID of the authority within which partial term matching
1404      *     will be performed.
1405      * @param partialTerm A partial term to match item resources.
1406      * @return The number of item resources matched by the partial term.
1407      */
1408     private int readItemListByPartialTerm(String testName, String vcsid, String partialTerm) {
1409
1410         // Perform setup.
1411         setupReadList(testName);
1412
1413         // Submit the request to the service and store the response.
1414         PersonAuthorityClient client = new PersonAuthorityClient();
1415         ClientResponse<PersonsCommonList> res = null;
1416         if (vcsid != null) {
1417             res = client.readItemList(vcsid, partialTerm);
1418         } else {
1419             Assert.fail("readItemListByPartialTerm passed null csid!");
1420         }
1421         PersonsCommonList list = null;
1422         try {
1423             int statusCode = res.getStatus();
1424
1425             // Check the status code of the response: does it match
1426             // the expected response(s)?
1427             if(logger.isDebugEnabled()){
1428                 logger.debug(testName + ": status = " + statusCode);
1429             }
1430             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1431                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1432             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1433
1434             list = res.getEntity();
1435         } finally {
1436             res.releaseConnection();
1437         }
1438
1439         List<PersonsCommonList.PersonListItem> items = list.getPersonListItem();
1440         int nItemsReturned = items.size();
1441
1442         return nItemsReturned;
1443     }
1444
1445     // ---------------------------------------------------------------
1446     // CRUD tests : UPDATE tests
1447     // ---------------------------------------------------------------
1448     // Success outcomes
1449     /* (non-Javadoc)
1450      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
1451      */
1452     @Override
1453     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1454         groups = {"update"}, dependsOnGroups = {"read", "readList", "readListByPartialTerm"})
1455     public void update(String testName) throws Exception {
1456
1457         // Perform setup.
1458         setupUpdate(testName);
1459
1460         // Retrieve the contents of a resource to update.
1461         PersonAuthorityClient client = new PersonAuthorityClient();
1462         MultipartInput input = null;
1463         ClientResponse<MultipartInput> res = client.read(knownResourceId);
1464         try {
1465                 if(logger.isDebugEnabled()){
1466                     logger.debug(testName + ": read status = " + res.getStatus());
1467                 }
1468                 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1469         
1470                 if(logger.isDebugEnabled()){
1471                     logger.debug("got PersonAuthority to update with ID: " + knownResourceId);
1472                 }
1473                 input = res.getEntity();
1474         } finally {
1475                 res.releaseConnection();
1476         }
1477         
1478         PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
1479                 client.getCommonPartName(), PersonauthoritiesCommon.class);
1480         Assert.assertNotNull(personAuthority);
1481
1482         // Update the contents of this resource.
1483         personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
1484         personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
1485         if(logger.isDebugEnabled()){
1486             logger.debug("to be updated PersonAuthority");
1487             logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
1488         }
1489
1490         // Submit the updated resource to the service and store the response.
1491         MultipartOutput output = new MultipartOutput();
1492         OutputPart commonPart = output.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);
1493         commonPart.getHeaders().add("label", client.getCommonPartName());
1494         res = client.update(knownResourceId, output);
1495         try {
1496                 int statusCode = res.getStatus();
1497         
1498                 // Check the status code of the response: does it match the expected response(s)?
1499                 if(logger.isDebugEnabled()){
1500                     logger.debug(testName + ": status = " + statusCode);
1501                 }
1502                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1503                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1504                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1505         
1506                 // Retrieve the updated resource and verify that its contents exist.
1507                 input = (MultipartInput) res.getEntity();
1508         } finally {
1509                 res.releaseConnection();
1510         }
1511         
1512         PersonauthoritiesCommon updatedPersonAuthority =
1513                 (PersonauthoritiesCommon) extractPart(input,
1514                         client.getCommonPartName(), PersonauthoritiesCommon.class);
1515         Assert.assertNotNull(updatedPersonAuthority);
1516
1517         // Verify that the updated resource received the correct data.
1518         Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
1519                 personAuthority.getDisplayName(),
1520                 "Data in updated object did not match submitted data.");
1521     }
1522
1523     /**
1524      * Update item.
1525      *
1526      * @param testName the test name
1527      * @throws Exception the exception
1528      */
1529     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1530         groups = {"update"}, dependsOnMethods = {"update"})
1531     public void updateItem(String testName) throws Exception {
1532
1533         // Perform setup.
1534         setupUpdate(testName);
1535
1536         // Retrieve the contents of a resource to update.
1537         PersonAuthorityClient client = new PersonAuthorityClient();
1538         MultipartInput input = null;
1539         ClientResponse<MultipartInput> res =
1540                 client.readItem(knownResourceId, knownItemResourceId);
1541         try {
1542                 if(logger.isDebugEnabled()){
1543                     logger.debug(testName + ": read status = " + res.getStatus());
1544                 }
1545                 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1546         
1547                 if(logger.isDebugEnabled()){
1548                     logger.debug("got Person to update with ID: " +
1549                         knownItemResourceId +
1550                         " in PersonAuthority: " + knownResourceId );
1551                 }
1552                 input = res.getEntity();
1553         } finally {
1554                 res.releaseConnection();
1555         }
1556         
1557         PersonsCommon person = (PersonsCommon) extractPart(input,
1558                 client.getItemCommonPartName(), PersonsCommon.class);
1559         Assert.assertNotNull(person);
1560
1561         // Update the contents of this resource.
1562         person.setCsid(null);
1563         person.setForeName("updated-" + person.getForeName());
1564         if(logger.isDebugEnabled()){
1565             logger.debug("to be updated Person");
1566             logger.debug(objectAsXmlString(person,
1567                 PersonsCommon.class));
1568         }        
1569
1570         // Submit the updated resource to the service and store the response.
1571         MultipartOutput output = new MultipartOutput();
1572         OutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
1573         commonPart.getHeaders().add("label", client.getItemCommonPartName());
1574         res = client.updateItem(knownResourceId, knownItemResourceId, output);
1575         try {
1576                 int statusCode = res.getStatus();
1577         
1578                 // Check the status code of the response: does it match the expected response(s)?
1579                 if(logger.isDebugEnabled()){
1580                     logger.debug(testName + ": status = " + statusCode);
1581                 }
1582                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1583                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1584                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1585         
1586                 // Retrieve the updated resource and verify that its contents exist.
1587                 input = (MultipartInput) res.getEntity();
1588         } finally {
1589                 res.releaseConnection();
1590         }
1591         
1592         PersonsCommon updatedPerson =
1593                 (PersonsCommon) extractPart(input,
1594                         client.getItemCommonPartName(), PersonsCommon.class);
1595         Assert.assertNotNull(updatedPerson);
1596
1597         // Verify that the updated resource received the correct data.
1598         Assert.assertEquals(updatedPerson.getForeName(),
1599                 person.getForeName(),
1600                 "Data in updated Person did not match submitted data.");
1601     }
1602
1603     /**
1604      * Update contact.
1605      *
1606      * @param testName the test name
1607      * @throws Exception the exception
1608      */
1609     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1610         groups = {"update"}, dependsOnMethods = {"updateItem"})
1611     public void updateContact(String testName) throws Exception {
1612         // Perform setup.
1613         setupUpdate(testName);
1614
1615         // Retrieve the contents of a resource to update.
1616         PersonAuthorityClient client = new PersonAuthorityClient();
1617         MultipartInput input = null;
1618         ClientResponse<MultipartInput> res =
1619                 client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1620         try {
1621                 if(logger.isDebugEnabled()){
1622                     logger.debug(testName + ": read status = " + res.getStatus());
1623                 }
1624                 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1625         
1626                 if(logger.isDebugEnabled()){
1627                     logger.debug("got Contact to update with ID: " +
1628                         knownContactResourceId +
1629                         " in item: " + knownItemResourceId +
1630                         " in parent: " + knownResourceId );
1631                 }
1632                 input = res.getEntity();
1633         } finally {
1634                 res.releaseConnection();
1635         }
1636         
1637         ContactsCommon contact = (ContactsCommon) extractPart(input,
1638                 new ContactClient().getCommonPartName(), ContactsCommon.class);
1639         Assert.assertNotNull(contact);
1640
1641         // Update the contents of this resource.
1642         contact.setAddressPlace("updated-" + contact.getAddressPlace());
1643         if(logger.isDebugEnabled()){
1644             logger.debug("to be updated Contact");
1645             logger.debug(objectAsXmlString(contact,
1646                 ContactsCommon.class));
1647         }
1648
1649         // Submit the updated resource to the service and store the response.
1650         MultipartOutput output = new MultipartOutput();
1651         OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
1652         commonPart.getHeaders().add("label", new ContactClient().getCommonPartName());
1653         res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1654         try {
1655                 int statusCode = res.getStatus();
1656         
1657                 // Check the status code of the response: does it match the expected response(s)?
1658                 if(logger.isDebugEnabled()){
1659                     logger.debug(testName + ": status = " + statusCode);
1660                 }
1661                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1662                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1663                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1664         
1665                 // Retrieve the updated resource and verify that its contents exist.
1666                 input = (MultipartInput) res.getEntity();
1667         } finally {
1668                 res.releaseConnection();
1669         }
1670         ContactsCommon updatedContact =
1671                 (ContactsCommon) extractPart(input,
1672                         new ContactClient().getCommonPartName(), ContactsCommon.class);
1673         Assert.assertNotNull(updatedContact);
1674
1675         // Verify that the updated resource received the correct data.
1676         Assert.assertEquals(updatedContact.getAddressPlace(),
1677                 contact.getAddressPlace(),
1678                 "Data in updated Contact did not match submitted data.");
1679     }
1680
1681     // Failure outcomes
1682     // Placeholders until the three tests below can be uncommented.
1683     // See Issue CSPACE-401.
1684     /* (non-Javadoc)
1685      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
1686      */
1687     @Override
1688     public void updateWithEmptyEntityBody(String testName) throws Exception {
1689         //Should this really be empty?
1690     }
1691
1692     /* (non-Javadoc)
1693      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
1694      */
1695     @Override
1696     public void updateWithMalformedXml(String testName) throws Exception {
1697         //Should this really be empty?
1698     }
1699
1700     /* (non-Javadoc)
1701      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
1702      */
1703     @Override
1704     public void updateWithWrongXmlSchema(String testName) throws Exception {
1705         //Should this really be empty?
1706     }
1707
1708 /*
1709     @Override
1710     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1711         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1712     public void updateWithEmptyEntityBody(String testName) throws Exception {
1713
1714     // Perform setup.
1715     setupUpdateWithEmptyEntityBody(testName);
1716
1717     // Submit the request to the service and store the response.
1718     String method = REQUEST_TYPE.httpMethodName();
1719     String url = getResourceURL(knownResourceId);
1720     String mediaType = MediaType.APPLICATION_XML;
1721     final String entity = "";
1722     int statusCode = submitRequest(method, url, mediaType, entity);
1723
1724     // Check the status code of the response: does it match
1725     // the expected response(s)?
1726     if(logger.isDebugEnabled()){
1727         logger.debug(testName + ": url=" + url +
1728             " status=" + statusCode);
1729      }
1730     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1731     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1732     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1733     }
1734
1735     @Override
1736     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1737         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1738     public void updateWithMalformedXml(String testName) throws Exception {
1739
1740     // Perform setup.
1741     setupUpdateWithMalformedXml(testName);
1742
1743     // Submit the request to the service and store the response.
1744     String method = REQUEST_TYPE.httpMethodName();
1745     String url = getResourceURL(knownResourceId);
1746     String mediaType = MediaType.APPLICATION_XML;
1747     final String entity = MALFORMED_XML_DATA;
1748     int statusCode = submitRequest(method, url, mediaType, entity);
1749
1750     // Check the status code of the response: does it match
1751     // the expected response(s)?
1752     if(logger.isDebugEnabled()){
1753         logger.debug(testName + ": url=" + url +
1754            " status=" + statusCode);
1755      }
1756     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1757     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1758     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1759     }
1760
1761     @Override
1762     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1763         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1764     public void updateWithWrongXmlSchema(String testName) throws Exception {
1765
1766     // Perform setup.
1767     setupUpdateWithWrongXmlSchema(testName);
1768
1769     // Submit the request to the service and store the response.
1770     String method = REQUEST_TYPE.httpMethodName();
1771     String url = getResourceURL(knownResourceId);
1772     String mediaType = MediaType.APPLICATION_XML;
1773     final String entity = WRONG_XML_SCHEMA_DATA;
1774     int statusCode = submitRequest(method, url, mediaType, entity);
1775
1776     // Check the status code of the response: does it match
1777     // the expected response(s)?
1778     if(logger.isDebugEnabled()){
1779         logger.debug("updateWithWrongXmlSchema: url=" + url +
1780             " status=" + statusCode);
1781      }
1782     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1783     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1784     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1785     }
1786 */
1787
1788     /* (non-Javadoc)
1789  * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
1790  */
1791 @Override
1792     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1793         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1794     public void updateNonExistent(String testName) throws Exception {
1795
1796         // Perform setup.
1797         setupUpdateNonExistent(testName);
1798
1799         // Submit the request to the service and store the response.
1800         // Note: The ID(s) used when creating the request payload may be arbitrary.
1801         // The only relevant ID may be the one used in update(), below.
1802         PersonAuthorityClient client = new PersonAuthorityClient();
1803         String displayName = "displayName-NON_EXISTENT_ID";
1804         String fullRefName = PersonAuthorityClientUtils.createPersonAuthRefName(displayName, true);
1805         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
1806                                 displayName, fullRefName, client.getCommonPartName());
1807         ClientResponse<MultipartInput> res =
1808                 client.update(NON_EXISTENT_ID, multipart);
1809         try {
1810                 int statusCode = res.getStatus();
1811         
1812                 // Check the status code of the response: does it match
1813                 // the expected response(s)?
1814                 if(logger.isDebugEnabled()){
1815                     logger.debug(testName + ": status = " + statusCode);
1816                 }
1817                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1818                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1819                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1820         } finally {
1821                 res.releaseConnection();
1822         }
1823     }
1824
1825     /**
1826      * Update non existent item.
1827      *
1828      * @param testName the test name
1829      * @throws Exception the exception
1830      */
1831     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1832         groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1833     public void updateNonExistentItem(String testName) throws Exception {
1834
1835         // Perform setup.
1836         setupUpdateNonExistent(testName);
1837
1838         // Submit the request to the service and store the response.
1839         // Note: The ID used in this 'create' call may be arbitrary.
1840         // The only relevant ID may be the one used in update(), below.
1841         PersonAuthorityClient client = new PersonAuthorityClient();
1842         Map<String, String> nonexMap = new HashMap<String,String>();
1843         nonexMap.put(PersonJAXBSchema.FORE_NAME, "John");
1844         nonexMap.put(PersonJAXBSchema.SUR_NAME, "Wayne");
1845         nonexMap.put(PersonJAXBSchema.GENDER, "male");
1846         MultipartOutput multipart = 
1847         PersonAuthorityClientUtils.createPersonInstance(NON_EXISTENT_ID, 
1848                         PersonAuthorityClientUtils.createPersonRefName(NON_EXISTENT_ID, NON_EXISTENT_ID, true), nonexMap,
1849                         client.getItemCommonPartName() );
1850         ClientResponse<MultipartInput> res =
1851                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1852         try {
1853                 int statusCode = res.getStatus();
1854         
1855                 // Check the status code of the response: does it match
1856                 // the expected response(s)?
1857                 if(logger.isDebugEnabled()){
1858                     logger.debug(testName + ": status = " + statusCode);
1859                 }
1860                 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1861                         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1862                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1863         } finally {
1864                 res.releaseConnection();
1865         }
1866     }
1867
1868     /**
1869      * Update non existent contact.
1870      *
1871      * @param testName the test name
1872      * @throws Exception the exception
1873      */
1874     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1875         groups = {"update"}, dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1876     public void updateNonExistentContact(String testName) throws Exception {
1877         // Currently a no-op test
1878     }
1879
1880     // ---------------------------------------------------------------
1881     // CRUD tests : DELETE tests
1882     // ---------------------------------------------------------------
1883     // Success outcomes
1884
1885     // Note: delete sub-resources in ascending hierarchical order,
1886     // before deleting their parents.
1887
1888     /**
1889      * Delete contact.
1890      *
1891      * @param testName the test name
1892      * @throws Exception the exception
1893      */
1894     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, 
1895         groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "readListByPartialTerm", "update"})
1896     public void deleteContact(String testName) throws Exception {
1897
1898         // Perform setup.
1899         setupDelete(testName);
1900
1901          if(logger.isDebugEnabled()){
1902             logger.debug("parentcsid =" + knownResourceId +
1903                 " itemcsid = " + knownItemResourceId +
1904                 " csid = " + knownContactResourceId);
1905         }
1906
1907         // Submit the request to the service and store the response.
1908         PersonAuthorityClient client = new PersonAuthorityClient();
1909         ClientResponse<Response> res =
1910             client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1911         int statusCode = res.getStatus();
1912         res.releaseConnection();
1913
1914         // Check the status code of the response: does it match
1915         // the expected response(s)?
1916         if(logger.isDebugEnabled()){
1917             logger.debug(testName + ": status = " + statusCode);
1918         }
1919         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1920                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1921         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1922     }
1923
1924    /**
1925     * Delete item.
1926     *
1927     * @param testName the test name
1928     * @throws Exception the exception
1929     */
1930    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1931         groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1932     public void deleteItem(String testName) throws Exception {
1933
1934         // Perform setup.
1935         setupDelete(testName);
1936
1937         if(logger.isDebugEnabled()){
1938             logger.debug("parentcsid =" + knownResourceId +
1939                 " itemcsid = " + knownItemResourceId);
1940         }
1941
1942         // Submit the request to the service and store the response.
1943         PersonAuthorityClient client = new PersonAuthorityClient();
1944         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1945         int statusCode = res.getStatus();
1946         res.releaseConnection();
1947
1948         // Check the status code of the response: does it match
1949         // the expected response(s)?
1950         if(logger.isDebugEnabled()){
1951             logger.debug(testName + ": status = " + statusCode);
1952         }
1953         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1954                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1955         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1956     }
1957
1958     /* (non-Javadoc)
1959      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
1960      */
1961     @Override
1962     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1963         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1964     public void delete(String testName) throws Exception {
1965
1966         // Perform setup.
1967         setupDelete(testName);
1968
1969         if(logger.isDebugEnabled()){
1970             logger.debug("parentcsid =" + knownResourceId);
1971         }
1972
1973         // Submit the request to the service and store the response.
1974         PersonAuthorityClient client = new PersonAuthorityClient();
1975         ClientResponse<Response> res = client.delete(knownResourceId);
1976         int statusCode = res.getStatus();
1977         res.releaseConnection();
1978
1979         // Check the status code of the response: does it match
1980         // the expected response(s)?
1981         if(logger.isDebugEnabled()){
1982             logger.debug(testName + ": status = " + statusCode);
1983         }
1984         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1985                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1986         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1987     }
1988
1989     // Failure outcomes
1990     /* (non-Javadoc)
1991      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
1992      */
1993     @Override
1994     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1995         groups = {"delete"}, dependsOnMethods = {"delete"})
1996     public void deleteNonExistent(String testName) throws Exception {
1997
1998         // Perform setup.
1999         setupDeleteNonExistent(testName);
2000
2001         // Submit the request to the service and store the response.
2002         PersonAuthorityClient client = new PersonAuthorityClient();
2003         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
2004         int statusCode = res.getStatus();
2005         res.releaseConnection();
2006
2007         // Check the status code of the response: does it match
2008         // the expected response(s)?
2009         if(logger.isDebugEnabled()){
2010             logger.debug(testName + ": status = " + statusCode);
2011         }
2012         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
2013                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
2014         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
2015     }
2016
2017     /**
2018      * Delete non existent item.
2019      *
2020      * @param testName the test name
2021      */
2022     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
2023         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
2024     public void deleteNonExistentItem(String testName) {
2025
2026         // Perform setup.
2027         setupDeleteNonExistent(testName);
2028
2029         // Submit the request to the service and store the response.
2030         PersonAuthorityClient client = new PersonAuthorityClient();
2031         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
2032         int statusCode = res.getStatus();
2033         res.releaseConnection();
2034
2035         // Check the status code of the response: does it match
2036         // the expected response(s)?
2037         if(logger.isDebugEnabled()){
2038             logger.debug(testName + ": status = " + statusCode);
2039         }
2040         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
2041                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
2042         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
2043     }
2044
2045     /**
2046      * Delete non existent contact.
2047      *
2048      * @param testName the test name
2049      */
2050     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
2051         groups = {"delete"}, dependsOnMethods = {"deleteContact"})
2052     public void deleteNonExistentContact(String testName) {
2053
2054         // Perform setup.
2055         setupDeleteNonExistent(testName);
2056
2057         // Submit the request to the service and store the response.
2058         PersonAuthorityClient client = new PersonAuthorityClient();
2059         ClientResponse<Response> res =
2060             client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
2061         int statusCode = res.getStatus();
2062         res.releaseConnection();
2063
2064         // Check the status code of the response: does it match
2065         // the expected response(s)?
2066         if(logger.isDebugEnabled()){
2067             logger.debug(testName + ": status = " + statusCode);
2068         }
2069         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
2070                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
2071         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
2072     }
2073
2074     // ---------------------------------------------------------------
2075     // Utility tests : tests of code used in tests above
2076     // ---------------------------------------------------------------
2077     /**
2078      * Tests the code for manually submitting data that is used by several
2079      * of the methods above.
2080      */
2081     @Test(dependsOnMethods = {"create", "read"})
2082     public void testSubmitRequest() {
2083
2084         // Expected status code: 200 OK
2085         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
2086
2087         // Submit the request to the service and store the response.
2088         String method = ServiceRequestType.READ.httpMethodName();
2089         String url = getResourceURL(knownResourceId);
2090         int statusCode = submitRequest(method, url);
2091
2092         // Check the status code of the response: does it match
2093         // the expected response(s)?
2094         if(logger.isDebugEnabled()){
2095             logger.debug("testSubmitRequest: url=" + url +
2096                 " status=" + statusCode);
2097         }
2098         Assert.assertEquals(statusCode, EXPECTED_STATUS);
2099
2100     }
2101
2102     /**
2103      * Test item submit request.
2104      */
2105     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
2106     public void testItemSubmitRequest() {
2107
2108         // Expected status code: 200 OK
2109         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
2110
2111         // Submit the request to the service and store the response.
2112         String method = ServiceRequestType.READ.httpMethodName();
2113         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
2114         int statusCode = submitRequest(method, url);
2115
2116         // Check the status code of the response: does it match
2117         // the expected response(s)?
2118         if(logger.isDebugEnabled()){
2119             logger.debug("testItemSubmitRequest: url=" + url +
2120                 " status=" + statusCode);
2121         }
2122         Assert.assertEquals(statusCode, EXPECTED_STATUS);
2123
2124     }
2125
2126     /**
2127      * Test contact submit request.
2128      */
2129     @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
2130     public void testContactSubmitRequest() {
2131
2132         // Expected status code: 200 OK
2133         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
2134
2135         // Submit the request to the service and store the response.
2136         String method = ServiceRequestType.READ.httpMethodName();
2137         String url = getContactResourceURL(knownResourceId,
2138             knownItemResourceId, knownContactResourceId);
2139         int statusCode = submitRequest(method, url);
2140
2141         // Check the status code of the response: does it match
2142         // the expected response(s)?
2143         if(logger.isDebugEnabled()){
2144             logger.debug("testItemSubmitRequest: url=" + url +
2145                 " status=" + statusCode);
2146         }
2147         Assert.assertEquals(statusCode, EXPECTED_STATUS);
2148
2149     }
2150
2151
2152     // ---------------------------------------------------------------
2153     // Cleanup of resources created during testing
2154     // ---------------------------------------------------------------
2155     
2156     /**
2157      * Deletes all resources created by tests, after all tests have been run.
2158      *
2159      * This cleanup method will always be run, even if one or more tests fail.
2160      * For this reason, it attempts to remove all resources created
2161      * at any point during testing, even if some of those resources
2162      * may be expected to be deleted by certain tests.
2163      */
2164
2165     @AfterClass(alwaysRun=true)
2166     @Override
2167     public void cleanUp() {
2168         String noTest = System.getProperty("noTestCleanup");
2169         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
2170             if (logger.isDebugEnabled()) {
2171                 logger.debug("Skipping Cleanup phase ...");
2172             }
2173             return;
2174         }
2175         if (logger.isDebugEnabled()) {
2176             logger.debug("Cleaning up temporary resources created for testing ...");
2177         }
2178         String parentResourceId;
2179         String itemResourceId;
2180         String contactResourceId;
2181         // Clean up contact resources.
2182         PersonAuthorityClient client = new PersonAuthorityClient();
2183         parentResourceId = knownResourceId;
2184         for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
2185             contactResourceId = entry.getKey();
2186             itemResourceId = entry.getValue();
2187             // Note: Any non-success responses from the delete operation
2188             // below are ignored and not reported.
2189             ClientResponse<Response> res =
2190                 client.deleteContact(parentResourceId, itemResourceId, contactResourceId);
2191             res.releaseConnection();
2192         }
2193         // Clean up item resources.
2194         for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
2195             itemResourceId = entry.getKey();
2196             parentResourceId = entry.getValue();
2197             // Note: Any non-success responses from the delete operation
2198             // below are ignored and not reported.
2199             ClientResponse<Response> res =
2200                 client.deleteItem(parentResourceId, itemResourceId);
2201             res.releaseConnection();
2202         }
2203         // Clean up parent resources.
2204         super.cleanUp();
2205     }
2206
2207     // ---------------------------------------------------------------
2208     // Utility methods used by tests above
2209     // ---------------------------------------------------------------
2210     /* (non-Javadoc)
2211      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
2212      */
2213     @Override
2214     public String getServicePathComponent() {
2215         return SERVICE_PATH_COMPONENT;
2216     }
2217
2218     /**
2219      * Gets the item service path component.
2220      *
2221      * @return the item service path component
2222      */
2223     public String getItemServicePathComponent() {
2224         return ITEM_SERVICE_PATH_COMPONENT;
2225     }
2226
2227     /**
2228      * Gets the contact service path component.
2229      *
2230      * @return the contact service path component
2231      */
2232     public String getContactServicePathComponent() {
2233         return CONTACT_SERVICE_PATH_COMPONENT;
2234     }
2235
2236     /**
2237      * Returns the root URL for the item service.
2238      *
2239      * This URL consists of a base URL for all services, followed by
2240      * a path component for the owning parent, followed by the
2241      * path component for the items.
2242      *
2243      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2244      * parent authority resource of the relevant item resource.
2245      *
2246      * @return The root URL for the item service.
2247      */
2248     protected String getItemServiceRootURL(String parentResourceIdentifier) {
2249         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
2250     }
2251
2252     /**
2253      * Returns the URL of a specific item resource managed by a service, and
2254      * designated by an identifier (such as a universally unique ID, or UUID).
2255      *
2256      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2257      * parent authority resource of the relevant item resource.
2258      *
2259      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
2260      * item resource.
2261      *
2262      * @return The URL of a specific item resource managed by a service.
2263      */
2264     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
2265         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
2266     }
2267
2268
2269     /**
2270      * Returns the root URL for the contact service.
2271      *
2272      * This URL consists of a base URL for all services, followed by
2273      * a path component for the owning authority, followed by the
2274      * path component for the owning item, followed by the path component
2275      * for the contact service.
2276      *
2277      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2278      * parent authority resource of the relevant item resource.
2279      *
2280      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
2281      * item resource.
2282      *
2283      * @return The root URL for the contact service.
2284      */
2285     protected String getContactServiceRootURL(String parentResourceIdentifier,
2286         String itemResourceIdentifier) {
2287         return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/" +
2288                 getContactServicePathComponent();
2289     }
2290
2291     /**
2292      * Returns the URL of a specific contact resource managed by a service, and
2293      * designated by an identifier (such as a universally unique ID, or UUID).
2294      *
2295      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
2296      * parent resource of the relevant item resource.
2297      *
2298      * @param  resourceIdentifier  An identifier (such as a UUID) for an
2299      * item resource.
2300      *
2301      * @return The URL of a specific resource managed by a service.
2302      */
2303     protected String getContactResourceURL(String parentResourceIdentifier,
2304         String itemResourceIdentifier, String contactResourceIdentifier) {
2305         return getContactServiceRootURL(parentResourceIdentifier,
2306             itemResourceIdentifier) + "/" + contactResourceIdentifier;
2307     }
2308 }