Prevent NPE for Credentials
[netconf.git] / apps / netconf-topology / src / test / java / org / opendaylight / netconf / topology / spi / NetconfNodeHandlerTest.java
index e43172857bade4f03d059e0a6df123dfd745e819..18873a58b9ae4cb9af5d4df7411179ffba62d812 100644 (file)
@@ -7,10 +7,8 @@
  */
 package org.opendaylight.netconf.topology.spi;
 
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyLong;
 import static org.mockito.ArgumentMatchers.eq;
@@ -29,15 +27,16 @@ import io.netty.util.TimerTask;
 import java.net.InetSocketAddress;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
-import org.junit.After;
-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.ArgumentCaptor;
 import org.mockito.Captor;
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
 import org.opendaylight.netconf.api.CapabilityURN;
 import org.opendaylight.netconf.client.NetconfClientFactory;
@@ -62,11 +61,11 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.credentials.credentials.KeyAuthBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.credentials.credentials.LoginPwUnencryptedBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.credentials.credentials.key.auth.KeyBasedBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.credentials.credentials.login.pw.unencrypted.LoginPasswordUnencryptedBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.credentials.credentials.KeyAuthBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.credentials.credentials.LoginPwUnencryptedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.credentials.credentials.key.auth.KeyBasedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.credentials.credentials.login.pw.unencrypted.LoginPasswordUnencryptedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev240611.NetconfNodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yangtools.yang.common.Decimal64;
 import org.opendaylight.yangtools.yang.common.Uint16;
@@ -75,8 +74,8 @@ import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class NetconfNodeHandlerTest {
+@ExtendWith(MockitoExtension.class)
+class NetconfNodeHandlerTest {
     private static final RemoteDeviceId DEVICE_ID = new RemoteDeviceId("netconf-topology",
         new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9999));
     private static final NodeId NODE_ID = new NodeId("testing-node");
@@ -124,18 +123,18 @@ public class NetconfNodeHandlerTest {
     private NetconfTopologySchemaAssembler schemaAssembler;
     private NetconfNodeHandler handler;
 
-    @BeforeClass
-    public static void beforeClass() throws Exception {
+    @BeforeAll
+    static void beforeClass() throws Exception {
         BASE_SCHEMAS = new DefaultBaseNetconfSchemaProvider(new DefaultYangParserFactory());
     }
 
-    @BeforeClass
-    public static void afterClass() throws Exception {
+    @AfterAll
+    static void afterClass() throws Exception {
         BASE_SCHEMAS = null;
     }
 
-    @Before
-    public void before() {
+    @BeforeEach
+    void before() {
         schemaAssembler = new NetconfTopologySchemaAssembler(1, 1, 0, TimeUnit.SECONDS);
 
         // Instantiate the handler
@@ -167,13 +166,13 @@ public class NetconfNodeHandlerTest {
                 .build(), null);
     }
 
-    @After
-    public void after() {
+    @AfterEach
+    void after() {
         schemaAssembler.close();
     }
 
     @Test
-    public void successfulOnDeviceConnectedPropagates() throws Exception {
+    void successfulOnDeviceConnectedPropagates() throws Exception {
         assertSuccessfulConnect();
         assertEquals(1, handler.attempts());
 
@@ -194,7 +193,7 @@ public class NetconfNodeHandlerTest {
     }
 
     @Test
-    public void failedSchemaCausesReconnect() throws Exception {
+    void failedSchemaCausesReconnect() throws Exception {
         assertSuccessfulConnect();
         assertEquals(1, handler.attempts());
 
@@ -212,7 +211,7 @@ public class NetconfNodeHandlerTest {
     }
 
     @Test
-    public void downAfterUpCausesReconnect() throws Exception {
+    void downAfterUpCausesReconnect() throws Exception {
         // Let's borrow common bits
         successfulOnDeviceConnectedPropagates();
 
@@ -230,7 +229,7 @@ public class NetconfNodeHandlerTest {
     }
 
     @Test
-    public void socketFailuresAreRetried() throws Exception {
+    void socketFailuresAreRetried() throws Exception {
         final var firstFuture = SettableFuture.create();
         final var secondFuture = SettableFuture.create();
         doReturn(firstFuture, secondFuture).when(clientFactory).createClient(any());
@@ -251,7 +250,7 @@ public class NetconfNodeHandlerTest {
         final var throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
         doNothing().when(delegate).onDeviceFailed(throwableCaptor.capture());
         secondFuture.setException(new AssertionError("second"));
-        assertThat(throwableCaptor.getValue(), instanceOf(ConnectGivenUpException.class));
+        assertInstanceOf(ConnectGivenUpException.class, throwableCaptor.getValue());
 
         // but nothing else happens
         assertEquals(2, handler.attempts());
@@ -267,7 +266,7 @@ public class NetconfNodeHandlerTest {
     }
 
     @Test
-    public void failToConnectOnUnsupportedConfiguration() {
+    void failToConnectOnUnsupportedConfiguration() {
         final var defaultTimer = new DefaultNetconfTimer();
         final var factory = new NetconfClientFactoryImpl(defaultTimer);
 
@@ -308,7 +307,7 @@ public class NetconfNodeHandlerTest {
         // attempt to connect fails due to unsupported configuration, and there is attempt to reconnect
         final var captor = ArgumentCaptor.forClass(Throwable.class);
         verify(delegate).onDeviceFailed(captor.capture());
-        assertTrue(captor.getValue() instanceof ConnectGivenUpException);
+        assertInstanceOf(ConnectGivenUpException.class, captor.getValue());
         assertEquals(1, keyAuthHandler.attempts());
     }
 }