Merge "ArpHandler to ignore ip packet sent to default GW"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / ControllerContextTest.java
index 39c0d3b34f67159e7de8a1c1fc4a73809ae648b8..c68fcb90714567849192db25d9dc676e43f700a9 100644 (file)
@@ -1,12 +1,20 @@
 package org.opendaylight.controller.sal.restconf.impl.test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 import java.io.FileNotFoundException;
 import java.util.Set;
 
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.opendaylight.controller.sal.core.api.mount.MountInstance;
+import org.opendaylight.controller.sal.core.api.mount.MountService;
 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
 import org.opendaylight.controller.sal.restconf.impl.InstanceIdWithSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
@@ -19,14 +27,16 @@ public class ControllerContextTest {
 
     @BeforeClass
     public static void init() throws FileNotFoundException {
-        Set<Module> allModules = TestUtils.loadModules(ControllerContextTest.class.getResource("/full-versions/yangs").getPath());
+        Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
+        assertNotNull(allModules);
         SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
         controllerContext.setSchemas(schemaContext);
     }
 
     @Test
     public void testToInstanceIdentifierList() throws FileNotFoundException {
-        InstanceIdWithSchemaNode instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
+        InstanceIdWithSchemaNode instanceIdentifier = controllerContext
+                .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
 
         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo/full-name");
@@ -49,28 +59,63 @@ public class ControllerContextTest {
 
     }
 
+    @Test
+    public void testToInstanceIdentifierMountPoint() throws FileNotFoundException {
+        try {
+            String mountPointPath = "simple-nodes:user/foo/boo";
+            String nestedPath = "simple-nodes:user/foo/boo/simple-nodes:users";
+            InstanceIdWithSchemaNode mountInstanceIdentifier = controllerContext.toInstanceIdentifier(mountPointPath);
+            assertEquals("user", mountInstanceIdentifier.getSchemaNode().getQName().getLocalName());
+
+            MountInstance mountInstance = mock(MountInstance.class);
+            MountService mountService = mock(MountService.class);
+
+            controllerContext.setMountService(mountService);
+            // when(mountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(null);
+
+            when(mountService.getMountPoint(eq(mountInstanceIdentifier.getInstanceIdentifier()))).thenReturn(
+                    mountInstance);
+
+            when(mountInstance.getSchemaContext()).thenReturn(controllerContext.getGlobalSchema());
+
+            InstanceIdWithSchemaNode mountedInstanceIdentifier = controllerContext.toInstanceIdentifier(nestedPath);
+            assertEquals("users", mountedInstanceIdentifier.getSchemaNode().getQName().getLocalName());
+
+            mountedInstanceIdentifier = controllerContext.toInstanceIdentifier(mountPointPath + "/" + mountPointPath);
+            assertEquals("user", mountedInstanceIdentifier.getSchemaNode().getQName().getLocalName());
+
+            mountedInstanceIdentifier = controllerContext
+                    .toInstanceIdentifier("simple-nodes:user/foo/var/simple-nodes:users");
+            assertNull(mountedInstanceIdentifier);
+
+        } finally {
+            controllerContext.setMountService(null);
+        }
+
+    }
+
     @Test
     public void testToInstanceIdentifierContainer() throws FileNotFoundException {
         InstanceIdWithSchemaNode instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
         assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
-        assertEquals(2, ((ContainerSchemaNode)instanceIdentifier.getSchemaNode()).getChildNodes().size());
+        assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
     }
 
     @Test
     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
         InstanceIdWithSchemaNode instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:food/beer");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "beer");
-        
+
         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
         assertNull(instanceIdentifier);
-        
+
         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
         assertNull(instanceIdentifier);
-        
+
         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
         assertNull(instanceIdentifier);
-        
+
     }
 
 }