Modernize collection allocation
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestGetOperationTest.java
index 051ca9e7da976616a10418fda25dd7327a15a565..e595877373eb1da4cee00f996f4e4f2fd94c8594 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.sal.restconf.impl.test;
 
 import static org.junit.Assert.assertEquals;
@@ -14,16 +13,16 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
 import java.net.URI;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -41,13 +40,11 @@ import org.glassfish.jersey.test.JerseyTest;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
-import org.mockito.Mockito;
-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.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
-import org.opendaylight.netconf.sal.rest.impl.RestconfApplication;
 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
@@ -86,15 +83,18 @@ public class RestGetOperationTest extends JerseyTest {
         }
     }
 
-    private static BrokerFacade brokerFacade;
-    private static RestconfImpl restconfImpl;
     private static SchemaContext schemaContextYangsIetf;
     private static SchemaContext schemaContextTestModule;
+    private static SchemaContext schemaContextModules;
+    private static SchemaContext schemaContextBehindMountPoint;
+
     @SuppressWarnings("rawtypes")
     private static NormalizedNode answerFromGet;
 
-    private static SchemaContext schemaContextModules;
-    private static SchemaContext schemaContextBehindMountPoint;
+    private BrokerFacade brokerFacade;
+    private RestconfImpl restconfImpl;
+    private ControllerContext controllerContext;
+    private DOMMountPoint mountInstance;
 
     private static final String RESTCONF_NS = "urn:ietf:params:xml:ns:yang:ietf-restconf";
 
@@ -102,13 +102,10 @@ public class RestGetOperationTest extends JerseyTest {
     public static void init() throws Exception {
         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
-        brokerFacade = mock(BrokerFacade.class);
-        restconfImpl = RestconfImpl.getInstance();
-        restconfImpl.setBroker(brokerFacade);
-        answerFromGet = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
-
         schemaContextModules = TestUtils.loadSchemaContext("/modules");
         schemaContextBehindMountPoint = TestUtils.loadSchemaContext("/modules/modules-behind-mount-point");
+
+        answerFromGet = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
     }
 
     @Override
@@ -118,18 +115,22 @@ public class RestGetOperationTest extends JerseyTest {
         // enable(TestProperties.DUMP_ENTITY);
         // enable(TestProperties.RECORD_LOG_LEVEL);
         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
+
+        mountInstance = mock(DOMMountPoint.class);
+        controllerContext = TestRestconfUtils.newControllerContext(schemaContextYangsIetf, mountInstance);
+        brokerFacade = mock(BrokerFacade.class);
+        restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
+
         ResourceConfig resourceConfig = new ResourceConfig();
         resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(),
-            new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader());
-        resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
-        resourceConfig.registerClasses(new RestconfApplication().getClasses());
+            new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(controllerContext),
+            new JsonNormalizedNodeBodyReader(controllerContext),
+            new RestconfDocumentedExceptionMapper(controllerContext));
         return resourceConfig;
     }
 
