Merge "Bug 1328: Improved argument checks in generated RPC Router"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / sal / binding / test / RuntimeCodeGeneratorTest.java
index 4afbc298426e943810f8149a0d17a20ae9e93210..9ba65339710b50c5e3a1e453fa5b5ff3805484f3 100644 (file)
@@ -10,11 +10,11 @@ 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;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 
 import javassist.ClassPool;
@@ -40,8 +40,6 @@ import org.opendaylight.yangtools.yang.binding.BaseIdentity;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
 
 public class RuntimeCodeGeneratorTest {
 
@@ -115,7 +113,7 @@ public class RuntimeCodeGeneratorTest {
 
     }
 
-    private void verifyRouting(RpcRouter<FooService> product) {
+    private void verifyRouting(final RpcRouter<FooService> product) {
         assertNotNull("Routing table should be initialized", product.getRoutingTable(BaseIdentity.class));
 
         RpcRoutingTable<BaseIdentity, FooService> routingTable = product.getRoutingTable(BaseIdentity.class);
@@ -157,9 +155,34 @@ 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(int serviceSize, int instancesPerService) {
+    private InstanceIdentifier<?>[][] identifiers(final int serviceSize, final int instancesPerService) {
         InstanceIdentifier<?>[][] ret = new InstanceIdentifier[serviceSize][];
         int service = 0;
         for (int i = 0; i < serviceSize; i++) {
@@ -175,23 +198,19 @@ public class RuntimeCodeGeneratorTest {
         return ret;
     }
 
-    private InstanceIdentifier<?> referencableIdentifier(int i) {
-        ReferencableObjectKey key = new ReferencableObjectKey(i);
-        IdentifiableItem<ReferencableObject, ReferencableObjectKey> pathArg = new IdentifiableItem<>(
-                ReferencableObject.class, key);
-        return new InstanceIdentifier<ReferencableObject>(Arrays.<PathArgument> asList(pathArg),
-                ReferencableObject.class);
+    private InstanceIdentifier<?> referencableIdentifier(final int i) {
+        return InstanceIdentifier.builder(ReferencableObject.class, new ReferencableObjectKey(i)).build();
     }
 
     private static class SimpleInputImpl implements SimpleInput {
         private final InstanceIdentifier<?> identifier;
 
-        public SimpleInputImpl(InstanceIdentifier<?> _identifier) {
+        public SimpleInputImpl(final InstanceIdentifier<?> _identifier) {
             this.identifier = _identifier;
         }
 
         @Override
-        public <E extends Augmentation<SimpleInput>> E getAugmentation(Class<E> augmentationType) {
+        public <E extends Augmentation<SimpleInput>> E getAugmentation(final Class<E> augmentationType) {
             return null;
         }
 
@@ -230,7 +249,7 @@ public class RuntimeCodeGeneratorTest {
         List<FooUpdate> receivedFoos = new ArrayList<>();
 
         @Override
-        public void onFooUpdate(FooUpdate notification) {
+        public void onFooUpdate(final FooUpdate notification) {
             receivedFoos.add(notification);
         }
 
@@ -242,12 +261,12 @@ public class RuntimeCodeGeneratorTest {
         List<FlowDelete> receivedDeletes = new ArrayList<>();
 
         @Override
-        public void onBarUpdate(BarUpdate notification) {
+        public void onBarUpdate(final BarUpdate notification) {
             receivedBars.add(notification);
         }
 
         @Override
-        public void onFlowDelete(FlowDelete notification) {
+        public void onFlowDelete(final FlowDelete notification) {
             receivedDeletes.add(notification);
         }