Changed mount point URI decoding in restconf
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / ControllerContextTest.java
index c68fcb90714567849192db25d9dc676e43f700a9..4c5922d73fa962b3aaea2d547846c8905e66bb8a 100644 (file)
@@ -2,21 +2,19 @@ package org.opendaylight.controller.sal.restconf.impl.test;
 
 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.After;
 import org.junit.BeforeClass;
+import org.junit.Rule;
 import org.junit.Test;
-import org.opendaylight.controller.sal.core.api.mount.MountInstance;
-import org.opendaylight.controller.sal.core.api.mount.MountService;
+import org.junit.rules.ExpectedException;
 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
 import org.opendaylight.controller.sal.restconf.impl.InstanceIdWithSchemaNode;
+import org.opendaylight.controller.sal.restconf.impl.ResponseException;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -25,6 +23,9 @@ public class ControllerContextTest {
 
     private static final ControllerContext controllerContext = ControllerContext.getInstance();
 
+    @Rule
+    public ExpectedException exception = ExpectedException.none();
+
     @BeforeClass
     public static void init() throws FileNotFoundException {
         Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
@@ -33,6 +34,11 @@ public class ControllerContextTest {
         controllerContext.setSchemas(schemaContext);
     }
 
+    @After
+    public void releaseMountService() {
+        controllerContext.setMountService(null);
+    }
+
     @Test
     public void testToInstanceIdentifierList() throws FileNotFoundException {
         InstanceIdWithSchemaNode instanceIdentifier = controllerContext
@@ -50,48 +56,20 @@ public class ControllerContextTest {
 
         instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:users/user/foo");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
-
-        instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
-        assertNull(instanceIdentifier);
-
-        instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
-        assertNull(instanceIdentifier);
-
     }
 
     @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);
-        }
+    public void testToInstanceIdentifierListWithNullKey() {
+        exception.expect(ResponseException.class);
+        exception.expectMessage("HTTP 400 Bad Request");
+        controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
+    }
 
+    @Test
+    public void testToInstanceIdentifierListWithMissingKey() {
+        exception.expect(ResponseException.class);
+        exception.expectMessage("HTTP 400 Bad Request");
+        controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
     }
 
     @Test
@@ -104,18 +82,30 @@ public class ControllerContextTest {
 
     @Test
     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
-        InstanceIdWithSchemaNode instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:food/beer");
+        InstanceIdWithSchemaNode instanceIdentifier = controllerContext
+                .toInstanceIdentifier("simple-nodes:food/nonalcoholic/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);
+    @Test
+    public void testToInstanceIdentifierChoiceException() {
+        exception.expect(ResponseException.class);
+        exception.expectMessage("HTTP 400 Bad Request");
+        controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
+    }
 
-        instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
-        assertNull(instanceIdentifier);
+    @Test
+    public void testToInstanceIdentifierCaseException() {
+        exception.expect(ResponseException.class);
+        exception.expectMessage("HTTP 400 Bad Request");
+        controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
+    }
 
+    @Test
+    public void testToInstanceIdentifierChoiceCaseException() {
+        exception.expect(ResponseException.class);
+        exception.expectMessage("HTTP 400 Bad Request");
+        controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
     }
 
 }