-    private static void setControllerContext(final SchemaContext schemaContext) {
-        final ControllerContext controllerContext = ControllerContext.getInstance();
+    private void setControllerContext(final SchemaContext schemaContext) {
         controllerContext.setSchemas(schemaContext);
-        restconfImpl.setControllerContext(controllerContext);
     }
 
     /**
@@ -167,14 +168,8 @@ public class RestGetOperationTest extends JerseyTest {
     @Test
     public void getDataWithUrlMountPoint() throws Exception {
         when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
-                Mockito.anyString())).thenReturn(
-                prepareCnDataForMountPointTest(false));
-        final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
+                isNull())).thenReturn(prepareCnDataForMountPointTest(false));
         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
-        final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
-        when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
-
-        ControllerContext.getInstance().setMountService(mockMountService);
 
         String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1";
         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
@@ -195,13 +190,9 @@ public class RestGetOperationTest extends JerseyTest {
     public void getDataWithSlashesBehindMountPoint() throws Exception {
         final YangInstanceIdentifier awaitedInstanceIdentifier = prepareInstanceIdentifierForList();
         when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), eq(awaitedInstanceIdentifier),
-                Mockito.anyString())).thenReturn(prepareCnDataForSlashesBehindMountPointTest());
-        final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
-        when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
-        final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
-        when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
+                isNull())).thenReturn(prepareCnDataForSlashesBehindMountPointTest());
 
-        ControllerContext.getInstance().setMountService(mockMountService);
+        when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
 
         final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/"
                 + "test-module:cont/lst1/GigabitEthernet0%2F0%2F0%2F0";
@@ -229,14 +220,9 @@ public class RestGetOperationTest extends JerseyTest {
     @Test
     public void getDataMountPointIntoHighestElement() throws Exception {
         when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
-                Mockito.anyString())).thenReturn(
-                prepareCnDataForMountPointTest(true));
-        final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
-        when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
-        final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
-        when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
+                isNull())).thenReturn(prepareCnDataForMountPointTest(true));
 
-        ControllerContext.getInstance().setMountService(mockMountService);
+        when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
 
         final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
@@ -269,9 +255,7 @@ public class RestGetOperationTest extends JerseyTest {
     // /modules
     @Test
     public void getModulesTest() throws Exception {
-        final ControllerContext controllerContext = ControllerContext.getInstance();
-        controllerContext.setGlobalSchema(schemaContextModules);
-        restconfImpl.setControllerContext(controllerContext);
+        setControllerContext(schemaContextModules);
 
         final String uri = "/modules";
 
@@ -379,43 +363,13 @@ public class RestGetOperationTest extends JerseyTest {
         }
     }
 
-    private static Matcher validateOperationsResponseXml(final String searchIn, final String rpcName,
-                                                         final String namespace) {
-        final StringBuilder regex = new StringBuilder();
-
-        regex.append("^");
-
-        regex.append(".*<operations");
-        regex.append(".*xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\"");
-        regex.append(".*>");
-
-        regex.append(".*<");
-        regex.append(".*" + rpcName);
-        regex.append(".*" + namespace);
-        regex.append(".*/");
-        regex.append(".*>");
-
-        regex.append(".*</operations.*");
-        regex.append(".*>");
-
-        regex.append(".*");
-        regex.append("$");
-        final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
-        return ptrn.matcher(searchIn);
-    }
-
     // /operations/pathToMountPoint/yang-ext:mount
     @Ignore
     @Test
     public void getOperationsBehindMountPointTest() throws Exception {
         setControllerContext(schemaContextModules);
 
-        final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
         when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
-        final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
-        when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
-
-        ControllerContext.getInstance().setMountService(mockMountService);
 
         final String uri = "/operations/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
 
@@ -449,12 +403,7 @@ public class RestGetOperationTest extends JerseyTest {
     public void getModulesBehindMountPoint() throws Exception {
         setControllerContext(schemaContextModules);
 
-        final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
         when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
-        final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
-        when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
-
-        ControllerContext.getInstance().setMountService(mockMountService);
 
         final String uri = "/modules/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
 
@@ -482,12 +431,7 @@ public class RestGetOperationTest extends JerseyTest {
     public void getModuleBehindMountPoint() throws Exception {
         setControllerContext(schemaContextModules);
 
-        final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
         when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
-        final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
-        when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
-
-        ControllerContext.getInstance().setMountService(mockMountService);
 
         final String uri = "/modules/module/ietf-interfaces:interfaces/interface/0/yang-ext:mount/"
                 + "module1-behind-mount-point/2014-02-03";
@@ -657,13 +601,13 @@ public class RestGetOperationTest extends JerseyTest {
     }
 
     @SuppressWarnings("unchecked")
-    private static void mockReadOperationalDataMethod() {
+    private void mockReadOperationalDataMethod() {
         when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(answerFromGet);
     }
 
     @SuppressWarnings("unchecked")
-    private static void mockReadConfigurationDataMethod() {
-        when(brokerFacade.readConfigurationData(any(YangInstanceIdentifier.class), Mockito.anyString()))
+    private void mockReadConfigurationDataMethod() {
+        when(brokerFacade.readConfigurationData(any(YangInstanceIdentifier.class), isNull()))
                 .thenReturn(answerFromGet);
     }
 
@@ -735,7 +679,7 @@ public class RestGetOperationTest extends JerseyTest {
     }
 
     @SuppressWarnings({"rawtypes", "unchecked"})
-    private static void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) {
+    private void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) {
         try {
             final QName qNameDepth1Cont = QName.create("urn:nested:module", "2014-06-3", "depth1-cont");
             final YangInstanceIdentifier ii = YangInstanceIdentifier.builder().node(qNameDepth1Cont).build();
@@ -770,7 +714,7 @@ public class RestGetOperationTest extends JerseyTest {
             return;
         }
 
-        final Map<String, NodeData> expChildMap = Maps.newHashMap();
+        final Map<String, NodeData> expChildMap = new HashMap<>();
         for (final NodeData expChild : (List<NodeData>) nodeData.data) {
             expChildMap.put(expChild.key.toString(), expChild);
         }