Modernize collection allocation
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestGetOperationTest.java
index 20b19c8b38b7be4777b9588e20d93779e0566159..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,17 +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.Date;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -42,21 +40,19 @@ 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;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
-import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
+import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -87,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";
 
@@ -103,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
@@ -119,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);
     }
 
     /**
@@ -168,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));
@@ -196,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";
@@ -224,22 +214,15 @@ public class RestGetOperationTest extends JerseyTest {
     }
 
     private static QName newTestModuleQName(final String localPart) throws Exception {
-        final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-09");
-        final URI uri = URI.create("test:module");
-        return QName.create(uri, revision, localPart);
+        return QName.create(URI.create("test:module"), Revision.of("2014-01-09"), localPart);
     }
 
     @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));
@@ -272,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";
 
@@ -325,7 +306,7 @@ public class RestGetOperationTest extends JerseyTest {
 
         assertEquals("module2", qname.getLocalName());
         assertEquals("module:2", qname.getNamespace().toString());
-        assertEquals("2014-01-02", qname.getFormattedRevision());
+        assertEquals("2014-01-02", qname.getRevision().get().toString());
 
         response = target(uri).request("application/yang.api+json").get();
         assertEquals(200, response.getStatus());
@@ -382,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/";
 
@@ -452,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/";
 
@@ -485,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";
@@ -513,7 +454,7 @@ public class RestGetOperationTest extends JerseyTest {
         final QName module = assertedModuleXmlToModuleQName(responseXml.getDocumentElement());
 
         assertEquals("module1-behind-mount-point", module.getLocalName());
-        assertEquals("2014-02-03", module.getFormattedRevision());
+        assertEquals("2014-02-03", module.getRevision().get().toString());
         assertEquals("module:1:behind:mount:point", module.getNamespace().toString());
 
 
@@ -660,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);
     }
 
@@ -738,12 +679,12 @@ 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();
             final NormalizedNode value =
-                    (Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(qNameDepth1Cont)).build());
+                    Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(qNameDepth1Cont)).build();
             when(brokerFacade.readConfigurationData(eq(ii))).thenReturn(value);
             restconfImpl.readConfigurationData("nested-module:depth1-cont", uriInfo);
             fail("Expected RestconfDocumentedException");
@@ -773,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);
         }
@@ -790,7 +731,7 @@ public class RestGetOperationTest extends JerseyTest {
                     "Unexpected child element for parent \"" + element.getLocalName() + "\": "
                             + actualElement.getLocalName(), expChild);
 
-            if ((expChild.data == null) || (expChild.data instanceof List)) {
+            if (expChild.data == null || expChild.data instanceof List) {
                 verifyContainerElement(actualElement, expChild);
             } else {
                 assertEquals("Text content for element: " + actualElement.getLocalName(), expChild.data,