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.OrganizationJAXBSchema;
33 import org.collectionspace.services.client.OrgAuthorityClient;
34 import org.collectionspace.services.client.OrgAuthorityClientUtils;
35 import org.collectionspace.services.organization.OrgauthoritiesCommon;
36 import org.collectionspace.services.organization.OrgauthoritiesCommonList;
37 import org.collectionspace.services.organization.OrganizationsCommon;
38 import org.collectionspace.services.organization.OrganizationsCommonList;
40 import org.jboss.resteasy.client.ClientResponse;
41 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
42 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
43 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
51 * OrgAuthorityServiceTest, carries out tests against a
52 * deployed and running OrgAuthority Service.
54 * $LastChangedRevision: 753 $
55 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
57 public class OrgAuthorityServiceTest extends AbstractServiceTestImpl {
59 private final Logger logger =
60 LoggerFactory.getLogger(OrgAuthorityServiceTest.class);
62 // Instance variables specific to this test.
63 private OrgAuthorityClient client = new OrgAuthorityClient();
64 final String SERVICE_PATH_COMPONENT = "orgauthorities";
65 final String ITEM_SERVICE_PATH_COMPONENT = "items";
66 private final String TEST_ORG_SHORTNAME = "Test Org";
67 private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
68 private String knownResourceId = null;
69 private String lastOrgAuthId = null;
70 private String knownResourceRefName = null;
71 private String knownItemResourceId = null;
72 private int nItemsToCreateInList = 3;
73 private List<String> allResourceIdsCreated = new ArrayList<String>();
74 private Map<String, String> allResourceItemIdsCreated =
75 new HashMap<String, String>();
77 // ---------------------------------------------------------------
78 // CRUD tests : CREATE tests
79 // ---------------------------------------------------------------
82 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.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 = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
94 MultipartOutput multipart =
95 OrgAuthorityClientUtils.createOrgAuthorityInstance(
97 client.getCommonPartName());
98 ClientResponse<Response> res = client.create(multipart);
99 int statusCode = res.getStatus();
101 // Check the status code of the response: does it match
102 // the expected response(s)?
105 // Does it fall within the set of valid status codes?
106 // Does it exactly match the expected status code?
107 if(logger.isDebugEnabled()){
108 logger.debug(testName + ": status = " + statusCode);
110 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
111 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
112 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
114 // Store the refname from the first resource created
115 // for additional tests below.
116 knownResourceRefName = refName;
118 lastOrgAuthId = extractId(res);
119 // Store the ID returned from the first resource created
120 // for additional tests below.
121 if (knownResourceId == null){
122 knownResourceId = lastOrgAuthId;
123 if (logger.isDebugEnabled()) {
124 logger.debug(testName + ": knownResourceId=" + knownResourceId);
127 // Store the IDs from every resource created by tests,
128 // so they can be deleted after tests have been run.
129 allResourceIdsCreated.add(extractId(res));
133 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
134 dependsOnMethods = {"create"})
135 public void createItem(String testName) {
136 setupCreate(testName);
138 knownItemResourceId = createItemInAuthority(lastOrgAuthId, knownResourceRefName);
139 if(logger.isDebugEnabled()){
140 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
144 private String createItemInAuthority(String vcsid, String orgAuthorityRefName) {
146 final String testName = "createItemInAuthority";
147 if(logger.isDebugEnabled()){
148 logger.debug(testName + ":...");
151 // Submit the request to the service and store the response.
152 String identifier = createIdentifier();
153 String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, identifier, true);
154 Map<String, String> testOrgMap = new HashMap<String,String>();
155 testOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, TEST_ORG_SHORTNAME);
156 testOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "The real official test organization");
157 testOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "joe@test.org");
158 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "May 26, 1907");
159 testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
160 testOrgMap.put(OrganizationJAXBSchema.FUNCTION, "For testing");
161 String newID = OrgAuthorityClientUtils.createItemInAuthority(
162 vcsid, orgAuthorityRefName, testOrgMap, client);
163 // Store the ID returned from the first item resource created
164 // for additional tests below.
165 if (knownItemResourceId == null){
166 knownItemResourceId = newID;
167 if (logger.isDebugEnabled()) {
168 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
172 // Store the IDs from any item resources created
173 // by tests, along with the IDs of their parents, so these items
174 // can be deleted after all tests have been run.
175 allResourceItemIdsCreated.put(newID, vcsid);
181 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
182 dependsOnMethods = {"create", "createItem"})
183 public void createList(String testName) throws Exception {
184 for (int i = 0; i < 3; i++) {
186 knownResourceId = lastOrgAuthId;
187 if (logger.isDebugEnabled()) {
188 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
190 // Add nItemsToCreateInList items to each orgauthority
191 for (int j = 0; j < nItemsToCreateInList; j++) {
192 createItem(testName);
198 // Placeholders until the three tests below can be uncommented.
199 // See Issue CSPACE-401.
201 public void createWithEmptyEntityBody(String testName) throws Exception {
205 public void createWithMalformedXml(String testName) throws Exception {
209 public void createWithWrongXmlSchema(String testName) throws Exception {
214 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
215 dependsOnMethods = {"create", "testSubmitRequest"})
216 public void createWithEmptyEntityBody(String testName) throws Exception {
219 setupCreateWithEmptyEntityBody(testName);
221 // Submit the request to the service and store the response.
222 String method = REQUEST_TYPE.httpMethodName();
223 String url = getServiceRootURL();
224 String mediaType = MediaType.APPLICATION_XML;
225 final String entity = "";
226 int statusCode = submitRequest(method, url, mediaType, entity);
228 // Check the status code of the response: does it match
229 // the expected response(s)?
230 if(logger.isDebugEnabled()) {
231 logger.debug(testName + ": url=" + url +
232 " status=" + statusCode);
234 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
235 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
236 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
240 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
241 dependsOnMethods = {"create", "testSubmitRequest"})
242 public void createWithMalformedXml(String testName) throws Exception {
245 setupCreateWithMalformedXml(testName);
247 // Submit the request to the service and store the response.
248 String method = REQUEST_TYPE.httpMethodName();
249 String url = getServiceRootURL();
250 String mediaType = MediaType.APPLICATION_XML;
251 final String entity = MALFORMED_XML_DATA; // Constant from base class.
252 int statusCode = submitRequest(method, url, mediaType, entity);
254 // Check the status code of the response: does it match
255 // the expected response(s)?
256 if(logger.isDebugEnabled()){
257 logger.debug(testName + ": url=" + url +
258 " status=" + statusCode);
260 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
261 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
262 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
266 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
267 dependsOnMethods = {"create", "testSubmitRequest"})
268 public void createWithWrongXmlSchema(String testName) throws Exception {
271 setupCreateWithWrongXmlSchema(testName);
273 // Submit the request to the service and store the response.
274 String method = REQUEST_TYPE.httpMethodName();
275 String url = getServiceRootURL();
276 String mediaType = MediaType.APPLICATION_XML;
277 final String entity = WRONG_XML_SCHEMA_DATA;
278 int statusCode = submitRequest(method, url, mediaType, entity);
280 // Check the status code of the response: does it match
281 // the expected response(s)?
282 if(logger.isDebugEnabled()){
283 logger.debug(testName + ": url=" + url +
284 " status=" + statusCode);
286 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
287 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
288 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
292 // ---------------------------------------------------------------
293 // CRUD tests : READ tests
294 // ---------------------------------------------------------------
297 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
298 dependsOnMethods = {"create"})
299 public void read(String testName) throws Exception {
304 // Submit the request to the service and store the response.
305 ClientResponse<MultipartInput> res = client.read(knownResourceId);
306 int statusCode = res.getStatus();
308 // Check the status code of the response: does it match
309 // the expected response(s)?
310 if(logger.isDebugEnabled()){
311 logger.debug(testName + ": status = " + statusCode);
313 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
314 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
315 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
316 //FIXME: remove the following try catch once Aron fixes signatures
318 MultipartInput input = (MultipartInput) res.getEntity();
319 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
320 client.getCommonPartName(), OrgauthoritiesCommon.class);
321 Assert.assertNotNull(orgAuthority);
322 } catch (Exception e) {
323 throw new RuntimeException(e);
328 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
329 dependsOnMethods = {"read"})
330 public void readByName(String testName) throws Exception {
335 // Submit the request to the service and store the response.
336 ClientResponse<MultipartInput> res = client.read(knownResourceId);
337 int statusCode = res.getStatus();
339 // Check the status code of the response: does it match
340 // the expected response(s)?
341 if(logger.isDebugEnabled()){
342 logger.debug(testName + ": status = " + statusCode);
344 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
345 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
346 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
347 //FIXME: remove the following try catch once Aron fixes signatures
349 MultipartInput input = (MultipartInput) res.getEntity();
350 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
351 client.getCommonPartName(), OrgauthoritiesCommon.class);
352 Assert.assertNotNull(orgAuthority);
353 } catch (Exception e) {
354 throw new RuntimeException(e);
359 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
360 dependsOnMethods = {"createItem", "read"})
361 public void readItem(String testName) throws Exception {
366 // Submit the request to the service and store the response.
367 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
368 int statusCode = res.getStatus();
370 // Check the status code of the response: does it match
371 // the expected response(s)?
372 if(logger.isDebugEnabled()){
373 logger.debug(testName + ": status = " + statusCode);
375 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
376 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
377 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
379 // Check whether we've received a organization.
380 MultipartInput input = (MultipartInput) res.getEntity();
381 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
382 client.getItemCommonPartName(), OrganizationsCommon.class);
383 Assert.assertNotNull(organization);
384 boolean showFull = true;
385 if(showFull && logger.isDebugEnabled()){
386 logger.debug(testName + ": returned payload:");
387 logger.debug(objectAsXmlString(organization,
388 OrganizationsCommon.class));
390 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
393 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
394 dependsOnMethods = {"readItem", "updateItem"})
395 public void verifyItemDisplayName(String testName) throws Exception {
398 setupUpdate(testName);
400 // Submit the request to the service and store the response.
401 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
402 int statusCode = res.getStatus();
404 // Check the status code of the response: does it match
405 // the expected response(s)?
406 if(logger.isDebugEnabled()){
407 logger.debug(testName + ": status = " + statusCode);
409 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
410 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
411 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
413 // Check whether organization has expected displayName.
414 MultipartInput input = (MultipartInput) res.getEntity();
415 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
416 client.getItemCommonPartName(), OrganizationsCommon.class);
417 Assert.assertNotNull(organization);
418 String displayName = organization.getDisplayName();
419 // Make sure displayName matches computed form
420 String expectedDisplayName =
421 OrgAuthorityClientUtils.prepareDefaultDisplayName(
422 TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
423 Assert.assertNotNull(displayName, expectedDisplayName);
425 // Update the shortName and verify the computed name is updated.
426 organization.setDisplayNameComputed(true);
427 organization.setShortName("updated-" + TEST_ORG_SHORTNAME);
428 expectedDisplayName =
429 OrgAuthorityClientUtils.prepareDefaultDisplayName(
430 "updated-" + TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
432 // Submit the updated resource to the service and store the response.
433 MultipartOutput output = new MultipartOutput();
434 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
435 commonPart.getHeaders().add("label", client.getItemCommonPartName());
436 res = client.updateItem(knownResourceId, knownItemResourceId, output);
437 statusCode = res.getStatus();
439 // Check the status code of the response: does it match the expected response(s)?
440 if(logger.isDebugEnabled()){
441 logger.debug("updateItem: status = " + statusCode);
443 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
444 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
445 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
447 // Retrieve the updated resource and verify that its contents exist.
448 input = (MultipartInput) res.getEntity();
449 OrganizationsCommon updatedOrganization =
450 (OrganizationsCommon) extractPart(input,
451 client.getItemCommonPartName(), OrganizationsCommon.class);
452 Assert.assertNotNull(updatedOrganization);
454 // Verify that the updated resource received the correct data.
455 Assert.assertEquals(updatedOrganization.getShortName(),
456 organization.getShortName(),
457 "Updated ShortName in Organization did not match submitted data.");
458 // Verify that the updated resource computes the right displayName.
459 Assert.assertEquals(updatedOrganization.getDisplayName(),
461 "Updated ShortName in Organization not reflected in computed DisplayName.");
463 // Now Update the displayName, not computed and verify the computed name is overriden.
464 organization.setDisplayNameComputed(false);
465 expectedDisplayName = "TestName";
466 organization.setDisplayName(expectedDisplayName);
468 // Submit the updated resource to the service and store the response.
469 output = new MultipartOutput();
470 commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
471 commonPart.getHeaders().add("label", client.getItemCommonPartName());
472 res = client.updateItem(knownResourceId, knownItemResourceId, output);
473 statusCode = res.getStatus();
475 // Check the status code of the response: does it match the expected response(s)?
476 if(logger.isDebugEnabled()){
477 logger.debug("updateItem: status = " + statusCode);
479 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
480 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
481 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
483 // Retrieve the updated resource and verify that its contents exist.
484 input = (MultipartInput) res.getEntity();
485 updatedOrganization =
486 (OrganizationsCommon) extractPart(input,
487 client.getItemCommonPartName(), OrganizationsCommon.class);
488 Assert.assertNotNull(updatedOrganization);
490 // Verify that the updated resource received the correct data.
491 Assert.assertEquals(updatedOrganization.isDisplayNameComputed(), false,
492 "Updated displayNameComputed in Organization did not match submitted data.");
493 // Verify that the updated resource computes the right displayName.
494 Assert.assertEquals(updatedOrganization.getDisplayName(),
496 "Updated DisplayName (not computed) in Organization not stored.");
499 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
500 dependsOnMethods = {"verifyItemDisplayName"})
501 public void verifyIllegalItemDisplayName(String testName) throws Exception {
504 setupUpdateWithWrongXmlSchema(testName);
506 // Submit the request to the service and store the response.
507 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
508 int statusCode = res.getStatus();
510 // Check the status code of the response: does it match
511 // the expected response(s)?
512 if(logger.isDebugEnabled()){
513 logger.debug(testName + ": status = " + statusCode);
515 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
516 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
517 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
519 // Check whether organization has expected displayName.
520 MultipartInput input = (MultipartInput) res.getEntity();
521 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
522 client.getItemCommonPartName(), OrganizationsCommon.class);
523 Assert.assertNotNull(organization);
524 // Try to Update with computed false and no displayName
525 organization.setDisplayNameComputed(false);
526 organization.setDisplayName(null);
528 // Submit the updated resource to the service and store the response.
529 MultipartOutput output = new MultipartOutput();
530 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
531 commonPart.getHeaders().add("label", client.getItemCommonPartName());
532 res = client.updateItem(knownResourceId, knownItemResourceId, output);
533 statusCode = res.getStatus();
535 // Check the status code of the response: does it match the expected response(s)?
536 if(logger.isDebugEnabled()){
537 logger.debug("updateItem: status = " + statusCode);
539 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
540 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
541 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
546 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
547 dependsOnMethods = {"read"})
548 public void readNonExistent(String testName) {
551 setupReadNonExistent(testName);
553 // Submit the request to the service and store the response.
554 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
555 int statusCode = res.getStatus();
557 // Check the status code of the response: does it match
558 // the expected response(s)?
559 if(logger.isDebugEnabled()){
560 logger.debug(testName + ": status = " + statusCode);
562 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
563 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
564 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
567 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
568 dependsOnMethods = {"readItem", "readNonExistent"})
569 public void readItemNonExistent(String testName) {
572 setupReadNonExistent(testName);
574 // Submit the request to the service and store the response.
575 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
576 int statusCode = res.getStatus();
578 // Check the status code of the response: does it match
579 // the expected response(s)?
580 if(logger.isDebugEnabled()){
581 logger.debug(testName + ": status = " + statusCode);
583 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
584 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
585 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
587 // ---------------------------------------------------------------
588 // CRUD tests : READ_LIST tests
589 // ---------------------------------------------------------------
593 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
594 dependsOnMethods = {"createList", "read"})
595 public void readList(String testName) throws Exception {
598 setupReadList(testName);
600 // Submit the request to the service and store the response.
601 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
602 OrgauthoritiesCommonList list = res.getEntity();
603 int statusCode = res.getStatus();
605 // Check the status code of the response: does it match
606 // the expected response(s)?
607 if(logger.isDebugEnabled()){
608 logger.debug(testName + ": status = " + statusCode);
610 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
611 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
612 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
614 // Optionally output additional data about list members for debugging.
615 boolean iterateThroughList = false;
616 if (iterateThroughList && logger.isDebugEnabled()) {
617 List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
618 list.getOrgauthorityListItem();
620 for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
621 String csid = item.getCsid();
622 logger.debug(testName + ": list-item[" + i + "] csid=" +
624 logger.debug(testName + ": list-item[" + i + "] displayName=" +
625 item.getDisplayName());
626 logger.debug(testName + ": list-item[" + i + "] URI=" +
634 @Test(dependsOnMethods = {"createList", "readItem"})
635 public void readItemList() {
636 readItemList(knownResourceId);
639 private void readItemList(String vcsid) {
641 final String testName = "readItemList";
644 setupReadList(testName);
646 // Submit the request to the service and store the response.
647 ClientResponse<OrganizationsCommonList> res =
648 client.readItemList(vcsid);
649 OrganizationsCommonList list = res.getEntity();
650 int statusCode = res.getStatus();
652 // Check the status code of the response: does it match
653 // the expected response(s)?
654 if(logger.isDebugEnabled()){
655 logger.debug(" " + testName + ": status = " + statusCode);
657 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
658 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
659 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
661 List<OrganizationsCommonList.OrganizationListItem> items =
662 list.getOrganizationListItem();
663 int nItemsReturned = items.size();
664 if(logger.isDebugEnabled()){
665 logger.debug(" " + testName + ": Expected "
666 + nItemsToCreateInList+" items; got: "+nItemsReturned);
668 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
671 for (OrganizationsCommonList.OrganizationListItem item : items) {
672 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
673 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
674 // Optionally output additional data about list members for debugging.
675 boolean showDetails = true;
676 if (showDetails && logger.isDebugEnabled()) {
677 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
679 logger.debug(" " + testName + ": list-item[" + i + "] refName=" +
681 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
682 item.getDisplayName());
683 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
692 // ---------------------------------------------------------------
693 // CRUD tests : UPDATE tests
694 // ---------------------------------------------------------------
697 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
698 dependsOnMethods = {"read"})
699 public void update(String testName) throws Exception {
702 setupUpdate(testName);
704 // Retrieve the contents of a resource to update.
705 ClientResponse<MultipartInput> res =
706 client.read(knownResourceId);
707 if(logger.isDebugEnabled()){
708 logger.debug(testName + ": read status = " + res.getStatus());
710 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
712 if(logger.isDebugEnabled()){
713 logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
715 MultipartInput input = (MultipartInput) res.getEntity();
716 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
717 client.getCommonPartName(), OrgauthoritiesCommon.class);
718 Assert.assertNotNull(orgAuthority);
720 // Update the contents of this resource.
721 orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
722 orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
723 if(logger.isDebugEnabled()){
724 logger.debug("to be updated OrgAuthority");
725 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
728 // Submit the updated resource to the service and store the response.
729 MultipartOutput output = new MultipartOutput();
730 OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
731 commonPart.getHeaders().add("label", client.getCommonPartName());
732 res = client.update(knownResourceId, output);
733 int statusCode = res.getStatus();
735 // Check the status code of the response: does it match the expected response(s)?
736 if(logger.isDebugEnabled()){
737 logger.debug("update: status = " + statusCode);
739 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
740 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
741 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
743 // Retrieve the updated resource and verify that its contents exist.
744 input = (MultipartInput) res.getEntity();
745 OrgauthoritiesCommon updatedOrgAuthority =
746 (OrgauthoritiesCommon) extractPart(input,
747 client.getCommonPartName(), OrgauthoritiesCommon.class);
748 Assert.assertNotNull(updatedOrgAuthority);
750 // Verify that the updated resource received the correct data.
751 Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
752 orgAuthority.getDisplayName(),
753 "Data in updated object did not match submitted data.");
756 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
757 dependsOnMethods = {"readItem", "update"})
758 public void updateItem(String testName) throws Exception {
761 setupUpdate(testName);
763 ClientResponse<MultipartInput> res =
764 client.readItem(knownResourceId, knownItemResourceId);
765 if(logger.isDebugEnabled()){
766 logger.debug(testName + ": read status = " + res.getStatus());
768 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
770 if(logger.isDebugEnabled()){
771 logger.debug("got Organization to update with ID: " +
772 knownItemResourceId +
773 " in OrgAuthority: " + knownResourceId );
775 MultipartInput input = (MultipartInput) res.getEntity();
776 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
777 client.getItemCommonPartName(), OrganizationsCommon.class);
778 Assert.assertNotNull(organization);
780 // Update the contents of this resource.
781 organization.setShortName("updated-" + organization.getShortName());
782 if(logger.isDebugEnabled()){
783 logger.debug("to be updated Organization");
784 logger.debug(objectAsXmlString(organization,
785 OrganizationsCommon.class));
788 // Submit the updated resource to the service and store the response.
789 MultipartOutput output = new MultipartOutput();
790 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
791 commonPart.getHeaders().add("label", client.getItemCommonPartName());
792 res = client.updateItem(knownResourceId, knownItemResourceId, output);
793 int statusCode = res.getStatus();
795 // Check the status code of the response: does it match the expected response(s)?
796 if(logger.isDebugEnabled()){
797 logger.debug("updateItem: status = " + statusCode);
799 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
800 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
801 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
803 // Retrieve the updated resource and verify that its contents exist.
804 input = (MultipartInput) res.getEntity();
805 OrganizationsCommon updatedOrganization =
806 (OrganizationsCommon) extractPart(input,
807 client.getItemCommonPartName(), OrganizationsCommon.class);
808 Assert.assertNotNull(updatedOrganization);
810 // Verify that the updated resource received the correct data.
811 Assert.assertEquals(updatedOrganization.getShortName(),
812 organization.getShortName(),
813 "Data in updated Organization did not match submitted data.");
817 // Placeholders until the three tests below can be uncommented.
818 // See Issue CSPACE-401.
820 public void updateWithEmptyEntityBody(String testName) throws Exception {
824 public void updateWithMalformedXml(String testName) throws Exception {
828 public void updateWithWrongXmlSchema(String testName) throws Exception {
833 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
834 dependsOnMethods = {"create", "update", "testSubmitRequest"})
835 public void updateWithEmptyEntityBody(String testName) throws Exception {
838 setupUpdateWithEmptyEntityBody(testName);
840 // Submit the request to the service and store the response.
841 String method = REQUEST_TYPE.httpMethodName();
842 String url = getResourceURL(knownResourceId);
843 String mediaType = MediaType.APPLICATION_XML;
844 final String entity = "";
845 int statusCode = submitRequest(method, url, mediaType, entity);
847 // Check the status code of the response: does it match
848 // the expected response(s)?
849 if(logger.isDebugEnabled()){
850 logger.debug(testName + ": url=" + url +
851 " status=" + statusCode);
853 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
854 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
855 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
859 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
860 dependsOnMethods = {"create", "update", "testSubmitRequest"})
861 public void updateWithMalformedXml(String testName) throws Exception {
864 setupUpdateWithMalformedXml(testName);
866 // Submit the request to the service and store the response.
867 String method = REQUEST_TYPE.httpMethodName();
868 String url = getResourceURL(knownResourceId);
869 String mediaType = MediaType.APPLICATION_XML;
870 final String entity = MALFORMED_XML_DATA;
871 int statusCode = submitRequest(method, url, mediaType, entity);
873 // Check the status code of the response: does it match
874 // the expected response(s)?
875 if(logger.isDebugEnabled()){
876 logger.debug(testName + ": url=" + url +
877 " status=" + statusCode);
879 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
880 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
881 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
885 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
886 dependsOnMethods = {"create", "update", "testSubmitRequest"})
887 public void updateWithWrongXmlSchema(String testName) throws Exception {
890 setupUpdateWithWrongXmlSchema(testName);
892 // Submit the request to the service and store the response.
893 String method = REQUEST_TYPE.httpMethodName();
894 String url = getResourceURL(knownResourceId);
895 String mediaType = MediaType.APPLICATION_XML;
896 final String entity = WRONG_XML_SCHEMA_DATA;
897 int statusCode = submitRequest(method, url, mediaType, entity);
899 // Check the status code of the response: does it match
900 // the expected response(s)?
901 if(logger.isDebugEnabled()){
902 logger.debug("updateWithWrongXmlSchema: url=" + url +
903 " status=" + statusCode);
905 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
906 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
907 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
913 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
914 dependsOnMethods = {"update", "testSubmitRequest"})
915 public void updateNonExistent(String testName) throws Exception {
918 setupUpdateNonExistent(testName);
920 // Submit the request to the service and store the response.
921 // Note: The ID used in this 'create' call may be arbitrary.
922 // The only relevant ID may be the one used in update(), below.
924 // The only relevant ID may be the one used in update(), below.
925 MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
926 ClientResponse<MultipartInput> res =
927 client.update(NON_EXISTENT_ID, multipart);
928 int statusCode = res.getStatus();
930 // Check the status code of the response: does it match
931 // the expected response(s)?
932 if(logger.isDebugEnabled()){
933 logger.debug(testName + ": status = " + statusCode);
935 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
936 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
937 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
940 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
941 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
942 public void updateNonExistentItem(String testName) throws Exception {
945 setupUpdateNonExistent(testName);
947 // Submit the request to the service and store the response.
948 // Note: The ID used in this 'create' call may be arbitrary.
949 // The only relevant ID may be the one used in update(), below.
951 // The only relevant ID may be the one used in update(), below.
952 Map<String, String> nonexOrgMap = new HashMap<String,String>();
953 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
954 String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, NON_EXISTENT_ID, true);
955 MultipartOutput multipart =
956 OrgAuthorityClientUtils.createOrganizationInstance(
957 knownResourceId, refName,
958 nonexOrgMap, client.getItemCommonPartName() );
959 ClientResponse<MultipartInput> res =
960 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
961 int statusCode = res.getStatus();
963 // Check the status code of the response: does it match
964 // the expected response(s)?
965 if(logger.isDebugEnabled()){
966 logger.debug(testName + ": status = " + statusCode);
968 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
969 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
970 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
973 // ---------------------------------------------------------------
974 // CRUD tests : DELETE tests
975 // ---------------------------------------------------------------
978 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
979 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
980 public void delete(String testName) throws Exception {
983 setupDelete(testName);
985 // Submit the request to the service and store the response.
986 ClientResponse<Response> res = client.delete(knownResourceId);
987 int statusCode = res.getStatus();
989 // Check the status code of the response: does it match
990 // the expected response(s)?
991 if(logger.isDebugEnabled()){
992 logger.debug(testName + ": status = " + statusCode);
994 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
995 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
996 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
999 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1000 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
1001 "updateItem", "verifyIllegalItemDisplayName"})
1002 public void deleteItem(String testName) throws Exception {
1005 setupDelete(testName);
1007 // Submit the request to the service and store the response.
1008 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1009 int statusCode = res.getStatus();
1011 // Check the status code of the response: does it match
1012 // the expected response(s)?
1013 if(logger.isDebugEnabled()){
1014 logger.debug("delete: status = " + statusCode);
1016 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1017 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1018 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1023 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1024 dependsOnMethods = {"delete"})
1025 public void deleteNonExistent(String testName) throws Exception {
1028 setupDeleteNonExistent(testName);
1030 // Submit the request to the service and store the response.
1031 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1032 int statusCode = res.getStatus();
1034 // Check the status code of the response: does it match
1035 // the expected response(s)?
1036 if(logger.isDebugEnabled()){
1037 logger.debug(testName + ": status = " + statusCode);
1039 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1040 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1041 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1044 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1045 dependsOnMethods = {"deleteItem"})
1046 public void deleteNonExistentItem(String testName) {
1049 setupDeleteNonExistent(testName);
1051 // Submit the request to the service and store the response.
1052 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1053 int statusCode = res.getStatus();
1055 // Check the status code of the response: does it match
1056 // the expected response(s)?
1057 if(logger.isDebugEnabled()){
1058 logger.debug(testName + ": status = " + statusCode);
1060 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1061 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1062 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1065 // ---------------------------------------------------------------
1066 // Utility tests : tests of code used in tests above
1067 // ---------------------------------------------------------------
1069 * Tests the code for manually submitting data that is used by several
1070 * of the methods above.
1072 @Test(dependsOnMethods = {"create", "read"})
1073 public void testSubmitRequest() {
1075 // Expected status code: 200 OK
1076 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1078 // Submit the request to the service and store the response.
1079 String method = ServiceRequestType.READ.httpMethodName();
1080 String url = getResourceURL(knownResourceId);
1081 int statusCode = submitRequest(method, url);
1083 // Check the status code of the response: does it match
1084 // the expected response(s)?
1085 if(logger.isDebugEnabled()){
1086 logger.debug("testSubmitRequest: url=" + url +
1087 " status=" + statusCode);
1089 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1093 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1094 public void testItemSubmitRequest() {
1096 // Expected status code: 200 OK
1097 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1099 // Submit the request to the service and store the response.
1100 String method = ServiceRequestType.READ.httpMethodName();
1101 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1102 int statusCode = submitRequest(method, url);
1104 // Check the status code of the response: does it match
1105 // the expected response(s)?
1106 if(logger.isDebugEnabled()){
1107 logger.debug("testItemSubmitRequest: url=" + url +
1108 " status=" + statusCode);
1110 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1114 // ---------------------------------------------------------------
1115 // Cleanup of resources created during testing
1116 // ---------------------------------------------------------------
1119 * Deletes all resources created by tests, after all tests have been run.
1121 * This cleanup method will always be run, even if one or more tests fail.
1122 * For this reason, it attempts to remove all resources created
1123 * at any point during testing, even if some of those resources
1124 * may be expected to be deleted by certain tests.
1126 @AfterClass(alwaysRun=true)
1127 public void cleanUp() {
1128 if (logger.isDebugEnabled()) {
1129 logger.debug("Cleaning up temporary resources created for testing ...");
1131 // Clean up organization resources.
1132 String orgAuthorityResourceId;
1133 String organizationResourceId;
1134 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1135 organizationResourceId = entry.getKey();
1136 orgAuthorityResourceId = entry.getValue();
1137 // Note: Any non-success responses are ignored and not reported.
1138 ClientResponse<Response> res =
1139 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
1141 // Clean up orgAuthority resources.
1142 for (String resourceId : allResourceIdsCreated) {
1143 // Note: Any non-success responses are ignored and not reported.
1144 ClientResponse<Response> res = client.delete(resourceId);
1148 // ---------------------------------------------------------------
1149 // Utility methods used by tests above
1150 // ---------------------------------------------------------------
1152 public String getServicePathComponent() {
1153 return SERVICE_PATH_COMPONENT;
1156 public String getItemServicePathComponent() {
1157 return ITEM_SERVICE_PATH_COMPONENT;
1161 * Returns the root URL for a service.
1163 * This URL consists of a base URL for all services, followed by
1164 * a path component for the owning orgAuthority, followed by the
1165 * path component for the items.
1167 * @return The root URL for a service.
1169 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1170 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1174 * Returns the URL of a specific resource managed by a service, and
1175 * designated by an identifier (such as a universally unique ID, or UUID).
1177 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1179 * @return The URL of a specific resource managed by a service.
1181 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1182 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1185 private MultipartOutput createOrgAuthorityInstance(String identifier) {
1186 String displayName = "displayName-" + identifier;
1187 String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
1188 return OrgAuthorityClientUtils.createOrgAuthorityInstance(
1189 displayName, refName,
1190 client.getCommonPartName());