From 01f3efa15c9da190e283c9ddd3ecda3fb6fcb56c Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Tue, 8 Jul 2014 09:43:24 +0200 Subject: [PATCH] Bug 1328: Improved argument checks in generated RPC Router Change-Id: I5e4bd55a4a31b4b671b44163286c6b3ebe7707bf Signed-off-by: Tony Tkacik --- .../codegen/impl/RuntimeCodeGenerator.xtend | 6 +++++ .../test/RuntimeCodeGeneratorTest.java | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/RuntimeCodeGenerator.xtend b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/RuntimeCodeGenerator.xtend index 00c9f1eb91..834eb4f5fb 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/RuntimeCodeGenerator.xtend +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/RuntimeCodeGenerator.xtend @@ -82,6 +82,12 @@ class RuntimeCodeGenerator extends AbstractRuntimeCodeGenerator { val rpcMeta = metadata.getRpcMethod(name); val bodyTmp = ''' { + if($1 == null) { + throw new IllegalArgumentException("RPC input must not be null and must contain a value for field «rpcMeta.inputRouteGetter.name»"); + } + if($1.«rpcMeta.inputRouteGetter.name»() == null) { + throw new IllegalArgumentException("Field «rpcMeta.inputRouteGetter.name» must not be null"); + } final «InstanceIdentifier.name» identifier = $1.«rpcMeta.inputRouteGetter.name»()«IF rpcMeta. routeEncapsulated».getValue()«ENDIF»; «supertype.name» instance = («supertype.name») «rpcMeta.context.routingTableField».get(identifier); diff --git a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/RuntimeCodeGeneratorTest.java b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/RuntimeCodeGeneratorTest.java index e6cd1aa1ad..9ba6533971 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/RuntimeCodeGeneratorTest.java +++ b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/RuntimeCodeGeneratorTest.java @@ -10,6 +10,7 @@ package org.opendaylight.controller.sal.binding.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -154,6 +155,31 @@ public class RuntimeCodeGeneratorTest { // We should have call to instance 1 verify(service[1]).simple(instance_1_input[0]); + + /* + * Generated RPC service should throw illegalArgumentException + * with message if rpc input is null. + */ + try { + product.getInvocationProxy().simple(null); + fail("Generated RPC router should throw IllegalArgumentException on null input"); + } catch (IllegalArgumentException e){ + assertNotNull(e.getMessage()); + } + + + /* + * Generated RPC service should throw illegalArgumentException + * with message if rpc route is null. + */ + try { + SimpleInput withoutValue = new SimpleInputImpl(null); + product.getInvocationProxy().simple(withoutValue); + fail("Generated RPC router should throw IllegalArgumentException on null value for route"); + } catch (IllegalArgumentException e){ + assertNotNull(e.getMessage()); + } + } private InstanceIdentifier[][] identifiers(final int serviceSize, final int instancesPerService) { -- 2.36.6