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 lastOrgAuthId = 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 createRefName(String displayName) {
74 return displayName.replaceAll("\\W", "");
77 // ---------------------------------------------------------------
78 // CRUD tests : CREATE tests
79 // ---------------------------------------------------------------
82 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
83 public void create(String testName) throws Exception {
85 // Perform setup, such as initializing the type of service request
86 // (e.g. CREATE, DELETE), its valid and expected status codes, and
87 // its associated HTTP method name (e.g. POST, DELETE).
88 setupCreate(testName);
90 // Submit the request to the service and store the response.
91 String identifier = createIdentifier();
92 String displayName = "displayName-" + identifier;
93 String refName = createRefName(displayName);
94 String typeName = "vocabType-" + identifier;
95 MultipartOutput multipart =
96 createOrgAuthorityInstance(displayName, refName, typeName);
97 ClientResponse<Response> res = client.create(multipart);
98 int statusCode = res.getStatus();
100 // Check the status code of the response: does it match
101 // the expected response(s)?
104 // Does it fall within the set of valid status codes?
105 // Does it exactly match the expected status code?
106 if(logger.isDebugEnabled()){
107 logger.debug(testName + ": status = " + statusCode);
109 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
110 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
111 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
113 // Store the refname from the first resource created
114 // for additional tests below.
115 knownResourceRefName = refName;
117 lastOrgAuthId = extractId(res);
118 // Store the ID returned from the first resource created
119 // for additional tests below.
120 if (knownResourceId == null){
121 knownResourceId = lastOrgAuthId;
122 if (logger.isDebugEnabled()) {
123 logger.debug(testName + ": knownResourceId=" + knownResourceId);
126 // Store the IDs from every resource created by tests,
127 // so they can be deleted after tests have been run.
128 allResourceIdsCreated.add(extractId(res));
132 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
133 dependsOnMethods = {"create"})
134 public void createItem(String testName) {
135 setupCreate(testName);
137 knownItemResourceId = createItemInAuthority(lastOrgAuthId);
138 if(logger.isDebugEnabled()){
139 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
143 private String createItemInAuthority(String vcsid) {
145 final String testName = "createItemInAuthority";
146 if(logger.isDebugEnabled()){
147 logger.debug(testName + ":...");
150 // Submit the request to the service and store the response.
151 String identifier = createIdentifier();
152 String refName = createRefName(identifier);
153 MultipartOutput multipart = createOrganizationInstance(vcsid,
154 identifier, refName, "Longer Name for "+identifier,
155 null, "joe@org.org", "1910", null, "Anytown, USA", "testing",
156 "This is a fake organization that was created by a test method." );
157 ClientResponse<Response> res = client.createItem(vcsid, multipart);
158 int statusCode = res.getStatus();
160 // Check the status code of the response: does it match
161 // the expected response(s)?
162 if(logger.isDebugEnabled()){
163 logger.debug(testName + ": status = " + statusCode);
165 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
166 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
167 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
169 // Store the ID returned from the first item resource created
170 // for additional tests below.
171 if (knownItemResourceId == null){
172 knownItemResourceId = extractId(res);
173 if (logger.isDebugEnabled()) {
174 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
178 // Store the IDs from any item resources created
179 // by tests, along with the IDs of their parents, so these items
180 // can be deleted after all tests have been run.
182 // Item resource IDs are unique, so these are used as keys;
183 // the non-unique IDs of their parents are stored as associated values.
184 allResourceItemIdsCreated.put(extractId(res), vcsid);
186 return extractId(res);
190 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
191 dependsOnMethods = {"create", "createItem"})
192 public void createList(String testName) throws Exception {
193 for (int i = 0; i < 3; i++) {
195 knownResourceId = lastOrgAuthId;
196 if (logger.isDebugEnabled()) {
197 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
199 // Add nItemsToCreateInList items to each orgauthority
200 for (int j = 0; j < nItemsToCreateInList; j++) {
201 createItem(testName);
207 // Placeholders until the three tests below can be uncommented.
208 // See Issue CSPACE-401.
210 public void createWithEmptyEntityBody(String testName) throws Exception {
214 public void createWithMalformedXml(String testName) throws Exception {
218 public void createWithWrongXmlSchema(String testName) throws Exception {
223 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
224 dependsOnMethods = {"create", "testSubmitRequest"})
225 public void createWithEmptyEntityBody(String testName) throws Exception {
228 setupCreateWithEmptyEntityBody(testName);
230 // Submit the request to the service and store the response.
231 String method = REQUEST_TYPE.httpMethodName();
232 String url = getServiceRootURL();
233 String mediaType = MediaType.APPLICATION_XML;
234 final String entity = "";
235 int statusCode = submitRequest(method, url, mediaType, entity);
237 // Check the status code of the response: does it match
238 // the expected response(s)?
239 if(logger.isDebugEnabled()) {
240 logger.debug(testName + ": url=" + url +
241 " status=" + statusCode);
243 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
244 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
245 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
249 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
250 dependsOnMethods = {"create", "testSubmitRequest"})
251 public void createWithMalformedXml(String testName) throws Exception {
254 setupCreateWithMalformedXml(testName);
256 // Submit the request to the service and store the response.
257 String method = REQUEST_TYPE.httpMethodName();
258 String url = getServiceRootURL();
259 String mediaType = MediaType.APPLICATION_XML;
260 final String entity = MALFORMED_XML_DATA; // Constant from base class.
261 int statusCode = submitRequest(method, url, mediaType, entity);
263 // Check the status code of the response: does it match
264 // the expected response(s)?
265 if(logger.isDebugEnabled()){
266 logger.debug(testName + ": url=" + url +
267 " status=" + statusCode);
269 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
270 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
271 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
275 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
276 dependsOnMethods = {"create", "testSubmitRequest"})
277 public void createWithWrongXmlSchema(String testName) throws Exception {
280 setupCreateWithWrongXmlSchema(testName);
282 // Submit the request to the service and store the response.
283 String method = REQUEST_TYPE.httpMethodName();
284 String url = getServiceRootURL();
285 String mediaType = MediaType.APPLICATION_XML;
286 final String entity = WRONG_XML_SCHEMA_DATA;
287 int statusCode = submitRequest(method, url, mediaType, entity);
289 // Check the status code of the response: does it match
290 // the expected response(s)?
291 if(logger.isDebugEnabled()){
292 logger.debug(testName + ": url=" + url +
293 " status=" + statusCode);
295 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
296 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
297 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
301 // ---------------------------------------------------------------
302 // CRUD tests : READ tests
303 // ---------------------------------------------------------------
306 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
307 dependsOnMethods = {"create"})
308 public void read(String testName) throws Exception {
313 // Submit the request to the service and store the response.
314 ClientResponse<MultipartInput> res = client.read(knownResourceId);
315 int statusCode = res.getStatus();
317 // Check the status code of the response: does it match
318 // the expected response(s)?
319 if(logger.isDebugEnabled()){
320 logger.debug(testName + ": status = " + statusCode);
322 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
323 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
324 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
325 //FIXME: remove the following try catch once Aron fixes signatures
327 MultipartInput input = (MultipartInput) res.getEntity();
328 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
329 client.getCommonPartName(), OrgauthoritiesCommon.class);
330 Assert.assertNotNull(orgAuthority);
331 } catch (Exception e) {
332 throw new RuntimeException(e);
337 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
338 dependsOnMethods = {"read"})
339 public void readByName(String testName) throws Exception {
344 // Submit the request to the service and store the response.
345 ClientResponse<MultipartInput> res = client.read(knownResourceId);
346 int statusCode = res.getStatus();
348 // Check the status code of the response: does it match
349 // the expected response(s)?
350 if(logger.isDebugEnabled()){
351 logger.debug(testName + ": status = " + statusCode);
353 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
354 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
355 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
356 //FIXME: remove the following try catch once Aron fixes signatures
358 MultipartInput input = (MultipartInput) res.getEntity();
359 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
360 client.getCommonPartName(), OrgauthoritiesCommon.class);
361 Assert.assertNotNull(orgAuthority);
362 } catch (Exception e) {
363 throw new RuntimeException(e);
368 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
369 dependsOnMethods = {"createItem", "read"})
370 public void readItem(String testName) throws Exception {
375 // Submit the request to the service and store the response.
376 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
377 int statusCode = res.getStatus();
379 // Check the status code of the response: does it match
380 // the expected response(s)?
381 if(logger.isDebugEnabled()){
382 logger.debug(testName + ": status = " + statusCode);
384 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
385 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
386 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
388 // Check whether we've received a organization.
389 MultipartInput input = (MultipartInput) res.getEntity();
390 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
391 client.getItemCommonPartName(), OrganizationsCommon.class);
392 Assert.assertNotNull(organization);
398 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
399 dependsOnMethods = {"read"})
400 public void readNonExistent(String testName) {
403 setupReadNonExistent(testName);
405 // Submit the request to the service and store the response.
406 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
407 int statusCode = res.getStatus();
409 // Check the status code of the response: does it match
410 // the expected response(s)?
411 if(logger.isDebugEnabled()){
412 logger.debug(testName + ": status = " + statusCode);
414 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
415 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
416 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
419 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
420 dependsOnMethods = {"readItem", "readNonExistent"})
421 public void readItemNonExistent(String testName) {
424 setupReadNonExistent(testName);
426 // Submit the request to the service and store the response.
427 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
428 int statusCode = res.getStatus();
430 // Check the status code of the response: does it match
431 // the expected response(s)?
432 if(logger.isDebugEnabled()){
433 logger.debug(testName + ": status = " + statusCode);
435 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
436 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
437 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
439 // ---------------------------------------------------------------
440 // CRUD tests : READ_LIST tests
441 // ---------------------------------------------------------------
445 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
446 dependsOnMethods = {"createList", "read"})
447 public void readList(String testName) throws Exception {
450 setupReadList(testName);
452 // Submit the request to the service and store the response.
453 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
454 OrgauthoritiesCommonList list = res.getEntity();
455 int statusCode = res.getStatus();
457 // Check the status code of the response: does it match
458 // the expected response(s)?
459 if(logger.isDebugEnabled()){
460 logger.debug(testName + ": status = " + statusCode);
462 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
463 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
464 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
466 // Optionally output additional data about list members for debugging.
467 boolean iterateThroughList = false;
468 if (iterateThroughList && logger.isDebugEnabled()) {
469 List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
470 list.getOrgauthorityListItem();
472 for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
473 String csid = item.getCsid();
474 logger.debug(testName + ": list-item[" + i + "] csid=" +
476 logger.debug(testName + ": list-item[" + i + "] displayName=" +
477 item.getDisplayName());
478 logger.debug(testName + ": list-item[" + i + "] URI=" +
486 @Test(dependsOnMethods = {"createList", "readItem"})
487 public void readItemList() {
488 readItemList(knownResourceId);
491 private void readItemList(String vcsid) {
493 final String testName = "readItemList";
496 setupReadList(testName);
498 // Submit the request to the service and store the response.
499 ClientResponse<OrganizationsCommonList> res =
500 client.readItemList(vcsid);
501 OrganizationsCommonList list = res.getEntity();
502 int statusCode = res.getStatus();
504 // Check the status code of the response: does it match
505 // the expected response(s)?
506 if(logger.isDebugEnabled()){
507 logger.debug(" " + testName + ": status = " + statusCode);
509 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
510 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
511 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
513 List<OrganizationsCommonList.OrganizationListItem> items =
514 list.getOrganizationListItem();
515 int nItemsReturned = items.size();
516 if(logger.isDebugEnabled()){
517 logger.debug(" " + testName + ": Expected "
518 + nItemsToCreateInList+" items; got: "+nItemsReturned);
520 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
522 // Optionally output additional data about list members for debugging.
523 boolean iterateThroughList = false;
524 if (iterateThroughList && logger.isDebugEnabled()) {
526 for (OrganizationsCommonList.OrganizationListItem item : items) {
527 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
529 logger.debug(" " + testName + ": list-item[" + i + "] shortName=" +
530 item.getShortName());
531 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
540 // ---------------------------------------------------------------
541 // CRUD tests : UPDATE tests
542 // ---------------------------------------------------------------
545 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
546 dependsOnMethods = {"read"})
547 public void update(String testName) throws Exception {
550 setupUpdate(testName);
552 // Retrieve the contents of a resource to update.
553 ClientResponse<MultipartInput> res =
554 client.read(knownResourceId);
555 if(logger.isDebugEnabled()){
556 logger.debug(testName + ": read status = " + res.getStatus());
558 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
560 if(logger.isDebugEnabled()){
561 logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
563 MultipartInput input = (MultipartInput) res.getEntity();
564 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
565 client.getCommonPartName(), OrgauthoritiesCommon.class);
566 Assert.assertNotNull(orgAuthority);
568 // Update the contents of this resource.
569 orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
570 orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
571 if(logger.isDebugEnabled()){
572 logger.debug("to be updated OrgAuthority");
573 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
576 // Submit the updated resource to the service and store the response.
577 MultipartOutput output = new MultipartOutput();
578 OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
579 commonPart.getHeaders().add("label", client.getCommonPartName());
580 res = client.update(knownResourceId, output);
581 int statusCode = res.getStatus();
583 // Check the status code of the response: does it match the expected response(s)?
584 if(logger.isDebugEnabled()){
585 logger.debug("update: status = " + statusCode);
587 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
588 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
589 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
591 // Retrieve the updated resource and verify that its contents exist.
592 input = (MultipartInput) res.getEntity();
593 OrgauthoritiesCommon updatedOrgAuthority =
594 (OrgauthoritiesCommon) extractPart(input,
595 client.getCommonPartName(), OrgauthoritiesCommon.class);
596 Assert.assertNotNull(updatedOrgAuthority);
598 // Verify that the updated resource received the correct data.
599 Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
600 orgAuthority.getDisplayName(),
601 "Data in updated object did not match submitted data.");
604 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
605 dependsOnMethods = {"readItem", "update"})
606 public void updateItem(String testName) throws Exception {
609 setupUpdate(testName);
611 ClientResponse<MultipartInput> res =
612 client.readItem(knownResourceId, knownItemResourceId);
613 if(logger.isDebugEnabled()){
614 logger.debug(testName + ": read status = " + res.getStatus());
616 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
618 if(logger.isDebugEnabled()){
619 logger.debug("got Organization to update with ID: " +
620 knownItemResourceId +
621 " in OrgAuthority: " + knownResourceId );
623 MultipartInput input = (MultipartInput) res.getEntity();
624 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
625 client.getItemCommonPartName(), OrganizationsCommon.class);
626 Assert.assertNotNull(organization);
628 // Update the contents of this resource.
629 organization.setShortName("updated-" + organization.getShortName());
630 if(logger.isDebugEnabled()){
631 logger.debug("to be updated Organization");
632 logger.debug(objectAsXmlString(organization,
633 OrganizationsCommon.class));
636 // Submit the updated resource to the service and store the response.
637 MultipartOutput output = new MultipartOutput();
638 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
639 commonPart.getHeaders().add("label", client.getItemCommonPartName());
640 res = client.updateItem(knownResourceId, knownItemResourceId, output);
641 int statusCode = res.getStatus();
643 // Check the status code of the response: does it match the expected response(s)?
644 if(logger.isDebugEnabled()){
645 logger.debug("updateItem: status = " + statusCode);
647 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
648 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
649 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
651 // Retrieve the updated resource and verify that its contents exist.
652 input = (MultipartInput) res.getEntity();
653 OrganizationsCommon updatedOrganization =
654 (OrganizationsCommon) extractPart(input,
655 client.getItemCommonPartName(), OrganizationsCommon.class);
656 Assert.assertNotNull(updatedOrganization);
658 // Verify that the updated resource received the correct data.
659 Assert.assertEquals(updatedOrganization.getShortName(),
660 organization.getShortName(),
661 "Data in updated Organization did not match submitted data.");
665 // Placeholders until the three tests below can be uncommented.
666 // See Issue CSPACE-401.
668 public void updateWithEmptyEntityBody(String testName) throws Exception {
672 public void updateWithMalformedXml(String testName) throws Exception {
676 public void updateWithWrongXmlSchema(String testName) throws Exception {
681 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
682 dependsOnMethods = {"create", "update", "testSubmitRequest"})
683 public void updateWithEmptyEntityBody(String testName) throws Exception {
686 setupUpdateWithEmptyEntityBody(testName);
688 // Submit the request to the service and store the response.
689 String method = REQUEST_TYPE.httpMethodName();
690 String url = getResourceURL(knownResourceId);
691 String mediaType = MediaType.APPLICATION_XML;
692 final String entity = "";
693 int statusCode = submitRequest(method, url, mediaType, entity);
695 // Check the status code of the response: does it match
696 // the expected response(s)?
697 if(logger.isDebugEnabled()){
698 logger.debug(testName + ": url=" + url +
699 " status=" + statusCode);
701 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
702 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
703 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
707 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
708 dependsOnMethods = {"create", "update", "testSubmitRequest"})
709 public void updateWithMalformedXml(String testName) throws Exception {
712 setupUpdateWithMalformedXml(testName);
714 // Submit the request to the service and store the response.
715 String method = REQUEST_TYPE.httpMethodName();
716 String url = getResourceURL(knownResourceId);
717 String mediaType = MediaType.APPLICATION_XML;
718 final String entity = MALFORMED_XML_DATA;
719 int statusCode = submitRequest(method, url, mediaType, entity);
721 // Check the status code of the response: does it match
722 // the expected response(s)?
723 if(logger.isDebugEnabled()){
724 logger.debug(testName + ": url=" + url +
725 " status=" + statusCode);
727 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
728 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
729 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
733 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
734 dependsOnMethods = {"create", "update", "testSubmitRequest"})
735 public void updateWithWrongXmlSchema(String testName) throws Exception {
738 setupUpdateWithWrongXmlSchema(testName);
740 // Submit the request to the service and store the response.
741 String method = REQUEST_TYPE.httpMethodName();
742 String url = getResourceURL(knownResourceId);
743 String mediaType = MediaType.APPLICATION_XML;
744 final String entity = WRONG_XML_SCHEMA_DATA;
745 int statusCode = submitRequest(method, url, mediaType, entity);
747 // Check the status code of the response: does it match
748 // the expected response(s)?
749 if(logger.isDebugEnabled()){
750 logger.debug("updateWithWrongXmlSchema: url=" + url +
751 " status=" + statusCode);
753 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
754 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
755 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
761 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
762 dependsOnMethods = {"update", "testSubmitRequest"})
763 public void updateNonExistent(String testName) throws Exception {
766 setupUpdateNonExistent(testName);
768 // Submit the request to the service and store the response.
769 // Note: The ID used in this 'create' call may be arbitrary.
770 // The only relevant ID may be the one used in update(), below.
772 // The only relevant ID may be the one used in update(), below.
773 MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
774 ClientResponse<MultipartInput> res =
775 client.update(NON_EXISTENT_ID, multipart);
776 int statusCode = res.getStatus();
778 // Check the status code of the response: does it match
779 // the expected response(s)?
780 if(logger.isDebugEnabled()){
781 logger.debug(testName + ": status = " + statusCode);
783 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
784 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
785 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
788 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
789 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
790 public void updateNonExistentItem(String testName) throws Exception {
793 setupUpdateNonExistent(testName);
795 // Submit the request to the service and store the response.
796 // Note: The ID used in this 'create' call may be arbitrary.
797 // The only relevant ID may be the one used in update(), below.
799 // The only relevant ID may be the one used in update(), below.
800 MultipartOutput multipart = createOrganizationInstance(
801 knownResourceId, NON_EXISTENT_ID, createRefName(NON_EXISTENT_ID),
802 null, null, null, null, null, null, null, null);
803 ClientResponse<MultipartInput> res =
804 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
805 int statusCode = res.getStatus();
807 // Check the status code of the response: does it match
808 // the expected response(s)?
809 if(logger.isDebugEnabled()){
810 logger.debug(testName + ": status = " + statusCode);
812 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
813 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
814 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
817 // ---------------------------------------------------------------
818 // CRUD tests : DELETE tests
819 // ---------------------------------------------------------------
822 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
823 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
824 public void delete(String testName) throws Exception {
827 setupDelete(testName);
829 // Submit the request to the service and store the response.
830 ClientResponse<Response> res = client.delete(knownResourceId);
831 int statusCode = res.getStatus();
833 // Check the status code of the response: does it match
834 // the expected response(s)?
835 if(logger.isDebugEnabled()){
836 logger.debug(testName + ": status = " + statusCode);
838 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
839 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
840 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
843 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
844 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
846 public void deleteItem(String testName) throws Exception {
849 setupDelete(testName);
851 // Submit the request to the service and store the response.
852 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
853 int statusCode = res.getStatus();
855 // Check the status code of the response: does it match
856 // the expected response(s)?
857 if(logger.isDebugEnabled()){
858 logger.debug("delete: status = " + statusCode);
860 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
861 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
862 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
867 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
868 dependsOnMethods = {"delete"})
869 public void deleteNonExistent(String testName) throws Exception {
872 setupDeleteNonExistent(testName);
874 // Submit the request to the service and store the response.
875 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
876 int statusCode = res.getStatus();
878 // Check the status code of the response: does it match
879 // the expected response(s)?
880 if(logger.isDebugEnabled()){
881 logger.debug(testName + ": status = " + statusCode);
883 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
884 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
885 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
888 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
889 dependsOnMethods = {"deleteItem"})
890 public void deleteNonExistentItem(String testName) {
893 setupDeleteNonExistent(testName);
895 // Submit the request to the service and store the response.
896 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
897 int statusCode = res.getStatus();
899 // Check the status code of the response: does it match
900 // the expected response(s)?
901 if(logger.isDebugEnabled()){
902 logger.debug(testName + ": status = " + statusCode);
904 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
905 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
906 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
909 // ---------------------------------------------------------------
910 // Utility tests : tests of code used in tests above
911 // ---------------------------------------------------------------
913 * Tests the code for manually submitting data that is used by several
914 * of the methods above.
916 @Test(dependsOnMethods = {"create", "read"})
917 public void testSubmitRequest() {
919 // Expected status code: 200 OK
920 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
922 // Submit the request to the service and store the response.
923 String method = ServiceRequestType.READ.httpMethodName();
924 String url = getResourceURL(knownResourceId);
925 int statusCode = submitRequest(method, url);
927 // Check the status code of the response: does it match
928 // the expected response(s)?
929 if(logger.isDebugEnabled()){
930 logger.debug("testSubmitRequest: url=" + url +
931 " status=" + statusCode);
933 Assert.assertEquals(statusCode, EXPECTED_STATUS);
937 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
938 public void testItemSubmitRequest() {
940 // Expected status code: 200 OK
941 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
943 // Submit the request to the service and store the response.
944 String method = ServiceRequestType.READ.httpMethodName();
945 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
946 int statusCode = submitRequest(method, url);
948 // Check the status code of the response: does it match
949 // the expected response(s)?
950 if(logger.isDebugEnabled()){
951 logger.debug("testItemSubmitRequest: url=" + url +
952 " status=" + statusCode);
954 Assert.assertEquals(statusCode, EXPECTED_STATUS);
958 // ---------------------------------------------------------------
959 // Cleanup of resources created during testing
960 // ---------------------------------------------------------------
963 * Deletes all resources created by tests, after all tests have been run.
965 * This cleanup method will always be run, even if one or more tests fail.
966 * For this reason, it attempts to remove all resources created
967 * at any point during testing, even if some of those resources
968 * may be expected to be deleted by certain tests.
970 @AfterClass(alwaysRun=true)
971 public void cleanUp() {
972 if (logger.isDebugEnabled()) {
973 logger.debug("Cleaning up temporary resources created for testing ...");
975 // Clean up organization resources.
976 String orgAuthorityResourceId;
977 String organizationResourceId;
978 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
979 organizationResourceId = entry.getKey();
980 orgAuthorityResourceId = entry.getValue();
981 // Note: Any non-success responses are ignored and not reported.
982 ClientResponse<Response> res =
983 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
985 // Clean up orgAuthority resources.
986 for (String resourceId : allResourceIdsCreated) {
987 // Note: Any non-success responses are ignored and not reported.
988 ClientResponse<Response> res = client.delete(resourceId);
992 // ---------------------------------------------------------------
993 // Utility methods used by tests above
994 // ---------------------------------------------------------------
996 public String getServicePathComponent() {
997 return SERVICE_PATH_COMPONENT;
1000 public String getItemServicePathComponent() {
1001 return ITEM_SERVICE_PATH_COMPONENT;
1005 * Returns the root URL for a service.
1007 * This URL consists of a base URL for all services, followed by
1008 * a path component for the owning orgAuthority, followed by the
1009 * path component for the items.
1011 * @return The root URL for a service.
1013 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1014 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1018 * Returns the URL of a specific resource managed by a service, and
1019 * designated by an identifier (such as a universally unique ID, or UUID).
1021 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1023 * @return The URL of a specific resource managed by a service.
1025 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1026 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1029 private MultipartOutput createOrgAuthorityInstance(String identifier) {
1030 String displayName = "displayName-" + identifier;
1031 String refName = createRefName(displayName);
1032 String typeName = "vocabType-" + identifier;
1033 return createOrgAuthorityInstance(
1034 displayName, refName,typeName );
1037 private MultipartOutput createOrgAuthorityInstance(
1038 String displayName, String refName, String vocabType) {
1039 OrgauthoritiesCommon orgAuthority = new OrgauthoritiesCommon();
1040 orgAuthority.setDisplayName(displayName);
1042 orgAuthority.setRefName(refName);
1043 orgAuthority.setVocabType(vocabType);
1044 MultipartOutput multipart = new MultipartOutput();
1045 OutputPart commonPart = multipart.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
1046 commonPart.getHeaders().add("label", client.getCommonPartName());
1048 if(logger.isDebugEnabled()) {
1049 logger.debug("to be created, orgAuthority common");
1050 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
1055 private MultipartOutput createOrganizationInstance(String inAuthority,
1056 String shortName, String refName, String longName,
1057 String nameAdditions, String contactName,
1058 String foundingDate, String dissolutionDate, String foundingPlace,
1059 String function, String description ) {
1060 OrganizationsCommon organization = new OrganizationsCommon();
1061 organization.setInAuthority(inAuthority);
1062 organization.setShortName(shortName);
1064 organization.setRefName(refName);
1066 organization.setLongName(longName);
1067 if(nameAdditions!=null)
1068 organization.setNameAdditions(nameAdditions);
1069 if(contactName!=null)
1070 organization.setContactName(contactName);
1071 if(foundingDate!=null)
1072 organization.setFoundingDate(foundingDate);
1073 if(dissolutionDate!=null)
1074 organization.setDissolutionDate(dissolutionDate);
1075 if(foundingPlace!=null)
1076 organization.setFoundingPlace(foundingPlace);
1078 organization.setFunction(function);
1079 if(description!=null)
1080 organization.setDescription(description);
1081 MultipartOutput multipart = new MultipartOutput();
1082 OutputPart commonPart = multipart.addPart(organization,
1083 MediaType.APPLICATION_XML_TYPE);
1084 commonPart.getHeaders().add("label", client.getItemCommonPartName());
1086 if(logger.isDebugEnabled()){
1087 logger.debug("to be created, organization common");
1088 logger.debug(objectAsXmlString(organization,
1089 OrganizationsCommon.class));