Use ControllerContext non-statically
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / URITest.java
index e3999f63d79c231f6d2e3c993165124ef232390e..164a241565bf5c78144c863c4d219e3b0bfb658f 100644 (file)
@@ -8,64 +8,60 @@
 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.assertTrue;
-import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 import java.io.FileNotFoundException;
-import java.util.Set;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 
 public class URITest {
 
-    private static final ControllerContext CONTROLLER_CONTEXT = ControllerContext.getInstance();
+    private static SchemaContext schemaContext;
+    private static SchemaContext mountSchemaContext;
+
+    private final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
+    private final ControllerContext controllerContext =
+            TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
 
     @Rule
     public ExpectedException exception = ExpectedException.none();
 
     @BeforeClass
     public static void init() throws FileNotFoundException, ReactorException {
-        final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
-        final Set<Module> allModules = schemaContext.getModules();
-        assertNotNull(allModules);
-
-        CONTROLLER_CONTEXT.setSchemas(schemaContext);
+        schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
+        mountSchemaContext = TestUtils.loadSchemaContext("/test-config-data/yang2");
     }
 
     @Test
     public void testToInstanceIdentifierList() throws FileNotFoundException {
-        InstanceIdentifierContext<?> instanceIdentifier = CONTROLLER_CONTEXT
+        InstanceIdentifierContext<?> instanceIdentifier = controllerContext
                 .toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
 
-        instanceIdentifier = CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
+        instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass");
 
-        instanceIdentifier = CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user/foo/boo");
+        instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
 
-        instanceIdentifier = CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user//boo");
+        instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user");
 
     }
@@ -73,19 +69,19 @@ public class URITest {
     @Test
     public void testToInstanceIdentifierListWithNullKey() {
         this.exception.expect(RestconfDocumentedException.class);
-        CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user/null/boo");
+        controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
     }
 
     @Test
     public void testToInstanceIdentifierListWithMissingKey() {
         this.exception.expect(RestconfDocumentedException.class);
-        CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user/foo");
+        controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
     }
 
     @Test
     public void testToInstanceIdentifierContainer() throws FileNotFoundException {
         final InstanceIdentifierContext<?> instanceIdentifier =
-                CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:users");
+                controllerContext.toInstanceIdentifier("simple-nodes:users");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users");
         assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode);
         assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size());
@@ -95,7 +91,7 @@ public class URITest {
     @Ignore //jenkins has problem with JerseyTest
     // - we expecting problems with singletons ControllerContext as schemaContext holder
     public void testToInstanceIdentifierChoice() throws FileNotFoundException {
-        final InstanceIdentifierContext<?> instanceIdentifier = CONTROLLER_CONTEXT
+        final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
                 .toInstanceIdentifier("simple-nodes:food/nonalcoholic");
         assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic");
     }
@@ -103,31 +99,31 @@ public class URITest {
     @Test
     public void testToInstanceIdentifierChoiceException() {
         this.exception.expect(RestconfDocumentedException.class);
-        CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:food/snack");
+        controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
     }
 
     @Test
     public void testToInstanceIdentifierCaseException() {
         this.exception.expect(RestconfDocumentedException.class);
-        CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:food/sports-arena");
+        controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
     }
 
     @Test
     public void testToInstanceIdentifierChoiceCaseException() {
         this.exception.expect(RestconfDocumentedException.class);
-        CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
+        controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
     }
 
     @Test
     public void testToInstanceIdentifierWithoutNode() {
         this.exception.expect(RestconfDocumentedException.class);
-        CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes");
+        controllerContext.toInstanceIdentifier("simple-nodes");
     }
 
     @Test
     public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException {
         initMountService(true);
-        final InstanceIdentifierContext<?> instanceIdentifier = CONTROLLER_CONTEXT
+        final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
         assertEquals(
                 "[(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)class, "
@@ -140,43 +136,29 @@ public class URITest {
     @Test
     public void testMountPointWithoutExternModul() throws FileNotFoundException, ReactorException {
         initMountService(true);
-        final InstanceIdentifierContext<?> instanceIdentifier = CONTROLLER_CONTEXT
+        final InstanceIdentifierContext<?> instanceIdentifier = controllerContext
                 .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/");
         assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments()));
     }
 
-    @Test
-    public void testMountPointWithoutMountService() throws FileNotFoundException {
-        this.exception.expect(RestconfDocumentedException.class);
-
-        CONTROLLER_CONTEXT.setMountService(null);
-        CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name");
-    }
-
     @Test
     public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
         initMountService(false);
         this.exception.expect(RestconfDocumentedException.class);
 
-        CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
+        controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
     }
 
     public void initMountService(final boolean withSchema) throws FileNotFoundException, ReactorException {
-        final DOMMountPointService mountService = mock(DOMMountPointService.class);
-        CONTROLLER_CONTEXT.setMountService(mountService);
         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
         final RestconfImpl restconfImpl = RestconfImpl.getInstance();
         restconfImpl.setBroker(brokerFacade);
-        restconfImpl.setControllerContext(CONTROLLER_CONTEXT);
-        final SchemaContext schemaContext2 = TestUtils.loadSchemaContext("/test-config-data/yang2");
-        final Set<Module> modules2 = schemaContext2.getModules();
+        restconfImpl.setControllerContext(controllerContext);
 
-        final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
         if (withSchema) {
-            when(mountInstance.getSchemaContext()).thenReturn(schemaContext2);
+            when(mountInstance.getSchemaContext()).thenReturn(mountSchemaContext);
         } else {
             when(mountInstance.getSchemaContext()).thenReturn(null);
         }
-        when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
     }
 }