Move RemoteDeviceId
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / NetconfStateSchemasTest.java
index 45792d760352843c5d478a5d11329ab17914879a..dbcb82dc5a867cdeeb9094cef513277a975edd24 100644 (file)
@@ -8,43 +8,45 @@
 package org.opendaylight.netconf.sal.connect.netconf;
 
 import static org.hamcrest.CoreMatchers.hasItem;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
-import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
-import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
-import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
-import com.google.common.util.concurrent.FluentFuture;
+import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.net.InetSocketAddress;
-import java.util.Collections;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
-import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
-import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
 import org.opendaylight.yangtools.util.xml.UntrustedXML;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
@@ -55,17 +57,17 @@ import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NetconfStateSchemasTest {
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
+public class NetconfStateSchemasTest extends AbstractBaseSchemasTest {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfStateSchemasTest.class);
 
-    private static final NetconfSessionPreferences CAPS = NetconfSessionPreferences.fromStrings(Collections.singleton(
+    private static final NetconfSessionPreferences CAPS = NetconfSessionPreferences.fromStrings(Set.of(
         "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring&revision=2010-10-04"));
     private final RemoteDeviceId deviceId = new RemoteDeviceId("device", new InetSocketAddress(99));
     private final int numberOfSchemas = 73;
@@ -75,24 +77,20 @@ public class NetconfStateSchemasTest {
     @Mock
     private DOMRpcService rpc;
 
-    private SchemaContext schemaContext;
+    private EffectiveModelContext schemaContext;
 
     @Before
     public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
-        schemaContext = BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext();
-        final DataSchemaNode schemasNode =
-                ((ContainerSchemaNode) schemaContext
-                        .getDataChildByName(NetconfState.QNAME)).getDataChildByName(Schemas.QNAME);
+        schemaContext = BASE_SCHEMAS.getBaseSchemaWithNotifications().getEffectiveModelContext();
 
         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
-        final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext, schemasNode, false);
+        final XmlParserStream xmlParser = XmlParserStream.create(writer,
+            SchemaInferenceStack.ofDataTreePath(schemaContext, NetconfState.QNAME, Schemas.QNAME).toInference(), false);
 
         xmlParser.parse(UntrustedXML.createXMLStreamReader(getClass().getResourceAsStream(
                 "/netconf-state.schemas.payload.xml")));
         compositeNodeSchemas = (ContainerNode) resultHolder.getResult();
-
     }
 
     @Test
@@ -122,8 +120,8 @@ public class NetconfStateSchemasTest {
                         .NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME))
                 .withChild(data)
                 .build();
-        when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any()))
-            .thenReturn(immediateFluentFuture(new DefaultDOMRpcResult(rpcReply)));
+        doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(rpcReply))).when(rpc)
+            .invokeRpc(eq(NETCONF_GET_QNAME), any());
         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId, schemaContext);
         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
         assertEquals(numberOfLegalSchemas, availableYangSchemasQNames.size());
@@ -134,52 +132,47 @@ public class NetconfStateSchemasTest {
 
     @Test
     public void testCreateMonitoringNotSupported() throws Exception {
-        final NetconfSessionPreferences caps = NetconfSessionPreferences.fromStrings(Collections.emptySet());
+        final NetconfSessionPreferences caps = NetconfSessionPreferences.fromStrings(Set.of());
         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, caps, deviceId, schemaContext);
         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
-        Assert.assertTrue(availableYangSchemasQNames.isEmpty());
+        assertTrue(availableYangSchemasQNames.isEmpty());
     }
 
     @Test
     public void testCreateFail() throws Exception {
-        when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(
-                immediateFailedFluentFuture(new DOMRpcImplementationNotAvailableException("not available")));
+        when(rpc.invokeRpc(eq(NETCONF_GET_QNAME), any())).thenReturn(
+                Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException("not available")));
         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId, schemaContext);
         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
-        Assert.assertTrue(availableYangSchemasQNames.isEmpty());
+        assertTrue(availableYangSchemasQNames.isEmpty());
     }
 
     @Test
     public void testCreateRpcError() throws Exception {
-        final RpcError rpcError = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "fail", "fail");
-        when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any()))
-            .thenReturn(immediateFluentFuture(new DefaultDOMRpcResult(rpcError)));
+        final RpcError rpcError = RpcResultBuilder.newError(ErrorType.RPC, new ErrorTag("fail"), "fail");
+        doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(rpcError))).when(rpc)
+            .invokeRpc(eq(NETCONF_GET_QNAME), any());
         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId, schemaContext);
         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
-        Assert.assertTrue(availableYangSchemasQNames.isEmpty());
+        assertTrue(availableYangSchemasQNames.isEmpty());
     }
 
-    @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
-    @Test(expected = RuntimeException.class)
-    public void testCreateInterrupted() throws Throwable {
+    @Test
+    public void testCreateInterrupted() {
         //NetconfStateSchemas.create calls Thread.currentThread().interrupt(), so it must run in its own thread
         final Future<?> testFuture = Executors.newSingleThreadExecutor().submit(() -> {
             final ListenableFuture<DOMRpcResult> interruptedFuture = mock(ListenableFuture.class);
             try {
                 when(interruptedFuture.get()).thenThrow(new InterruptedException("interrupted"));
-                when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(
-                        FluentFuture.from(interruptedFuture));
+                doReturn(interruptedFuture).when(rpc).invokeRpc(eq(NETCONF_GET_QNAME), any());
                 NetconfStateSchemas.create(rpc, CAPS, deviceId, schemaContext);
             } catch (final InterruptedException | ExecutionException e) {
                 LOG.info("Operation failed.", e);
             }
-
         });
-        try {
-            testFuture.get(3, TimeUnit.SECONDS);
-        } catch (final ExecutionException e) {
-            throw e.getCause();
-        }
+
+        assertThat(assertThrows(ExecutionException.class, () -> testFuture.get(3, TimeUnit.SECONDS)).getCause(),
+            instanceOf(RuntimeException.class));
     }
 
     @Test
@@ -190,10 +183,9 @@ public class NetconfStateSchemasTest {
                 new NetconfStateSchemas.RemoteYangSchema(NetconfState.QNAME);
         final NetconfStateSchemas.RemoteYangSchema schema3 =
                 new NetconfStateSchemas.RemoteYangSchema(Schemas.QNAME);
-        Assert.assertEquals(schema1, schema2);
-        Assert.assertEquals(schema2, schema1);
-        Assert.assertNotEquals(schema1, schema3);
-        Assert.assertNotEquals(schema2, schema3);
-
+        assertEquals(schema1, schema2);
+        assertEquals(schema2, schema1);
+        assertNotEquals(schema1, schema3);
+        assertNotEquals(schema2, schema3);
     }
 }