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.
176 // Item resource IDs are unique, so these are used as keys;
177 // the non-unique IDs of their parents are stored as associated values.
178 allResourceItemIdsCreated.put(newID, vcsid);
184 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
185 dependsOnMethods = {"create", "createItem"})
186 public void createList(String testName) throws Exception {
187 for (int i = 0; i < 3; i++) {
189 knownResourceId = lastOrgAuthId;
190 if (logger.isDebugEnabled()) {
191 logger.debug(testName + ": Resetting knownResourceId to" + knownResourceId);
193 // Add nItemsToCreateInList items to each orgauthority
194 for (int j = 0; j < nItemsToCreateInList; j++) {
195 createItem(testName);
201 // Placeholders until the three tests below can be uncommented.
202 // See Issue CSPACE-401.
204 public void createWithEmptyEntityBody(String testName) throws Exception {
208 public void createWithMalformedXml(String testName) throws Exception {
212 public void createWithWrongXmlSchema(String testName) throws Exception {
217 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
218 dependsOnMethods = {"create", "testSubmitRequest"})
219 public void createWithEmptyEntityBody(String testName) throws Exception {
222 setupCreateWithEmptyEntityBody(testName);
224 // Submit the request to the service and store the response.
225 String method = REQUEST_TYPE.httpMethodName();
226 String url = getServiceRootURL();
227 String mediaType = MediaType.APPLICATION_XML;
228 final String entity = "";
229 int statusCode = submitRequest(method, url, mediaType, entity);
231 // Check the status code of the response: does it match
232 // the expected response(s)?
233 if(logger.isDebugEnabled()) {
234 logger.debug(testName + ": url=" + url +
235 " status=" + statusCode);
237 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
238 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
239 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
243 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
244 dependsOnMethods = {"create", "testSubmitRequest"})
245 public void createWithMalformedXml(String testName) throws Exception {
248 setupCreateWithMalformedXml(testName);
250 // Submit the request to the service and store the response.
251 String method = REQUEST_TYPE.httpMethodName();
252 String url = getServiceRootURL();
253 String mediaType = MediaType.APPLICATION_XML;
254 final String entity = MALFORMED_XML_DATA; // Constant from base class.
255 int statusCode = submitRequest(method, url, mediaType, entity);
257 // Check the status code of the response: does it match
258 // the expected response(s)?
259 if(logger.isDebugEnabled()){
260 logger.debug(testName + ": url=" + url +
261 " status=" + statusCode);
263 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
264 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
265 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
269 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
270 dependsOnMethods = {"create", "testSubmitRequest"})
271 public void createWithWrongXmlSchema(String testName) throws Exception {
274 setupCreateWithWrongXmlSchema(testName);
276 // Submit the request to the service and store the response.
277 String method = REQUEST_TYPE.httpMethodName();
278 String url = getServiceRootURL();
279 String mediaType = MediaType.APPLICATION_XML;
280 final String entity = WRONG_XML_SCHEMA_DATA;
281 int statusCode = submitRequest(method, url, mediaType, entity);
283 // Check the status code of the response: does it match
284 // the expected response(s)?
285 if(logger.isDebugEnabled()){
286 logger.debug(testName + ": url=" + url +
287 " status=" + statusCode);
289 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
290 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
291 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
295 // ---------------------------------------------------------------
296 // CRUD tests : READ tests
297 // ---------------------------------------------------------------
300 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
301 dependsOnMethods = {"create"})
302 public void read(String testName) throws Exception {
307 // Submit the request to the service and store the response.
308 ClientResponse<MultipartInput> res = client.read(knownResourceId);
309 int statusCode = res.getStatus();
311 // Check the status code of the response: does it match
312 // the expected response(s)?
313 if(logger.isDebugEnabled()){
314 logger.debug(testName + ": status = " + statusCode);
316 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
317 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
318 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
319 //FIXME: remove the following try catch once Aron fixes signatures
321 MultipartInput input = (MultipartInput) res.getEntity();
322 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
323 client.getCommonPartName(), OrgauthoritiesCommon.class);
324 Assert.assertNotNull(orgAuthority);
325 } catch (Exception e) {
326 throw new RuntimeException(e);
331 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
332 dependsOnMethods = {"read"})
333 public void readByName(String testName) throws Exception {
338 // Submit the request to the service and store the response.
339 ClientResponse<MultipartInput> res = client.read(knownResourceId);
340 int statusCode = res.getStatus();
342 // Check the status code of the response: does it match
343 // the expected response(s)?
344 if(logger.isDebugEnabled()){
345 logger.debug(testName + ": status = " + statusCode);
347 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
348 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
349 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
350 //FIXME: remove the following try catch once Aron fixes signatures
352 MultipartInput input = (MultipartInput) res.getEntity();
353 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
354 client.getCommonPartName(), OrgauthoritiesCommon.class);
355 Assert.assertNotNull(orgAuthority);
356 } catch (Exception e) {
357 throw new RuntimeException(e);
362 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
363 dependsOnMethods = {"createItem", "read"})
364 public void readItem(String testName) throws Exception {
369 // Submit the request to the service and store the response.
370 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
371 int statusCode = res.getStatus();
373 // Check the status code of the response: does it match
374 // the expected response(s)?
375 if(logger.isDebugEnabled()){
376 logger.debug(testName + ": status = " + statusCode);
378 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
379 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
380 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
382 // Check whether we've received a organization.
383 MultipartInput input = (MultipartInput) res.getEntity();
384 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
385 client.getItemCommonPartName(), OrganizationsCommon.class);
386 Assert.assertNotNull(organization);
387 boolean showFull = true;
388 if(showFull && logger.isDebugEnabled()){
389 logger.debug(testName + ": returned payload:");
390 logger.debug(objectAsXmlString(organization,
391 OrganizationsCommon.class));
393 Assert.assertEquals(organization.getInAuthority(), knownResourceId);
396 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
397 dependsOnMethods = {"readItem", "updateItem"})
398 public void verifyItemDisplayName(String testName) throws Exception {
403 // Submit the request to the service and store the response.
404 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
405 int statusCode = res.getStatus();
407 // Check the status code of the response: does it match
408 // the expected response(s)?
409 if(logger.isDebugEnabled()){
410 logger.debug(testName + ": status = " + statusCode);
412 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
413 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
414 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
416 // Check whether organization has expected displayName.
417 MultipartInput input = (MultipartInput) res.getEntity();
418 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
419 client.getItemCommonPartName(), OrganizationsCommon.class);
420 Assert.assertNotNull(organization);
421 String displayName = organization.getDisplayName();
422 // Make sure displayName matches computed form
423 String expectedDisplayName =
424 OrgAuthorityClientUtils.prepareDefaultDisplayName(
425 TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
426 Assert.assertNotNull(displayName, expectedDisplayName);
428 // Update the shortName and verify the computed name is updated.
429 organization.setDisplayNameComputed(true);
430 organization.setShortName("updated-" + TEST_ORG_SHORTNAME);
431 expectedDisplayName =
432 OrgAuthorityClientUtils.prepareDefaultDisplayName(
433 "updated-" + TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
435 // Submit the updated resource to the service and store the response.
436 MultipartOutput output = new MultipartOutput();
437 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
438 commonPart.getHeaders().add("label", client.getItemCommonPartName());
439 res = client.updateItem(knownResourceId, knownItemResourceId, output);
440 statusCode = res.getStatus();
442 // Check the status code of the response: does it match the expected response(s)?
443 if(logger.isDebugEnabled()){
444 logger.debug("updateItem: status = " + statusCode);
446 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
447 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
448 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
450 // Retrieve the updated resource and verify that its contents exist.
451 input = (MultipartInput) res.getEntity();
452 OrganizationsCommon updatedOrganization =
453 (OrganizationsCommon) extractPart(input,
454 client.getItemCommonPartName(), OrganizationsCommon.class);
455 Assert.assertNotNull(updatedOrganization);
457 // Verify that the updated resource received the correct data.
458 Assert.assertEquals(updatedOrganization.getShortName(),
459 organization.getShortName(),
460 "Updated ShortName in Organization did not match submitted data.");
461 // Verify that the updated resource computes the right displayName.
462 Assert.assertEquals(updatedOrganization.getDisplayName(),
464 "Updated ShortName in Organization not reflected in computed DisplayName.");
466 // Now Update the displayName, not computed and verify the computed name is overriden.
467 organization.setDisplayNameComputed(false);
468 expectedDisplayName = "TestName";
469 organization.setDisplayName(expectedDisplayName);
471 // Submit the updated resource to the service and store the response.
472 output = new MultipartOutput();
473 commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
474 commonPart.getHeaders().add("label", client.getItemCommonPartName());
475 res = client.updateItem(knownResourceId, knownItemResourceId, output);
476 statusCode = res.getStatus();
478 // Check the status code of the response: does it match the expected response(s)?
479 if(logger.isDebugEnabled()){
480 logger.debug("updateItem: status = " + statusCode);
482 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
483 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
484 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
486 // Retrieve the updated resource and verify that its contents exist.
487 input = (MultipartInput) res.getEntity();
488 updatedOrganization =
489 (OrganizationsCommon) extractPart(input,
490 client.getItemCommonPartName(), OrganizationsCommon.class);
491 Assert.assertNotNull(updatedOrganization);
493 // Verify that the updated resource received the correct data.
494 Assert.assertEquals(updatedOrganization.isDisplayNameComputed(), false,
495 "Updated displayNameComputed in Organization did not match submitted data.");
496 // Verify that the updated resource computes the right displayName.
497 Assert.assertEquals(updatedOrganization.getDisplayName(),
499 "Updated DisplayName (not computed) in Organization not stored.");
504 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
505 dependsOnMethods = {"read"})
506 public void readNonExistent(String testName) {
509 setupReadNonExistent(testName);
511 // Submit the request to the service and store the response.
512 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
513 int statusCode = res.getStatus();
515 // Check the status code of the response: does it match
516 // the expected response(s)?
517 if(logger.isDebugEnabled()){
518 logger.debug(testName + ": status = " + statusCode);
520 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
521 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
522 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
525 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
526 dependsOnMethods = {"readItem", "readNonExistent"})
527 public void readItemNonExistent(String testName) {
530 setupReadNonExistent(testName);
532 // Submit the request to the service and store the response.
533 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
534 int statusCode = res.getStatus();
536 // Check the status code of the response: does it match
537 // the expected response(s)?
538 if(logger.isDebugEnabled()){
539 logger.debug(testName + ": status = " + statusCode);
541 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
542 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
543 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
545 // ---------------------------------------------------------------
546 // CRUD tests : READ_LIST tests
547 // ---------------------------------------------------------------
551 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
552 dependsOnMethods = {"createList", "read"})
553 public void readList(String testName) throws Exception {
556 setupReadList(testName);
558 // Submit the request to the service and store the response.
559 ClientResponse<OrgauthoritiesCommonList> res = client.readList();
560 OrgauthoritiesCommonList list = res.getEntity();
561 int statusCode = res.getStatus();
563 // Check the status code of the response: does it match
564 // the expected response(s)?
565 if(logger.isDebugEnabled()){
566 logger.debug(testName + ": status = " + statusCode);
568 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
569 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
570 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
572 // Optionally output additional data about list members for debugging.
573 boolean iterateThroughList = false;
574 if (iterateThroughList && logger.isDebugEnabled()) {
575 List<OrgauthoritiesCommonList.OrgauthorityListItem> items =
576 list.getOrgauthorityListItem();
578 for (OrgauthoritiesCommonList.OrgauthorityListItem item : items) {
579 String csid = item.getCsid();
580 logger.debug(testName + ": list-item[" + i + "] csid=" +
582 logger.debug(testName + ": list-item[" + i + "] displayName=" +
583 item.getDisplayName());
584 logger.debug(testName + ": list-item[" + i + "] URI=" +
592 @Test(dependsOnMethods = {"createList", "readItem"})
593 public void readItemList() {
594 readItemList(knownResourceId);
597 private void readItemList(String vcsid) {
599 final String testName = "readItemList";
602 setupReadList(testName);
604 // Submit the request to the service and store the response.
605 ClientResponse<OrganizationsCommonList> res =
606 client.readItemList(vcsid);
607 OrganizationsCommonList list = res.getEntity();
608 int statusCode = res.getStatus();
610 // Check the status code of the response: does it match
611 // the expected response(s)?
612 if(logger.isDebugEnabled()){
613 logger.debug(" " + testName + ": status = " + statusCode);
615 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
616 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
617 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
619 List<OrganizationsCommonList.OrganizationListItem> items =
620 list.getOrganizationListItem();
621 int nItemsReturned = items.size();
622 if(logger.isDebugEnabled()){
623 logger.debug(" " + testName + ": Expected "
624 + nItemsToCreateInList+" items; got: "+nItemsReturned);
626 Assert.assertEquals( nItemsReturned, nItemsToCreateInList);
629 for (OrganizationsCommonList.OrganizationListItem item : items) {
630 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
631 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
632 // Optionally output additional data about list members for debugging.
633 boolean showDetails = true;
634 if (showDetails && logger.isDebugEnabled()) {
635 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
637 logger.debug(" " + testName + ": list-item[" + i + "] refName=" +
639 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
640 item.getDisplayName());
641 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
650 // ---------------------------------------------------------------
651 // CRUD tests : UPDATE tests
652 // ---------------------------------------------------------------
655 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
656 dependsOnMethods = {"read"})
657 public void update(String testName) throws Exception {
660 setupUpdate(testName);
662 // Retrieve the contents of a resource to update.
663 ClientResponse<MultipartInput> res =
664 client.read(knownResourceId);
665 if(logger.isDebugEnabled()){
666 logger.debug(testName + ": read status = " + res.getStatus());
668 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
670 if(logger.isDebugEnabled()){
671 logger.debug("got OrgAuthority to update with ID: " + knownResourceId);
673 MultipartInput input = (MultipartInput) res.getEntity();
674 OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input,
675 client.getCommonPartName(), OrgauthoritiesCommon.class);
676 Assert.assertNotNull(orgAuthority);
678 // Update the contents of this resource.
679 orgAuthority.setDisplayName("updated-" + orgAuthority.getDisplayName());
680 orgAuthority.setVocabType("updated-" + orgAuthority.getVocabType());
681 if(logger.isDebugEnabled()){
682 logger.debug("to be updated OrgAuthority");
683 logger.debug(objectAsXmlString(orgAuthority, OrgauthoritiesCommon.class));
686 // Submit the updated resource to the service and store the response.
687 MultipartOutput output = new MultipartOutput();
688 OutputPart commonPart = output.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
689 commonPart.getHeaders().add("label", client.getCommonPartName());
690 res = client.update(knownResourceId, output);
691 int statusCode = res.getStatus();
693 // Check the status code of the response: does it match the expected response(s)?
694 if(logger.isDebugEnabled()){
695 logger.debug("update: status = " + statusCode);
697 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
698 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
699 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
701 // Retrieve the updated resource and verify that its contents exist.
702 input = (MultipartInput) res.getEntity();
703 OrgauthoritiesCommon updatedOrgAuthority =
704 (OrgauthoritiesCommon) extractPart(input,
705 client.getCommonPartName(), OrgauthoritiesCommon.class);
706 Assert.assertNotNull(updatedOrgAuthority);
708 // Verify that the updated resource received the correct data.
709 Assert.assertEquals(updatedOrgAuthority.getDisplayName(),
710 orgAuthority.getDisplayName(),
711 "Data in updated object did not match submitted data.");
714 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
715 dependsOnMethods = {"readItem", "update"})
716 public void updateItem(String testName) throws Exception {
719 setupUpdate(testName);
721 ClientResponse<MultipartInput> res =
722 client.readItem(knownResourceId, knownItemResourceId);
723 if(logger.isDebugEnabled()){
724 logger.debug(testName + ": read status = " + res.getStatus());
726 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
728 if(logger.isDebugEnabled()){
729 logger.debug("got Organization to update with ID: " +
730 knownItemResourceId +
731 " in OrgAuthority: " + knownResourceId );
733 MultipartInput input = (MultipartInput) res.getEntity();
734 OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
735 client.getItemCommonPartName(), OrganizationsCommon.class);
736 Assert.assertNotNull(organization);
738 // Update the contents of this resource.
739 organization.setShortName("updated-" + organization.getShortName());
740 if(logger.isDebugEnabled()){
741 logger.debug("to be updated Organization");
742 logger.debug(objectAsXmlString(organization,
743 OrganizationsCommon.class));
746 // Submit the updated resource to the service and store the response.
747 MultipartOutput output = new MultipartOutput();
748 OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
749 commonPart.getHeaders().add("label", client.getItemCommonPartName());
750 res = client.updateItem(knownResourceId, knownItemResourceId, output);
751 int statusCode = res.getStatus();
753 // Check the status code of the response: does it match the expected response(s)?
754 if(logger.isDebugEnabled()){
755 logger.debug("updateItem: status = " + statusCode);
757 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
758 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
759 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
761 // Retrieve the updated resource and verify that its contents exist.
762 input = (MultipartInput) res.getEntity();
763 OrganizationsCommon updatedOrganization =
764 (OrganizationsCommon) extractPart(input,
765 client.getItemCommonPartName(), OrganizationsCommon.class);
766 Assert.assertNotNull(updatedOrganization);
768 // Verify that the updated resource received the correct data.
769 Assert.assertEquals(updatedOrganization.getShortName(),
770 organization.getShortName(),
771 "Data in updated Organization did not match submitted data.");
775 // Placeholders until the three tests below can be uncommented.
776 // See Issue CSPACE-401.
778 public void updateWithEmptyEntityBody(String testName) throws Exception {
782 public void updateWithMalformedXml(String testName) throws Exception {
786 public void updateWithWrongXmlSchema(String testName) throws Exception {
791 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
792 dependsOnMethods = {"create", "update", "testSubmitRequest"})
793 public void updateWithEmptyEntityBody(String testName) throws Exception {
796 setupUpdateWithEmptyEntityBody(testName);
798 // Submit the request to the service and store the response.
799 String method = REQUEST_TYPE.httpMethodName();
800 String url = getResourceURL(knownResourceId);
801 String mediaType = MediaType.APPLICATION_XML;
802 final String entity = "";
803 int statusCode = submitRequest(method, url, mediaType, entity);
805 // Check the status code of the response: does it match
806 // the expected response(s)?
807 if(logger.isDebugEnabled()){
808 logger.debug(testName + ": url=" + url +
809 " status=" + statusCode);
811 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
812 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
813 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
817 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
818 dependsOnMethods = {"create", "update", "testSubmitRequest"})
819 public void updateWithMalformedXml(String testName) throws Exception {
822 setupUpdateWithMalformedXml(testName);
824 // Submit the request to the service and store the response.
825 String method = REQUEST_TYPE.httpMethodName();
826 String url = getResourceURL(knownResourceId);
827 String mediaType = MediaType.APPLICATION_XML;
828 final String entity = MALFORMED_XML_DATA;
829 int statusCode = submitRequest(method, url, mediaType, entity);
831 // Check the status code of the response: does it match
832 // the expected response(s)?
833 if(logger.isDebugEnabled()){
834 logger.debug(testName + ": url=" + url +
835 " status=" + statusCode);
837 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
838 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
839 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
843 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
844 dependsOnMethods = {"create", "update", "testSubmitRequest"})
845 public void updateWithWrongXmlSchema(String testName) throws Exception {
848 setupUpdateWithWrongXmlSchema(testName);
850 // Submit the request to the service and store the response.
851 String method = REQUEST_TYPE.httpMethodName();
852 String url = getResourceURL(knownResourceId);
853 String mediaType = MediaType.APPLICATION_XML;
854 final String entity = WRONG_XML_SCHEMA_DATA;
855 int statusCode = submitRequest(method, url, mediaType, entity);
857 // Check the status code of the response: does it match
858 // the expected response(s)?
859 if(logger.isDebugEnabled()){
860 logger.debug("updateWithWrongXmlSchema: url=" + url +
861 " status=" + statusCode);
863 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
864 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
865 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
871 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
872 dependsOnMethods = {"update", "testSubmitRequest"})
873 public void updateNonExistent(String testName) throws Exception {
876 setupUpdateNonExistent(testName);
878 // Submit the request to the service and store the response.
879 // Note: The ID used in this 'create' call may be arbitrary.
880 // The only relevant ID may be the one used in update(), below.
882 // The only relevant ID may be the one used in update(), below.
883 MultipartOutput multipart = createOrgAuthorityInstance(NON_EXISTENT_ID);
884 ClientResponse<MultipartInput> res =
885 client.update(NON_EXISTENT_ID, multipart);
886 int statusCode = res.getStatus();
888 // Check the status code of the response: does it match
889 // the expected response(s)?
890 if(logger.isDebugEnabled()){
891 logger.debug(testName + ": status = " + statusCode);
893 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
894 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
895 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
898 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
899 dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
900 public void updateNonExistentItem(String testName) throws Exception {
903 setupUpdateNonExistent(testName);
905 // Submit the request to the service and store the response.
906 // Note: The ID used in this 'create' call may be arbitrary.
907 // The only relevant ID may be the one used in update(), below.
909 // The only relevant ID may be the one used in update(), below.
910 Map<String, String> nonexOrgMap = new HashMap<String,String>();
911 nonexOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Non-existent");
912 String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, NON_EXISTENT_ID, true);
913 MultipartOutput multipart =
914 OrgAuthorityClientUtils.createOrganizationInstance(
915 knownResourceId, refName,
916 nonexOrgMap, client.getItemCommonPartName() );
917 ClientResponse<MultipartInput> res =
918 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
919 int statusCode = res.getStatus();
921 // Check the status code of the response: does it match
922 // the expected response(s)?
923 if(logger.isDebugEnabled()){
924 logger.debug(testName + ": status = " + statusCode);
926 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
927 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
928 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
931 // ---------------------------------------------------------------
932 // CRUD tests : DELETE tests
933 // ---------------------------------------------------------------
936 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
937 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
938 public void delete(String testName) throws Exception {
941 setupDelete(testName);
943 // Submit the request to the service and store the response.
944 ClientResponse<Response> res = client.delete(knownResourceId);
945 int statusCode = res.getStatus();
947 // Check the status code of the response: does it match
948 // the expected response(s)?
949 if(logger.isDebugEnabled()){
950 logger.debug(testName + ": status = " + statusCode);
952 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
953 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
954 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
957 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
958 dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
959 "updateItem", "verifyItemDisplayName"})
960 public void deleteItem(String testName) throws Exception {
963 setupDelete(testName);
965 // Submit the request to the service and store the response.
966 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
967 int statusCode = res.getStatus();
969 // Check the status code of the response: does it match
970 // the expected response(s)?
971 if(logger.isDebugEnabled()){
972 logger.debug("delete: status = " + statusCode);
974 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
975 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
976 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
981 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
982 dependsOnMethods = {"delete"})
983 public void deleteNonExistent(String testName) throws Exception {
986 setupDeleteNonExistent(testName);
988 // Submit the request to the service and store the response.
989 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
990 int statusCode = res.getStatus();
992 // Check the status code of the response: does it match
993 // the expected response(s)?
994 if(logger.isDebugEnabled()){
995 logger.debug(testName + ": status = " + statusCode);
997 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
998 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
999 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1002 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1003 dependsOnMethods = {"deleteItem"})
1004 public void deleteNonExistentItem(String testName) {
1007 setupDeleteNonExistent(testName);
1009 // Submit the request to the service and store the response.
1010 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1011 int statusCode = res.getStatus();
1013 // Check the status code of the response: does it match
1014 // the expected response(s)?
1015 if(logger.isDebugEnabled()){
1016 logger.debug(testName + ": status = " + statusCode);
1018 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1019 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1020 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1023 // ---------------------------------------------------------------
1024 // Utility tests : tests of code used in tests above
1025 // ---------------------------------------------------------------
1027 * Tests the code for manually submitting data that is used by several
1028 * of the methods above.
1030 @Test(dependsOnMethods = {"create", "read"})
1031 public void testSubmitRequest() {
1033 // Expected status code: 200 OK
1034 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1036 // Submit the request to the service and store the response.
1037 String method = ServiceRequestType.READ.httpMethodName();
1038 String url = getResourceURL(knownResourceId);
1039 int statusCode = submitRequest(method, url);
1041 // Check the status code of the response: does it match
1042 // the expected response(s)?
1043 if(logger.isDebugEnabled()){
1044 logger.debug("testSubmitRequest: url=" + url +
1045 " status=" + statusCode);
1047 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1051 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1052 public void testItemSubmitRequest() {
1054 // Expected status code: 200 OK
1055 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1057 // Submit the request to the service and store the response.
1058 String method = ServiceRequestType.READ.httpMethodName();
1059 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1060 int statusCode = submitRequest(method, url);
1062 // Check the status code of the response: does it match
1063 // the expected response(s)?
1064 if(logger.isDebugEnabled()){
1065 logger.debug("testItemSubmitRequest: url=" + url +
1066 " status=" + statusCode);
1068 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1072 // ---------------------------------------------------------------
1073 // Cleanup of resources created during testing
1074 // ---------------------------------------------------------------
1077 * Deletes all resources created by tests, after all tests have been run.
1079 * This cleanup method will always be run, even if one or more tests fail.
1080 * For this reason, it attempts to remove all resources created
1081 * at any point during testing, even if some of those resources
1082 * may be expected to be deleted by certain tests.
1084 @AfterClass(alwaysRun=true)
1085 public void cleanUp() {
1086 if (logger.isDebugEnabled()) {
1087 logger.debug("Cleaning up temporary resources created for testing ...");
1089 // Clean up organization resources.
1090 String orgAuthorityResourceId;
1091 String organizationResourceId;
1092 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
1093 organizationResourceId = entry.getKey();
1094 orgAuthorityResourceId = entry.getValue();
1095 // Note: Any non-success responses are ignored and not reported.
1096 ClientResponse<Response> res =
1097 client.deleteItem(orgAuthorityResourceId, organizationResourceId);
1099 // Clean up orgAuthority resources.
1100 for (String resourceId : allResourceIdsCreated) {
1101 // Note: Any non-success responses are ignored and not reported.
1102 ClientResponse<Response> res = client.delete(resourceId);
1106 // ---------------------------------------------------------------
1107 // Utility methods used by tests above
1108 // ---------------------------------------------------------------
1110 public String getServicePathComponent() {
1111 return SERVICE_PATH_COMPONENT;
1114 public String getItemServicePathComponent() {
1115 return ITEM_SERVICE_PATH_COMPONENT;
1119 * Returns the root URL for a service.
1121 * This URL consists of a base URL for all services, followed by
1122 * a path component for the owning orgAuthority, followed by the
1123 * path component for the items.
1125 * @return The root URL for a service.
1127 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1128 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1132 * Returns the URL of a specific resource managed by a service, and
1133 * designated by an identifier (such as a universally unique ID, or UUID).
1135 * @param resourceIdentifier An identifier (such as a UUID) for a resource.
1137 * @return The URL of a specific resource managed by a service.
1139 protected String getItemResourceURL(String parentResourceIdentifier, String resourceIdentifier) {
1140 return getItemServiceRootURL(parentResourceIdentifier) + "/" + resourceIdentifier;
1143 private MultipartOutput createOrgAuthorityInstance(String identifier) {
1144 String displayName = "displayName-" + identifier;
1145 String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true);
1146 return OrgAuthorityClientUtils.createOrgAuthorityInstance(
1147 displayName, refName,
1148 client.getCommonPartName());