Migrate plugins/netconf-server-mdsal/operations tests to JUnit5
[netconf.git] / plugins / netconf-server-mdsal / src / test / java / org / opendaylight / netconf / server / mdsal / operations / RuntimeRpcTest.java
index 6bbbad1c81ce3e8600da4624cd7a5cae6533a1c6..4e9b2618cabc74157eebac011d8cd2f980000767 100644 (file)
@@ -7,39 +7,41 @@
  */
 package org.opendaylight.netconf.server.mdsal.operations;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doNothing;
 
-import com.google.common.base.Preconditions;
-import com.google.common.io.ByteSource;
+import com.google.common.io.CharSource;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
 import java.io.Serial;
 import java.util.Collection;
 import java.util.List;
+import java.util.function.Consumer;
 import javax.xml.parsers.ParserConfigurationException;
 import org.custommonkey.xmlunit.DetailedDiff;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
 import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
 import org.opendaylight.mdsal.dom.api.DOMRpcException;
 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService.YangTextSourceExtension;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.xml.XmlUtil;
@@ -48,8 +50,7 @@ import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExe
 import org.opendaylight.netconf.server.mdsal.CurrentSchemaContext;
 import org.opendaylight.netconf.test.util.XmlFileLoader;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
@@ -59,22 +60,20 @@ import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
-import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
+import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class RuntimeRpcTest {
+@ExtendWith(MockitoExtension.class)
+class RuntimeRpcTest {
     private static final Logger LOG = LoggerFactory.getLogger(RuntimeRpcTest.class);
     private static final SessionIdType SESSION_ID_FOR_REPORTING = new SessionIdType(Uint32.valueOf(123));
     private static final Document RPC_REPLY_OK = getReplyOk();
@@ -95,8 +94,8 @@ public class RuntimeRpcTest {
         }
 
         @Override
-        public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
-            return NoOpListenerRegistration.of(listener);
+        public Registration registerRpcListener(final DOMRpcAvailabilityListener listener) {
+            return () -> { };
         }
     };
 
@@ -110,8 +109,8 @@ public class RuntimeRpcTest {
         }
 
         @Override
-        public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
-            return NoOpListenerRegistration.of(listener);
+        public Registration registerRpcListener(final DOMRpcAvailabilityListener listener) {
+            return () -> { };
         }
     };
 
@@ -123,7 +122,7 @@ public class RuntimeRpcTest {
                 .findFirst().orElse(null);
             final RpcDefinition rpcDefinition = getRpcDefinitionFromModule(module, module.getNamespace(),
                 type.getLocalName());
-            final ContainerNode node = Builders.containerBuilder()
+            final ContainerNode node = ImmutableNodes.newContainerBuilder()
                     .withNodeIdentifier(new NodeIdentifier(rpcDefinition.getOutput().getQName()))
                     .withValue(children)
                     .build();
@@ -132,8 +131,8 @@ public class RuntimeRpcTest {
         }
 
         @Override
-        public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T lsnr) {
-            return NoOpListenerRegistration.of(lsnr);
+        public Registration registerRpcListener(final DOMRpcAvailabilityListener lsnr) {
+            return () -> { };
         }
     };
 
