Cleanup netconf-impl tests 02/96302/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 23 May 2021 17:57:41 +0000 (19:57 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 23 May 2021 17:58:32 +0000 (19:58 +0200)
Use static imports for assert methods, and use assertThrows() instead
of try/catch blocks.

Change-Id: I0054becc441cbe33c6aedf156b267429eb53e224
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/NetconfServerSessionListenerTest.java
netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/NetconfServerSessionTest.java
netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/osgi/AggregatedNetconfOperationServiceFactoryTest.java
netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/osgi/NetconfOperationRouterImplTest.java

index 1e739900d78143d3a4557f771bdb59a4d695b779..80ce81a1426934b166f09d82d543813cebebe746 100644 (file)
@@ -5,9 +5,10 @@
  * 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.netconf.impl;
 
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.argThat;
 import static org.mockito.Mockito.doNothing;
@@ -18,7 +19,6 @@ import static org.mockito.Mockito.verify;
 import io.netty.channel.embedded.EmbeddedChannel;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -102,7 +102,7 @@ public class NetconfServerSessionListenerTest {
         channel.runPendingTasks();
         final NetconfMessage sentMsg = channel.readOutbound();
         final Diff diff = XMLUnit.compareXML(reply, sentMsg.getDocument());
-        Assert.assertTrue(diff.toString(), diff.similar());
+        assertTrue(diff.toString(), diff.similar());
     }
 
     @Test
@@ -112,12 +112,9 @@ public class NetconfServerSessionListenerTest {
                 XmlUtil.readXmlToDocument("<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
                         + "<example/></rpc>");
         final NetconfMessage msg = new NetconfMessage(reply);
-        try {
-            listener.onMessage(session, msg);
-            Assert.fail("Expected exception " + IllegalStateException.class);
-        } catch (final IllegalStateException e) {
-            verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_FAIL)));
-        }
+        final IllegalStateException ex = assertThrows(IllegalStateException.class,
+            () -> listener.onMessage(session, msg));
+        verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_FAIL)));
     }
 
     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
@@ -146,7 +143,7 @@ public class NetconfServerSessionListenerTest {
         System.out.println(XmlUtil.toString(sentMsg.getDocument()));
         System.out.println(XmlUtil.toString(reply));
         final Diff diff = XMLUnit.compareXML(reply, sentMsg.getDocument());
-        Assert.assertTrue(diff.toString(), diff.similar());
+        assertTrue(diff.toString(), diff.similar());
     }
 
     @Test
index 46cb6bd73249a6387d730ecb99c0cf346b77a211..ab8904f9fa0dae5ed736394bbb3cd5cff128a7f5 100644 (file)
@@ -5,9 +5,10 @@
  * 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.netconf.impl;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doNothing;
