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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c)) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
33 import org.collectionspace.services.PersonJAXBSchema;
34 import org.collectionspace.services.client.PersonAuthorityClient;
35 import org.collectionspace.services.client.PersonAuthorityClientUtils;
36 import org.collectionspace.services.person.PersonauthoritiesCommon;
37 import org.collectionspace.services.person.PersonauthoritiesCommonList;
38 import org.collectionspace.services.person.PersonsCommon;
39 import org.collectionspace.services.person.PersonsCommonList;
40 import org.jboss.resteasy.client.ClientResponse;
41 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
42 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
43 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
51 * PersonAuthorityServiceTest, carries out tests against a
52 * deployed and running PersonAuthority Service.
54 * $LastChangedRevision: 753 $
55 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
57 public class PersonAuthorityServiceTest extends AbstractServiceTest {
59 private final Logger logger =
60 LoggerFactory.getLogger(PersonAuthorityServiceTest.class);
62 // Instance variables specific to this test.
63 private PersonAuthorityClient client = new PersonAuthorityClient();
64 final String SERVICE_PATH_COMPONENT = "personauthorities";
65 final String ITEM_SERVICE_PATH_COMPONENT = "items";
66 private String knownResourceId = null;
67 private String lastPersonAuthId = null;
68 private String knownResourceRefName = null;
69 private String knownItemResourceId = null;
70 private int nItemsToCreateInList = 3;
71 private List<String> allResourceIdsCreated = new ArrayList<String>();
72 private Map<String, String> allResourceItemIdsCreated =
73 new HashMap<String, String>();
75 protected String createPersonAuthRefName(String personAuthorityName) {
76 return "urn:cspace:org.collectionspace.demo:personauthority:name("
77 +personAuthorityName+")";
80 protected String createPersonRefName(
81 String personAuthRefName, String personName) {
82 return personAuthRefName+":person:name("+personName+")";
86 // ---------------------------------------------------------------
87 // CRUD tests : CREATE tests
88 // ---------------------------------------------------------------
91 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
92 public void create(String testName) throws Exception {
94 // Perform setup, such as initializing the type of service request
95 // (e.g. CREATE, DELETE), its valid and expected status codes, and
96 // its associated HTTP method name (e.g. POST, DELETE).
97 setupCreate(testName);
99 // Submit the request to the service and store the response.
100 String identifier = createIdentifier();
101 String displayName = "displayName-" + identifier;
102 String refName = createPersonAuthRefName(displayName);
103 MultipartOutput multipart =
104 PersonAuthorityClientUtils.createPersonAuthorityInstance(
105 displayName, refName, client.getCommonPartName());
106 ClientResponse<Response> res = client.create(multipart);
107 int statusCode = res.getStatus();
109 // Check the status code of the response: does it match
110 // the expected response(s)?
113 // Does it fall within the set of valid status codes?
114 // Does it exactly match the expected status code?
115 if(logger.isDebugEnabled()){
116 logger.debug(testName + ": status = " + statusCode);
118 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
119 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
120 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
122 // Store the refname from the first resource created
123 // for additional tests below.
124 knownResourceRefName = refName;
126 lastPersonAuthId = PersonAuthorityClientUtils.extractId(res);
127 // Store the ID returned from the first resource created
128 // for additional tests below.
129 if (knownResourceId == null){
130 knownResourceId = lastPersonAuthId;
131 if (logger.isDebugEnabled()) {
132 logger.debug(testName + ": knownResourceId=" + knownResourceId);
135 // Store the IDs from every resource created by tests,
136 // so they can be deleted after tests have been run.
137 allResourceIdsCreated.add(lastPersonAuthId);
141 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
142 dependsOnMethods = {"create"})
143 public void createItem(String testName) {
144 setupCreate(testName);
146 knownItemResourceId = createItemInAuthority(lastPersonAuthId, knownResourceRefName);
147 if(logger.isDebugEnabled()){
148 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
152 private String createItemInAuthority(String vcsid, String authRefName) {
154 final String testName = "createItemInAuthority";
155 if(logger.isDebugEnabled()){
156 logger.debug(testName + ":...");
159 // Submit the request to the service and store the response.
160 String identifier = createIdentifier();
161 Map<String, String> johnWayneMap = new HashMap<String,String>();
162 johnWayneMap.put(PersonJAXBSchema.FORE_NAME, "John");
163 johnWayneMap.put(PersonJAXBSchema.SUR_NAME, "Wayne");
164 johnWayneMap.put(PersonJAXBSchema.GENDER, "male");
165 johnWayneMap.put(PersonJAXBSchema.BIRTH_DATE, "May 26, 1907");
166 johnWayneMap.put(PersonJAXBSchema.BIRTH_PLACE, "Winterset, Iowa");
167 johnWayneMap.put(PersonJAXBSchema.DEATH_DATE, "June 11, 1979");
168 johnWayneMap.put(PersonJAXBSchema.BIO_NOTE, "born Marion Robert Morrison and better" +
169 "known by his stage name John Wayne, was an American film actor, director " +
170 "and producer. He epitomized rugged masculinity and has become an enduring " +
171 "American icon. He is famous for his distinctive voice, walk and height. " +
172 "He was also known for his conservative political views and his support in " +
173 "the 1950s for anti-communist positions.");
174 String refName = createPersonRefName(authRefName, "John Wayne");
175 MultipartOutput multipart =
176 PersonAuthorityClientUtils.createPersonInstance(vcsid, refName, johnWayneMap,
177 client.getItemCommonPartName() );
178 ClientResponse<Response> res = client.createItem(vcsid, multipart);
179 int statusCode = res.getStatus();
180 String extractedID = PersonAuthorityClientUtils.extractId(res);
182 // Check the status code of the response: does it match
183 // the expected response(s)?
184 if(logger.isDebugEnabled()){
185 logger.debug(testName + ": status = " + statusCode);
187 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
188 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
189 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
192 // Store the ID returned from the first item resource created
193 // for additional tests below.
194 if (knownItemResourceId == null){
195 knownItemResourceId = extractedID;
196 if (logger.isDebugEnabled()) {
197 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
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.
205 // Item resource IDs are unique, so these are used as keys;
206 // the non-unique IDs of their parents are stored as associated values.
207 allResourceItemIdsCreated.put(extractedID, vcsid);
213 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
214 dependsOnMethods = {"create", "createItem"})
215 public void createList(String testName) throws Exception {
216 for (int i = 0; i < 3; i++) {
218 knownResourceId = lastPersonAuthId;
219 if (logger.isDebugEnabled()) {
220 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
222 // Add nItemsToCreateInList items to each personauthority
223 for (int j = 0; j < nItemsToCreateInList; j++) {
224 createItem(testName);
230 // Placeholders until the three tests below can be uncommented.
231 // See Issue CSPACE-401.
233 public void createWithEmptyEntityBody(String testName) throws Exception {
237 public void createWithMalformedXml(String testName) throws Exception {
241 public void createWithWrongXmlSchema(String testName) throws Exception {
246 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
247 dependsOnMethods = {"create", "testSubmitRequest"})
248 public void createWithEmptyEntityBody(String testName) throws Exception {
251 setupCreateWithEmptyEntityBody(testName);
253 // Submit the request to the service and store the response.
254 String method = REQUEST_TYPE.httpMethodName();
255 String url = getServiceRootURL();
256 String mediaType = MediaType.APPLICATION_XML;
257 final String entity = "";
258 int statusCode = submitRequest(method, url, mediaType, entity);
260 // Check the status code of the response: does it match
261 // the expected response(s)?
262 if(logger.isDebugEnabled()) {
263 logger.debug(testName + ": url=" + url +
264 " status=" + statusCode);
266 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
267 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
268 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
272 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
273 dependsOnMethods = {"create", "testSubmitRequest"})
274 public void createWithMalformedXml(String testName) throws Exception {
277 setupCreateWithMalformedXml(testName);
279 // Submit the request to the service and store the response.
280 String method = REQUEST_TYPE.httpMethodName();
281 String url = getServiceRootURL();
282 String mediaType = MediaType.APPLICATION_XML;
283 final String entity = MALFORMED_XML_DATA; // Constant from base class.
284 int statusCode = submitRequest(method, url, mediaType, entity);
286 // Check the status code of the response: does it match
287 // the expected response(s)?
288 if(logger.isDebugEnabled()){
289 logger.debug(testName + ": url=" + url +
290 " status=" + statusCode);
292 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
293 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
294 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
298 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
299 dependsOnMethods = {"create", "testSubmitRequest"})
300 public void createWithWrongXmlSchema(String testName) throws Exception {
303 setupCreateWithWrongXmlSchema(testName);
305 // Submit the request to the service and store the response.
306 String method = REQUEST_TYPE.httpMethodName();
307 String url = getServiceRootURL();
308 String mediaType = MediaType.APPLICATION_XML;
309 final String entity = WRONG_XML_SCHEMA_DATA;
310 int statusCode = submitRequest(method, url, mediaType, entity);
312 // Check the status code of the response: does it match
313 // the expected response(s)?
314 if(logger.isDebugEnabled()){
315 logger.debug(testName + ": url=" + url +
316 " status=" + statusCode);
318 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
319 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
320 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
324 // ---------------------------------------------------------------
325 // CRUD tests : READ tests
326 // ---------------------------------------------------------------
329 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
330 dependsOnMethods = {"create"})
331 public void read(String testName) throws Exception {
336 // Submit the request to the service and store the response.
337 ClientResponse<MultipartInput> res = client.read(knownResourceId);
338 int statusCode = res.getStatus();
340 // Check the status code of the response: does it match
341 // the expected response(s)?
342 if(logger.isDebugEnabled()){
343 logger.debug(testName + ": status = " + statusCode);
345 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
346 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
347 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
348 //FIXME: remove the following try catch once Aron fixes signatures
350 MultipartInput input = (MultipartInput) res.getEntity();
351 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
352 client.getCommonPartName(), PersonauthoritiesCommon.class);
353 Assert.assertNotNull(personAuthority);
354 } catch (Exception e) {
355 throw new RuntimeException(e);
360 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
361 dependsOnMethods = {"read"})
362 public void readByName(String testName) throws Exception {
367 // Submit the request to the service and store the response.
368 ClientResponse<MultipartInput> res = client.read(knownResourceId);
369 int statusCode = res.getStatus();
371 // Check the status code of the response: does it match
372 // the expected response(s)?
373 if(logger.isDebugEnabled()){
374 logger.debug(testName + ": status = " + statusCode);
376 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
377 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
378 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
379 //FIXME: remove the following try catch once Aron fixes signatures
381 MultipartInput input = (MultipartInput) res.getEntity();
382 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
383 client.getCommonPartName(), PersonauthoritiesCommon.class);
384 Assert.assertNotNull(personAuthority);
385 } catch (Exception e) {
386 throw new RuntimeException(e);
391 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
392 dependsOnMethods = {"createItem", "read"})
393 public void readItem(String testName) throws Exception {
398 // Submit the request to the service and store the response.
399 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
400 int statusCode = res.getStatus();
402 // Check the status code of the response: does it match
403 // the expected response(s)?
404 if(logger.isDebugEnabled()){
405 logger.debug(testName + ": status = " + statusCode);
407 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
408 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
409 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
411 // Check whether we've received a person.
412 MultipartInput input = (MultipartInput) res.getEntity();
413 PersonsCommon person = (PersonsCommon) extractPart(input,
414 client.getItemCommonPartName(), PersonsCommon.class);
415 Assert.assertNotNull(person);
416 boolean showFull = true;
417 if(showFull && logger.isDebugEnabled()){
418 logger.debug(testName + ": returned payload:");
419 logger.debug(objectAsXmlString(person, PersonsCommon.class));
421 Assert.assertEquals(person.getInAuthority(), knownResourceId);
427 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
428 dependsOnMethods = {"read"})
429 public void readNonExistent(String testName) {
432 setupReadNonExistent(testName);
434 // Submit the request to the service and store the response.
435 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
436 int statusCode = res.getStatus();
438 // Check the status code of the response: does it match
439 // the expected response(s)?
440 if(logger.isDebugEnabled()){
441 logger.debug(testName + ": status = " + statusCode);
443 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
444 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
445 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
448 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
449 dependsOnMethods = {"readItem", "readNonExistent"})
450 public void readItemNonExistent(String testName) {
453 setupReadNonExistent(testName);
455 // Submit the request to the service and store the response.
456 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
457 int statusCode = res.getStatus();
459 // Check the status code of the response: does it match
460 // the expected response(s)?
461 if(logger.isDebugEnabled()){
462 logger.debug(testName + ": status = " + statusCode);
464 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
465 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
466 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
468 // ---------------------------------------------------------------
469 // CRUD tests : READ_LIST tests
470 // ---------------------------------------------------------------
474 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
475 dependsOnMethods = {"createList", "read"})
476 public void readList(String testName) throws Exception {
479 setupReadList(testName);
481 // Submit the request to the service and store the response.
482 ClientResponse<PersonauthoritiesCommonList> res = client.readList();
483 PersonauthoritiesCommonList list = res.getEntity();
484 int statusCode = res.getStatus();
486 // Check the status code of the response: does it match
487 // the expected response(s)?
488 if(logger.isDebugEnabled()){
489 logger.debug(testName + ": status = " + statusCode);
491 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
492 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
493 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
495 // Optionally output additional data about list members for debugging.
496 boolean iterateThroughList = false;
497 if (iterateThroughList && logger.isDebugEnabled()) {
498 List<PersonauthoritiesCommonList.PersonauthorityListItem> items =
499 list.getPersonauthorityListItem();
501 for (PersonauthoritiesCommonList.PersonauthorityListItem item : items) {
502 String csid = item.getCsid();
503 logger.debug(testName + ": list-item[" + i + "] csid=" +
505 logger.debug(testName + ": list-item[" + i + "] displayName=" +
506 item.getDisplayName());
507 logger.debug(testName + ": list-item[" + i + "] URI=" +
515 @Test(dependsOnMethods = {"createList", "readItem"})
516 public void readItemList() {
517 readItemList(knownResourceId);
520 private void readItemList(String vcsid) {
522 final String testName = "readItemList";
525 setupReadList(testName);
527 // Submit the request to the service and store the response.
528 ClientResponse<PersonsCommonList> res =
529 client.readItemList(vcsid);
530 PersonsCommonList list = res.getEntity();
531 int statusCode = res.getStatus();
533 // Check the status code of the response: does it match
534 // the expected response(s)?
535 if(logger.isDebugEnabled()){
536 logger.debug(" " + testName + ": status = " + statusCode);
538 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
539 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
540 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
542 List<PersonsCommonList.PersonListItem> items =
543 list.getPersonListItem();
544 int nItemsReturned = items.size();
545 if(logger.isDebugEnabled()){
546 logger.debug(" " + testName + ": Expected "
547 + nItemsToCreateInList+" items; got: "+nItemsReturned);
549 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
552 for (PersonsCommonList.PersonListItem item : items) {
553 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
554 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
555 // Optionally output additional data about list members for debugging.
556 boolean showDetails = true;
557 if (showDetails && logger.isDebugEnabled()) {
558 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
560 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
561 item.getDisplayName());
562 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
571 // ---------------------------------------------------------------
572 // CRUD tests : UPDATE tests
573 // ---------------------------------------------------------------
576 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
577 dependsOnMethods = {"read"})
578 public void update(String testName) throws Exception {
581 setupUpdate(testName);
583 // Retrieve the contents of a resource to update.
584 ClientResponse<MultipartInput> res =
585 client.read(knownResourceId);
586 if(logger.isDebugEnabled()){
587 logger.debug(testName + ": read status = " + res.getStatus());
589 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
591 if(logger.isDebugEnabled()){
592 logger.debug("got PersonAuthority to update with ID: " + knownResourceId);
594 MultipartInput input = (MultipartInput) res.getEntity();
595 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
596 client.getCommonPartName(), PersonauthoritiesCommon.class);
597 Assert.assertNotNull(personAuthority);
599 // Update the contents of this resource.
600 personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
601 personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
602 if(logger.isDebugEnabled()){
603 logger.debug("to be updated PersonAuthority");
604 logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
607 // Submit the updated resource to the service and store the response.
608 MultipartOutput output = new MultipartOutput();
609 OutputPart commonPart = output.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);
610 commonPart.getHeaders().add("label", client.getCommonPartName());
611 res = client.update(knownResourceId, output);
612 int statusCode = res.getStatus();
614 // Check the status code of the response: does it match the expected response(s)?
615 if(logger.isDebugEnabled()){
616 logger.debug("update: status = " + statusCode);
618 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
619 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
620 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
622 // Retrieve the updated resource and verify that its contents exist.
623 input = (MultipartInput) res.getEntity();
624 PersonauthoritiesCommon updatedPersonAuthority =
625 (PersonauthoritiesCommon) extractPart(input,
626 client.getCommonPartName(), PersonauthoritiesCommon.class);
627 Assert.assertNotNull(updatedPersonAuthority);
629 // Verify that the updated resource received the correct data.
630 Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
631 personAuthority.getDisplayName(),
632 "Data in updated object did not match submitted data.");
635 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
636 dependsOnMethods = {"readItem", "update"})
637 public void updateItem(String testName) throws Exception {
640 setupUpdate(testName);
642 ClientResponse<MultipartInput> res =
643 client.readItem(knownResourceId, knownItemResourceId);
644 if(logger.isDebugEnabled()){
645 logger.debug(testName + ": read status = " + res.getStatus());
647 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
649 if(logger.isDebugEnabled()){
650 logger.debug("got Person to update with ID: " +
651 knownItemResourceId +
652 " in PersonAuthority: " + knownResourceId );
654 MultipartInput input = (MultipartInput) res.getEntity();
655 PersonsCommon person = (PersonsCommon) extractPart(input,
656 client.getItemCommonPartName(), PersonsCommon.class);
657 Assert.assertNotNull(person);
659 // Update the contents of this resource.
660 person.setForeName("updated-" + person.getForeName());
661 if(logger.isDebugEnabled()){
662 logger.debug("to be updated Person");
663 logger.debug(objectAsXmlString(person,
664 PersonsCommon.class));
667 // Submit the updated resource to the service and store the response.
668 MultipartOutput output = new MultipartOutput();
669 OutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
670 commonPart.getHeaders().add("label", client.getItemCommonPartName());
671 res = client.updateItem(knownResourceId, knownItemResourceId, output);
672 int statusCode = res.getStatus();
674 // Check the status code of the response: does it match the expected response(s)?
675 if(logger.isDebugEnabled()){
676 logger.debug("updateItem: status = " + statusCode);
678 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
679 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
680 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
682 // Retrieve the updated resource and verify that its contents exist.
683 input = (MultipartInput) res.getEntity();
684 PersonsCommon updatedPerson =
685 (PersonsCommon) extractPart(input,
686 client.getItemCommonPartName(), PersonsCommon.class);
687 Assert.assertNotNull(updatedPerson);
689 // Verify that the updated resource received the correct data.
690 Assert.assertEquals(updatedPerson.getForeName(),
691 person.getForeName(),
692 "Data in updated Person did not match submitted data.");
696 // Placeholders until the three tests below can be uncommented.
697 // See Issue CSPACE-401.
699 public void updateWithEmptyEntityBody(String testName) throws Exception {
703 public void updateWithMalformedXml(String testName) throws Exception {
707 public void updateWithWrongXmlSchema(String testName) throws Exception {
712 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
713 dependsOnMethods = {"create", "update", "testSubmitRequest"})
714 public void updateWithEmptyEntityBody(String testName) throws Exception {
717 setupUpdateWithEmptyEntityBody(testName);
719 // Submit the request to the service and store the response.
720 String method = REQUEST_TYPE.httpMethodName();
721 String url = getResourceURL(knownResourceId);
722 String mediaType = MediaType.APPLICATION_XML;
723 final String entity = "";
724 int statusCode = submitRequest(method, url, mediaType, entity);
726 // Check the status code of the response: does it match
727 // the expected response(s)?
728 if(logger.isDebugEnabled()){
729 logger.debug(testName + ": url=" + url +
730 " status=" + statusCode);
732 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
733 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
734 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
738 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
739 dependsOnMethods = {"create", "update", "testSubmitRequest"})
740 public void updateWithMalformedXml(String testName) throws Exception {
743 setupUpdateWithMalformedXml(testName);
745 // Submit the request to the service and store the response.
746 String method = REQUEST_TYPE.httpMethodName();
747 String url = getResourceURL(knownResourceId);
748 String mediaType = MediaType.APPLICATION_XML;
749 final String entity = MALFORMED_XML_DATA;
750 int statusCode = submitRequest(method, url, mediaType, entity);
752 // Check the status code of the response: does it match
753 // the expected response(s)?
754 if(logger.isDebugEnabled()){
755 logger.debug(testName + ": url=" + url +
756 " status=" + statusCode);
758 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
759 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
760 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
764 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
765 dependsOnMethods = {"create", "update", "testSubmitRequest"})
766 public void updateWithWrongXmlSchema(String testName) throws Exception {
769 setupUpdateWithWrongXmlSchema(testName);
771 // Submit the request to the service and store the response.
772 String method = REQUEST_TYPE.httpMethodName();
773 String url = getResourceURL(knownResourceId);
774 String mediaType = MediaType.APPLICATION_XML;
775 final String entity = WRONG_XML_SCHEMA_DATA;
776 int statusCode = submitRequest(method, url, mediaType, entity);
778 // Check the status code of the response: does it match
779 // the expected response(s)?
780 if(logger.isDebugEnabled()){
781 logger.debug("updateWithWrongXmlSchema: url=" + url +
782 " status=" + statusCode);
784 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
785 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
786 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
792 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
793 dependsOnMethods = {"update", "testSubmitRequest"})
794 public void updateNonExistent(String testName) throws Exception {
797 setupUpdateNonExistent(testName);
799 // Submit the request to the service and store the response.
800 // Note: The ID used in this 'create' call may be arbitrary.
801 // The only relevant ID may be the one used in update(), below.
803 // The only relevant ID may be the one used in update(), below.
804 MultipartOutput multipart = createPersonAuthorityInstance(NON_EXISTENT_ID);
805 ClientResponse<MultipartInput> res =
806 client.update(NON_EXISTENT_ID, multipart);
807 int statusCode = res.getStatus();
809 // Check the status code of the response: does it match
810 // the expected response(s)?
811 if(logger.isDebugEnabled()){
812 logger.debug(testName + ": status = " + statusCode);
814 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
815 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
816 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
819 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
820 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
821 public void updateNonExistentItem(String testName) throws Exception {
824 setupUpdateNonExistent(testName);
826 // Submit the request to the service and store the response.
827 // Note: The ID used in this 'create' call may be arbitrary.
828 // The only relevant ID may be the one used in update(), below.
830 // The only relevant ID may be the one used in update(), below.
831 Map<String, String> nonexMap = new HashMap<String,String>();
832 nonexMap.put(PersonJAXBSchema.FORE_NAME, "John");
833 nonexMap.put(PersonJAXBSchema.SUR_NAME, "Wayne");
834 nonexMap.put(PersonJAXBSchema.GENDER, "male");
835 MultipartOutput multipart =
836 PersonAuthorityClientUtils.createPersonInstance(NON_EXISTENT_ID,
837 createPersonRefName(NON_EXISTENT_ID, NON_EXISTENT_ID), nonexMap,
838 client.getItemCommonPartName() );
839 ClientResponse<MultipartInput> res =
840 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
841 int statusCode = res.getStatus();
843 // Check the status code of the response: does it match
844 // the expected response(s)?
845 if(logger.isDebugEnabled()){
846 logger.debug(testName + ": status = " + statusCode);
848 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
849 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
850 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
853 // ---------------------------------------------------------------
854 // CRUD tests : DELETE tests
855 // ---------------------------------------------------------------
858 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
859 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
860 public void delete(String testName) throws Exception {
863 setupDelete(testName);
865 // Submit the request to the service and store the response.
866 ClientResponse<Response> res = client.delete(knownResourceId);
867 int statusCode = res.getStatus();
869 // Check the status code of the response: does it match
870 // the expected response(s)?
871 if(logger.isDebugEnabled()){
872 logger.debug(testName + ": status = " + statusCode);
874 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
875 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
876 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
879 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
880 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
882 public void deleteItem(String testName) throws Exception {
885 setupDelete(testName);
887 // Submit the request to the service and store the response.
888 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
889 int statusCode = res.getStatus();
891 // Check the status code of the response: does it match
892 // the expected response(s)?
893 if(logger.isDebugEnabled()){
894 logger.debug("delete: status = " + statusCode);
896 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
897 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
898 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
903 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
904 dependsOnMethods = {"delete"})
905 public void deleteNonExistent(String testName) throws Exception {
908 setupDeleteNonExistent(testName);
910 // Submit the request to the service and store the response.
911 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
912 int statusCode = res.getStatus();
914 // Check the status code of the response: does it match
915 // the expected response(s)?
916 if(logger.isDebugEnabled()){
917 logger.debug(testName + ": status = " + statusCode);
919 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
920 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
921 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
924 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
925 dependsOnMethods = {"deleteItem"})
926 public void deleteNonExistentItem(String testName) {
929 setupDeleteNonExistent(testName);
931 // Submit the request to the service and store the response.
932 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
933 int statusCode = res.getStatus();
935 // Check the status code of the response: does it match
936 // the expected response(s)?
937 if(logger.isDebugEnabled()){
938 logger.debug(testName + ": status = " + statusCode);
940 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
941 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
942 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
945 // ---------------------------------------------------------------
946 // Utility tests : tests of code used in tests above
947 // ---------------------------------------------------------------
949 * Tests the code for manually submitting data that is used by several
950 * of the methods above.
952 @Test(dependsOnMethods = {"create", "read"})
953 public void testSubmitRequest() {
955 // Expected status code: 200 OK
956 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
958 // Submit the request to the service and store the response.
959 String method = ServiceRequestType.READ.httpMethodName();
960 String url = getResourceURL(knownResourceId);
961 int statusCode = submitRequest(method, url);
963 // Check the status code of the response: does it match
964 // the expected response(s)?
965 if(logger.isDebugEnabled()){
966 logger.debug("testSubmitRequest: url=" + url +
967 " status=" + statusCode);
969 Assert.assertEquals(statusCode, EXPECTED_STATUS);
973 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
974 public void testItemSubmitRequest() {
976 // Expected status code: 200 OK
977 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
979 // Submit the request to the service and store the response.
980 String method = ServiceRequestType.READ.httpMethodName();
981 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
982 int statusCode = submitRequest(method, url);
984 // Check the status code of the response: does it match
985 // the expected response(s)?
986 if(logger.isDebugEnabled()){
987 logger.debug("testItemSubmitRequest: url=" + url +
988 " status=" + statusCode);
990 Assert.assertEquals(statusCode, EXPECTED_STATUS);
994 // ---------------------------------------------------------------
995 // Cleanup of resources created during testing
996 // ---------------------------------------------------------------
999 * Deletes all resources created by tests, after all tests have been run.
1001 * This cleanup method will always be run, even if one or more tests fail.
1002 * For this reason, it attempts to remove all resources created
1003 * at any point during testing, even if some of those resources
1004 * may be expected to be deleted by certain tests.
1006 @AfterClass(alwaysRun=true)
1007 public void cleanUp() {
1008 if (logger.isDebugEnabled()) {
1009 logger.debug("Cleaning up temporary resources created for testing ...");
1011 // Clean up person resources.
1012 String personAuthorityResourceId;
1013 String personResourceId;
1014 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1015 personResourceId = entry.getKey();
1016 personAuthorityResourceId = entry.getValue();
1017 // Note: Any non-success responses are ignored and not reported.
1018 ClientResponse<Response> res =
1019 client.deleteItem(personAuthorityResourceId, personResourceId);
1021 // Clean up personAuthority resources.
1022 for (String resourceId : allResourceIdsCreated) {
1023 // Note: Any non-success responses are ignored and not reported.
1024 ClientResponse<Response> res = client.delete(resourceId);
1028 // ---------------------------------------------------------------
1029 // Utility methods used by tests above
1030 // ---------------------------------------------------------------
1032 public String getServicePathComponent() {
1033 return SERVICE_PATH_COMPONENT;
1036 public String getItemServicePathComponent() {
1037 return ITEM_SERVICE_PATH_COMPONENT;
1041 * Returns the root URL for a service.
1043 * This URL consists of a base URL for all services, followed by
1044 * a path component for the owning personAuthority, followed by the
1045 * path component for the items.
1047 * @return The root URL for a service.
1049 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1050 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1054 * Returns the URL of a specific resource managed by a service, and
1055 * designated by an identifier (such as a universally unique ID, or UUID).
1057 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1059 * @return The URL of a specific resource managed by a service.
1061 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1062 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1065 private MultipartOutput createPersonAuthorityInstance(String identifier) {
1066 String displayName = "displayName-" + identifier;
1067 String refName = createPersonAuthRefName(displayName);
1069 PersonAuthorityClientUtils.createPersonAuthorityInstance(
1070 displayName, refName, client.getCommonPartName());