Merge "BUG 274 REST response instead of NumberFormatException"
authorEd Warnicke <eaw@cisco.com>
Thu, 1 May 2014 15:17:55 +0000 (15:17 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 1 May 2014 15:17:55 +0000 (15:17 +0000)
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;
+        }
+        
+    }    
+ }