Bug fix and tests for handling a 2nd RPC in a YANG file

Change-Id: Ieb7faf18e140098880e732f825df3f17a96c1adb
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultDataTreeBuilder.java b/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultDataTreeBuilder.java
index 829ddb8..59aac5a 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultDataTreeBuilder.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultDataTreeBuilder.java
@@ -223,6 +223,11 @@
             while (child != null && !(child instanceof YangRpc)) {
                 child = child.getNextSibling();
             }
+            while (child != null && child instanceof YangRpc &&
+                    !name.contains(child.getJavaPackage() + "."
+                    + child.getJavaClassNameOrBuiltInType().toLowerCase())) {
+                child = child.getNextSibling();
+            }
             if (child != null) {
                 //Rpc should be part of resource identifier for input/output
                 // nodes.
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/MoIdToRscIdTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/MoIdToRscIdTest.java
index 6bc150e..acba744 100644
--- a/runtime/src/test/java/org/onosproject/yang/runtime/impl/MoIdToRscIdTest.java
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/MoIdToRscIdTest.java
@@ -782,7 +782,7 @@
         YangRpc rpc = new YangRpc() {
             @Override
             public String getJavaPackage() {
-                return null;
+                return "org.onosproject.yang.runtime.mockclass.testmodule";
             }
 
             @Override
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/RpcContextTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/RpcContextTest.java
index 573c04c..225c883 100644
--- a/runtime/src/test/java/org/onosproject/yang/runtime/impl/RpcContextTest.java
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/RpcContextTest.java
@@ -15,12 +15,22 @@
  */
 package org.onosproject.yang.runtime.impl;
 
+import org.junit.Before;
 import org.junit.Test;
-import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.gen.v1.hello.rev20150105.hello.hellosecond.DefaultHelloSecondInput;
+import org.onosproject.yang.gen.v1.hello.rev20150105.hello.hellosecond.HelloSecondInput;
+import org.onosproject.yang.gen.v1.hello.rev20150105.hello.helloworld.DefaultHelloWorldInput;
+import org.onosproject.yang.gen.v1.hello.rev20150105.hello.helloworld.HelloWorldInput;
+import org.onosproject.yang.model.DefaultModelObjectData;
+import org.onosproject.yang.model.ModelConverter;
+import org.onosproject.yang.model.ModelObject;
+import org.onosproject.yang.model.ModelObjectData;
+import org.onosproject.yang.model.ResourceData;
 import org.onosproject.yang.model.ResourceId;
 import org.onosproject.yang.model.RpcContext;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.onosproject.yang.runtime.SerializerHelper.addToResourceId;
 import static org.onosproject.yang.runtime.SerializerHelper.initializeResourceId;
 import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.processSchemaRegistry;
@@ -35,12 +45,12 @@
     public static final String NS = "urn:params:xml:ns:yang:hello";
     String value = null;
     TestYangSerializerContext context = new TestYangSerializerContext();
-    DataNode.Builder dBlr;
 
     /**
      * Sets up all prerequisite.
      */
-    private void setUp() {
+    @Before
+    public void setUp() {
         processSchemaRegistry();
         reg = registry();
         builder = new ModIdToRscIdConverter(reg);
@@ -51,7 +61,6 @@
      */
     @Test
     public void checkRpcContext() {
-        setUp();
         ResourceId.Builder rIdBlr = initializeResourceId(context);
         rIdBlr = addToResourceId(rIdBlr, "hello-world", NS, value);
 
@@ -63,4 +72,30 @@
         assertEquals(context.serviceIntf().toString(), "interface org.onosproject" +
                 ".yang.gen.v1.hello.rev20150105.HelloService");
     }
+
+    /*
+     * Test the file rpc_test.yang.
+     * This specifically tests that when 2 RPC's are specified in a single
+     * YANG file that both RPC inputs can be handled through the ModelConverter
+     */
+    @Test
+    public void checkRpcConverter() {
+        ModelConverter mc = new DefaultModelConverter(reg);
+        HelloWorldInput hwInput = new DefaultHelloWorldInput();
+        hwInput.x("input-sample");
+        ModelObjectData hwInputMod = DefaultModelObjectData.builder().
+                            addModelObject((ModelObject) hwInput).build();
+        assertNotNull(hwInputMod);
+        ResourceData rd = mc.createDataNode(hwInputMod);
+        assertNotNull(rd);
+
+        //Now test converting to a second RPC in the same YANG
+        HelloSecondInput hsInput = new DefaultHelloSecondInput();
+        hsInput.x("second-input");
+        ModelObjectData hsInputMod = DefaultModelObjectData.builder().
+                addModelObject((ModelObject) hsInput).build();
+        assertNotNull(hsInputMod);
+        ResourceData rdSecond = mc.createDataNode(hsInputMod);
+        assertNotNull(rdSecond);
+    }
 }
diff --git a/runtime/src/test/resources/schemaProviderTestYangFiles/rpc_test.yang b/runtime/src/test/resources/schemaProviderTestYangFiles/rpc_test.yang
index c1968a3..fdd4a43 100644
--- a/runtime/src/test/resources/schemaProviderTestYangFiles/rpc_test.yang
+++ b/runtime/src/test/resources/schemaProviderTestYangFiles/rpc_test.yang
@@ -20,4 +20,18 @@
             }
         }
     }
+
+    rpc hello-second {
+        input {
+            leaf x {
+                type string;
+            }
+        }
+
+        output {
+            leaf greeting {
+                type string;
+            }
+        }
+    }
 }
\ No newline at end of file