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.OrgAuthorityClient;
33 import org.collectionspace.services.organization.OrgauthoritiesCommon;
34 import org.collectionspace.services.organization.OrgauthoritiesCommonList;
35 import org.collectionspace.services.organization.OrganizationsCommon;
36 import org.collectionspace.services.organization.OrganizationsCommonList;
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 * OrgAuthorityServiceTest, carries out tests against a
50 * deployed and running OrgAuthority Service.
52 * $LastChangedRevision: 753 $
53 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
55 public class OrgAuthorityServiceTest extends AbstractServiceTest {
57 private final Logger logger =
58 LoggerFactory.getLogger(OrgAuthorityServiceTest.class);
60 // Instance variables specific to this test.
61 private OrgAuthorityClient client = new OrgAuthorityClient();
62 final String SERVICE_PATH_COMPONENT = "orgauthorities";
63 final String ITEM_SERVICE_PATH_COMPONENT = "items";
64 private String knownResourceId = null;
65 private String knownResourceRefName = null;
66 private String knownItemResourceId = null;
67 private List<String> allResourceIdsCreated = new ArrayList<String>();
68 private Map<String, String> allResourceItemIdsCreated =
69 new HashMap<String, String>();
71 protected String createRefName(String displayName) {
72 return displayName.replaceAll("\\W", "");
75 // ---------------------------------------------------------------
76 // CRUD tests : CREATE tests
77 // ---------------------------------------------------------------
80 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
81 public void create(String testName) throws Exception {
83 // Perform setup, such as initializing the type of service request
84 // (e.g. CREATE, DELETE), its valid and expected status codes, and
85 // its associated HTTP method name (e.g. POST, DELETE).
86 setupCreate(testName);
88 // Submit the request to the service and store the response.
89 String identifier = createIdentifier();
90 String displayName = "displayName-" + identifier;
91 String refName = createRefName(displayName);
92 String typeName = "vocabType-" + identifier;
93 MultipartOutput multipart =
94 createOrgAuthorityInstance(displayName, refName, typeName);
95 ClientResponse<Response> res = client.create(multipart);
96 int statusCode = res.getStatus();
98 // Check the status code of the response: does it match
99 // the expected response(s)?
102 // Does it fall within the set of valid status codes?
103 // Does it exactly match the expected status code?
104 if(logger.isDebugEnabled()){
105 logger.debug(testName + ": status = " + statusCode);
107 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
108 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
109 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
111 // Store the refname from the first resource created
112 // for additional tests below.
113 knownResourceRefName = refName;
115 // Store the ID returned from the first resource created
116 // for additional tests below.
117 if (knownResourceId == null){
118 knownResourceId = extractId(res);
119 if (logger.isDebugEnabled()) {
120 logger.debug(testName + ": knownResourceId=" + knownResourceId);
123 // Store the IDs from every resource created by tests,
124 // so they can be deleted after tests have been run.
125 allResourceIdsCreated.add(extractId(res));
129 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
130 dependsOnMethods = {"create"})
131 public void createItem(String testName) {
132 setupCreate(testName);
134 knownItemResourceId = createItemInAuthority(knownResourceId);
135 if(logger.isDebugEnabled()){
136 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
140 private String createItemInAuthority(String vcsid) {
142 final String testName = "createItemInAuthority";
143 if(logger.isDebugEnabled()){
144 logger.debug(testName + ":...");
147 // Submit the request to the service and store the response.
148 String identifier = createIdentifier();
149 String refName = createRefName(identifier);
150 MultipartOutput multipart = createOrganizationInstance(vcsid,
151 identifier, refName, "Longer Name for "+identifier,
152 "This is a fake organization that was created by a test method.");
153 ClientResponse<Response> res = client.createItem(vcsid, multipart);
154 int statusCode = res.getStatus();
156 // Check the status code of the response: does it match
157 // the expected response(s)?
158 if(logger.isDebugEnabled()){
159 logger.debug(testName + ": status = " + statusCode);
161 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
162 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
163 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
165 // Store the ID returned from the first item resource created
166 // for additional tests below.
167 if (knownItemResourceId == null){
168 knownItemResourceId = extractId(res);
169 if (logger.isDebugEnabled()) {
170 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
174 // Store the IDs from any item resources created
175 // by tests, along with the IDs of their parents, so these items
176 // can be deleted after all tests have been run.
178 // Item resource IDs are unique, so these are used as keys;
179 // the non-unique IDs of their parents are stored as associated values.
180 allResourceItemIdsCreated.put(extractId(res), vcsid);
182 return extractId(res);
186 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
187 dependsOnMethods = {"create", "createItem"})
188 public void createList(String testName) throws Exception {
189 for (int i = 0; i < 3; i++) {
191 // Add 3 items to each orgauthority
192 for (int j = 0; j < 3; j++) {
193 createItem(testName);
199 // Placeholders until the three tests below can be uncommented.
200 // See Issue CSPACE-401.
202 public void createWithEmptyEntityBody(String testName) throws Exception {
206 public void createWithMalformedXml(String testName) throws Exception {
210 public void createWithWrongXmlSchema(String testName) throws Exception {
215 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
216 dependsOnMethods = {"create", "testSubmitRequest"})
217 public void createWithEmptyEntityBody(String testName) throws Exception {
220 setupCreateWithEmptyEntityBody(testName);
222 // Submit the request to the service and store the response.
223 String method = REQUEST_TYPE.httpMethodName();
224 String url = getServiceRootURL();
225 String mediaType = MediaType.APPLICATION_XML;
226 final String entity = "";
227 int statusCode = submitRequest(method, url, mediaType, entity);
229 // Check the status code of the response: does it match
230 // the expected response(s)?
231 if(logger.isDebugEnabled()) {
232 logger.debug(testName + ": url=" + url +
233 " status=" + statusCode);
235 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
236 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
237 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
241 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
242 dependsOnMethods = {"create", "testSubmitRequest"})
243 public void createWithMalformedXml(String testName) throws Exception {
246 setupCreateWithMalformedXml(testName);
248 // Submit the request to the service and store the response.
249 String method = REQUEST_TYPE.httpMethodName();
250 String url = getServiceRootURL();
251 String mediaType = MediaType.APPLICATION_XML;
252 final String entity = MALFORMED_XML_DATA; // Constant from base class.
253 int statusCode = submitRequest(method, url, mediaType, entity);
255 // Check the status code of the response: does it match
256 // the expected response(s)?
257 if(logger.isDebugEnabled()){
258 logger.debug(testName + ": url=" + url +
259 " status=" + statusCode);
261 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
262 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
263 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
267 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
268 dependsOnMethods = {"create", "testSubmitRequest"})
269 public void createWithWrongXmlSchema(String testName) throws Exception {
272 setupCreateWithWrongXmlSchema(testName);
274 // Submit the request to the service and store the response.
275 String method = REQUEST_TYPE.httpMethodName();
276 String url = getServiceRootURL();
277 String mediaType = MediaType.APPLICATION_XML;
278 final String entity = WRONG_XML_SCHEMA_DATA;
279 int statusCode = submitRequest(method, url, mediaType, entity);
281 // Check the status code of the response: does it match
282 // the expected response(s)?
283 if(logger.isDebugEnabled()){
284 logger.debug(testName + ": url=" + url +
285 " status=" + statusCode);
287 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
288 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
289 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
293 // ---------------------------------------------------------------
294 // CRUD tests : READ tests
295 // ---------------------------------------------------------------
298 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
299 dependsOnMethods = {"create"})
300 public void read(String testName) throws Exception {
305 // Submit the request to the service and store the response.
306 ClientResponse<MultipartInput> res = client.read(knownResourceId);
307 int statusCode = res.getStatus();
309 // Check the status code of the response: does it match
310 // the expected response(s)?
311 if(logger.isDebugEnabled()){
312 logger.debug(testName + ": status = " + statusCode);
314 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
315 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
316 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
317 //FIXME: remove the following try catch once Aron fixes signatures
319 MultipartInput input = (MultipartInput) res.getEntity();
320 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
321 client.getCommonPartName(), OrgauthoritiesCommon.class);
322 Assert.assertNotNull(orgAuthority);
323 } catch (Exception e) {
324 throw new RuntimeException(e);
329 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
330 dependsOnMethods = {"read"})
331 public void readByName(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 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
352 client.getCommonPartName(), OrgauthoritiesCommon.class);
353 Assert.assertNotNull(orgAuthority);
354 } catch (Exception e) {
355 throw new RuntimeException(e);
360 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
361 dependsOnMethods = {"createItem", "read"})
362 public void readItem(String testName) throws Exception {
367 // Submit the request to the service and store the response.
368 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
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);
380 // Check whether we've received a organization.
381 MultipartInput input = (MultipartInput) res.getEntity();
382 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
383 client.getItemCommonPartName(), OrganizationsCommon.class);
384 Assert.assertNotNull(organization);
390 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
391 dependsOnMethods = {"read"})
392 public void readNonExistent(String testName) {
395 setupReadNonExistent(testName);
397 // Submit the request to the service and store the response.
398 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
399 int statusCode = res.getStatus();
401 // Check the status code of the response: does it match
402 // the expected response(s)?
403 if(logger.isDebugEnabled()){
404 logger.debug(testName + ": status = " + statusCode);
406 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
407 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
408 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
411 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
412 dependsOnMethods = {"readItem", "readNonExistent"})
413 public void readItemNonExistent(String testName) {
416 setupReadNonExistent(testName);
418 // Submit the request to the service and store the response.
419 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
420 int statusCode = res.getStatus();
422 // Check the status code of the response: does it match
423 // the expected response(s)?
424 if(logger.isDebugEnabled()){
425 logger.debug(testName + ": status = " + statusCode);
427 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
428 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
429 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
431 // ---------------------------------------------------------------
432 // CRUD tests : READ_LIST tests
433 // ---------------------------------------------------------------
437 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
438 dependsOnMethods = {"createList", "read"})
439 public void readList(String testName) throws Exception {
442 setupReadList(testName);
444 // Submit the request to the service and store the response.
445 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
446 OrgauthoritiesCommonList list = res.getEntity();
447 int statusCode = res.getStatus();
449 // Check the status code of the response: does it match
450 // the expected response(s)?
451 if(logger.isDebugEnabled()){
452 logger.debug(testName + ": status = " + statusCode);
454 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
455 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
456 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
458 // Optionally output additional data about list members for debugging.
459 boolean iterateThroughList = false;
460 if (iterateThroughList && logger.isDebugEnabled()) {
461 List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
462 list.getOrgauthorityListItem();
464 for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
465 String csid = item.getCsid();
466 logger.debug(testName + ": list-item[" + i + "] csid=" +
468 logger.debug(testName + ": list-item[" + i + "] displayName=" +
469 item.getDisplayName());
470 logger.debug(testName + ": list-item[" + i + "] URI=" +
478 @Test(dependsOnMethods = {"createList", "readItem"})
479 public void readItemList() {
480 readItemList(knownResourceId);
483 private void readItemList(String vcsid) {
485 final String testName = "readItemList";
488 setupReadList(testName);
490 // Submit the request to the service and store the response.
491 ClientResponse<OrganizationsCommonList> res =
492 client.readItemList(vcsid);
493 OrganizationsCommonList list = res.getEntity();
494 int statusCode = res.getStatus();
496 // Check the status code of the response: does it match
497 // the expected response(s)?
498 if(logger.isDebugEnabled()){
499 logger.debug(" " + testName + ": status = " + statusCode);
501 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
502 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
503 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
505 // Optionally output additional data about list members for debugging.
506 boolean iterateThroughList = false;
507 if (iterateThroughList && logger.isDebugEnabled()) {
508 List<OrganizationsCommonList.OrganizationListItem> items =
509 list.getOrganizationListItem();
511 for (OrganizationsCommonList.OrganizationListItem item : items) {
512 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
514 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
515 item.getDisplayName());
516 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
525 // ---------------------------------------------------------------
526 // CRUD tests : UPDATE tests
527 // ---------------------------------------------------------------
530 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
531 dependsOnMethods = {"read"})
532 public void update(String testName) throws Exception {
535 setupUpdate(testName);
537 // Retrieve the contents of a resource to update.
538 ClientResponse<MultipartInput> res =
539 client.read(knownResourceId);
540 if(logger.isDebugEnabled()){
541 logger.debug(testName + ": read status = " + res.getStatus());
543 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
545 if(logger.isDebugEnabled()){
546 logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
548 MultipartInput input = (MultipartInput) res.getEntity();
549 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
550 client.getCommonPartName(), OrgauthoritiesCommon.class);
551 Assert.assertNotNull(orgAuthority);
553 // Update the contents of this resource.
554 orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
555 orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
556 if(logger.isDebugEnabled()){
557 logger.debug("to be updated OrgAuthority");
558 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
561 // Submit the updated resource to the service and store the response.
562 MultipartOutput output = new MultipartOutput();
563 OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
564 commonPart.getHeaders().add("label", client.getCommonPartName());
565 res = client.update(knownResourceId, output);
566 int statusCode = res.getStatus();
568 // Check the status code of the response: does it match the expected response(s)?
569 if(logger.isDebugEnabled()){
570 logger.debug("update: status = " + statusCode);
572 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
573 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
574 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
576 // Retrieve the updated resource and verify that its contents exist.
577 input = (MultipartInput) res.getEntity();
578 OrgauthoritiesCommon updatedOrgAuthority =
579 (OrgauthoritiesCommon) extractPart(input,
580 client.getCommonPartName(), OrgauthoritiesCommon.class);
581 Assert.assertNotNull(updatedOrgAuthority);
583 // Verify that the updated resource received the correct data.
584 Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
585 orgAuthority.getDisplayName(),
586 "Data in updated object did not match submitted data.");
589 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
590 dependsOnMethods = {"readItem", "update"})
591 public void updateItem(String testName) throws Exception {
594 setupUpdate(testName);
596 ClientResponse<MultipartInput> res =
597 client.readItem(knownResourceId, knownItemResourceId);
598 if(logger.isDebugEnabled()){
599 logger.debug(testName + ": read status = " + res.getStatus());
601 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
603 if(logger.isDebugEnabled()){
604 logger.debug("got Organization to update with ID: " +
605 knownItemResourceId +
606 " in OrgAuthority: " + knownResourceId );
608 MultipartInput input = (MultipartInput) res.getEntity();
609 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
610 client.getItemCommonPartName(), OrganizationsCommon.class);
611 Assert.assertNotNull(organization);
613 // Update the contents of this resource.
614 organization.setDisplayName("updated-" + organization.getDisplayName());
615 if(logger.isDebugEnabled()){
616 logger.debug("to be updated Organization");
617 logger.debug(objectAsXmlString(organization,
618 OrganizationsCommon.class));
621 // Submit the updated resource to the service and store the response.
622 MultipartOutput output = new MultipartOutput();
623 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
624 commonPart.getHeaders().add("label", client.getItemCommonPartName());
625 res = client.updateItem(knownResourceId, knownItemResourceId, output);
626 int statusCode = res.getStatus();
628 // Check the status code of the response: does it match the expected response(s)?
629 if(logger.isDebugEnabled()){
630 logger.debug("updateItem: status = " + statusCode);
632 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
633 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
634 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
636 // Retrieve the updated resource and verify that its contents exist.
637 input = (MultipartInput) res.getEntity();
638 OrganizationsCommon updatedOrganization =
639 (OrganizationsCommon) extractPart(input,
640 client.getItemCommonPartName(), OrganizationsCommon.class);
641 Assert.assertNotNull(updatedOrganization);
643 // Verify that the updated resource received the correct data.
644 Assert.assertEquals(updatedOrganization.getDisplayName(),
645 organization.getDisplayName(),
646 "Data in updated Organization did not match submitted data.");
650 // Placeholders until the three tests below can be uncommented.
651 // See Issue CSPACE-401.
653 public void updateWithEmptyEntityBody(String testName) throws Exception {
657 public void updateWithMalformedXml(String testName) throws Exception {
661 public void updateWithWrongXmlSchema(String testName) throws Exception {
666 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
667 dependsOnMethods = {"create", "update", "testSubmitRequest"})
668 public void updateWithEmptyEntityBody(String testName) throws Exception {
671 setupUpdateWithEmptyEntityBody(testName);
673 // Submit the request to the service and store the response.
674 String method = REQUEST_TYPE.httpMethodName();
675 String url = getResourceURL(knownResourceId);
676 String mediaType = MediaType.APPLICATION_XML;
677 final String entity = "";
678 int statusCode = submitRequest(method, url, mediaType, entity);
680 // Check the status code of the response: does it match
681 // the expected response(s)?
682 if(logger.isDebugEnabled()){
683 logger.debug(testName + ": url=" + url +
684 " status=" + statusCode);
686 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
687 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
688 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
692 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
693 dependsOnMethods = {"create", "update", "testSubmitRequest"})
694 public void updateWithMalformedXml(String testName) throws Exception {
697 setupUpdateWithMalformedXml(testName);
699 // Submit the request to the service and store the response.
700 String method = REQUEST_TYPE.httpMethodName();
701 String url = getResourceURL(knownResourceId);
702 String mediaType = MediaType.APPLICATION_XML;
703 final String entity = MALFORMED_XML_DATA;
704 int statusCode = submitRequest(method, url, mediaType, entity);
706 // Check the status code of the response: does it match
707 // the expected response(s)?
708 if(logger.isDebugEnabled()){
709 logger.debug(testName + ": url=" + url +
710 " status=" + statusCode);
712 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
713 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
714 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
718 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
719 dependsOnMethods = {"create", "update", "testSubmitRequest"})
720 public void updateWithWrongXmlSchema(String testName) throws Exception {
723 setupUpdateWithWrongXmlSchema(testName);
725 // Submit the request to the service and store the response.
726 String method = REQUEST_TYPE.httpMethodName();
727 String url = getResourceURL(knownResourceId);
728 String mediaType = MediaType.APPLICATION_XML;
729 final String entity = WRONG_XML_SCHEMA_DATA;
730 int statusCode = submitRequest(method, url, mediaType, entity);
732 // Check the status code of the response: does it match
733 // the expected response(s)?
734 if(logger.isDebugEnabled()){
735 logger.debug("updateWithWrongXmlSchema: url=" + url +
736 " status=" + statusCode);
738 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
739 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
740 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
746 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
747 dependsOnMethods = {"update", "testSubmitRequest"})
748 public void updateNonExistent(String testName) throws Exception {
751 setupUpdateNonExistent(testName);
753 // Submit the request to the service and store the response.
754 // Note: The ID used in this 'create' call may be arbitrary.
755 // The only relevant ID may be the one used in update(), below.
757 // The only relevant ID may be the one used in update(), below.
758 MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
759 ClientResponse<MultipartInput> res =
760 client.update(NON_EXISTENT_ID, multipart);
761 int statusCode = res.getStatus();
763 // Check the status code of the response: does it match
764 // the expected response(s)?
765 if(logger.isDebugEnabled()){
766 logger.debug(testName + ": status = " + statusCode);
768 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
769 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
770 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
773 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
774 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
775 public void updateNonExistentItem(String testName) throws Exception {
778 setupUpdateNonExistent(testName);
780 // Submit the request to the service and store the response.
781 // Note: The ID used in this 'create' call may be arbitrary.
782 // The only relevant ID may be the one used in update(), below.
784 // The only relevant ID may be the one used in update(), below.
785 MultipartOutput multipart = createOrganizationInstance(
786 knownResourceId, NON_EXISTENT_ID, createRefName(NON_EXISTENT_ID),
788 ClientResponse<MultipartInput> res =
789 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
790 int statusCode = res.getStatus();
792 // Check the status code of the response: does it match
793 // the expected response(s)?
794 if(logger.isDebugEnabled()){
795 logger.debug(testName + ": status = " + statusCode);
797 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
798 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
799 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
802 // ---------------------------------------------------------------
803 // CRUD tests : DELETE tests
804 // ---------------------------------------------------------------
807 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
808 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
809 public void delete(String testName) throws Exception {
812 setupDelete(testName);
814 // Submit the request to the service and store the response.
815 ClientResponse<Response> res = client.delete(knownResourceId);
816 int statusCode = res.getStatus();
818 // Check the status code of the response: does it match
819 // the expected response(s)?
820 if(logger.isDebugEnabled()){
821 logger.debug(testName + ": status = " + statusCode);
823 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
824 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
825 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
828 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
829 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
831 public void deleteItem(String testName) throws Exception {
834 setupDelete(testName);
836 // Submit the request to the service and store the response.
837 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
838 int statusCode = res.getStatus();
840 // Check the status code of the response: does it match
841 // the expected response(s)?
842 if(logger.isDebugEnabled()){
843 logger.debug("delete: status = " + statusCode);
845 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
846 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
847 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
852 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
853 dependsOnMethods = {"delete"})
854 public void deleteNonExistent(String testName) throws Exception {
857 setupDeleteNonExistent(testName);
859 // Submit the request to the service and store the response.
860 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
861 int statusCode = res.getStatus();
863 // Check the status code of the response: does it match
864 // the expected response(s)?
865 if(logger.isDebugEnabled()){
866 logger.debug(testName + ": status = " + statusCode);
868 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
869 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
870 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
873 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
874 dependsOnMethods = {"deleteItem"})
875 public void deleteNonExistentItem(String testName) {
878 setupDeleteNonExistent(testName);
880 // Submit the request to the service and store the response.
881 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
882 int statusCode = res.getStatus();
884 // Check the status code of the response: does it match
885 // the expected response(s)?
886 if(logger.isDebugEnabled()){
887 logger.debug(testName + ": status = " + statusCode);
889 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
890 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
891 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
894 // ---------------------------------------------------------------
895 // Utility tests : tests of code used in tests above
896 // ---------------------------------------------------------------
898 * Tests the code for manually submitting data that is used by several
899 * of the methods above.
901 @Test(dependsOnMethods = {"create", "read"})
902 public void testSubmitRequest() {
904 // Expected status code: 200 OK
905 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
907 // Submit the request to the service and store the response.
908 String method = ServiceRequestType.READ.httpMethodName();
909 String url = getResourceURL(knownResourceId);
910 int statusCode = submitRequest(method, url);
912 // Check the status code of the response: does it match
913 // the expected response(s)?
914 if(logger.isDebugEnabled()){
915 logger.debug("testSubmitRequest: url=" + url +
916 " status=" + statusCode);
918 Assert.assertEquals(statusCode, EXPECTED_STATUS);
922 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
923 public void testItemSubmitRequest() {
925 // Expected status code: 200 OK
926 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
928 // Submit the request to the service and store the response.
929 String method = ServiceRequestType.READ.httpMethodName();
930 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
931 int statusCode = submitRequest(method, url);
933 // Check the status code of the response: does it match
934 // the expected response(s)?
935 if(logger.isDebugEnabled()){
936 logger.debug("testItemSubmitRequest: url=" + url +
937 " status=" + statusCode);
939 Assert.assertEquals(statusCode, EXPECTED_STATUS);
943 // ---------------------------------------------------------------
944 // Cleanup of resources created during testing
945 // ---------------------------------------------------------------
948 * Deletes all resources created by tests, after all tests have been run.
950 * This cleanup method will always be run, even if one or more tests fail.
951 * For this reason, it attempts to remove all resources created
952 * at any point during testing, even if some of those resources
953 * may be expected to be deleted by certain tests.
955 @AfterClass(alwaysRun=true)
956 public void cleanUp() {
957 if (logger.isDebugEnabled()) {
958 logger.debug("Cleaning up temporary resources created for testing ...");
960 // Clean up organization resources.
961 String orgAuthorityResourceId;
962 String organizationResourceId;
963 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
964 organizationResourceId = entry.getKey();
965 orgAuthorityResourceId = entry.getValue();
966 // Note: Any non-success responses are ignored and not reported.
967 ClientResponse<Response> res =
968 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
970 // Clean up orgAuthority resources.
971 for (String resourceId : allResourceIdsCreated) {
972 // Note: Any non-success responses are ignored and not reported.
973 ClientResponse<Response> res = client.delete(resourceId);
977 // ---------------------------------------------------------------
978 // Utility methods used by tests above
979 // ---------------------------------------------------------------
981 public String getServicePathComponent() {
982 return SERVICE_PATH_COMPONENT;
985 public String getItemServicePathComponent() {
986 return ITEM_SERVICE_PATH_COMPONENT;
990 * Returns the root URL for a service.
992 * This URL consists of a base URL for all services, followed by
993 * a path component for the owning orgAuthority, followed by the
994 * path component for the items.
996 * @return The root URL for a service.
998 protected String getItemServiceRootURL(String parentResourceIdentifier) {
999 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1003 * Returns the URL of a specific resource managed by a service, and
1004 * designated by an identifier (such as a universally unique ID, or UUID).
1006 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1008 * @return The URL of a specific resource managed by a service.
1010 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1011 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1014 private MultipartOutput createOrgAuthorityInstance(String identifier) {
1015 String displayName = "displayName-" + identifier;
1016 String refName = createRefName(displayName);
1017 String typeName = "vocabType-" + identifier;
1018 return createOrgAuthorityInstance(
1019 displayName, refName,typeName );
1022 private MultipartOutput createOrgAuthorityInstance(
1023 String displayName, String refName, String vocabType) {
1024 OrgauthoritiesCommon orgAuthority = new OrgauthoritiesCommon();
1025 orgAuthority.setDisplayName(displayName);
1027 orgAuthority.setRefName(refName);
1028 orgAuthority.setVocabType(vocabType);
1029 MultipartOutput multipart = new MultipartOutput();
1030 OutputPart commonPart = multipart.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
1031 commonPart.getHeaders().add("label", client.getCommonPartName());
1033 if(logger.isDebugEnabled()) {
1034 logger.debug("to be created, orgAuthority common");
1035 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
1040 private MultipartOutput createOrganizationInstance(String inAuthority,
1041 String displayName, String refName, String longName, String description) {
1042 OrganizationsCommon organization = new OrganizationsCommon();
1043 organization.setDisplayName(displayName);
1045 organization.setRefName(refName);
1047 organization.setLongName(longName);
1048 if(description!=null)
1049 organization.setDescription(description);
1050 MultipartOutput multipart = new MultipartOutput();
1051 OutputPart commonPart = multipart.addPart(organization,
1052 MediaType.APPLICATION_XML_TYPE);
1053 commonPart.getHeaders().add("label", client.getItemCommonPartName());
1055 if(logger.isDebugEnabled()){
1056 logger.debug("to be created, organization common");
1057 logger.debug(objectAsXmlString(organization,
1058 OrganizationsCommon.class));