@@ -143,55 +142,52 @@ public class RuntimeRpcTest {
     @Mock
     private DOMSchemaService schemaService;
     @Mock
-    private EffectiveModelContextListener listener;
+    private Consumer<EffectiveModelContext> listener;
     @Mock
-    private ListenerRegistration<?> registration;
+    private Registration registration;
     @Mock
-    private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
+    private YangTextSourceExtension sourceProvider;
 
-    @BeforeClass
-    public static void beforeClass() {
+    @BeforeAll
+    static void beforeAll() {
         SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/yang/mdsal-netconf-rpc-test.yang");
     }
 
-    @AfterClass
-    public static void afterClass() {
+    @AfterAll
+    static void afterAll() {
         SCHEMA_CONTEXT = null;
     }
 
-    @Before
-    public void setUp() throws Exception {
+    @BeforeEach
+    void setUp() {
         doNothing().when(registration).close();
         doAnswer(invocationOnMock -> {
-            ((EffectiveModelContextListener) invocationOnMock.getArguments()[0]).onModelContextUpdated(SCHEMA_CONTEXT);
+            ((Consumer<EffectiveModelContext>) invocationOnMock.getArgument(0)).accept(SCHEMA_CONTEXT);
             return registration;
-        }).when(schemaService).registerSchemaContextListener(any(EffectiveModelContextListener.class));
+        }).when(schemaService).registerSchemaContextListener(any());
 
         XMLUnit.setIgnoreWhitespace(true);
         XMLUnit.setIgnoreAttributeOrder(true);
 
-        doAnswer(invocationOnMock -> {
-            final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
-            final YangTextSchemaSource yangTextSchemaSource =
-                    YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
-            return Futures.immediateFuture(yangTextSchemaSource);
-        }).when(sourceProvider).getSource(any(SourceIdentifier.class));
+        doAnswer(invocationOnMock -> Futures.immediateFuture(new DelegatedYangTextSource(
+            invocationOnMock.getArgument(0, SourceIdentifier.class), CharSource.wrap("module test"))))
+            .when(sourceProvider).getYangTexttSource(any(SourceIdentifier.class));
 
         currentSchemaContext = CurrentSchemaContext.create(schemaService, sourceProvider);
     }
 
-    @After
-    public void tearDown() {
+    @AfterEach
+    void tearDown() {
         currentSchemaContext.close();
     }
 
     @Test
-    public void testVoidOutputRpc() throws Exception {
+    void testVoidOutputRpc() throws Exception {
         final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-output.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
-        Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+        assertNotNull(priority);
 
         final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
 
@@ -199,39 +195,39 @@ public class RuntimeRpcTest {
     }
 
     @Test
-    public void testSuccesfullNonVoidInvocation() throws Exception {
+    void testSuccesfullNonVoidInvocation() throws Exception {
         final RuntimeRpc rpc = new RuntimeRpc(
             SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
-        Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+        assertNotNull(priority);
 
         final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
         verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid-control.xml"));
     }
 
     @Test
-    public void testSuccesfullContainerInvocation() throws Exception {
+    void testSuccesfullContainerInvocation() throws Exception {
         final RuntimeRpc rpc = new RuntimeRpc(
             SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
-        Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+        assertNotNull(priority);
 
         final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
         verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container-control.xml"));
     }
 
     @Test
-    public void testFailedInvocation() throws Exception {
+    void testFailedInvocation() throws Exception {
         final RuntimeRpc rpc = new RuntimeRpc(
                 SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_FAILED_INVOCATION);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
-        Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+        assertNotNull(priority);
 
         final DocumentedException e = assertThrows(DocumentedException.class,
                 () -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
@@ -242,12 +238,12 @@ public class RuntimeRpcTest {
     }
 
     @Test
-    public void testVoidInputOutputRpc() throws Exception {
+    void testVoidInputOutputRpc() throws Exception {
         final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-input-output.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
-        Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+        assertNotNull(priority);
 
         final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
 
@@ -255,7 +251,7 @@ public class RuntimeRpcTest {
     }
 
     @Test
-    public void testBadNamespaceInRpc() throws Exception {
+    void testBadNamespaceInRpc() throws Exception {
         final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-bad-namespace.xml");
 
@@ -271,7 +267,7 @@ public class RuntimeRpcTest {
         final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
         dd.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
         //we care about order so response has to be identical
-        assertTrue(dd.identical());
+        assertTrue(dd.identical(), dd.toString());
     }
 
     private static RpcDefinition getRpcDefinitionFromModule(final Module module, final XMLNamespace namespaceURI,