BUG 274 REST response instead of NumberFormatException 58/6358/2
authorJozef Gloncak <jgloncak@cisco.com>
Thu, 24 Apr 2014 13:57:14 +0000 (15:57 +0200)
committerJozef Gloncak <jgloncak@cisco.com>
Wed, 30 Apr 2014 06:28:44 +0000 (06:28 +0000)
Raised NumberFormatException is now catched and thrown next as
WebApplicationException which is displayed as return message for REST
call.

Change-Id: I1b4ee74c6a953fe3b64ef47c07477c29d892a87c
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CodecsExceptionsCatchingTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/decoding-exception/yang/number.yang [new file with mode: 0644]

index a0bd99c..f1901d7 100644 (file)
@@ -588,7 +588,11 @@ class RestconfImpl implements RestconfService {
         }
         if (node instanceof CompositeNodeWrapper) {
             if ((node  as CompositeNodeWrapper).changeAllowed) {
-                normalizeNode(node as CompositeNodeWrapper, schema, null, mountPoint)
+                try {
+                    normalizeNode(node as CompositeNodeWrapper, schema, null, mountPoint)
+                } catch (NumberFormatException e) {
+                    throw new ResponseException(BAD_REQUEST,e.message)
+                }
             }
             return (node as CompositeNodeWrapper).unwrap()
         }
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CodecsExceptionsCatchingTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CodecsExceptionsCatchingTest.java
new file mode 100644 (file)
index 0000000..767aaf3
--- /dev/null
@@ -0,0 +1,59 @@
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.FileNotFoundException;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
+import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
+import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
+import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
+import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
+import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class CodecsExceptionsCatchingTest extends JerseyTest {
+
+    private static RestconfImpl restConf;
+    private static ControllerContext controllerContext = ControllerContext.getInstance();
+
+    @BeforeClass
+    public static void init() throws FileNotFoundException {
+        restConf = RestconfImpl.getInstance();
+        controllerContext = ControllerContext.getInstance();
+        SchemaContext schemaContext = TestUtils.loadSchemaContext("/decoding-exception/yang");
+        controllerContext.setGlobalSchema(schemaContext);
+        restConf.setControllerContext(controllerContext);
+    }
+
+    @Override
+    protected Application configure() {
+        /* enable/disable Jersey logs to console */
+        // enable(TestProperties.LOG_TRAFFIC);
+        // enable(TestProperties.DUMP_ENTITY);
+        // enable(TestProperties.RECORD_LOG_LEVEL);
+        // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
+        ResourceConfig resourceConfig = new ResourceConfig();
+        resourceConfig = resourceConfig.registerInstances(restConf, StructuredDataToXmlProvider.INSTANCE,
+                StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
+                JsonToCompositeNodeProvider.INSTANCE);
+        return resourceConfig;
+    }
+
+    @Test
+    public void StringToNumberConversionError() {
+        Response response = target("/config/number:cont").request(MediaType.APPLICATION_XML).put(
+                Entity.entity("<cont xmlns=\"number\"><lf>3f</lf></cont>", MediaType.APPLICATION_XML));
+        String exceptionMessage = response.readEntity(String.class);
+        assertTrue(exceptionMessage.contains("Incorrect lexical representation of Integer value: 3f"));
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/decoding-exception/yang/number.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/decoding-exception/yang/number.yang
new file mode 100644 (file)
index 0000000..c463882
--- /dev/null
@@ -0,0 +1,17 @@
+ module number {
+
+   namespace "number";
+   prefix "number";
+
+   revision 2014-04-24 {
+   }
+
+    
+
+    container cont {
+        leaf lf {
+            type uint8;
+        }
+        
+    }    
+ }