@@ -17,7 +18,6 @@ import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelInboundHandlerAdapter;
 import io.netty.channel.ChannelOutboundHandlerAdapter;
 import io.netty.channel.embedded.EmbeddedChannel;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -41,13 +41,13 @@ import org.w3c.dom.Document;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class NetconfServerSessionTest {
-
     private static final String HOST = "127.0.0.1";
     private static final String PORT = "17830";
     private static final String SSH_TRANSPORT = "ssh";
     private static final String TCP_TRANSPORT = "tcp";
     private static final String SESSION_ID = "1";
     private static final String USER = "admin";
+
     private NetconfServerSession session;
     private EmbeddedChannel channel;
     private NetconfMessage msg;
@@ -77,7 +77,7 @@ public class NetconfServerSessionTest {
         session.sendMessage(msg);
         channel.runPendingTasks();
         final Object o = channel.readOutbound();
-        Assert.assertEquals(msg, o);
+        assertEquals(msg, o);
         verify(listener).onSessionTerminated(eq(session), any());
     }
 
@@ -86,7 +86,7 @@ public class NetconfServerSessionTest {
         session.sendMessage(msg);
         channel.runPendingTasks();
         final Object o = channel.readOutbound();
-        Assert.assertEquals(msg, o);
+        assertEquals(msg, o);
     }
 
     @Test
@@ -97,7 +97,7 @@ public class NetconfServerSessionTest {
         session.sendMessage(notif);
         channel.runPendingTasks();
         final Object o = channel.readOutbound();
-        Assert.assertEquals(notif, o);
+        assertEquals(notif, o);
         verify(listener).onNotification(session, notif);
     }
 
@@ -107,7 +107,7 @@ public class NetconfServerSessionTest {
         final Session managementSession = this.session.toManagementSession();
         this.session.onIncommingRpcSuccess();
         final Session afterRpcSuccess = this.session.toManagementSession();
-        Assert.assertEquals(managementSession.getInRpcs().getValue().toJava() + 1,
+        assertEquals(managementSession.getInRpcs().getValue().toJava() + 1,
                 afterRpcSuccess.getInRpcs().getValue().longValue());
     }
 
@@ -117,7 +117,7 @@ public class NetconfServerSessionTest {
         final Session managementSession = this.session.toManagementSession();
         this.session.onIncommingRpcFail();
         final Session afterRpcSuccess = this.session.toManagementSession();
-        Assert.assertEquals(managementSession.getInBadRpcs().getValue().toJava() + 1,
+        assertEquals(managementSession.getInBadRpcs().getValue().toJava() + 1,
                 afterRpcSuccess.getInBadRpcs().getValue().longValue());
     }
 
@@ -127,7 +127,7 @@ public class NetconfServerSessionTest {
         final Session managementSession = this.session.toManagementSession();
         this.session.onOutgoingRpcError();
         final Session afterRpcSuccess = this.session.toManagementSession();
-        Assert.assertEquals(managementSession.getOutRpcErrors().getValue().toJava() + 1,
+        assertEquals(managementSession.getOutRpcErrors().getValue().toJava() + 1,
                 afterRpcSuccess.getOutRpcErrors().getValue().longValue());
     }
 
@@ -139,10 +139,10 @@ public class NetconfServerSessionTest {
         final NetconfServerSession tcpSession = new NetconfServerSession(listener, ch, 1L, header);
         tcpSession.sessionUp();
         final Session managementSession = tcpSession.toManagementSession();
-        Assert.assertEquals(HOST, managementSession.getSourceHost().getIpAddress().getIpv4Address().getValue());
-        Assert.assertEquals(managementSession.getUsername(), USER);
-        Assert.assertEquals(managementSession.getSessionId().toString(), SESSION_ID);
-        Assert.assertEquals(managementSession.getTransport(), NetconfTcp.class);
+        assertEquals(HOST, managementSession.getSourceHost().getIpAddress().getIpv4Address().getValue());
+        assertEquals(managementSession.getUsername(), USER);
+        assertEquals(managementSession.getSessionId().toString(), SESSION_ID);
+        assertEquals(managementSession.getTransport(), NetconfTcp.class);
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -164,15 +164,15 @@ public class NetconfServerSessionTest {
         final NetconfServerSession tcpSession = new NetconfServerSession(listener, ch, 1L, header);
         tcpSession.sessionUp();
         final Session managementSession = tcpSession.toManagementSession();
-        Assert.assertEquals("::1", managementSession.getSourceHost().getIpAddress().getIpv6Address().getValue());
-        Assert.assertEquals(managementSession.getUsername(), USER);
-        Assert.assertEquals(managementSession.getSessionId().toString(), SESSION_ID);
-        Assert.assertEquals(managementSession.getTransport(), NetconfSsh.class);
+        assertEquals("::1", managementSession.getSourceHost().getIpAddress().getIpv6Address().getValue());
+        assertEquals(managementSession.getUsername(), USER);
+        assertEquals(managementSession.getSessionId().toString(), SESSION_ID);
+        assertEquals(managementSession.getTransport(), NetconfSsh.class);
     }
 
     @Test
     public void testThisInstance() throws Exception {
-        Assert.assertEquals(session, session.thisInstance());
+        assertEquals(session, session.thisInstance());
     }
 
     @Test
@@ -194,13 +194,12 @@ public class NetconfServerSessionTest {
         session.stopExiCommunication();
         //handler is replaced only after next send message call
         final ChannelHandler exiEncoder = channel.pipeline().get(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER);
-        Assert.assertTrue(ChannelOutboundHandlerAdapter.class.equals(exiEncoder.getClass()));
+        assertTrue(ChannelOutboundHandlerAdapter.class.equals(exiEncoder.getClass()));
         session.sendMessage(msg);
         channel.runPendingTasks();
         final ChannelHandler decoder = channel.pipeline().get(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER);
-        Assert.assertTrue(NetconfXMLToMessageDecoder.class.equals(decoder.getClass()));
+        assertTrue(NetconfXMLToMessageDecoder.class.equals(decoder.getClass()));
         final ChannelHandler encoder = channel.pipeline().get(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER);
-        Assert.assertTrue(NetconfMessageToXMLEncoder.class.equals(encoder.getClass()));
+        assertTrue(NetconfMessageToXMLEncoder.class.equals(encoder.getClass()));
     }
-
 }
index 1251357ea850a2f02c191aa2d50888aca1be9a9d..776fbf21b943a005ac5424a8d4d4a2d6773aee86 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.netconf.impl.osgi;
 
+import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.times;
@@ -15,7 +16,6 @@ import static org.mockito.Mockito.verify;
 import com.google.common.collect.Sets;
 import java.util.HashSet;
 import java.util.Set;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -102,7 +102,7 @@ public class AggregatedNetconfOperationServiceFactoryTest {
         aggregatedFactory.onAddNetconfOperationServiceFactory(factory2);
         final Set<Capability> actual = aggregatedFactory.getCapabilities();
         Set<Capability> expected = Sets.union(factory1Caps, factory2Caps);
-        Assert.assertEquals(expected, actual);
+        assertEquals(expected, actual);
     }
 
     @Test
@@ -123,5 +123,4 @@ public class AggregatedNetconfOperationServiceFactoryTest {
         verify(autoCloseable1, times(2)).close();
         verify(autoCloseable2, times(2)).close();
     }
-
 }
index 6c1ae122a0c76da71fa5f3dbfc0425be8a8e226d..5abcd7512b357dceceb06297d16b1e394c5e17f1 100644 (file)
@@ -7,6 +7,10 @@
  */
 package org.opendaylight.netconf.impl.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.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
@@ -16,7 +20,6 @@ import java.io.IOException;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -93,27 +96,24 @@ public class NetconfOperationRouterImplTest {
         //max priority message is first in chain
         verify(maxPrioMock).handle(any(Document.class), highPriorityChainEx.capture());
         final NetconfOperationChainedExecution chainedExecution = highPriorityChainEx.getValue();
-        Assert.assertFalse(chainedExecution.isExecutionTermination());
+        assertFalse(chainedExecution.isExecutionTermination());
 
         //execute next in chain
         final Document execute = chainedExecution.execute(XmlUtil.newDocument());
-        Assert.assertEquals(DEFAULT_PRIORITY_REPLY, XmlUtil.toString(execute).trim());
+        assertEquals(DEFAULT_PRIORITY_REPLY, XmlUtil.toString(execute).trim());
 
         //default priority message is second and last
         verify(defaultPrioMock).handle(any(Document.class), defaultPriorityChainEx.capture());
-        Assert.assertTrue(defaultPriorityChainEx.getValue().isExecutionTermination());
+        assertTrue(defaultPriorityChainEx.getValue().isExecutionTermination());
 
-        Assert.assertEquals(MAX_PRIORITY_REPLY, XmlUtil.toString(document).trim());
+        assertEquals(MAX_PRIORITY_REPLY, XmlUtil.toString(document).trim());
     }
 
     @Test
     public void testOnNetconfMessageFail() throws Exception {
-        try {
-            emptyOperationRouter.onNetconfMessage(TEST_RPC_DOC, null);
-            Assert.fail("Exception expected");
-        } catch (final DocumentedException e) {
-            Assert.assertEquals(e.getErrorTag(), DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED);
-        }
+        final DocumentedException ex =  assertThrows(DocumentedException.class,
+            () -> emptyOperationRouter.onNetconfMessage(TEST_RPC_DOC, null));
+        assertEquals(DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED, ex.getErrorTag());
     }
 
     @Test