From: Ed Warnicke Date: Thu, 1 May 2014 15:17:55 +0000 (+0000) Subject: Merge "BUG 274 REST response instead of NumberFormatException" X-Git-Tag: autorelease-tag-v20140601202136_82eb3f9~147 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=6b1c7f6de0e8dc1f7d3b005d5a33ae6df6adb5da;hp=027bc8f87341f432654c3aaa7771658c25d2ca7d Merge "BUG 274 REST response instead of NumberFormatException" --- diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend index a0bd99c5d2..f1901d7112 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend @@ -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 index 0000000000..767aaf36c1 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CodecsExceptionsCatchingTest.java @@ -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("3f", 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 index 0000000000..c4638827ef --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/decoding-exception/yang/number.yang @@ -0,0 +1,17 @@ + module number { + + namespace "number"; + prefix "number"; + + revision 2014-04-24 { + } + + + + container cont { + leaf lf { + type uint8; + } + + } + }