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 null, "joe@org.org", "1910", null, "Anytown, USA", "testing",
153 "This is a fake organization that was created by a test method." );
154 ClientResponse<Response> res = client.createItem(vcsid, multipart);
155 int statusCode = res.getStatus();
157 // Check the status code of the response: does it match
158 // the expected response(s)?
159 if(logger.isDebugEnabled()){
160 logger.debug(testName + ": status = " + statusCode);
162 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
163 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
164 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
166 // Store the ID returned from the first item resource created
167 // for additional tests below.
168 if (knownItemResourceId == null){
169 knownItemResourceId = extractId(res);
170 if (logger.isDebugEnabled()) {
171 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
175 // Store the IDs from any item resources created
176 // by tests, along with the IDs of their parents, so these items
177 // can be deleted after all tests have been run.
179 // Item resource IDs are unique, so these are used as keys;
180 // the non-unique IDs of their parents are stored as associated values.
181 allResourceItemIdsCreated.put(extractId(res), vcsid);
183 return extractId(res);
187 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
188 dependsOnMethods = {"create", "createItem"})
189 public void createList(String testName) throws Exception {
190 for (int i = 0; i < 3; i++) {
192 // Add 3 items to each orgauthority
193 for (int j = 0; j < 3; j++) {
194 createItem(testName);
200 // Placeholders until the three tests below can be uncommented.
201 // See Issue CSPACE-401.
203 public void createWithEmptyEntityBody(String testName) throws Exception {
207 public void createWithMalformedXml(String testName) throws Exception {
211 public void createWithWrongXmlSchema(String testName) throws Exception {
216 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
217 dependsOnMethods = {"create", "testSubmitRequest"})
218 public void createWithEmptyEntityBody(String testName) throws Exception {
221 setupCreateWithEmptyEntityBody(testName);
223 // Submit the request to the service and store the response.
224 String method = REQUEST_TYPE.httpMethodName();
225 String url = getServiceRootURL();
226 String mediaType = MediaType.APPLICATION_XML;
227 final String entity = "";
228 int statusCode = submitRequest(method, url, mediaType, entity);
230 // Check the status code of the response: does it match
231 // the expected response(s)?
232 if(logger.isDebugEnabled()) {
233 logger.debug(testName + ": url=" + url +
234 " status=" + statusCode);
236 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
237 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
238 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
242 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
243 dependsOnMethods = {"create", "testSubmitRequest"})
244 public void createWithMalformedXml(String testName) throws Exception {
247 setupCreateWithMalformedXml(testName);
249 // Submit the request to the service and store the response.
250 String method = REQUEST_TYPE.httpMethodName();
251 String url = getServiceRootURL();
252 String mediaType = MediaType.APPLICATION_XML;
253 final String entity = MALFORMED_XML_DATA; // Constant from base class.
254 int statusCode = submitRequest(method, url, mediaType, entity);
256 // Check the status code of the response: does it match
257 // the expected response(s)?
258 if(logger.isDebugEnabled()){
259 logger.debug(testName + ": url=" + url +
260 " status=" + statusCode);
262 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
263 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
264 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
268 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
269 dependsOnMethods = {"create", "testSubmitRequest"})
270 public void createWithWrongXmlSchema(String testName) throws Exception {
273 setupCreateWithWrongXmlSchema(testName);
275 // Submit the request to the service and store the response.
276 String method = REQUEST_TYPE.httpMethodName();
277 String url = getServiceRootURL();
278 String mediaType = MediaType.APPLICATION_XML;
279 final String entity = WRONG_XML_SCHEMA_DATA;
280 int statusCode = submitRequest(method, url, mediaType, entity);
282 // Check the status code of the response: does it match
283 // the expected response(s)?
284 if(logger.isDebugEnabled()){
285 logger.debug(testName + ": url=" + url +
286 " status=" + statusCode);
288 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
294 // ---------------------------------------------------------------
295 // CRUD tests : READ tests
296 // ---------------------------------------------------------------
299 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
300 dependsOnMethods = {"create"})
301 public void read(String testName) throws Exception {
306 // Submit the request to the service and store the response.
307 ClientResponse<MultipartInput> res = client.read(knownResourceId);
308 int statusCode = res.getStatus();
310 // Check the status code of the response: does it match
311 // the expected response(s)?
312 if(logger.isDebugEnabled()){
313 logger.debug(testName + ": status = " + statusCode);
315 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
316 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
317 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
318 //FIXME: remove the following try catch once Aron fixes signatures
320 MultipartInput input = (MultipartInput) res.getEntity();
321 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
322 client.getCommonPartName(), OrgauthoritiesCommon.class);
323 Assert.assertNotNull(orgAuthority);
324 } catch (Exception e) {
325 throw new RuntimeException(e);
330 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
331 dependsOnMethods = {"read"})
332 public void readByName(String testName) throws Exception {
337 // Submit the request to the service and store the response.
338 ClientResponse<MultipartInput> res = client.read(knownResourceId);
339 int statusCode = res.getStatus();
341 // Check the status code of the response: does it match
342 // the expected response(s)?
343 if(logger.isDebugEnabled()){
344 logger.debug(testName + ": status = " + statusCode);
346 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
347 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
348 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
349 //FIXME: remove the following try catch once Aron fixes signatures
351 MultipartInput input = (MultipartInput) res.getEntity();
352 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
353 client.getCommonPartName(), OrgauthoritiesCommon.class);
354 Assert.assertNotNull(orgAuthority);
355 } catch (Exception e) {
356 throw new RuntimeException(e);
361 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
362 dependsOnMethods = {"createItem", "read"})
363 public void readItem(String testName) throws Exception {
368 // Submit the request to the service and store the response.
369 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
370 int statusCode = res.getStatus();
372 // Check the status code of the response: does it match
373 // the expected response(s)?
374 if(logger.isDebugEnabled()){
375 logger.debug(testName + ": status = " + statusCode);
377 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
378 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
379 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
381 // Check whether we've received a organization.
382 MultipartInput input = (MultipartInput) res.getEntity();
383 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
384 client.getItemCommonPartName(), OrganizationsCommon.class);
385 Assert.assertNotNull(organization);
391 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
392 dependsOnMethods = {"read"})
393 public void readNonExistent(String testName) {
396 setupReadNonExistent(testName);
398 // Submit the request to the service and store the response.
399 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
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);
412 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
413 dependsOnMethods = {"readItem", "readNonExistent"})
414 public void readItemNonExistent(String testName) {
417 setupReadNonExistent(testName);
419 // Submit the request to the service and store the response.
420 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
421 int statusCode = res.getStatus();
423 // Check the status code of the response: does it match
424 // the expected response(s)?
425 if(logger.isDebugEnabled()){
426 logger.debug(testName + ": status = " + statusCode);
428 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
429 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
430 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
432 // ---------------------------------------------------------------
433 // CRUD tests : READ_LIST tests
434 // ---------------------------------------------------------------
438 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
439 dependsOnMethods = {"createList", "read"})
440 public void readList(String testName) throws Exception {
443 setupReadList(testName);
445 // Submit the request to the service and store the response.
446 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
447 OrgauthoritiesCommonList list = res.getEntity();
448 int statusCode = res.getStatus();
450 // Check the status code of the response: does it match
451 // the expected response(s)?
452 if(logger.isDebugEnabled()){
453 logger.debug(testName + ": status = " + statusCode);
455 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
456 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
457 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
459 // Optionally output additional data about list members for debugging.
460 boolean iterateThroughList = false;
461 if (iterateThroughList && logger.isDebugEnabled()) {
462 List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
463 list.getOrgauthorityListItem();
465 for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
466 String csid = item.getCsid();
467 logger.debug(testName + ": list-item[" + i + "] csid=" +
469 logger.debug(testName + ": list-item[" + i + "] displayName=" +
470 item.getDisplayName());
471 logger.debug(testName + ": list-item[" + i + "] URI=" +
479 @Test(dependsOnMethods = {"createList", "readItem"})
480 public void readItemList() {
481 readItemList(knownResourceId);
484 private void readItemList(String vcsid) {
486 final String testName = "readItemList";
489 setupReadList(testName);
491 // Submit the request to the service and store the response.
492 ClientResponse<OrganizationsCommonList> res =
493 client.readItemList(vcsid);
494 OrganizationsCommonList list = res.getEntity();
495 int statusCode = res.getStatus();
497 // Check the status code of the response: does it match
498 // the expected response(s)?
499 if(logger.isDebugEnabled()){
500 logger.debug(" " + testName + ": status = " + statusCode);
502 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
503 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
504 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
506 // Optionally output additional data about list members for debugging.
507 boolean iterateThroughList = false;
508 if (iterateThroughList && logger.isDebugEnabled()) {
509 List<OrganizationsCommonList.OrganizationListItem> items =
510 list.getOrganizationListItem();
512 for (OrganizationsCommonList.OrganizationListItem item : items) {
513 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
515 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
516 item.getDisplayName());
517 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
526 // ---------------------------------------------------------------
527 // CRUD tests : UPDATE tests
528 // ---------------------------------------------------------------
531 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
532 dependsOnMethods = {"read"})
533 public void update(String testName) throws Exception {
536 setupUpdate(testName);
538 // Retrieve the contents of a resource to update.
539 ClientResponse<MultipartInput> res =
540 client.read(knownResourceId);
541 if(logger.isDebugEnabled()){
542 logger.debug(testName + ": read status = " + res.getStatus());
544 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
546 if(logger.isDebugEnabled()){
547 logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
549 MultipartInput input = (MultipartInput) res.getEntity();
550 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
551 client.getCommonPartName(), OrgauthoritiesCommon.class);
552 Assert.assertNotNull(orgAuthority);
554 // Update the contents of this resource.
555 orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
556 orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
557 if(logger.isDebugEnabled()){
558 logger.debug("to be updated OrgAuthority");
559 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
562 // Submit the updated resource to the service and store the response.
563 MultipartOutput output = new MultipartOutput();
564 OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
565 commonPart.getHeaders().add("label", client.getCommonPartName());
566 res = client.update(knownResourceId, output);
567 int statusCode = res.getStatus();
569 // Check the status code of the response: does it match the expected response(s)?
570 if(logger.isDebugEnabled()){
571 logger.debug("update: status = " + statusCode);
573 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
574 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
575 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
577 // Retrieve the updated resource and verify that its contents exist.
578 input = (MultipartInput) res.getEntity();
579 OrgauthoritiesCommon updatedOrgAuthority =
580 (OrgauthoritiesCommon) extractPart(input,
581 client.getCommonPartName(), OrgauthoritiesCommon.class);
582 Assert.assertNotNull(updatedOrgAuthority);
584 // Verify that the updated resource received the correct data.
585 Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
586 orgAuthority.getDisplayName(),
587 "Data in updated object did not match submitted data.");
590 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
591 dependsOnMethods = {"readItem", "update"})
592 public void updateItem(String testName) throws Exception {
595 setupUpdate(testName);
597 ClientResponse<MultipartInput> res =
598 client.readItem(knownResourceId, knownItemResourceId);
599 if(logger.isDebugEnabled()){
600 logger.debug(testName + ": read status = " + res.getStatus());
602 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
604 if(logger.isDebugEnabled()){
605 logger.debug("got Organization to update with ID: " +
606 knownItemResourceId +
607 " in OrgAuthority: " + knownResourceId );
609 MultipartInput input = (MultipartInput) res.getEntity();
610 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
611 client.getItemCommonPartName(), OrganizationsCommon.class);
612 Assert.assertNotNull(organization);
614 // Update the contents of this resource.
615 organization.setShortName("updated-" + organization.getShortName());
616 if(logger.isDebugEnabled()){
617 logger.debug("to be updated Organization");
618 logger.debug(objectAsXmlString(organization,
619 OrganizationsCommon.class));
622 // Submit the updated resource to the service and store the response.
623 MultipartOutput output = new MultipartOutput();
624 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
625 commonPart.getHeaders().add("label", client.getItemCommonPartName());
626 res = client.updateItem(knownResourceId, knownItemResourceId, output);
627 int statusCode = res.getStatus();
629 // Check the status code of the response: does it match the expected response(s)?
630 if(logger.isDebugEnabled()){
631 logger.debug("updateItem: status = " + statusCode);
633 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
634 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
635 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
637 // Retrieve the updated resource and verify that its contents exist.
638 input = (MultipartInput) res.getEntity();
639 OrganizationsCommon updatedOrganization =
640 (OrganizationsCommon) extractPart(input,
641 client.getItemCommonPartName(), OrganizationsCommon.class);
642 Assert.assertNotNull(updatedOrganization);
644 // Verify that the updated resource received the correct data.
645 Assert.assertEquals(updatedOrganization.getShortName(),
646 organization.getShortName(),
647 "Data in updated Organization did not match submitted data.");
651 // Placeholders until the three tests below can be uncommented.
652 // See Issue CSPACE-401.
654 public void updateWithEmptyEntityBody(String testName) throws Exception {
658 public void updateWithMalformedXml(String testName) throws Exception {
662 public void updateWithWrongXmlSchema(String testName) throws Exception {
667 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
668 dependsOnMethods = {"create", "update", "testSubmitRequest"})
669 public void updateWithEmptyEntityBody(String testName) throws Exception {
672 setupUpdateWithEmptyEntityBody(testName);
674 // Submit the request to the service and store the response.
675 String method = REQUEST_TYPE.httpMethodName();
676 String url = getResourceURL(knownResourceId);
677 String mediaType = MediaType.APPLICATION_XML;
678 final String entity = "";
679 int statusCode = submitRequest(method, url, mediaType, entity);
681 // Check the status code of the response: does it match
682 // the expected response(s)?
683 if(logger.isDebugEnabled()){
684 logger.debug(testName + ": url=" + url +
685 " status=" + statusCode);
687 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
688 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
689 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
693 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
694 dependsOnMethods = {"create", "update", "testSubmitRequest"})
695 public void updateWithMalformedXml(String testName) throws Exception {
698 setupUpdateWithMalformedXml(testName);
700 // Submit the request to the service and store the response.
701 String method = REQUEST_TYPE.httpMethodName();
702 String url = getResourceURL(knownResourceId);
703 String mediaType = MediaType.APPLICATION_XML;
704 final String entity = MALFORMED_XML_DATA;
705 int statusCode = submitRequest(method, url, mediaType, entity);
707 // Check the status code of the response: does it match
708 // the expected response(s)?
709 if(logger.isDebugEnabled()){
710 logger.debug(testName + ": url=" + url +
711 " status=" + statusCode);
713 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
714 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
715 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
719 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
720 dependsOnMethods = {"create", "update", "testSubmitRequest"})
721 public void updateWithWrongXmlSchema(String testName) throws Exception {
724 setupUpdateWithWrongXmlSchema(testName);
726 // Submit the request to the service and store the response.
727 String method = REQUEST_TYPE.httpMethodName();
728 String url = getResourceURL(knownResourceId);
729 String mediaType = MediaType.APPLICATION_XML;
730 final String entity = WRONG_XML_SCHEMA_DATA;
731 int statusCode = submitRequest(method, url, mediaType, entity);
733 // Check the status code of the response: does it match
734 // the expected response(s)?
735 if(logger.isDebugEnabled()){
736 logger.debug("updateWithWrongXmlSchema: url=" + url +
737 " status=" + statusCode);
739 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
740 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
741 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
747 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
748 dependsOnMethods = {"update", "testSubmitRequest"})
749 public void updateNonExistent(String testName) throws Exception {
752 setupUpdateNonExistent(testName);
754 // Submit the request to the service and store the response.
755 // Note: The ID used in this 'create' call may be arbitrary.
756 // The only relevant ID may be the one used in update(), below.
758 // The only relevant ID may be the one used in update(), below.
759 MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
760 ClientResponse<MultipartInput> res =
761 client.update(NON_EXISTENT_ID, multipart);
762 int statusCode = res.getStatus();
764 // Check the status code of the response: does it match
765 // the expected response(s)?
766 if(logger.isDebugEnabled()){
767 logger.debug(testName + ": status = " + statusCode);
769 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
770 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
771 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
774 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
775 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
776 public void updateNonExistentItem(String testName) throws Exception {
779 setupUpdateNonExistent(testName);
781 // Submit the request to the service and store the response.
782 // Note: The ID used in this 'create' call may be arbitrary.
783 // The only relevant ID may be the one used in update(), below.
785 // The only relevant ID may be the one used in update(), below.
786 MultipartOutput multipart = createOrganizationInstance(
787 knownResourceId, NON_EXISTENT_ID, createRefName(NON_EXISTENT_ID),
788 null, null, null, null, null, null, null, null);
789 ClientResponse<MultipartInput> res =
790 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
791 int statusCode = res.getStatus();
793 // Check the status code of the response: does it match
794 // the expected response(s)?
795 if(logger.isDebugEnabled()){
796 logger.debug(testName + ": status = " + statusCode);
798 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
799 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
800 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
803 // ---------------------------------------------------------------
804 // CRUD tests : DELETE tests
805 // ---------------------------------------------------------------
808 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
809 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
810 public void delete(String testName) throws Exception {
813 setupDelete(testName);
815 // Submit the request to the service and store the response.
816 ClientResponse<Response> res = client.delete(knownResourceId);
817 int statusCode = res.getStatus();
819 // Check the status code of the response: does it match
820 // the expected response(s)?
821 if(logger.isDebugEnabled()){
822 logger.debug(testName + ": status = " + statusCode);
824 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
825 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
826 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
829 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
830 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
832 public void deleteItem(String testName) throws Exception {
835 setupDelete(testName);
837 // Submit the request to the service and store the response.
838 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
839 int statusCode = res.getStatus();
841 // Check the status code of the response: does it match
842 // the expected response(s)?
843 if(logger.isDebugEnabled()){
844 logger.debug("delete: status = " + statusCode);
846 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
847 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
848 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
853 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
854 dependsOnMethods = {"delete"})
855 public void deleteNonExistent(String testName) throws Exception {
858 setupDeleteNonExistent(testName);
860 // Submit the request to the service and store the response.
861 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
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(testName + ": status = " + statusCode);
869 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
870 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
871 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
874 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
875 dependsOnMethods = {"deleteItem"})
876 public void deleteNonExistentItem(String testName) {
879 setupDeleteNonExistent(testName);
881 // Submit the request to the service and store the response.
882 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
883 int statusCode = res.getStatus();
885 // Check the status code of the response: does it match
886 // the expected response(s)?
887 if(logger.isDebugEnabled()){
888 logger.debug(testName + ": status = " + statusCode);
890 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
891 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
892 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
895 // ---------------------------------------------------------------
896 // Utility tests : tests of code used in tests above
897 // ---------------------------------------------------------------
899 * Tests the code for manually submitting data that is used by several
900 * of the methods above.
902 @Test(dependsOnMethods = {"create", "read"})
903 public void testSubmitRequest() {
905 // Expected status code: 200 OK
906 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
908 // Submit the request to the service and store the response.
909 String method = ServiceRequestType.READ.httpMethodName();
910 String url = getResourceURL(knownResourceId);
911 int statusCode = submitRequest(method, url);
913 // Check the status code of the response: does it match
914 // the expected response(s)?
915 if(logger.isDebugEnabled()){
916 logger.debug("testSubmitRequest: url=" + url +
917 " status=" + statusCode);
919 Assert.assertEquals(statusCode, EXPECTED_STATUS);
923 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
924 public void testItemSubmitRequest() {
926 // Expected status code: 200 OK
927 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
929 // Submit the request to the service and store the response.
930 String method = ServiceRequestType.READ.httpMethodName();
931 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
932 int statusCode = submitRequest(method, url);
934 // Check the status code of the response: does it match
935 // the expected response(s)?
936 if(logger.isDebugEnabled()){
937 logger.debug("testItemSubmitRequest: url=" + url +
938 " status=" + statusCode);
940 Assert.assertEquals(statusCode, EXPECTED_STATUS);
944 // ---------------------------------------------------------------
945 // Cleanup of resources created during testing
946 // ---------------------------------------------------------------
949 * Deletes all resources created by tests, after all tests have been run.
951 * This cleanup method will always be run, even if one or more tests fail.
952 * For this reason, it attempts to remove all resources created
953 * at any point during testing, even if some of those resources
954 * may be expected to be deleted by certain tests.
956 @AfterClass(alwaysRun=true)
957 public void cleanUp() {
958 if (logger.isDebugEnabled()) {
959 logger.debug("Cleaning up temporary resources created for testing ...");
961 // Clean up organization resources.
962 String orgAuthorityResourceId;
963 String organizationResourceId;
964 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
965 organizationResourceId = entry.getKey();
966 orgAuthorityResourceId = entry.getValue();
967 // Note: Any non-success responses are ignored and not reported.
968 ClientResponse<Response> res =
969 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
971 // Clean up orgAuthority resources.
972 for (String resourceId : allResourceIdsCreated) {
973 // Note: Any non-success responses are ignored and not reported.
974 ClientResponse<Response> res = client.delete(resourceId);
978 // ---------------------------------------------------------------
979 // Utility methods used by tests above
980 // ---------------------------------------------------------------
982 public String getServicePathComponent() {
983 return SERVICE_PATH_COMPONENT;
986 public String getItemServicePathComponent() {
987 return ITEM_SERVICE_PATH_COMPONENT;
991 * Returns the root URL for a service.
993 * This URL consists of a base URL for all services, followed by
994 * a path component for the owning orgAuthority, followed by the
995 * path component for the items.
997 * @return The root URL for a service.
999 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1000 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1004 * Returns the URL of a specific resource managed by a service, and
1005 * designated by an identifier (such as a universally unique ID, or UUID).
1007 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1009 * @return The URL of a specific resource managed by a service.
1011 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1012 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1015 private MultipartOutput createOrgAuthorityInstance(String identifier) {
1016 String displayName = "displayName-" + identifier;
1017 String refName = createRefName(displayName);
1018 String typeName = "vocabType-" + identifier;
1019 return createOrgAuthorityInstance(
1020 displayName, refName,typeName );
1023 private MultipartOutput createOrgAuthorityInstance(
1024 String displayName, String refName, String vocabType) {
1025 OrgauthoritiesCommon orgAuthority = new OrgauthoritiesCommon();
1026 orgAuthority.setDisplayName(displayName);
1028 orgAuthority.setRefName(refName);
1029 orgAuthority.setVocabType(vocabType);
1030 MultipartOutput multipart = new MultipartOutput();
1031 OutputPart commonPart = multipart.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
1032 commonPart.getHeaders().add("label", client.getCommonPartName());
1034 if(logger.isDebugEnabled()) {
1035 logger.debug("to be created, orgAuthority common");
1036 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
1041 private MultipartOutput createOrganizationInstance(String inAuthority,
1042 String shortName, String refName, String longName,
1043 String nameAdditions, String contactName,
1044 String foundingDate, String dissolutionDate, String foundingPlace,
1045 String function, String description ) {
1046 OrganizationsCommon organization = new OrganizationsCommon();
1047 organization.setShortName(shortName);
1049 organization.setRefName(refName);
1051 organization.setLongName(longName);
1052 if(nameAdditions!=null)
1053 organization.setNameAdditions(nameAdditions);
1054 if(contactName!=null)
1055 organization.setContactName(contactName);
1056 if(foundingDate!=null)
1057 organization.setFoundingDate(foundingDate);
1058 if(dissolutionDate!=null)
1059 organization.setDissolutionDate(dissolutionDate);
1060 if(foundingPlace!=null)
1061 organization.setFoundingPlace(foundingPlace);
1063 organization.setFunction(function);
1064 if(description!=null)
1065 organization.setDescription(description);
1066 MultipartOutput multipart = new MultipartOutput();
1067 OutputPart commonPart = multipart.addPart(organization,
1068 MediaType.APPLICATION_XML_TYPE);
1069 commonPart.getHeaders().add("label", client.getItemCommonPartName());
1071 if(logger.isDebugEnabled()){
1072 logger.debug("to be created, organization common");
1073 logger.debug(objectAsXmlString(organization,
1074 OrganizationsCommon.class));