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
this.uriPath = path;\r
}\r
}\r
- \r
+\r
private String getUriPath() {\r
return uriPath;\r
}\r
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
}\r
return builder;\r
}\r
- \r
+\r
@Override\r
public String toString() {\r
return getUriPath();\r
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
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
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