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;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.client.PersonAuthorityClient;
33 import org.collectionspace.services.person.PersonauthoritiesCommon;
34 import org.collectionspace.services.person.PersonauthoritiesCommonList;
35 import org.collectionspace.services.person.PersonsCommon;
36 import org.collectionspace.services.person.PersonsCommonList;
38 import org.jboss.resteasy.client.ClientResponse;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
40 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
41 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.testng.Assert;
45 import org.testng.annotations.AfterClass;
46 import org.testng.annotations.Test;
49 * PersonAuthorityServiceTest, carries out tests against a
50 * deployed and running PersonAuthority Service.
52 * $LastChangedRevision: 753 $
53 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
55 public class PersonAuthorityServiceTest extends AbstractServiceTest {
57 private final Logger logger =
58 LoggerFactory.getLogger(PersonAuthorityServiceTest.class);
60 // Instance variables specific to this test.
61 private PersonAuthorityClient client = new PersonAuthorityClient();
62 final String SERVICE_PATH_COMPONENT = "personauthorities";
63 final String ITEM_SERVICE_PATH_COMPONENT = "items";
64 private String knownResourceId = null;
65 private String lastPersonAuthId = null;
66 private String knownResourceRefName = null;
67 private String knownItemResourceId = null;
68 private int nItemsToCreateInList = 3;
69 private List<String> allResourceIdsCreated = new ArrayList<String>();
70 private Map<String, String> allResourceItemIdsCreated =
71 new HashMap<String, String>();
73 protected String createPersonAuthRefName(String personAuthorityName) {
74 return "urn:cspace:org.collectionspace.demo:personauthority:name("
75 +personAuthorityName+")";
78 protected String createPersonRefName(
79 String personAuthRefName, String personName) {
80 return personAuthRefName+":person:name("+personName+")";
84 // ---------------------------------------------------------------
85 // CRUD tests : CREATE tests
86 // ---------------------------------------------------------------
89 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
90 public void create(String testName) throws Exception {
92 // Perform setup, such as initializing the type of service request
93 // (e.g. CREATE, DELETE), its valid and expected status codes, and
94 // its associated HTTP method name (e.g. POST, DELETE).
95 setupCreate(testName);
97 // Submit the request to the service and store the response.
98 String identifier = createIdentifier();
99 String displayName = "displayName-" + identifier;
100 String refName = createPersonAuthRefName(displayName);
101 String typeName = "vocabType-" + identifier;
102 MultipartOutput multipart =
103 createPersonAuthorityInstance(displayName, refName, typeName);
104 ClientResponse<Response> res = client.create(multipart);
105 int statusCode = res.getStatus();
107 // Check the status code of the response: does it match
108 // the expected response(s)?
111 // Does it fall within the set of valid status codes?
112 // Does it exactly match the expected status code?
113 if(logger.isDebugEnabled()){
114 logger.debug(testName + ": status = " + statusCode);
116 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
117 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
118 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
120 // Store the refname from the first resource created
121 // for additional tests below.
122 knownResourceRefName = refName;
124 lastPersonAuthId = extractId(res);
125 // Store the ID returned from the first resource created
126 // for additional tests below.
127 if (knownResourceId == null){
128 knownResourceId = lastPersonAuthId;
129 if (logger.isDebugEnabled()) {
130 logger.debug(testName + ": knownResourceId=" + knownResourceId);
133 // Store the IDs from every resource created by tests,
134 // so they can be deleted after tests have been run.
135 allResourceIdsCreated.add(extractId(res));
139 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
140 dependsOnMethods = {"create"})
141 public void createItem(String testName) {
142 setupCreate(testName);
144 knownItemResourceId = createItemInAuthority(lastPersonAuthId, knownResourceRefName);
145 if(logger.isDebugEnabled()){
146 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
150 private String createItemInAuthority(String vcsid, String authRefName) {
152 final String testName = "createItemInAuthority";
153 if(logger.isDebugEnabled()){
154 logger.debug(testName + ":...");
157 // Submit the request to the service and store the response.
158 String identifier = createIdentifier();
159 String refName = createPersonRefName(authRefName, "Patrick Schmitz");
160 MultipartOutput multipart = createPersonInstance(vcsid, refName,
161 "Patrick","Lee","Schmitz","pls", null, null, null,
162 "1960", null, "Detroit, MI", null, "Coders", "American", "male",
163 null, null, null, null );
164 ClientResponse<Response> res = client.createItem(vcsid, multipart);
165 int statusCode = res.getStatus();
167 // Check the status code of the response: does it match
168 // the expected response(s)?
169 if(logger.isDebugEnabled()){
170 logger.debug(testName + ": status = " + statusCode);
172 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
176 // Store the ID returned from the first item resource created
177 // for additional tests below.
178 if (knownItemResourceId == null){
179 knownItemResourceId = extractId(res);
180 if (logger.isDebugEnabled()) {
181 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
185 // Store the IDs from any item resources created
186 // by tests, along with the IDs of their parents, so these items
187 // can be deleted after all tests have been run.
189 // Item resource IDs are unique, so these are used as keys;
190 // the non-unique IDs of their parents are stored as associated values.
191 allResourceItemIdsCreated.put(extractId(res), vcsid);
193 return extractId(res);
197 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
198 dependsOnMethods = {"create", "createItem"})
199 public void createList(String testName) throws Exception {
200 for (int i = 0; i < 3; i++) {
202 knownResourceId = lastPersonAuthId;
203 if (logger.isDebugEnabled()) {
204 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
206 // Add nItemsToCreateInList items to each personauthority
207 for (int j = 0; j < nItemsToCreateInList; j++) {
208 createItem(testName);
214 // Placeholders until the three tests below can be uncommented.
215 // See Issue CSPACE-401.
217 public void createWithEmptyEntityBody(String testName) throws Exception {
221 public void createWithMalformedXml(String testName) throws Exception {
225 public void createWithWrongXmlSchema(String testName) throws Exception {
230 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
231 dependsOnMethods = {"create", "testSubmitRequest"})
232 public void createWithEmptyEntityBody(String testName) throws Exception {
235 setupCreateWithEmptyEntityBody(testName);
237 // Submit the request to the service and store the response.
238 String method = REQUEST_TYPE.httpMethodName();
239 String url = getServiceRootURL();
240 String mediaType = MediaType.APPLICATION_XML;
241 final String entity = "";
242 int statusCode = submitRequest(method, url, mediaType, entity);
244 // Check the status code of the response: does it match
245 // the expected response(s)?
246 if(logger.isDebugEnabled()) {
247 logger.debug(testName + ": url=" + url +
248 " status=" + statusCode);
250 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
251 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
252 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
256 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
257 dependsOnMethods = {"create", "testSubmitRequest"})
258 public void createWithMalformedXml(String testName) throws Exception {
261 setupCreateWithMalformedXml(testName);
263 // Submit the request to the service and store the response.
264 String method = REQUEST_TYPE.httpMethodName();
265 String url = getServiceRootURL();
266 String mediaType = MediaType.APPLICATION_XML;
267 final String entity = MALFORMED_XML_DATA; // Constant from base class.
268 int statusCode = submitRequest(method, url, mediaType, entity);
270 // Check the status code of the response: does it match
271 // the expected response(s)?
272 if(logger.isDebugEnabled()){
273 logger.debug(testName + ": url=" + url +
274 " status=" + statusCode);
276 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
277 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
278 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
282 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
283 dependsOnMethods = {"create", "testSubmitRequest"})
284 public void createWithWrongXmlSchema(String testName) throws Exception {
287 setupCreateWithWrongXmlSchema(testName);
289 // Submit the request to the service and store the response.
290 String method = REQUEST_TYPE.httpMethodName();
291 String url = getServiceRootURL();
292 String mediaType = MediaType.APPLICATION_XML;
293 final String entity = WRONG_XML_SCHEMA_DATA;
294 int statusCode = submitRequest(method, url, mediaType, entity);
296 // Check the status code of the response: does it match
297 // the expected response(s)?
298 if(logger.isDebugEnabled()){
299 logger.debug(testName + ": url=" + url +
300 " status=" + statusCode);
302 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
303 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
304 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
308 // ---------------------------------------------------------------
309 // CRUD tests : READ tests
310 // ---------------------------------------------------------------
313 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
314 dependsOnMethods = {"create"})
315 public void read(String testName) throws Exception {
320 // Submit the request to the service and store the response.
321 ClientResponse<MultipartInput> res = client.read(knownResourceId);
322 int statusCode = res.getStatus();
324 // Check the status code of the response: does it match
325 // the expected response(s)?
326 if(logger.isDebugEnabled()){
327 logger.debug(testName + ": status = " + statusCode);
329 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
330 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
331 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
332 //FIXME: remove the following try catch once Aron fixes signatures
334 MultipartInput input = (MultipartInput) res.getEntity();
335 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
336 client.getCommonPartName(), PersonauthoritiesCommon.class);
337 Assert.assertNotNull(personAuthority);
338 } catch (Exception e) {
339 throw new RuntimeException(e);
344 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
345 dependsOnMethods = {"read"})
346 public void readByName(String testName) throws Exception {
351 // Submit the request to the service and store the response.
352 ClientResponse<MultipartInput> res = client.read(knownResourceId);
353 int statusCode = res.getStatus();
355 // Check the status code of the response: does it match
356 // the expected response(s)?
357 if(logger.isDebugEnabled()){
358 logger.debug(testName + ": status = " + statusCode);
360 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
361 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
362 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
363 //FIXME: remove the following try catch once Aron fixes signatures
365 MultipartInput input = (MultipartInput) res.getEntity();
366 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
367 client.getCommonPartName(), PersonauthoritiesCommon.class);
368 Assert.assertNotNull(personAuthority);
369 } catch (Exception e) {
370 throw new RuntimeException(e);
375 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
376 dependsOnMethods = {"createItem", "read"})
377 public void readItem(String testName) throws Exception {
382 // Submit the request to the service and store the response.
383 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
384 int statusCode = res.getStatus();
386 // Check the status code of the response: does it match
387 // the expected response(s)?
388 if(logger.isDebugEnabled()){
389 logger.debug(testName + ": status = " + statusCode);
391 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
392 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
393 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
395 // Check whether we've received a person.
396 MultipartInput input = (MultipartInput) res.getEntity();
397 PersonsCommon person = (PersonsCommon) extractPart(input,
398 client.getItemCommonPartName(), PersonsCommon.class);
399 Assert.assertNotNull(person);
405 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
406 dependsOnMethods = {"read"})
407 public void readNonExistent(String testName) {
410 setupReadNonExistent(testName);
412 // Submit the request to the service and store the response.
413 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
414 int statusCode = res.getStatus();
416 // Check the status code of the response: does it match
417 // the expected response(s)?
418 if(logger.isDebugEnabled()){
419 logger.debug(testName + ": status = " + statusCode);
421 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
422 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
423 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
426 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
427 dependsOnMethods = {"readItem", "readNonExistent"})
428 public void readItemNonExistent(String testName) {
431 setupReadNonExistent(testName);
433 // Submit the request to the service and store the response.
434 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
435 int statusCode = res.getStatus();
437 // Check the status code of the response: does it match
438 // the expected response(s)?
439 if(logger.isDebugEnabled()){
440 logger.debug(testName + ": status = " + statusCode);
442 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
443 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
444 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
446 // ---------------------------------------------------------------
447 // CRUD tests : READ_LIST tests
448 // ---------------------------------------------------------------
452 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
453 dependsOnMethods = {"createList", "read"})
454 public void readList(String testName) throws Exception {
457 setupReadList(testName);
459 // Submit the request to the service and store the response.
460 ClientResponse<PersonauthoritiesCommonList> res = client.readList();
461 PersonauthoritiesCommonList list = res.getEntity();
462 int statusCode = res.getStatus();
464 // Check the status code of the response: does it match
465 // the expected response(s)?
466 if(logger.isDebugEnabled()){
467 logger.debug(testName + ": status = " + statusCode);
469 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
473 // Optionally output additional data about list members for debugging.
474 boolean iterateThroughList = false;
475 if (iterateThroughList && logger.isDebugEnabled()) {
476 List<PersonauthoritiesCommonList.PersonauthorityListItem> items =
477 list.getPersonauthorityListItem();
479 for (PersonauthoritiesCommonList.PersonauthorityListItem item : items) {
480 String csid = item.getCsid();
481 logger.debug(testName + ": list-item[" + i + "] csid=" +
483 logger.debug(testName + ": list-item[" + i + "] displayName=" +
484 item.getDisplayName());
485 logger.debug(testName + ": list-item[" + i + "] URI=" +
493 @Test(dependsOnMethods = {"createList", "readItem"})
494 public void readItemList() {
495 readItemList(knownResourceId);
498 private void readItemList(String vcsid) {
500 final String testName = "readItemList";
503 setupReadList(testName);
505 // Submit the request to the service and store the response.
506 ClientResponse<PersonsCommonList> res =
507 client.readItemList(vcsid);
508 PersonsCommonList list = res.getEntity();
509 int statusCode = res.getStatus();
511 // Check the status code of the response: does it match
512 // the expected response(s)?
513 if(logger.isDebugEnabled()){
514 logger.debug(" " + testName + ": status = " + statusCode);
516 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
517 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
518 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
520 List<PersonsCommonList.PersonListItem> items =
521 list.getPersonListItem();
522 int nItemsReturned = items.size();
523 if(logger.isDebugEnabled()){
524 logger.debug(" " + testName + ": Expected "
525 + nItemsToCreateInList+" items; got: "+nItemsReturned);
527 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
529 // Optionally output additional data about list members for debugging.
530 boolean iterateThroughList = false;
531 if (iterateThroughList && logger.isDebugEnabled()) {
533 for (PersonsCommonList.PersonListItem item : items) {
534 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
536 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
537 item.getDisplayName());
538 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
547 // ---------------------------------------------------------------
548 // CRUD tests : UPDATE tests
549 // ---------------------------------------------------------------
552 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
553 dependsOnMethods = {"read"})
554 public void update(String testName) throws Exception {
557 setupUpdate(testName);
559 // Retrieve the contents of a resource to update.
560 ClientResponse<MultipartInput> res =
561 client.read(knownResourceId);
562 if(logger.isDebugEnabled()){
563 logger.debug(testName + ": read status = " + res.getStatus());
565 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
567 if(logger.isDebugEnabled()){
568 logger.debug("got PersonAuthority to update with ID: " + knownResourceId);
570 MultipartInput input = (MultipartInput) res.getEntity();
571 PersonauthoritiesCommon personAuthority = (PersonauthoritiesCommon) extractPart(input,
572 client.getCommonPartName(), PersonauthoritiesCommon.class);
573 Assert.assertNotNull(personAuthority);
575 // Update the contents of this resource.
576 personAuthority.setDisplayName("updated-" + personAuthority.getDisplayName());
577 personAuthority.setVocabType("updated-" + personAuthority.getVocabType());
578 if(logger.isDebugEnabled()){
579 logger.debug("to be updated PersonAuthority");
580 logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
583 // Submit the updated resource to the service and store the response.
584 MultipartOutput output = new MultipartOutput();
585 OutputPart commonPart = output.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);
586 commonPart.getHeaders().add("label", client.getCommonPartName());
587 res = client.update(knownResourceId, output);
588 int statusCode = res.getStatus();
590 // Check the status code of the response: does it match the expected response(s)?
591 if(logger.isDebugEnabled()){
592 logger.debug("update: status = " + statusCode);
594 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
595 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
596 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
598 // Retrieve the updated resource and verify that its contents exist.
599 input = (MultipartInput) res.getEntity();
600 PersonauthoritiesCommon updatedPersonAuthority =
601 (PersonauthoritiesCommon) extractPart(input,
602 client.getCommonPartName(), PersonauthoritiesCommon.class);
603 Assert.assertNotNull(updatedPersonAuthority);
605 // Verify that the updated resource received the correct data.
606 Assert.assertEquals(updatedPersonAuthority.getDisplayName(),
607 personAuthority.getDisplayName(),
608 "Data in updated object did not match submitted data.");
611 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
612 dependsOnMethods = {"readItem", "update"})
613 public void updateItem(String testName) throws Exception {
616 setupUpdate(testName);
618 ClientResponse<MultipartInput> res =
619 client.readItem(knownResourceId, knownItemResourceId);
620 if(logger.isDebugEnabled()){
621 logger.debug(testName + ": read status = " + res.getStatus());
623 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
625 if(logger.isDebugEnabled()){
626 logger.debug("got Person to update with ID: " +
627 knownItemResourceId +
628 " in PersonAuthority: " + knownResourceId );
630 MultipartInput input = (MultipartInput) res.getEntity();
631 PersonsCommon person = (PersonsCommon) extractPart(input,
632 client.getItemCommonPartName(), PersonsCommon.class);
633 Assert.assertNotNull(person);
635 // Update the contents of this resource.
636 person.setForeName("updated-" + person.getForeName());
637 if(logger.isDebugEnabled()){
638 logger.debug("to be updated Person");
639 logger.debug(objectAsXmlString(person,
640 PersonsCommon.class));
643 // Submit the updated resource to the service and store the response.
644 MultipartOutput output = new MultipartOutput();
645 OutputPart commonPart = output.addPart(person, MediaType.APPLICATION_XML_TYPE);
646 commonPart.getHeaders().add("label", client.getItemCommonPartName());
647 res = client.updateItem(knownResourceId, knownItemResourceId, output);
648 int statusCode = res.getStatus();
650 // Check the status code of the response: does it match the expected response(s)?
651 if(logger.isDebugEnabled()){
652 logger.debug("updateItem: status = " + statusCode);
654 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
655 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
656 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
658 // Retrieve the updated resource and verify that its contents exist.
659 input = (MultipartInput) res.getEntity();
660 PersonsCommon updatedPerson =
661 (PersonsCommon) extractPart(input,
662 client.getItemCommonPartName(), PersonsCommon.class);
663 Assert.assertNotNull(updatedPerson);
665 // Verify that the updated resource received the correct data.
666 Assert.assertEquals(updatedPerson.getForeName(),
667 person.getForeName(),
668 "Data in updated Person did not match submitted data.");
672 // Placeholders until the three tests below can be uncommented.
673 // See Issue CSPACE-401.
675 public void updateWithEmptyEntityBody(String testName) throws Exception {
679 public void updateWithMalformedXml(String testName) throws Exception {
683 public void updateWithWrongXmlSchema(String testName) throws Exception {
688 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
689 dependsOnMethods = {"create", "update", "testSubmitRequest"})
690 public void updateWithEmptyEntityBody(String testName) throws Exception {
693 setupUpdateWithEmptyEntityBody(testName);
695 // Submit the request to the service and store the response.
696 String method = REQUEST_TYPE.httpMethodName();
697 String url = getResourceURL(knownResourceId);
698 String mediaType = MediaType.APPLICATION_XML;
699 final String entity = "";
700 int statusCode = submitRequest(method, url, mediaType, entity);
702 // Check the status code of the response: does it match
703 // the expected response(s)?
704 if(logger.isDebugEnabled()){
705 logger.debug(testName + ": url=" + url +
706 " status=" + statusCode);
708 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
709 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
710 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
714 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
715 dependsOnMethods = {"create", "update", "testSubmitRequest"})
716 public void updateWithMalformedXml(String testName) throws Exception {
719 setupUpdateWithMalformedXml(testName);
721 // Submit the request to the service and store the response.
722 String method = REQUEST_TYPE.httpMethodName();
723 String url = getResourceURL(knownResourceId);
724 String mediaType = MediaType.APPLICATION_XML;
725 final String entity = MALFORMED_XML_DATA;
726 int statusCode = submitRequest(method, url, mediaType, entity);
728 // Check the status code of the response: does it match
729 // the expected response(s)?
730 if(logger.isDebugEnabled()){
731 logger.debug(testName + ": url=" + url +
732 " status=" + statusCode);
734 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
735 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
736 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
740 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
741 dependsOnMethods = {"create", "update", "testSubmitRequest"})
742 public void updateWithWrongXmlSchema(String testName) throws Exception {
745 setupUpdateWithWrongXmlSchema(testName);
747 // Submit the request to the service and store the response.
748 String method = REQUEST_TYPE.httpMethodName();
749 String url = getResourceURL(knownResourceId);
750 String mediaType = MediaType.APPLICATION_XML;
751 final String entity = WRONG_XML_SCHEMA_DATA;
752 int statusCode = submitRequest(method, url, mediaType, entity);
754 // Check the status code of the response: does it match
755 // the expected response(s)?
756 if(logger.isDebugEnabled()){
757 logger.debug("updateWithWrongXmlSchema: url=" + url +
758 " status=" + statusCode);
760 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
761 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
762 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
768 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
769 dependsOnMethods = {"update", "testSubmitRequest"})
770 public void updateNonExistent(String testName) throws Exception {
773 setupUpdateNonExistent(testName);
775 // Submit the request to the service and store the response.
776 // Note: The ID used in this 'create' call may be arbitrary.
777 // The only relevant ID may be the one used in update(), below.
779 // The only relevant ID may be the one used in update(), below.
780 MultipartOutput multipart = createPersonAuthorityInstance(NON_EXISTENT_ID);
781 ClientResponse<MultipartInput> res =
782 client.update(NON_EXISTENT_ID, multipart);
783 int statusCode = res.getStatus();
785 // Check the status code of the response: does it match
786 // the expected response(s)?
787 if(logger.isDebugEnabled()){
788 logger.debug(testName + ": status = " + statusCode);
790 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
791 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
792 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
795 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
796 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
797 public void updateNonExistentItem(String testName) throws Exception {
800 setupUpdateNonExistent(testName);
802 // Submit the request to the service and store the response.
803 // Note: The ID used in this 'create' call may be arbitrary.
804 // The only relevant ID may be the one used in update(), below.
806 // The only relevant ID may be the one used in update(), below.
807 MultipartOutput multipart = createPersonInstance(NON_EXISTENT_ID,
808 createPersonRefName(NON_EXISTENT_ID, NON_EXISTENT_ID),
809 "Patrick","Lee","Schmitz","pls", null, null, null,
810 "1960", null, "Detroit, MI", null, "Coders", "American", "male",
811 null, null, null, null );
812 ClientResponse<MultipartInput> res =
813 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
814 int statusCode = res.getStatus();
816 // Check the status code of the response: does it match
817 // the expected response(s)?
818 if(logger.isDebugEnabled()){
819 logger.debug(testName + ": status = " + statusCode);
821 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
822 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
823 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
826 // ---------------------------------------------------------------
827 // CRUD tests : DELETE tests
828 // ---------------------------------------------------------------
831 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
832 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
833 public void delete(String testName) throws Exception {
836 setupDelete(testName);
838 // Submit the request to the service and store the response.
839 ClientResponse<Response> res = client.delete(knownResourceId);
840 int statusCode = res.getStatus();
842 // Check the status code of the response: does it match
843 // the expected response(s)?
844 if(logger.isDebugEnabled()){
845 logger.debug(testName + ": status = " + statusCode);
847 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
848 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
849 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
852 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
853 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
855 public void deleteItem(String testName) throws Exception {
858 setupDelete(testName);
860 // Submit the request to the service and store the response.
861 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
862 int statusCode = res.getStatus();
864 // Check the status code of the response: does it match
865 // the expected response(s)?
866 if(logger.isDebugEnabled()){
867 logger.debug("delete: status = " + statusCode);
869 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
870 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
871 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
876 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
877 dependsOnMethods = {"delete"})
878 public void deleteNonExistent(String testName) throws Exception {
881 setupDeleteNonExistent(testName);
883 // Submit the request to the service and store the response.
884 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
885 int statusCode = res.getStatus();
887 // Check the status code of the response: does it match
888 // the expected response(s)?
889 if(logger.isDebugEnabled()){
890 logger.debug(testName + ": status = " + statusCode);
892 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
893 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
894 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
897 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
898 dependsOnMethods = {"deleteItem"})
899 public void deleteNonExistentItem(String testName) {
902 setupDeleteNonExistent(testName);
904 // Submit the request to the service and store the response.
905 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
906 int statusCode = res.getStatus();
908 // Check the status code of the response: does it match
909 // the expected response(s)?
910 if(logger.isDebugEnabled()){
911 logger.debug(testName + ": status = " + statusCode);
913 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
914 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
915 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
918 // ---------------------------------------------------------------
919 // Utility tests : tests of code used in tests above
920 // ---------------------------------------------------------------
922 * Tests the code for manually submitting data that is used by several
923 * of the methods above.
925 @Test(dependsOnMethods = {"create", "read"})
926 public void testSubmitRequest() {
928 // Expected status code: 200 OK
929 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
931 // Submit the request to the service and store the response.
932 String method = ServiceRequestType.READ.httpMethodName();
933 String url = getResourceURL(knownResourceId);
934 int statusCode = submitRequest(method, url);
936 // Check the status code of the response: does it match
937 // the expected response(s)?
938 if(logger.isDebugEnabled()){
939 logger.debug("testSubmitRequest: url=" + url +
940 " status=" + statusCode);
942 Assert.assertEquals(statusCode, EXPECTED_STATUS);
946 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
947 public void testItemSubmitRequest() {
949 // Expected status code: 200 OK
950 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
952 // Submit the request to the service and store the response.
953 String method = ServiceRequestType.READ.httpMethodName();
954 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
955 int statusCode = submitRequest(method, url);
957 // Check the status code of the response: does it match
958 // the expected response(s)?
959 if(logger.isDebugEnabled()){
960 logger.debug("testItemSubmitRequest: url=" + url +
961 " status=" + statusCode);
963 Assert.assertEquals(statusCode, EXPECTED_STATUS);
967 // ---------------------------------------------------------------
968 // Cleanup of resources created during testing
969 // ---------------------------------------------------------------
972 * Deletes all resources created by tests, after all tests have been run.
974 * This cleanup method will always be run, even if one or more tests fail.
975 * For this reason, it attempts to remove all resources created
976 * at any point during testing, even if some of those resources
977 * may be expected to be deleted by certain tests.
979 @AfterClass(alwaysRun=true)
980 public void cleanUp() {
981 if (logger.isDebugEnabled()) {
982 logger.debug("Cleaning up temporary resources created for testing ...");
984 // Clean up person resources.
985 String personAuthorityResourceId;
986 String personResourceId;
987 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
988 personResourceId = entry.getKey();
989 personAuthorityResourceId = entry.getValue();
990 // Note: Any non-success responses are ignored and not reported.
991 ClientResponse<Response> res =
992 client.deleteItem(personAuthorityResourceId, personResourceId);
994 // Clean up personAuthority resources.
995 for (String resourceId : allResourceIdsCreated) {
996 // Note: Any non-success responses are ignored and not reported.
997 ClientResponse<Response> res = client.delete(resourceId);
1001 // ---------------------------------------------------------------
1002 // Utility methods used by tests above
1003 // ---------------------------------------------------------------
1005 public String getServicePathComponent() {
1006 return SERVICE_PATH_COMPONENT;
1009 public String getItemServicePathComponent() {
1010 return ITEM_SERVICE_PATH_COMPONENT;
1014 * Returns the root URL for a service.
1016 * This URL consists of a base URL for all services, followed by
1017 * a path component for the owning personAuthority, followed by the
1018 * path component for the items.
1020 * @return The root URL for a service.
1022 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1023 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1027 * Returns the URL of a specific resource managed by a service, and
1028 * designated by an identifier (such as a universally unique ID, or UUID).
1030 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1032 * @return The URL of a specific resource managed by a service.
1034 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1035 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1038 private MultipartOutput createPersonAuthorityInstance(String identifier) {
1039 String displayName = "displayName-" + identifier;
1040 String refName = createPersonAuthRefName(displayName);
1041 String typeName = "vocabType-" + identifier;
1042 return createPersonAuthorityInstance(
1043 displayName, refName,typeName );
1046 private MultipartOutput createPersonAuthorityInstance(
1047 String displayName, String refName, String vocabType) {
1048 PersonauthoritiesCommon personAuthority = new PersonauthoritiesCommon();
1049 personAuthority.setDisplayName(displayName);
1051 personAuthority.setRefName(refName);
1052 personAuthority.setVocabType(vocabType);
1053 MultipartOutput multipart = new MultipartOutput();
1054 OutputPart commonPart = multipart.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);
1055 commonPart.getHeaders().add("label", client.getCommonPartName());
1057 if(logger.isDebugEnabled()) {
1058 logger.debug("to be created, personAuthority common");
1059 logger.debug(objectAsXmlString(personAuthority, PersonauthoritiesCommon.class));
1064 private MultipartOutput createPersonInstance(String inAuthority, String refName,
1065 String foreName, String middleName, String surName,
1066 String initials, String salutation, String title, String nameAdditions,
1067 String birthDate, String deathDate, String birthPlace, String deathPlace,
1068 String group, String nationality, String gender, String occupation,
1069 String schoolOrStyle, String bioNote, String nameNote ) {
1070 PersonsCommon person = new PersonsCommon();
1071 person.setInAuthority(inAuthority);
1072 person.setRefName(refName);
1074 person.setForeName(foreName);
1075 if(middleName!=null)
1076 person.setMiddleName(middleName);
1078 person.setSurName(surName);
1080 person.setInitials(initials);
1081 if(salutation!=null)
1082 person.setSalutation(salutation);
1084 person.setTitle(title);
1085 if(nameAdditions!=null)
1086 person.setNameAdditions(nameAdditions);
1088 person.setBirthDate(birthDate);
1090 person.setDeathDate(deathDate);
1091 if(birthPlace!=null)
1092 person.setBirthPlace(birthPlace);
1093 if(deathPlace!=null)
1094 person.setDeathPlace(deathPlace);
1096 person.setGroup(group);
1097 if(nationality!=null)
1098 person.setNationality(nationality);
1100 person.setGender(gender);
1101 if(occupation!=null)
1102 person.setOccupation(occupation);
1103 if(schoolOrStyle!=null)
1104 person.setSchoolOrStyle(schoolOrStyle);
1106 person.setBioNote(bioNote);
1108 person.setNameNote(nameNote);
1109 MultipartOutput multipart = new MultipartOutput();
1110 OutputPart commonPart = multipart.addPart(person,
1111 MediaType.APPLICATION_XML_TYPE);
1112 commonPart.getHeaders().add("label", client.getItemCommonPartName());
1114 if(logger.isDebugEnabled()){
1115 logger.debug("to be created, person common");
1116 logger.debug(objectAsXmlString(person,
1117 PersonsCommon.class));