Migrate netconf-server/osgi tests to JUnit5 74/111774/1
authorlubos-cicut <lubos.cicut@pantheon.tech>
Mon, 20 May 2024 12:35:31 +0000 (14:35 +0200)
committerlubos-cicut <lubos.cicut@pantheon.tech>
Mon, 20 May 2024 12:35:31 +0000 (14:35 +0200)
Migrate tests in protocol/netconf-server/osgi from JUnit4 to JUnit5.

JIRA: NETCONF-1310
Change-Id: I9062daee8f015e7dcbdbf615d28cb3f62bc840db
Signed-off-by: lubos-cicut <lubos.cicut@pantheon.tech>
protocol/netconf-server/src/test/java/org/opendaylight/netconf/server/osgi/AggregatedNetconfOperationServiceFactoryTest.java
protocol/netconf-server/src/test/java/org/opendaylight/netconf/server/osgi/NetconfCapabilityMonitoringServiceTest.java
protocol/netconf-server/src/test/java/org/opendaylight/netconf/server/osgi/NetconfOperationRouterImplTest.java
protocol/netconf-server/src/test/java/org/opendaylight/netconf/server/osgi/NetconfSessionMonitoringServiceTest.java

index bdb99ad379f2433ad97f21b964430fe42b102f31..335d201fad2c4a7cc3d1df0d5d209af62988044a 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.netconf.server.osgi;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.times;
@@ -16,19 +16,19 @@ import static org.mockito.Mockito.verify;
 import com.google.common.collect.Sets;
 import java.util.HashSet;
 import java.util.Set;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+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.netconf.server.api.monitoring.BasicCapability;
 import org.opendaylight.netconf.server.api.monitoring.Capability;
 import org.opendaylight.netconf.server.api.monitoring.CapabilityListener;
 import org.opendaylight.netconf.server.api.operations.NetconfOperationServiceFactory;
 import org.opendaylight.yangtools.concepts.Registration;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class AggregatedNetconfOperationServiceFactoryTest {
+@ExtendWith(MockitoExtension.class)
+class AggregatedNetconfOperationServiceFactoryTest {
 
     private final Set<Capability> factory1Caps = new HashSet<>();
     private final Set<Capability> factory2Caps = new HashSet<>();
@@ -52,8 +52,8 @@ public class AggregatedNetconfOperationServiceFactoryTest {
 
     private AggregatedNetconfOperationServiceFactory aggregatedFactory;
 
-    @Before
-    public void setUp() throws Exception {
+    @BeforeEach
+    void setUp() {
         factory1Caps.add(new BasicCapability("AAA"));
         factory1Caps.add(new BasicCapability("BBB"));
 
@@ -67,21 +67,15 @@ public class AggregatedNetconfOperationServiceFactoryTest {
 
         doReturn(reg1).when(factory1).registerCapabilityListener(listener1);
         doReturn(reg2).when(factory1).registerCapabilityListener(listener2);
-        doReturn(factory1Caps).when(factory1).getCapabilities();
 
         doReturn(reg1).when(factory2).registerCapabilityListener(listener1);
         doReturn(reg2).when(factory2).registerCapabilityListener(listener2);
-        doReturn(factory2Caps).when(factory2).getCapabilities();
-
-        doNothing().when(reg1).close();
-        doNothing().when(reg2).close();
-
-        doReturn(reg3).when(factory1).registerCapabilityListener(listener3);
-        doReturn(reg3).when(factory2).registerCapabilityListener(listener3);
     }
 
     @Test
-    public void testOnAddAndOnRemove() throws Exception {
+    void testOnAddAndOnRemove() {
+        doNothing().when(reg1).close();
+        doNothing().when(reg2).close();
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory1);
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory2);
 
@@ -98,7 +92,9 @@ public class AggregatedNetconfOperationServiceFactoryTest {
     }
 
     @Test
-    public void testGetCapabilities() throws Exception {
+    void testGetCapabilities() {
+        doReturn(factory1Caps).when(factory1).getCapabilities();
+        doReturn(factory2Caps).when(factory2).getCapabilities();
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory1);
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory2);
         final Set<Capability> actual = aggregatedFactory.getCapabilities();
@@ -107,7 +103,9 @@ public class AggregatedNetconfOperationServiceFactoryTest {
     }
 
     @Test
-    public void testRegisterCapabilityListener() throws Exception {
+    void testRegisterCapabilityListener() {
+        doReturn(reg3).when(factory1).registerCapabilityListener(listener3);
+        doReturn(reg3).when(factory2).registerCapabilityListener(listener3);
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory1);
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory2);
         aggregatedFactory.registerCapabilityListener(listener3);
@@ -117,7 +115,9 @@ public class AggregatedNetconfOperationServiceFactoryTest {
     }
 
     @Test
-    public void testClose() throws Exception {
+    void testClose() {
+        doNothing().when(reg1).close();
+        doNothing().when(reg2).close();
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory1);
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory2);
         aggregatedFactory.close();
index ec3a8c1b9294531e1d4924fe3bcf5e0c302ac538..ba1b09ae456534d7106e4d76bb41d537c0e5b29a 100644 (file)
@@ -7,9 +7,9 @@
  */
 package org.opendaylight.netconf.server.osgi;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
@@ -20,12 +20,12 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.netconf.api.CapabilityURN;
 import org.opendaylight.netconf.server.api.monitoring.BasicCapability;
 import org.opendaylight.netconf.server.api.monitoring.Capability;
@@ -40,8 +40,8 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.mon
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class NetconfCapabilityMonitoringServiceTest {
+@ExtendWith(MockitoExtension.class)
+class NetconfCapabilityMonitoringServiceTest {
     private static final String TEST_MODULE_CONTENT = "content";
     private static final String TEST_MODULE_CONTENT2 = "content2";
     private static final String TEST_MODULE_REV = "1970-01-01";
@@ -64,8 +64,8 @@ public class NetconfCapabilityMonitoringServiceTest {
 
     private NetconfCapabilityMonitoringService monitoringService;
 
-    @Before
-    public void setUp() {
+    @BeforeEach
+    void setUp() {
         moduleCapability1 = new YangModuleCapability(TEST_MODULE_NAMESPACE.getValue(), TEST_MODULE_NAME,
             TEST_MODULE_REV, TEST_MODULE_CONTENT);
 
@@ -86,8 +86,6 @@ public class NetconfCapabilityMonitoringServiceTest {
         doNothing().when(listener).onCapabilitiesChanged(any());
         doNothing().when(listener).onSchemasChanged(any());
 
-        doNothing().when(notificationPublisher).onCapabilityChanged(any());
-
         monitoringService = new NetconfCapabilityMonitoringService(operationServiceFactoryMock);
         monitoringService.onCapabilitiesChanged(capabilities, Set.of());
         monitoringService.setNotificationPublisher(notificationPublisher);
@@ -96,7 +94,8 @@ public class NetconfCapabilityMonitoringServiceTest {
     }
 
     @Test
-    public void testListeners() {
+    void testListeners() {
+        doNothing().when(notificationPublisher).onCapabilityChanged(any());
         HashSet<Capability> added = new HashSet<>();
         added.add(new BasicCapability("toAdd"));
         monitoringService.onCapabilitiesChanged(added, Set.of());
@@ -106,7 +105,7 @@ public class NetconfCapabilityMonitoringServiceTest {
     }
 
     @Test
-    public void testGetSchemas() {
+    void testGetSchemas() {
         Schemas schemas = monitoringService.getSchemas();
         Schema schema = schemas.nonnullSchema().values().iterator().next();
         assertEquals(TEST_MODULE_NAMESPACE, schema.getNamespace());
@@ -115,7 +114,8 @@ public class NetconfCapabilityMonitoringServiceTest {
     }
 
     @Test
-    public void testGetSchemaForCapability() {
+    void testGetSchemaForCapability() {
+        doNothing().when(notificationPublisher).onCapabilityChanged(any());
         //test multiple revisions of the same capability
         monitoringService.onCapabilitiesChanged(Set.of(moduleCapability2), Set.of());
         final String schema =
@@ -132,7 +132,7 @@ public class NetconfCapabilityMonitoringServiceTest {
     }
 
     @Test
-    public void testGetCapabilities() {
+    void testGetCapabilities() {
         Set<Uri> exp = new HashSet<>();
         for (Capability capability : capabilities) {
             exp.add(new Uri(capability.getCapabilityUri()));
@@ -146,14 +146,15 @@ public class NetconfCapabilityMonitoringServiceTest {
     }
 
     @Test
-    public void testClose() {
+    void testClose() {
         assertEquals(6, monitoringService.getCapabilities().requireCapability().size());
         monitoringService.close();
         assertEquals(Set.of(), monitoringService.getCapabilities().getCapability());
     }
 
     @Test
-    public void testOnCapabilitiesChanged() {
+    void testOnCapabilitiesChanged() {
+        doNothing().when(notificationPublisher).onCapabilityChanged(any());
         final String capUri = "test";
         final Uri uri = new Uri(capUri);
         final HashSet<Capability> testCaps = new HashSet<>();
index 07aea85208a486fc73c681730ecd7116fddaba6d..bf9f76e5975ff1e7defee299a6c33978b734a94d 100644 (file)
@@ -7,10 +7,10 @@
  */
 package org.opendaylight.netconf.server.osgi;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-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.assertFalse;
+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.doNothing;
 import static org.mockito.Mockito.doReturn;
@@ -18,13 +18,13 @@ import static org.mockito.Mockito.verify;
 
 import java.io.IOException;
 import java.util.Set;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+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.ArgumentCaptor;
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.server.api.operations.HandlingPriority;
@@ -37,8 +37,8 @@ import org.opendaylight.yangtools.yang.common.Uint32;
 import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class NetconfOperationRouterImplTest {
+@ExtendWith(MockitoExtension.class)
+class NetconfOperationRouterImplTest {
     private static final String TEST_RPC =
         "<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><test/></rpc>\n";
     private static final String MAX_PRIORITY_REPLY = "<high/>";
@@ -58,23 +58,14 @@ public class NetconfOperationRouterImplTest {
     private NetconfOperationRouterImpl operationRouter;
     private NetconfOperationRouterImpl emptyOperationRouter;
 
-    @BeforeClass
-    public static void suiteSetUp() throws IOException, SAXException {
+    @BeforeAll
+    static void suiteSetUp() throws IOException, SAXException {
         TEST_RPC_DOC = XmlUtil.readXmlToDocument(TEST_RPC);
     }
 
-    @Before
-    public void setUp() throws Exception {
-        doReturn(HandlingPriority.HANDLE_WITH_MAX_PRIORITY).when(maxPrioMock).canHandle(any(Document.class));
-        doReturn(XmlUtil.readXmlToDocument(MAX_PRIORITY_REPLY)).when(maxPrioMock).handle(any(Document.class),
-                any(NetconfOperationChainedExecution.class));
-
-        doReturn(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY).when(defaultPrioMock).canHandle(any(Document.class));
-        doReturn(XmlUtil.readXmlToDocument(DEFAULT_PRIORITY_REPLY)).when(defaultPrioMock).handle(any(Document.class),
-                any(NetconfOperationChainedExecution.class));
-
+    @BeforeEach
+    void setUp() {
         doReturn(Set.of(maxPrioMock, defaultPrioMock)).when(operationService).getNetconfOperations();
-        doNothing().when(operationService).close();
 
         final var sessionId = new SessionIdType(Uint32.ONE);
         operationRouter = new NetconfOperationRouterImpl(operationService, null, sessionId);
@@ -83,7 +74,14 @@ public class NetconfOperationRouterImplTest {
     }
 
     @Test
-    public void testOnNetconfMessage() throws Exception {
+    void testOnNetconfMessage() throws Exception {
+        doReturn(HandlingPriority.HANDLE_WITH_MAX_PRIORITY).when(maxPrioMock).canHandle(any(Document.class));
+        doReturn(XmlUtil.readXmlToDocument(MAX_PRIORITY_REPLY)).when(maxPrioMock).handle(any(Document.class),
+            any(NetconfOperationChainedExecution.class));
+
+        doReturn(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY).when(defaultPrioMock).canHandle(any(Document.class));
+        doReturn(XmlUtil.readXmlToDocument(DEFAULT_PRIORITY_REPLY)).when(defaultPrioMock).handle(any(Document.class),
+            any(NetconfOperationChainedExecution.class));
         final ArgumentCaptor<NetconfOperationChainedExecution> highPriorityChainEx =
                 ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
         final ArgumentCaptor<NetconfOperationChainedExecution> defaultPriorityChainEx =
@@ -108,14 +106,15 @@ public class NetconfOperationRouterImplTest {
     }
 
     @Test
-    public void testOnNetconfMessageFail() throws Exception {
+    void testOnNetconfMessageFail() {
         final DocumentedException ex =  assertThrows(DocumentedException.class,
             () -> emptyOperationRouter.onNetconfMessage(TEST_RPC_DOC, null));
         assertEquals(ErrorTag.OPERATION_NOT_SUPPORTED, ex.getErrorTag());
     }
 
     @Test
-    public void testClose() throws Exception {
+    void testClose() {
+        doNothing().when(operationService).close();
         operationRouter.close();
         verify(operationService).close();
     }
index 7daa2111784580fd9c11b76cc9e2a900524a6a5e..8c0345608d224612a37edb3c1f74dcd364c053de 100644 (file)
@@ -7,10 +7,10 @@
  */
 package org.opendaylight.netconf.server.osgi;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
@@ -20,13 +20,13 @@ import static org.mockito.Mockito.verify;
 import java.util.Collection;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Captor;
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.netconf.server.api.monitoring.NetconfManagementSession;
 import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
 import org.opendaylight.netconf.server.api.monitoring.SessionEvent;
@@ -37,8 +37,8 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.mon
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.SessionBuilder;
 import org.opendaylight.yangtools.yang.common.Uint32;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class NetconfSessionMonitoringServiceTest {
+@ExtendWith(MockitoExtension.class)
+class NetconfSessionMonitoringServiceTest {
     private static final Session SESSION_1 = new SessionBuilder()
             .setSessionId(Uint32.valueOf(1))
             .setSourceHost(new Host(new IpAddress(new Ipv4Address("0.0.0.0"))))
@@ -61,19 +61,17 @@ public class NetconfSessionMonitoringServiceTest {
 
     private NetconfSessionMonitoringService monitoringService;
 
-    @Before
-    public void setUp() {
+    @BeforeEach
+    void setUp() {
         doReturn(SESSION_1).when(sessionMock1).toManagementSession();
-        doReturn(SESSION_2).when(sessionMock2).toManagementSession();
         doNothing().when(listener).onSessionStarted(any());
-        doNothing().when(listener).onSessionEnded(any());
-
         monitoringService = new NetconfSessionMonitoringService.WithoutUpdates();
         monitoringService.registerListener(listener);
     }
 
     @Test
-    public void testListeners() {
+    void testListeners() {
+        doNothing().when(listener).onSessionEnded(any());
         monitoringService.onSessionUp(sessionMock1);
         monitoringService.onSessionDown(sessionMock1);
         verify(listener).onSessionStarted(any());
@@ -81,7 +79,7 @@ public class NetconfSessionMonitoringServiceTest {
     }
 
     @Test
-    public void testClose() {
+    void testClose() {
         monitoringService.onSessionUp(sessionMock1);
         assertEquals(1, monitoringService.getSessions().nonnullSession().size());
         monitoringService.close();
@@ -89,7 +87,8 @@ public class NetconfSessionMonitoringServiceTest {
     }
 
     @Test
-    public void testOnSessionUpAndDown() {
+    void testOnSessionUpAndDown() {
+        doNothing().when(listener).onSessionEnded(any());
         monitoringService.onSessionUp(sessionMock1);
         final var sessionUpCaptor = ArgumentCaptor.forClass(Session.class);
         verify(listener).onSessionStarted(sessionUpCaptor.capture());
@@ -108,7 +107,8 @@ public class NetconfSessionMonitoringServiceTest {
     }
 
     @Test
-    public void testListenerUpdateSession() {
+    void testListenerUpdateSession() {
+        doReturn(SESSION_2).when(sessionMock2).toManagementSession();
         final var executor = Executors.newScheduledThreadPool(1);
         try {
             try (var service = new NetconfSessionMonitoringService.WithUpdates(executor, 1, TimeUnit.SECONDS)) {