]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5271: Added negative tests; began logging non-fatal exceptions.
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 7 Jun 2012 22:36:59 +0000 (15:36 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 7 Jun 2012 22:36:59 +0000 (15:36 -0700)
services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java
services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java

index 261489d4095c81641a6178b207afe067e92f9400..bb8cdf7460e9704981ca73ac1c5a308240218ba8 100644 (file)
@@ -27,11 +27,15 @@ import java.util.Map;
 import javax.ws.rs.core.UriBuilder;\r
 import javax.ws.rs.core.UriBuilderException;\r
 import org.collectionspace.services.common.api.Tools;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
 \r
 public class UriTemplate {\r
 \r
-    UriBuilder builder;\r
-    String uriPath;\r
+    private static final Logger logger = LoggerFactory.getLogger(UriTemplate.class);\r
+    private UriBuilder builder;\r
+    private String uriPath;\r
+    private final static String EMPTY_STRING = "";\r
 \r
     public UriTemplate(String path) {\r
         setUriPath(path);\r
@@ -43,7 +47,7 @@ public class UriTemplate {
             this.uriPath = path;\r
         }\r
     }\r
-    \r
+\r
     private String getUriPath() {\r
         return uriPath;\r
     }\r
@@ -53,9 +57,9 @@ public class UriTemplate {
             try {\r
                 builder = UriBuilder.fromPath(getUriPath());\r
             } catch (IllegalArgumentException iae) {\r
-                // FIXME: Need to add logger and log error\r
-                // URIBuilder can't be created if path is null\r
-                // No other checking of path format is performed\r
+                logger.warn("URI path was null when attempting to creating new UriTemplate.");\r
+                // No other checking of path format, other than for null values,\r
+                // is performed automatically by this Exception handling.\r
             }\r
         }\r
     }\r
@@ -66,7 +70,7 @@ public class UriTemplate {
         }\r
         return builder;\r
     }\r
-    \r
+\r
     @Override\r
     public String toString() {\r
         return getUriPath();\r
@@ -77,18 +81,16 @@ public class UriTemplate {
         try {\r
             uri = getBuilder().buildFromMap(varsMap);\r
         } catch (IllegalArgumentException iae) {\r
-            // FIXME: Need to add logger and log error\r
-            // One or more parameters are missing or null.\r
+            logger.warn("One or more required parameter values were missing "\r
+                    + "when building URI value via URI Template: " + iae.getMessage());\r
         } catch (UriBuilderException ube) {\r
-            // FIXME: Need to add logger and log error\r
-            // URI can't be constructed based on current state of the builder\r
+            logger.warn("URI value can't be constructed due to state of URIBuilder: " + ube.getMessage());\r
         } finally {\r
             if (uri != null) {\r
                 return uri.toString();\r
             } else {\r
-                return "";\r
+                return EMPTY_STRING;\r
             }\r
         }\r
     }\r
-    \r
 }
\ No newline at end of file
index b897c322e2aa881e1382d1a7151f5e4b30d01a53..bdc2e4020fae516535f2a0ebc196c417355083ab 100644 (file)
@@ -32,10 +32,9 @@ import org.testng.Assert;
 import org.testng.annotations.Test;\r
 \r
 public class UriTemplateTest {\r
-    \r
+\r
     final static String EXAMPLE_SERVICE_NAME = "examples";\r
     final static String CSID = "a87f6616-4146-4c17-a41a-048597cc12aa";\r
-\r
     private static final Logger logger = LoggerFactory.getLogger(UriTemplateTest.class);\r
 \r
     private void testBanner(String msg) {\r
@@ -47,22 +46,48 @@ public class UriTemplateTest {
     public void createResourceUriTemplate() {\r
         testBanner("createResourceUriTemplate");\r
         UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r
-        Assert.assertNotNull(resourceTemplate, "Resource template is null.");\r
+        Assert.assertNotNull(resourceTemplate, "Resource template is null; it was not created successfully.");\r
         logger.debug("Resource template URI path = " + resourceTemplate.toString());\r
-        Assert.assertNotNull(resourceTemplate.toString(), "Resource template URI path is null.");\r
+        Assert.assertNotNull(resourceTemplate.toString(), "Resource template URI path is null; it was not set successfully.");\r
         Assert.assertEquals(resourceTemplate.toString(), UriTemplateFactory.RESOURCE_TEMPLATE_PATTERN,\r
                 "Resource template URI path doesn't match expected path.");\r
     }\r
-    \r
-    @Test (dependsOnMethods = {"createResourceUriTemplate"})\r
+\r
+    @Test(dependsOnMethods = {"createResourceUriTemplate"})\r
     public void buildResourceUri() {\r
         testBanner("buildResourceUri");\r
         UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r
-        Map<String,String> resourceUriVars = new HashMap<String,String>();\r
+        Map<String, String> resourceUriVars = new HashMap<String, String>();\r
         resourceUriVars.put("servicename", EXAMPLE_SERVICE_NAME);\r
         resourceUriVars.put("identifier", CSID);\r
         String uriStr = resourceTemplate.buildUri(resourceUriVars);\r
         Assert.assertFalse(Tools.isBlank(uriStr), "Generated URI string is null or blank.");\r
         logger.debug("Generated URI string = " + uriStr);\r
     }\r
+\r
+    @Test(dependsOnMethods = {"createResourceUriTemplate"})\r
+    public void buildResourceUriWithMissingValue() {\r
+        testBanner("buildResourceUriWithMissingValue");\r
+        UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r
+        Map<String, String> resourceUriVars = new HashMap<String, String>();\r
+        resourceUriVars.put("servicename", EXAMPLE_SERVICE_NAME);\r
+        // The required 'identifier' value is missing from the Map from which the URI will be built\r
+        logger.debug("This is a negative test, and an error message is expected here:");\r
+        String uriStr = resourceTemplate.buildUri(resourceUriVars);\r
+        Assert.assertTrue(Tools.isBlank(uriStr), "Generated URI string was not blank, but should have been.");\r
+        logger.debug("Generated URI string = " + uriStr);\r
+    }\r
+\r
+    @Test(dependsOnMethods = {"createResourceUriTemplate"})\r
+    public void buildResourceUriWithNullValue() {\r
+        testBanner("buildResourceUriWithNullValue");\r
+        UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r
+        Map<String, String> resourceUriVars = new HashMap<String, String>();\r
+        resourceUriVars.put("servicename", EXAMPLE_SERVICE_NAME);\r
+        resourceUriVars.put("identifier", null);\r
+        logger.debug("This is a negative test, and an error message is expected here:");\r
+        String uriStr = resourceTemplate.buildUri(resourceUriVars);\r
+        Assert.assertTrue(Tools.isBlank(uriStr), "Generated URI string was not blank, but should have been.");\r
+        logger.debug("Generated URI string = " + uriStr);\r
+    }\r
 }\r