]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b9db66bfd1534c79e6e61d82f065448c03eaf6ff
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c)) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31
32 import org.collectionspace.services.PersonJAXBSchema;
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.person.PersonauthoritiesCommon;
40 import org.collectionspace.services.person.PersonauthoritiesCommonList;
41 import org.collectionspace.services.person.PersonsCommon;
42 import org.collectionspace.services.person.PersonsCommonList;
43 import org.jboss.resteasy.client.ClientResponse;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
46 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.Test;
52
53 /**
54  * PersonAuthorityServiceTest, carries out tests against a
55  * deployed and running PersonAuthority Service.
56  *
57  * $LastChangedRevision: 753 $
58  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
59  */
60 public class PersonAuthorityServiceTest extends AbstractServiceTestImpl {
61
62     private final Logger logger =
63         LoggerFactory.getLogger(PersonAuthorityServiceTest.class);
64
65     // Instance variables specific to this test.
66     final String SERVICE_PATH_COMPONENT = "personauthorities";
67     final String ITEM_SERVICE_PATH_COMPONENT = "items";
68     final String CONTACT_SERVICE_PATH_COMPONENT = "contacts";
69     final String TEST_FORE_NAME = "John";
70     final String TEST_MIDDLE_NAME = null;
71     final String TEST_SUR_NAME = "Wayne";
72     final String TEST_BIRTH_DATE = "May 26, 1907";
73     final String TEST_DEATH_DATE = "June 11, 1979";
74  
75     private String knownResourceId = null;
76     private String knownResourceDisplayName = null;
77     private String knownResourceRefName = null;
78     private String knownItemResourceId = null;
79     private String knownContactResourceId = null;
80     private int nItemsToCreateInList = 3;
81     private List<String> allResourceIdsCreated = new ArrayList<String>();
82     private Map<String, String> allItemResourceIdsCreated =
83         new HashMap<String, String>();
84     private Map<String, String> allContactResourceIdsCreated =
85         new HashMap<String, String>();
86
87     // ---------------------------------------------------------------
88     // CRUD tests : CREATE tests
89     // ---------------------------------------------------------------
90     // Success outcomes
91     @Override
92     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
93         groups = {"create"})
94     public void create(String testName) throws Exception {
95
96         // Perform setup, such as initializing the type of service request
97         // (e.g. CREATE, DELETE), its valid and expected status codes, and
98         // its associated HTTP method name (e.g. POST, DELETE).
99         setupCreate(testName);
100
101         // Submit the request to the service and store the response.
102         PersonAuthorityClient client = new PersonAuthorityClient();
103         String identifier = createIdentifier();
104         String displayName = "displayName-" + identifier;
105         String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(displayName, false);
106         String fullRefName = PersonAuthorityClientUtils.createPersonAuthRefName(displayName, true);
107         MultipartOutput multipart = 
108             PersonAuthorityClientUtils.createPersonAuthorityInstance(
109             displayName, fullRefName, client.getCommonPartName());
110         ClientResponse<Response> res = client.create(multipart);
111         int statusCode = res.getStatus();
112
113         // Check the status code of the response: does it match
114         // the expected response(s)?
115         //
116         // Specifically:
117         // Does it fall within the set of valid status codes?
118         // Does it exactly match the expected status code?
119         if(logger.isDebugEnabled()){
120             logger.debug(testName + ": status = " + statusCode);
121         }
122         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
123                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
124         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
125
126         // Store the refname from the first resource created
127         // for additional tests below.
128         knownResourceRefName = baseRefName;
129
130         String newID = PersonAuthorityClientUtils.extractId(res);
131         // Store the ID returned from the first resource created
132         // for additional tests below.
133         if (knownResourceId == null){
134             knownResourceId = newID;
135             knownResourceDisplayName = displayName;
136             if (logger.isDebugEnabled()) {
137                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
138             }
139         }
140         // Store the IDs from every resource created by tests,
141         // so they can be deleted after tests have been run.
142         allResourceIdsCreated.add(newID);
143     }
144
145     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
146         groups = {"create"}, dependsOnMethods = {"create"})
147     public void createItem(String testName) {
148         setupCreate(testName);
149         String newID = createItemInAuthority(knownResourceId, knownResourceRefName);
150     }
151
152     private String createItemInAuthority(String vcsid, String authRefName) {
153
154         final String testName = "createItemInAuthority";
155         if(logger.isDebugEnabled()){
156             logger.debug(testName + ":...");
157         }
158
159         // Submit the request to the service and store the response.
160         PersonAuthorityClient client = new PersonAuthorityClient();
161         String identifier = createIdentifier();
162         String refName = PersonAuthorityClientUtils.createPersonRefName(authRefName, "John Wayne", true);
163         Map<String, String> johnWayneMap = new HashMap<String,String>();
164         johnWayneMap.put(PersonJAXBSchema.FORE_NAME, TEST_FORE_NAME);
165         johnWayneMap.put(PersonJAXBSchema.SUR_NAME, TEST_SUR_NAME);
166         johnWayneMap.put(PersonJAXBSchema.GENDER, "male");
167         johnWayneMap.put(PersonJAXBSchema.BIRTH_DATE, TEST_BIRTH_DATE);
168         johnWayneMap.put(PersonJAXBSchema.BIRTH_PLACE, "Winterset, Iowa");
169         johnWayneMap.put(PersonJAXBSchema.DEATH_DATE, TEST_DEATH_DATE);
170         johnWayneMap.put(PersonJAXBSchema.BIO_NOTE, "born Marion Robert Morrison and better" +
171             "known by his stage name John Wayne, was an American film actor, director " +
172             "and producer. He epitomized rugged masculinity and has become an enduring " +
173             "American icon. He is famous for his distinctive voice, walk and height. " +
174             "He was also known for his conservative political views and his support in " +
175             "the 1950s for anti-communist positions.");
176         MultipartOutput multipart = 
177             PersonAuthorityClientUtils.createPersonInstance(vcsid, refName, johnWayneMap,
178                 client.getItemCommonPartName() );
179         ClientResponse<Response> res = client.createItem(vcsid, multipart);
180         int statusCode = res.getStatus();
181         String newID = PersonAuthorityClientUtils.extractId(res);
182
183         // Check the status code of the response: does it match
184         // the expected response(s)?
185         if(logger.isDebugEnabled()){
186             logger.debug(testName + ": status = " + statusCode);
187         }
188         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
189                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
190         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
191
192         // Store the ID returned from the first item resource created
193         // for additional tests below.
194         if (knownItemResourceId == null){
195             knownItemResourceId = newID;
196             if (logger.isDebugEnabled()) {
197                 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
198             }
199         }
200
201         // Store the IDs from any item resources created
202         // by tests, along with the IDs of their parents, so these items
203         // can be deleted after all tests have been run.
204         allItemResourceIdsCreated.put(newID, vcsid);
205
206         return newID;
207     }
208
209     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
210         groups = {"create"}, dependsOnMethods = {"createItem"})
211     public void createContact(String testName) {
212         setupCreate(testName);
213         String newID = createContactInItem(knownResourceId, knownItemResourceId);
214     }
215
216    private String createContactInItem(String parentcsid, String itemcsid) {
217
218         final String testName = "createContactInItem";
219         setupCreate(testName);
220         if(logger.isDebugEnabled()){
221             logger.debug(testName + ":...");
222         }
223
224         // Submit the request to the service and store the response.
225         PersonAuthorityClient client = new PersonAuthorityClient();
226         String identifier = createIdentifier();
227         MultipartOutput multipart =
228             ContactClientUtils.createContactInstance(parentcsid,
229             itemcsid, identifier, new ContactClient().getCommonPartName());
230         ClientResponse<Response> res =
231              client.createContact(parentcsid, itemcsid, multipart);
232         int statusCode = res.getStatus();
233         String newID = PersonAuthorityClientUtils.extractId(res);
234
235         // Check the status code of the response: does it match
236         // the expected response(s)?
237         if(logger.isDebugEnabled()){
238             logger.debug(testName + ": status = " + statusCode);
239         }
240         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
241                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
242         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
243
244         // Store the ID returned from the first contact resource created
245         // for additional tests below.
246         if (knownContactResourceId == null){
247             knownContactResourceId = newID;
248             if (logger.isDebugEnabled()) {
249                 logger.debug(testName + ": knownContactResourceId=" + knownContactResourceId);
250             }
251         }
252
253         // Store the IDs from any contact resources created
254         // by tests, along with the IDs of their parent items,
255         // so these items can be deleted after all tests have been run.
256         allContactResourceIdsCreated.put(newID, itemcsid);
257
258         return newID;
259     }
260
261     // Failure outcomes
262
263     // Placeholders until the three tests below can be uncommented.
264     // See Issue CSPACE-401.
265     @Override
266     public void createWithEmptyEntityBody(String testName) throws Exception {
267     }
268
269     @Override
270     public void createWithMalformedXml(String testName) throws Exception {
271     }
272
273     @Override
274     public void createWithWrongXmlSchema(String testName) throws Exception {
275     }
276
277 /*
278     @Override
279     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
280         groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
281     public void createWithEmptyEntityBody(String testName) throws Exception {
282
283     // Perform setup.
284     setupCreateWithEmptyEntityBody(testName);
285
286     // Submit the request to the service and store the response.
287     String method = REQUEST_TYPE.httpMethodName();
288     String url = getServiceRootURL();
289     String mediaType = MediaType.APPLICATION_XML;
290     final String entity = "";
291     int statusCode = submitRequest(method, url, mediaType, entity);
292
293     // Check the status code of the response: does it match
294     // the expected response(s)?
295     if(logger.isDebugEnabled()) {
296         logger.debug(testName + ": url=" + url +
297             " status=" + statusCode);
298      }
299     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
300     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
301     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
302     }
303
304     @Override
305     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
306         groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
307     public void createWithMalformedXml(String testName) throws Exception {
308
309     // Perform setup.
310     setupCreateWithMalformedXml(testName);
311
312     // Submit the request to the service and store the response.
313     String method = REQUEST_TYPE.httpMethodName();
314     String url = getServiceRootURL();
315     String mediaType = MediaType.APPLICATION_XML;
316     final String entity = MALFORMED_XML_DATA; // Constant from base class.
317     int statusCode = submitRequest(method, url, mediaType, entity);
318
319     // Check the status code of the response: does it match
320     // the expected response(s)?
321     if(logger.isDebugEnabled()){
322         logger.debug(testName + ": url=" + url +
323             " status=" + statusCode);
324      }
325     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
326     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
327     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
328     }
329
330     @Override
331     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
332         groups = {"create"}, dependsOnMethods = {"create", "testSubmitRequest"})
333     public void createWithWrongXmlSchema(String testName) throws Exception {
334
335     // Perform setup.
336     setupCreateWithWrongXmlSchema(testName);
337
338     // Submit the request to the service and store the response.
339     String method = REQUEST_TYPE.httpMethodName();
340     String url = getServiceRootURL();
341     String mediaType = MediaType.APPLICATION_XML;
342     final String entity = WRONG_XML_SCHEMA_DATA;
343     int statusCode = submitRequest(method, url, mediaType, entity);
344
345     // Check the status code of the response: does it match
346     // the expected response(s)?
347     if(logger.isDebugEnabled()){
348         logger.debug(testName + ": url=" + url +
349             " status=" + statusCode);
350      }
351     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
352     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
353     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
354     }
355 */
356
357     // ---------------------------------------------------------------
358     // CRUD tests : CREATE LIST tests
359     // ---------------------------------------------------------------
360     // Success outcomes
361     @Override
362     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
363         groups = {"createList"}, dependsOnGroups = {"create"})
364     public void createList(String testName) throws Exception {
365         for (int i = 0; i < nItemsToCreateInList; i++) {
366             create(testName);
367         }
368     }
369
370     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
371         groups = {"createList"}, dependsOnMethods = {"createList"})
372     public void createItemList(String testName) throws Exception {
373         // Add items to the initially-created, known parent record.
374         for (int j = 0; j < nItemsToCreateInList; j++) {
375             createItem(testName);
376         }
377     }
378
379     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
380         groups = {"createList"}, dependsOnMethods = {"createItemList"})
381     public void createContactList(String testName) throws Exception {
382         // Add contacts to the initially-created, known item record.
383         for (int j = 0; j < nItemsToCreateInList; j++) {
384             createContact(testName);
385         }
386     }
387
388     // ---------------------------------------------------------------
389     // CRUD tests : READ tests
390     // ---------------------------------------------------------------
391     // Success outcomes
392     @Override
393     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
394         groups = {"read"}, dependsOnGroups = {"create"})
395     public void read(String testName) throws Exception {
396
397         // Perform setup.
398         setupRead();
399         
400         // Submit the request to the service and store the response.
401         PersonAuthorityClient client = new PersonAuthorityClient();
402         ClientResponse<MultipartInput> res = client.read(knownResourceId);
403         int statusCode = res.getStatus();
404
405         // Check the status code of the response: does it match
406         // the expected response(s)?
407         if(logger.isDebugEnabled()){
408             logger.debug(testName + ": status = " + statusCode);
409         }
410         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
411                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
412         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
413         //FIXME: remove the following try catch once Aron fixes signatures
414         try {
415             MultipartInput input = (MultipartInput) res.getEntity();
416             PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
417                     client.getCommonPartName(), PersonauthoritiesCommon.class);
418             Assert.assertNotNull(personAuthority);
419         } catch (Exception e) {
420             throw new RuntimeException(e);
421         }
422     }
423
424     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
425             groups = {"read"}, dependsOnGroups = {"create"})
426         public void readByName(String testName) throws Exception {
427
428             // Perform setup.
429         setupRead();
430
431         // Submit the request to the service and store the response.
432         PersonAuthorityClient client = new PersonAuthorityClient();
433         ClientResponse<MultipartInput> res = client.readByName(knownResourceDisplayName);
434         int statusCode = res.getStatus();
435
436         // Check the status code of the response: does it match
437         // the expected response(s)?
438         if(logger.isDebugEnabled()){
439             logger.debug(testName + ": status = " + statusCode);
440         }
441         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
442                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
443         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
444         //FIXME: remove the following try catch once Aron fixes signatures
445         try {
446             MultipartInput input = (MultipartInput) res.getEntity();
447             PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
448                     client.getCommonPartName(), PersonauthoritiesCommon.class);
449             Assert.assertNotNull(personAuthority);
450         } catch (Exception e) {
451             throw new RuntimeException(e);
452         }
453     }
454
455 /*
456     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
457         groups = {"read"}, dependsOnMethods = {"read"})
458     public void readByName(String testName) throws Exception {
459
460         // Perform setup.
461         setupRead();
462
463         // Submit the request to the service and store the response.
464         ClientResponse<MultipartInput> res = client.read(knownResourceId);
465         int statusCode = res.getStatus();
466
467         // Check the status code of the response: does it match
468         // the expected response(s)?
469         if(logger.isDebugEnabled()){
470             logger.debug(testName + ": status = " + statusCode);
471         }
472         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
473                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
474         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
475         //FIXME: remove the following try catch once Aron fixes signatures
476         try {
477             MultipartInput input = (MultipartInput) res.getEntity();
478             PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
479                     client.getCommonPartName(), PersonauthoritiesCommon.class);
480             Assert.assertNotNull(personAuthority);
481         } catch (Exception e) {
482             throw new RuntimeException(e);
483         }
484     }
485 */
486
487     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
488         groups = {"read"}, dependsOnMethods = {"read"})
489     public void readItem(String testName) throws Exception {
490
491         // Perform setup.
492         setupRead(testName);
493
494         // Submit the request to the service and store the response.
495         PersonAuthorityClient client = new PersonAuthorityClient();
496         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
497         int statusCode = res.getStatus();
498
499         // Check the status code of the response: does it match
500         // the expected response(s)?
501         if(logger.isDebugEnabled()){
502             logger.debug(testName + ": status = " + statusCode);
503         }
504         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
505                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
506         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
507
508         // Check whether we've received a person.
509         MultipartInput input = (MultipartInput) res.getEntity();
510         PersonsCommon person = (PersonsCommon) extractPart(input,
511                 client.getItemCommonPartName(), PersonsCommon.class);
512         Assert.assertNotNull(person);
513         boolean showFull = true;
514         if(showFull && logger.isDebugEnabled()){
515             logger.debug(testName + ": returned payload:");
516             logger.debug(objectAsXmlString(person, PersonsCommon.class));
517         }
518         Assert.assertEquals(person.getInAuthority(), knownResourceId);
519
520     }
521
522     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
523         dependsOnMethods = {"readItem", "updateItem"})
524     public void verifyItemDisplayName(String testName) throws Exception {
525
526         // Perform setup.
527         setupUpdate(testName);
528
529         // Submit the request to the service and store the response.
530         PersonAuthorityClient client = new PersonAuthorityClient();
531         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
532         int statusCode = res.getStatus();
533
534         // Check the status code of the response: does it match
535         // the expected response(s)?
536         if(logger.isDebugEnabled()){
537             logger.debug(testName + ": status = " + statusCode);
538         }
539         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
540                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
541         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
542
543         // Check whether person has expected displayName.
544         MultipartInput input = (MultipartInput) res.getEntity();
545         PersonsCommon person = (PersonsCommon) extractPart(input,
546                 client.getItemCommonPartName(), PersonsCommon.class);
547         Assert.assertNotNull(person);
548         String displayName = person.getDisplayName();
549         // Make sure displayName matches computed form
550         String expectedDisplayName = 
551             PersonAuthorityClientUtils.prepareDefaultDisplayName(
552                 TEST_FORE_NAME, null, TEST_SUR_NAME,
553                 TEST_BIRTH_DATE, TEST_DEATH_DATE);
554         Assert.assertNotNull(displayName, expectedDisplayName);
555         
556         // Update the shortName and verify the computed name is updated.
557         person.setCsid(null);
558         person.setDisplayNameComputed(true);
559         person.setForeName("updated-" + TEST_FORE_NAME);
560         expectedDisplayName = 
561             PersonAuthorityClientUtils.prepareDefaultDisplayName(
562                 "updated-" + TEST_FORE_NAME, null, TEST_SUR_NAME, 
563                 TEST_BIRTH_DATE, TEST_DEATH_DATE);
564
565         // Submit the updated resource to the service and store the response.
566         MultipartOutput output = new MultipartOutput();
567         OutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
568         commonPart.getHeaders().add("label", client.getItemCommonPartName());
569         res = client.updateItem(knownResourceId, knownItemResourceId, output);
570         statusCode = res.getStatus();
571
572         // Check the status code of the response: does it match the expected response(s)?
573         if(logger.isDebugEnabled()){
574             logger.debug("updateItem: status = " + statusCode);
575         }
576         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
577                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
578         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
579
580         // Retrieve the updated resource and verify that its contents exist.
581         input = (MultipartInput) res.getEntity();
582         PersonsCommon updatedPerson =
583                 (PersonsCommon) extractPart(input,
584                         client.getItemCommonPartName(), PersonsCommon.class);
585         Assert.assertNotNull(updatedPerson);
586
587         // Verify that the updated resource received the correct data.
588         Assert.assertEquals(updatedPerson.getForeName(), person.getForeName(),
589             "Updated ForeName in Person did not match submitted data.");
590         // Verify that the updated resource computes the right displayName.
591         Assert.assertEquals(updatedPerson.getDisplayName(), expectedDisplayName,
592             "Updated ForeName in Person not reflected in computed DisplayName.");
593
594         // Now Update the displayName, not computed and verify the computed name is overriden.
595         person.setDisplayNameComputed(false);
596         expectedDisplayName = "TestName";
597         person.setDisplayName(expectedDisplayName);
598
599         // Submit the updated resource to the service and store the response.
600         output = new MultipartOutput();
601         commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
602         commonPart.getHeaders().add("label", client.getItemCommonPartName());
603         res = client.updateItem(knownResourceId, knownItemResourceId, output);
604         statusCode = res.getStatus();
605
606         // Check the status code of the response: does it match the expected response(s)?
607         if(logger.isDebugEnabled()){
608             logger.debug("updateItem: status = " + statusCode);
609         }
610         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
611                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
612         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
613
614         // Retrieve the updated resource and verify that its contents exist.
615         input = (MultipartInput) res.getEntity();
616         updatedPerson =
617                 (PersonsCommon) extractPart(input,
618                         client.getItemCommonPartName(), PersonsCommon.class);
619         Assert.assertNotNull(updatedPerson);
620
621         // Verify that the updated resource received the correct data.
622         Assert.assertEquals(updatedPerson.isDisplayNameComputed(), false,
623                 "Updated displayNameComputed in Person did not match submitted data.");
624         // Verify that the updated resource computes the right displayName.
625         Assert.assertEquals(updatedPerson.getDisplayName(),
626                         expectedDisplayName,
627                 "Updated DisplayName (not computed) in Person not stored.");
628     }
629
630     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
631             dependsOnMethods = {"verifyItemDisplayName"})
632     public void verifyIllegalItemDisplayName(String testName) throws Exception {
633
634         // Perform setup.
635         setupUpdateWithWrongXmlSchema(testName);
636
637         // Submit the request to the service and store the response.
638         PersonAuthorityClient client = new PersonAuthorityClient();
639         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
640         int statusCode = res.getStatus();
641
642         // Check the status code of the response: does it match
643         // the expected response(s)?
644         if(logger.isDebugEnabled()){
645             logger.debug(testName + ": status = " + statusCode);
646         }
647         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
648                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
649         Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
650
651         // Check whether Person has expected displayName.
652         MultipartInput input = (MultipartInput) res.getEntity();
653         PersonsCommon person = (PersonsCommon) extractPart(input,
654                 client.getItemCommonPartName(), PersonsCommon.class);
655         Assert.assertNotNull(person);
656         // Try to Update with computed false and no displayName
657         person.setDisplayNameComputed(false);
658         person.setDisplayName(null);
659
660         // Submit the updated resource to the service and store the response.
661         MultipartOutput output = new MultipartOutput();
662         OutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
663         commonPart.getHeaders().add("label", client.getItemCommonPartName());
664         res = client.updateItem(knownResourceId, knownItemResourceId, output);
665         statusCode = res.getStatus();
666
667         // Check the status code of the response: does it match the expected response(s)?
668         if(logger.isDebugEnabled()){
669             logger.debug("updateItem: status = " + statusCode);
670         }
671         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
672                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
673         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
674     }
675     
676     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
677         groups = {"read"}, dependsOnMethods = {"readItem"})
678     public void readContact(String testName) throws Exception {
679
680         // Perform setup.
681         setupRead(testName);
682
683         // Submit the request to the service and store the response.
684         PersonAuthorityClient client = new PersonAuthorityClient();
685         ClientResponse<MultipartInput> res =
686             client.readContact(knownResourceId, knownItemResourceId,
687             knownContactResourceId);
688         int statusCode = res.getStatus();
689
690         // Check the status code of the response: does it match
691         // the expected response(s)?
692         if(logger.isDebugEnabled()){
693             logger.debug(testName + ": status = " + statusCode);
694         }
695         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
696                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
697         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
698
699         // Check whether we've received a contact.
700         MultipartInput input = (MultipartInput) res.getEntity();
701         ContactsCommon contact = (ContactsCommon) extractPart(input,
702                 new ContactClient().getCommonPartName(), ContactsCommon.class);
703         Assert.assertNotNull(contact);
704         boolean showFull = true;
705         if(showFull && logger.isDebugEnabled()){
706             logger.debug(testName + ": returned payload:");
707             logger.debug(objectAsXmlString(contact, ContactsCommon.class));
708         }
709         Assert.assertEquals(contact.getInAuthority(), knownResourceId);
710         Assert.assertEquals(contact.getInItem(), knownItemResourceId);
711
712     }
713
714     // Failure outcomes
715     @Override
716     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
717         groups = {"read"}, dependsOnMethods = {"read"})
718     public void readNonExistent(String testName) {
719
720         // Perform setup.
721         setupReadNonExistent(testName);
722
723         // Submit the request to the service and store the response.
724         PersonAuthorityClient client = new PersonAuthorityClient();
725         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
726         int statusCode = res.getStatus();
727
728         // Check the status code of the response: does it match
729         // the expected response(s)?
730         if(logger.isDebugEnabled()){
731             logger.debug(testName + ": status = " + statusCode);
732         }
733         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
734                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
735         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
736     }
737
738     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
739         groups = {"read"}, dependsOnMethods = {"readItem"})
740     public void readItemNonExistent(String testName) {
741
742         // Perform setup.
743         setupReadNonExistent(testName);
744
745         // Submit the request to the service and store the response.
746         PersonAuthorityClient client = new PersonAuthorityClient();
747         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
748         int statusCode = res.getStatus();
749
750         // Check the status code of the response: does it match
751         // the expected response(s)?
752         if(logger.isDebugEnabled()){
753             logger.debug(testName + ": status = " + statusCode);
754         }
755         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
756                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
757         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
758     }
759
760     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
761         groups = {"read"}, dependsOnMethods = {"readContact"})
762     public void readContactNonExistent(String testName) {
763
764         // Perform setup.
765         setupReadNonExistent(testName);
766
767         // Submit the request to the service and store the response.
768         PersonAuthorityClient client = new PersonAuthorityClient();
769         ClientResponse<MultipartInput> res =
770             client.readContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
771         int statusCode = res.getStatus();
772
773         // Check the status code of the response: does it match
774         // the expected response(s)?
775         if(logger.isDebugEnabled()){
776             logger.debug(testName + ": status = " + statusCode);
777         }
778         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
779                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
780         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
781     }
782
783     // ---------------------------------------------------------------
784     // CRUD tests : READ_LIST tests
785     // ---------------------------------------------------------------
786     // Success outcomes
787
788     @Override
789     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
790         groups = {"readList"}, dependsOnGroups = {"createList", "read"})
791     public void readList(String testName) throws Exception {
792
793         // Perform setup.
794         setupReadList(testName);
795
796         // Submit the request to the service and store the response.
797         PersonAuthorityClient client = new PersonAuthorityClient();
798         ClientResponse<PersonauthoritiesCommonList> res = client.readList();
799         PersonauthoritiesCommonList list = res.getEntity();
800         int statusCode = res.getStatus();
801
802         // Check the status code of the response: does it match
803         // the expected response(s)?
804         if(logger.isDebugEnabled()){
805             logger.debug(testName + ": status = " + statusCode);
806         }
807         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
808                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
809         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
810
811         // Optionally output additional data about list members for debugging.
812         boolean iterateThroughList = false;
813         if (iterateThroughList && logger.isDebugEnabled()) {
814             List<PersonauthoritiesCommonList.PersonauthorityListItem> items =
815                     list.getPersonauthorityListItem();
816             int i = 0;
817             for (PersonauthoritiesCommonList.PersonauthorityListItem item : items) {
818                 String csid = item.getCsid();
819                 logger.debug(testName + ": list-item[" + i + "] csid=" +
820                         csid);
821                 logger.debug(testName + ": list-item[" + i + "] displayName=" +
822                         item.getDisplayName());
823                 logger.debug(testName + ": list-item[" + i + "] URI=" +
824                         item.getUri());
825                 readItemList(csid, null);
826                 i++;
827             }
828         }
829     }
830
831     @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
832     public void readItemList() {
833         readItemList(knownResourceId, null);
834     }
835
836     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
837     public void readItemListByAuthorityName() {
838         readItemList(null, knownResourceDisplayName);
839     }
840     private void readItemList(String vcsid, String name) {
841
842         final String testName = "readItemList";
843
844         // Perform setup.
845         setupReadList(testName);
846         
847         // Submit the request to the service and store the response.
848         PersonAuthorityClient client = new PersonAuthorityClient();
849         ClientResponse<PersonsCommonList> res = null;
850         if(vcsid!= null) {
851                 res = client.readItemList(vcsid);
852         } else if(name!= null) {
853                 res = client.readItemListForNamedAuthority(name);
854         } else {
855                 Assert.fail("readItemList passed null csid and name!");
856         }
857         PersonsCommonList list = res.getEntity();
858         int statusCode = res.getStatus();
859
860         // Check the status code of the response: does it match
861         // the expected response(s)?
862         if(logger.isDebugEnabled()){
863             logger.debug(testName + ": status = " + statusCode);
864         }
865         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
866                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
867         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
868
869         List<PersonsCommonList.PersonListItem> items =
870             list.getPersonListItem();
871         int nItemsReturned = items.size();
872         // There will be one item created, associated with a
873         // known parent resource, by the createItem test.
874         //
875         // In addition, there will be 'nItemsToCreateInList'
876         // additional items created by the createItemList test,
877         // all associated with the same parent resource.
878         int nExpectedItems = nItemsToCreateInList + 1;
879         if(logger.isDebugEnabled()){
880             logger.debug(testName + ": Expected "
881                         + nExpectedItems +" items; got: "+nItemsReturned);
882         }
883         Assert.assertEquals(nItemsReturned, nExpectedItems);
884
885         int i = 0;
886         for (PersonsCommonList.PersonListItem item : items) {
887                 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
888                 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
889                 // Optionally output additional data about list members for debugging.
890                 boolean showDetails = true;
891                 if (showDetails && logger.isDebugEnabled()) {
892                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
893                         item.getCsid());
894                 logger.debug("  " + testName + ": list-item[" + i + "] refName=" +
895                         item.getRefName());
896                 logger.debug("  " + testName + ": list-item[" + i + "] displayName=" +
897                         item.getDisplayName());
898                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
899                         item.getUri());
900             }
901             i++;
902         }
903     }
904
905     @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
906     public void readContactList() {
907         readContactList(knownResourceId, knownItemResourceId);
908     }
909
910     private void readContactList(String parentcsid, String itemcsid) {
911         final String testName = "readContactList";
912
913         // Perform setup.
914         setupReadList(testName);
915
916         // Submit the request to the service and store the response.
917         PersonAuthorityClient client = new PersonAuthorityClient();
918         ClientResponse<ContactsCommonList> res =
919                 client.readContactList(parentcsid, itemcsid);
920         ContactsCommonList list = res.getEntity();
921         int statusCode = res.getStatus();
922
923         // Check the status code of the response: does it match
924         // the expected response(s)?
925         if(logger.isDebugEnabled()){
926             logger.debug(testName + ": status = " + statusCode);
927         }
928         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
929                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
930         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
931
932         List<ContactsCommonList.ContactListItem> listitems =
933             list.getContactListItem();
934         int nItemsReturned = listitems.size();
935         // There will be one item created, associated with a
936         // known parent resource, by the createItem test.
937         //
938         // In addition, there will be 'nItemsToCreateInList'
939         // additional items created by the createItemList test,
940         // all associated with the same parent resource.
941         int nExpectedItems = nItemsToCreateInList + 1;
942         if(logger.isDebugEnabled()){
943             logger.debug(testName + ": Expected "
944                         + nExpectedItems +" items; got: "+nItemsReturned);
945         }
946         Assert.assertEquals(nItemsReturned, nExpectedItems);
947
948         int i = 0;
949         for (ContactsCommonList.ContactListItem listitem : listitems) {
950                 // Optionally output additional data about list members for debugging.
951                 boolean showDetails = false;
952                 if (showDetails && logger.isDebugEnabled()) {
953                 logger.debug("  " + testName + ": list-item[" + i + "] csid=" +
954                         listitem.getCsid());
955                 logger.debug("  " + testName + ": list-item[" + i + "] addressPlace=" +
956                         listitem.getAddressPlace());
957                 logger.debug("  " + testName + ": list-item[" + i + "] URI=" +
958                         listitem.getUri());
959             }
960             i++;
961         }
962     }
963
964     // Failure outcomes
965     // None at present.
966
967     // ---------------------------------------------------------------
968     // CRUD tests : UPDATE tests
969     // ---------------------------------------------------------------
970     // Success outcomes
971     @Override
972     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
973         groups = {"update"}, dependsOnGroups = {"read", "readList"})
974     public void update(String testName) throws Exception {
975
976         // Perform setup.
977         setupUpdate(testName);
978
979         // Retrieve the contents of a resource to update.
980         PersonAuthorityClient client = new PersonAuthorityClient();
981         ClientResponse<MultipartInput> res =
982                 client.read(knownResourceId);
983         if(logger.isDebugEnabled()){
984             logger.debug(testName + ": read status = " + res.getStatus());
985         }
986         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
987
988         if(logger.isDebugEnabled()){
989             logger.debug("got PersonAuthority to update with ID: " + knownResourceId);
990         }
991         MultipartInput input = (MultipartInput) res.getEntity();
992         PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
993                 client.getCommonPartName(), PersonauthoritiesCommon.class);
994         Assert.assertNotNull(personAuthority);
995
996         // Update the contents of this resource.
997         personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
998         personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
999         if(logger.isDebugEnabled()){
1000             logger.debug("to be updated PersonAuthority");
1001             logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
1002         }
1003
1004         // Submit the updated resource to the service and store the response.
1005         MultipartOutput output = new MultipartOutput();
1006         OutputPart commonPart = output.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);
1007         commonPart.getHeaders().add("label", client.getCommonPartName());
1008         res = client.update(knownResourceId, output);
1009         int statusCode = res.getStatus();
1010
1011         // Check the status code of the response: does it match the expected response(s)?
1012         if(logger.isDebugEnabled()){
1013             logger.debug(testName + ": status = " + statusCode);
1014         }
1015         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1016                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1017         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1018
1019         // Retrieve the updated resource and verify that its contents exist.
1020         input = (MultipartInput) res.getEntity();
1021         PersonauthoritiesCommon updatedPersonAuthority =
1022                 (PersonauthoritiesCommon) extractPart(input,
1023                         client.getCommonPartName(), PersonauthoritiesCommon.class);
1024         Assert.assertNotNull(updatedPersonAuthority);
1025
1026         // Verify that the updated resource received the correct data.
1027         Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
1028                 personAuthority.getDisplayName(),
1029                 "Data in updated object did not match submitted data.");
1030     }
1031
1032     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1033         groups = {"update"}, dependsOnMethods = {"update"})
1034     public void updateItem(String testName) throws Exception {
1035
1036         // Perform setup.
1037         setupUpdate(testName);
1038
1039         // Retrieve the contents of a resource to update.
1040         PersonAuthorityClient client = new PersonAuthorityClient();
1041         ClientResponse<MultipartInput> res =
1042                 client.readItem(knownResourceId, knownItemResourceId);
1043         if(logger.isDebugEnabled()){
1044             logger.debug(testName + ": read status = " + res.getStatus());
1045         }
1046         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1047
1048         if(logger.isDebugEnabled()){
1049             logger.debug("got Person to update with ID: " +
1050                 knownItemResourceId +
1051                 " in PersonAuthority: " + knownResourceId );
1052         }
1053         MultipartInput input = (MultipartInput) res.getEntity();
1054         PersonsCommon person = (PersonsCommon) extractPart(input,
1055                 client.getItemCommonPartName(), PersonsCommon.class);
1056         Assert.assertNotNull(person);
1057
1058         // Update the contents of this resource.
1059         person.setCsid(null);
1060         person.setForeName("updated-" + person.getForeName());
1061         if(logger.isDebugEnabled()){
1062             logger.debug("to be updated Person");
1063             logger.debug(objectAsXmlString(person,
1064                 PersonsCommon.class));
1065         }        
1066
1067         // Submit the updated resource to the service and store the response.
1068         MultipartOutput output = new MultipartOutput();
1069         OutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
1070         commonPart.getHeaders().add("label", client.getItemCommonPartName());
1071         res = client.updateItem(knownResourceId, knownItemResourceId, output);
1072         int statusCode = res.getStatus();
1073
1074         // Check the status code of the response: does it match the expected response(s)?
1075         if(logger.isDebugEnabled()){
1076             logger.debug(testName + ": status = " + statusCode);
1077         }
1078         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1079                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1080         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1081
1082         // Retrieve the updated resource and verify that its contents exist.
1083         input = (MultipartInput) res.getEntity();
1084         PersonsCommon updatedPerson =
1085                 (PersonsCommon) extractPart(input,
1086                         client.getItemCommonPartName(), PersonsCommon.class);
1087         Assert.assertNotNull(updatedPerson);
1088
1089         // Verify that the updated resource received the correct data.
1090         Assert.assertEquals(updatedPerson.getForeName(),
1091                 person.getForeName(),
1092                 "Data in updated Person did not match submitted data.");
1093     }
1094
1095     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1096         groups = {"update"}, dependsOnMethods = {"updateItem"})
1097     public void updateContact(String testName) throws Exception {
1098
1099         // Perform setup.
1100         setupUpdate(testName);
1101
1102         // Retrieve the contents of a resource to update.
1103         PersonAuthorityClient client = new PersonAuthorityClient();
1104         ClientResponse<MultipartInput> res =
1105                 client.readContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1106         if(logger.isDebugEnabled()){
1107             logger.debug(testName + ": read status = " + res.getStatus());
1108         }
1109         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
1110
1111         if(logger.isDebugEnabled()){
1112             logger.debug("got Contact to update with ID: " +
1113                 knownContactResourceId +
1114                 " in item: " + knownItemResourceId +
1115                 " in parent: " + knownResourceId );
1116         }
1117         MultipartInput input = (MultipartInput) res.getEntity();
1118         ContactsCommon contact = (ContactsCommon) extractPart(input,
1119                 new ContactClient().getCommonPartName(), ContactsCommon.class);
1120         Assert.assertNotNull(contact);
1121
1122         // Update the contents of this resource.
1123         contact.setAddressPlace("updated-" + contact.getAddressPlace());
1124         if(logger.isDebugEnabled()){
1125             logger.debug("to be updated Contact");
1126             logger.debug(objectAsXmlString(contact,
1127                 ContactsCommon.class));
1128         }
1129
1130         // Submit the updated resource to the service and store the response.
1131         MultipartOutput output = new MultipartOutput();
1132         OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
1133         commonPart.getHeaders().add("label", new ContactClient().getCommonPartName());
1134         res = client.updateContact(knownResourceId, knownItemResourceId, knownContactResourceId, output);
1135         int statusCode = res.getStatus();
1136
1137         // Check the status code of the response: does it match the expected response(s)?
1138         if(logger.isDebugEnabled()){
1139             logger.debug(testName + ": status = " + statusCode);
1140         }
1141         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1142                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1143         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1144
1145         // Retrieve the updated resource and verify that its contents exist.
1146         input = (MultipartInput) res.getEntity();
1147         ContactsCommon updatedContact =
1148                 (ContactsCommon) extractPart(input,
1149                         new ContactClient().getCommonPartName(), ContactsCommon.class);
1150         Assert.assertNotNull(updatedContact);
1151
1152         // Verify that the updated resource received the correct data.
1153         Assert.assertEquals(updatedContact.getAddressPlace(),
1154                 contact.getAddressPlace(),
1155                 "Data in updated Contact did not match submitted data.");
1156     }
1157
1158     // Failure outcomes
1159     // Placeholders until the three tests below can be uncommented.
1160     // See Issue CSPACE-401.
1161     @Override
1162     public void updateWithEmptyEntityBody(String testName) throws Exception {
1163     }
1164
1165     @Override
1166     public void updateWithMalformedXml(String testName) throws Exception {
1167     }
1168
1169     @Override
1170     public void updateWithWrongXmlSchema(String testName) throws Exception {
1171     }
1172
1173 /*
1174     @Override
1175     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1176         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1177     public void updateWithEmptyEntityBody(String testName) throws Exception {
1178
1179     // Perform setup.
1180     setupUpdateWithEmptyEntityBody(testName);
1181
1182     // Submit the request to the service and store the response.
1183     String method = REQUEST_TYPE.httpMethodName();
1184     String url = getResourceURL(knownResourceId);
1185     String mediaType = MediaType.APPLICATION_XML;
1186     final String entity = "";
1187     int statusCode = submitRequest(method, url, mediaType, entity);
1188
1189     // Check the status code of the response: does it match
1190     // the expected response(s)?
1191     if(logger.isDebugEnabled()){
1192         logger.debug(testName + ": url=" + url +
1193             " status=" + statusCode);
1194      }
1195     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1196     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1197     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1198     }
1199
1200     @Override
1201     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1202         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1203     public void updateWithMalformedXml(String testName) throws Exception {
1204
1205     // Perform setup.
1206     setupUpdateWithMalformedXml(testName);
1207
1208     // Submit the request to the service and store the response.
1209     String method = REQUEST_TYPE.httpMethodName();
1210     String url = getResourceURL(knownResourceId);
1211     String mediaType = MediaType.APPLICATION_XML;
1212     final String entity = MALFORMED_XML_DATA;
1213     int statusCode = submitRequest(method, url, mediaType, entity);
1214
1215     // Check the status code of the response: does it match
1216     // the expected response(s)?
1217     if(logger.isDebugEnabled()){
1218         logger.debug(testName + ": url=" + url +
1219            " status=" + statusCode);
1220      }
1221     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1222     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1223     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1224     }
1225
1226     @Override
1227     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
1228         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1229     public void updateWithWrongXmlSchema(String testName) throws Exception {
1230
1231     // Perform setup.
1232     setupUpdateWithWrongXmlSchema(testName);
1233
1234     // Submit the request to the service and store the response.
1235     String method = REQUEST_TYPE.httpMethodName();
1236     String url = getResourceURL(knownResourceId);
1237     String mediaType = MediaType.APPLICATION_XML;
1238     final String entity = WRONG_XML_SCHEMA_DATA;
1239     int statusCode = submitRequest(method, url, mediaType, entity);
1240
1241     // Check the status code of the response: does it match
1242     // the expected response(s)?
1243     if(logger.isDebugEnabled()){
1244         logger.debug("updateWithWrongXmlSchema: url=" + url +
1245             " status=" + statusCode);
1246      }
1247     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1248     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1249     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1250     }
1251 */
1252
1253     @Override
1254     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1255         groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
1256     public void updateNonExistent(String testName) throws Exception {
1257
1258         // Perform setup.
1259         setupUpdateNonExistent(testName);
1260
1261         // Submit the request to the service and store the response.
1262         // Note: The ID(s) used when creating the request payload may be arbitrary.
1263         // The only relevant ID may be the one used in update(), below.
1264         PersonAuthorityClient client = new PersonAuthorityClient();
1265         String displayName = "displayName-NON_EXISTENT_ID";
1266         String fullRefName = PersonAuthorityClientUtils.createPersonAuthRefName(displayName, true);
1267         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(
1268                                 displayName, fullRefName, client.getCommonPartName());
1269         ClientResponse<MultipartInput> res =
1270                 client.update(NON_EXISTENT_ID, multipart);
1271         int statusCode = res.getStatus();
1272
1273         // Check the status code of the response: does it match
1274         // the expected response(s)?
1275         if(logger.isDebugEnabled()){
1276             logger.debug(testName + ": status = " + statusCode);
1277         }
1278         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1279                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1280         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1281     }
1282
1283     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1284         groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1285     public void updateNonExistentItem(String testName) throws Exception {
1286
1287         // Perform setup.
1288         setupUpdateNonExistent(testName);
1289
1290         // Submit the request to the service and store the response.
1291         // Note: The ID used in this 'create' call may be arbitrary.
1292         // The only relevant ID may be the one used in update(), below.
1293         PersonAuthorityClient client = new PersonAuthorityClient();
1294         Map<String, String> nonexMap = new HashMap<String,String>();
1295         nonexMap.put(PersonJAXBSchema.FORE_NAME, "John");
1296         nonexMap.put(PersonJAXBSchema.SUR_NAME, "Wayne");
1297         nonexMap.put(PersonJAXBSchema.GENDER, "male");
1298         MultipartOutput multipart = 
1299         PersonAuthorityClientUtils.createPersonInstance(NON_EXISTENT_ID, 
1300                         PersonAuthorityClientUtils.createPersonRefName(NON_EXISTENT_ID, NON_EXISTENT_ID, true), nonexMap,
1301                         client.getItemCommonPartName() );
1302         ClientResponse<MultipartInput> res =
1303                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1304         int statusCode = res.getStatus();
1305
1306         // Check the status code of the response: does it match
1307         // the expected response(s)?
1308         if(logger.isDebugEnabled()){
1309             logger.debug(testName + ": status = " + statusCode);
1310         }
1311         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1312                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1313         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1314     }
1315
1316     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1317         groups = {"update"}, dependsOnMethods = {"updateContact", "testContactSubmitRequest"})
1318     public void updateNonExistentContact(String testName) throws Exception {
1319         // Currently a no-op test
1320     }
1321
1322     // ---------------------------------------------------------------
1323     // CRUD tests : DELETE tests
1324     // ---------------------------------------------------------------
1325     // Success outcomes
1326
1327     // Note: delete sub-resources in ascending hierarchical order,
1328     // before deleting their parents.
1329
1330     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, 
1331         groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "update"})
1332     public void deleteContact(String testName) throws Exception {
1333
1334         // Perform setup.
1335         setupDelete(testName);
1336
1337          if(logger.isDebugEnabled()){
1338             logger.debug("parentcsid =" + knownResourceId +
1339                 " itemcsid = " + knownItemResourceId +
1340                 " csid = " + knownContactResourceId);
1341         }
1342
1343         // Submit the request to the service and store the response.
1344         PersonAuthorityClient client = new PersonAuthorityClient();
1345         ClientResponse<Response> res =
1346             client.deleteContact(knownResourceId, knownItemResourceId, knownContactResourceId);
1347         int statusCode = res.getStatus();
1348
1349         // Check the status code of the response: does it match
1350         // the expected response(s)?
1351         if(logger.isDebugEnabled()){
1352             logger.debug(testName + ": status = " + statusCode);
1353         }
1354         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1355                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1356         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1357     }
1358
1359    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1360         groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1361     public void deleteItem(String testName) throws Exception {
1362
1363         // Perform setup.
1364         setupDelete(testName);
1365
1366         if(logger.isDebugEnabled()){
1367             logger.debug("parentcsid =" + knownResourceId +
1368                 " itemcsid = " + knownItemResourceId);
1369         }
1370
1371         // Submit the request to the service and store the response.
1372         PersonAuthorityClient client = new PersonAuthorityClient();
1373         ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1374         int statusCode = res.getStatus();
1375
1376         // Check the status code of the response: does it match
1377         // the expected response(s)?
1378         if(logger.isDebugEnabled()){
1379             logger.debug(testName + ": status = " + statusCode);
1380         }
1381         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1382                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1383         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1384     }
1385
1386     @Override
1387     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1388         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1389     public void delete(String testName) throws Exception {
1390
1391         // Perform setup.
1392         setupDelete(testName);
1393
1394         if(logger.isDebugEnabled()){
1395             logger.debug("parentcsid =" + knownResourceId);
1396         }
1397
1398         // Submit the request to the service and store the response.
1399         PersonAuthorityClient client = new PersonAuthorityClient();
1400         ClientResponse<Response> res = client.delete(knownResourceId);
1401         int statusCode = res.getStatus();
1402
1403         // Check the status code of the response: does it match
1404         // the expected response(s)?
1405         if(logger.isDebugEnabled()){
1406             logger.debug(testName + ": status = " + statusCode);
1407         }
1408         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1409                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1410         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1411     }
1412
1413     // Failure outcomes
1414     @Override
1415     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1416         groups = {"delete"}, dependsOnMethods = {"delete"})
1417     public void deleteNonExistent(String testName) throws Exception {
1418
1419         // Perform setup.
1420         setupDeleteNonExistent(testName);
1421
1422         // Submit the request to the service and store the response.
1423         PersonAuthorityClient client = new PersonAuthorityClient();
1424         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1425         int statusCode = res.getStatus();
1426
1427         // Check the status code of the response: does it match
1428         // the expected response(s)?
1429         if(logger.isDebugEnabled()){
1430             logger.debug(testName + ": status = " + statusCode);
1431         }
1432         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1433                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1434         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1435     }
1436
1437     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1438         groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1439     public void deleteNonExistentItem(String testName) {
1440
1441         // Perform setup.
1442         setupDeleteNonExistent(testName);
1443
1444         // Submit the request to the service and store the response.
1445         PersonAuthorityClient client = new PersonAuthorityClient();
1446         ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1447         int statusCode = res.getStatus();
1448
1449         // Check the status code of the response: does it match
1450         // the expected response(s)?
1451         if(logger.isDebugEnabled()){
1452             logger.debug(testName + ": status = " + statusCode);
1453         }
1454         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1455                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1456         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1457     }
1458
1459     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1460         groups = {"delete"}, dependsOnMethods = {"deleteContact"})
1461     public void deleteNonExistentContact(String testName) {
1462
1463         // Perform setup.
1464         setupDeleteNonExistent(testName);
1465
1466         // Submit the request to the service and store the response.
1467         PersonAuthorityClient client = new PersonAuthorityClient();
1468         ClientResponse<Response> res =
1469             client.deleteContact(knownResourceId, knownItemResourceId, NON_EXISTENT_ID);
1470         int statusCode = res.getStatus();
1471
1472         // Check the status code of the response: does it match
1473         // the expected response(s)?
1474         if(logger.isDebugEnabled()){
1475             logger.debug(testName + ": status = " + statusCode);
1476         }
1477         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1478                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1479         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1480     }
1481
1482     // ---------------------------------------------------------------
1483     // Utility tests : tests of code used in tests above
1484     // ---------------------------------------------------------------
1485     /**
1486      * Tests the code for manually submitting data that is used by several
1487      * of the methods above.
1488      */
1489     @Test(dependsOnMethods = {"create", "read"})
1490     public void testSubmitRequest() {
1491
1492         // Expected status code: 200 OK
1493         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1494
1495         // Submit the request to the service and store the response.
1496         String method = ServiceRequestType.READ.httpMethodName();
1497         String url = getResourceURL(knownResourceId);
1498         int statusCode = submitRequest(method, url);
1499
1500         // Check the status code of the response: does it match
1501         // the expected response(s)?
1502         if(logger.isDebugEnabled()){
1503             logger.debug("testSubmitRequest: url=" + url +
1504                 " status=" + statusCode);
1505         }
1506         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1507
1508     }
1509
1510     @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1511     public void testItemSubmitRequest() {
1512
1513         // Expected status code: 200 OK
1514         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1515
1516         // Submit the request to the service and store the response.
1517         String method = ServiceRequestType.READ.httpMethodName();
1518         String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1519         int statusCode = submitRequest(method, url);
1520
1521         // Check the status code of the response: does it match
1522         // the expected response(s)?
1523         if(logger.isDebugEnabled()){
1524             logger.debug("testItemSubmitRequest: url=" + url +
1525                 " status=" + statusCode);
1526         }
1527         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1528
1529     }
1530
1531     @Test(dependsOnMethods = {"createContact", "readContact", "testItemSubmitRequest"})
1532     public void testContactSubmitRequest() {
1533
1534         // Expected status code: 200 OK
1535         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1536
1537         // Submit the request to the service and store the response.
1538         String method = ServiceRequestType.READ.httpMethodName();
1539         String url = getContactResourceURL(knownResourceId,
1540             knownItemResourceId, knownContactResourceId);
1541         int statusCode = submitRequest(method, url);
1542
1543         // Check the status code of the response: does it match
1544         // the expected response(s)?
1545         if(logger.isDebugEnabled()){
1546             logger.debug("testItemSubmitRequest: url=" + url +
1547                 " status=" + statusCode);
1548         }
1549         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1550
1551     }
1552
1553
1554     // ---------------------------------------------------------------
1555     // Cleanup of resources created during testing
1556     // ---------------------------------------------------------------
1557     
1558     /**
1559      * Deletes all resources created by tests, after all tests have been run.
1560      *
1561      * This cleanup method will always be run, even if one or more tests fail.
1562      * For this reason, it attempts to remove all resources created
1563      * at any point during testing, even if some of those resources
1564      * may be expected to be deleted by certain tests.
1565      */
1566
1567     @AfterClass(alwaysRun=true)
1568     public void cleanUp() {
1569         if (logger.isDebugEnabled()) {
1570             logger.debug("Cleaning up temporary resources created for testing ...");
1571         }
1572         String parentResourceId;
1573         String itemResourceId;
1574         String contactResourceId;
1575         // Clean up contact resources.
1576         PersonAuthorityClient client = new PersonAuthorityClient();
1577         parentResourceId = knownResourceId;
1578         for (Map.Entry<String, String> entry : allContactResourceIdsCreated.entrySet()) {
1579             contactResourceId = entry.getKey();
1580             itemResourceId = entry.getValue();
1581             // Note: Any non-success responses from the delete operation
1582             // below are ignored and not reported.
1583             ClientResponse<Response> res =
1584                 client.deleteContact(parentResourceId, itemResourceId, contactResourceId);
1585         }
1586         // Clean up item resources.
1587         for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
1588             itemResourceId = entry.getKey();
1589             parentResourceId = entry.getValue();
1590             // Note: Any non-success responses from the delete operation
1591             // below are ignored and not reported.
1592             ClientResponse<Response> res =
1593                 client.deleteItem(parentResourceId, itemResourceId);
1594         }
1595         // Clean up parent resources.
1596         for (String resourceId : allResourceIdsCreated) {
1597             // Note: Any non-success responses from the delete operation
1598             // below are ignored and not reported.
1599             ClientResponse<Response> res = client.delete(resourceId);
1600         }
1601     }
1602
1603     // ---------------------------------------------------------------
1604     // Utility methods used by tests above
1605     // ---------------------------------------------------------------
1606     @Override
1607     public String getServicePathComponent() {
1608         return SERVICE_PATH_COMPONENT;
1609     }
1610
1611     public String getItemServicePathComponent() {
1612         return ITEM_SERVICE_PATH_COMPONENT;
1613     }
1614
1615     public String getContactServicePathComponent() {
1616         return CONTACT_SERVICE_PATH_COMPONENT;
1617     }
1618
1619     /**
1620      * Returns the root URL for the item service.
1621      *
1622      * This URL consists of a base URL for all services, followed by
1623      * a path component for the owning parent, followed by the
1624      * path component for the items.
1625      *
1626      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1627      * parent authority resource of the relevant item resource.
1628      *
1629      * @return The root URL for the item service.
1630      */
1631     protected String getItemServiceRootURL(String parentResourceIdentifier) {
1632         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1633     }
1634
1635     /**
1636      * Returns the URL of a specific item resource managed by a service, and
1637      * designated by an identifier (such as a universally unique ID, or UUID).
1638      *
1639      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1640      * parent authority resource of the relevant item resource.
1641      *
1642      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1643      * item resource.
1644      *
1645      * @return The URL of a specific item resource managed by a service.
1646      */
1647     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1648         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
1649     }
1650
1651
1652     /**
1653      * Returns the root URL for the contact service.
1654      *
1655      * This URL consists of a base URL for all services, followed by
1656      * a path component for the owning authority, followed by the
1657      * path component for the owning item, followed by the path component
1658      * for the contact service.
1659      *
1660      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1661      * parent authority resource of the relevant item resource.
1662      *
1663      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
1664      * item resource.
1665      *
1666      * @return The root URL for the contact service.
1667      */
1668     protected String getContactServiceRootURL(String parentResourceIdentifier,
1669         String itemResourceIdentifier) {
1670         return getItemResourceURL(parentResourceIdentifier, itemResourceIdentifier) + "/" +
1671                 getContactServicePathComponent();
1672     }
1673
1674     /**
1675      * Returns the URL of a specific contact resource managed by a service, and
1676      * designated by an identifier (such as a universally unique ID, or UUID).
1677      *
1678      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
1679      * parent resource of the relevant item resource.
1680      *
1681      * @param  resourceIdentifier  An identifier (such as a UUID) for an
1682      * item resource.
1683      *
1684      * @return The URL of a specific resource managed by a service.
1685      */
1686     protected String getContactResourceURL(String parentResourceIdentifier,
1687         String itemResourceIdentifier, String contactResourceIdentifier) {
1688         return getContactServiceRootURL(parentResourceIdentifier,
1689             itemResourceIdentifier) + "/" + contactResourceIdentifier;
1690     }
1691 }