From 35318fdaa76e79673cae1942b5999b2e090d59e0 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Thu, 7 Jun 2012 15:36:59 -0700 Subject: [PATCH] CSPACE-5271: Added negative tests; began logging non-fatal exceptions. --- .../services/common/UriTemplate.java | 28 ++++++------- .../services/common/test/UriTemplateTest.java | 39 +++++++++++++++---- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java index 261489d40..bb8cdf746 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java @@ -27,11 +27,15 @@ import java.util.Map; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilderException; import org.collectionspace.services.common.api.Tools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class UriTemplate { - UriBuilder builder; - String uriPath; + private static final Logger logger = LoggerFactory.getLogger(UriTemplate.class); + private UriBuilder builder; + private String uriPath; + private final static String EMPTY_STRING = ""; public UriTemplate(String path) { setUriPath(path); @@ -43,7 +47,7 @@ public class UriTemplate { this.uriPath = path; } } - + private String getUriPath() { return uriPath; } @@ -53,9 +57,9 @@ public class UriTemplate { try { builder = UriBuilder.fromPath(getUriPath()); } catch (IllegalArgumentException iae) { - // FIXME: Need to add logger and log error - // URIBuilder can't be created if path is null - // No other checking of path format is performed + logger.warn("URI path was null when attempting to creating new UriTemplate."); + // No other checking of path format, other than for null values, + // is performed automatically by this Exception handling. } } } @@ -66,7 +70,7 @@ public class UriTemplate { } return builder; } - + @Override public String toString() { return getUriPath(); @@ -77,18 +81,16 @@ public class UriTemplate { try { uri = getBuilder().buildFromMap(varsMap); } catch (IllegalArgumentException iae) { - // FIXME: Need to add logger and log error - // One or more parameters are missing or null. + logger.warn("One or more required parameter values were missing " + + "when building URI value via URI Template: " + iae.getMessage()); } catch (UriBuilderException ube) { - // FIXME: Need to add logger and log error - // URI can't be constructed based on current state of the builder + logger.warn("URI value can't be constructed due to state of URIBuilder: " + ube.getMessage()); } finally { if (uri != null) { return uri.toString(); } else { - return ""; + return EMPTY_STRING; } } } - } \ No newline at end of file diff --git a/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java b/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java index b897c322e..bdc2e4020 100644 --- a/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java +++ b/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java @@ -32,10 +32,9 @@ import org.testng.Assert; import org.testng.annotations.Test; public class UriTemplateTest { - + final static String EXAMPLE_SERVICE_NAME = "examples"; final static String CSID = "a87f6616-4146-4c17-a41a-048597cc12aa"; - private static final Logger logger = LoggerFactory.getLogger(UriTemplateTest.class); private void testBanner(String msg) { @@ -47,22 +46,48 @@ public class UriTemplateTest { public void createResourceUriTemplate() { testBanner("createResourceUriTemplate"); UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE); - Assert.assertNotNull(resourceTemplate, "Resource template is null."); + Assert.assertNotNull(resourceTemplate, "Resource template is null; it was not created successfully."); logger.debug("Resource template URI path = " + resourceTemplate.toString()); - Assert.assertNotNull(resourceTemplate.toString(), "Resource template URI path is null."); + Assert.assertNotNull(resourceTemplate.toString(), "Resource template URI path is null; it was not set successfully."); Assert.assertEquals(resourceTemplate.toString(), UriTemplateFactory.RESOURCE_TEMPLATE_PATTERN, "Resource template URI path doesn't match expected path."); } - - @Test (dependsOnMethods = {"createResourceUriTemplate"}) + + @Test(dependsOnMethods = {"createResourceUriTemplate"}) public void buildResourceUri() { testBanner("buildResourceUri"); UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE); - Map resourceUriVars = new HashMap(); + Map resourceUriVars = new HashMap(); resourceUriVars.put("servicename", EXAMPLE_SERVICE_NAME); resourceUriVars.put("identifier", CSID); String uriStr = resourceTemplate.buildUri(resourceUriVars); Assert.assertFalse(Tools.isBlank(uriStr), "Generated URI string is null or blank."); logger.debug("Generated URI string = " + uriStr); } + + @Test(dependsOnMethods = {"createResourceUriTemplate"}) + public void buildResourceUriWithMissingValue() { + testBanner("buildResourceUriWithMissingValue"); + UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE); + Map resourceUriVars = new HashMap(); + resourceUriVars.put("servicename", EXAMPLE_SERVICE_NAME); + // The required 'identifier' value is missing from the Map from which the URI will be built + logger.debug("This is a negative test, and an error message is expected here:"); + String uriStr = resourceTemplate.buildUri(resourceUriVars); + Assert.assertTrue(Tools.isBlank(uriStr), "Generated URI string was not blank, but should have been."); + logger.debug("Generated URI string = " + uriStr); + } + + @Test(dependsOnMethods = {"createResourceUriTemplate"}) + public void buildResourceUriWithNullValue() { + testBanner("buildResourceUriWithNullValue"); + UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE); + Map resourceUriVars = new HashMap(); + resourceUriVars.put("servicename", EXAMPLE_SERVICE_NAME); + resourceUriVars.put("identifier", null); + logger.debug("This is a negative test, and an error message is expected here:"); + String uriStr = resourceTemplate.buildUri(resourceUriVars); + Assert.assertTrue(Tools.isBlank(uriStr), "Generated URI string was not blank, but should have been."); + logger.debug("Generated URI string = " + uriStr); + } } -- 2.47.3