Modernize PCEPSessionImplTest 20/100720/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 22 Apr 2022 14:24:45 +0000 (16:24 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 22 Apr 2022 15:31:35 +0000 (17:31 +0200)
Use static imports and proper hamcrest assertions.

Change-Id: I4f1c24a0d0d72554d688f9f7f71dc9d8ac9001ca
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImplTest.java

index 25488bb5a2570c14cdc669210dd23b76a5dd7039..8dca3c2470e51fc9f3d8559da6398ca732a586c4 100644 (file)
@@ -7,12 +7,20 @@
  */
 package org.opendaylight.protocol.pcep.impl;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+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.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.ArgumentMatchers;
-import org.mockito.Mockito;
 import org.opendaylight.protocol.pcep.PCEPSession;
 import org.opendaylight.protocol.pcep.TerminationReason;
 import org.opendaylight.protocol.pcep.impl.spi.Util;
@@ -28,165 +36,161 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObject;
 
 public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
-
     private PCEPSessionImpl session;
 
     @Before
     public void setup() {
-        this.session = new PCEPSessionImpl(this.listener, 0, this.channel, this.openMsg.getOpenMessage().getOpen(),
-            this.openMsg.getOpenMessage().getOpen());
-        this.session.sessionUp();
+        session = new PCEPSessionImpl(listener, 0, channel, openMsg.getOpenMessage().getOpen(),
+            openMsg.getOpenMessage().getOpen());
+        session.sessionUp();
     }
 
     @After
     public void tearDown() {
-        this.session.close();
+        session.close();
     }
 
     @Test
     public void testPcepSessionImpl() {
-        Assert.assertTrue(this.listener.up);
-
-        this.session.handleMessage(this.kaMsg);
-        Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
-
-        this.session.handleMessage(new PcreqBuilder().build());
-        Assert.assertEquals(2, this.session.getMessages().getReceivedMsgCount().intValue());
-        Assert.assertEquals(1, this.listener.messages.size());
-        Assert.assertTrue(this.listener.messages.get(0) instanceof Pcreq);
-        Assert.assertEquals(2, this.session.getMessages().getReceivedMsgCount().intValue());
-
-        this.session.handleMessage(this.closeMsg);
-        Assert.assertEquals(3, this.session.getMessages().getReceivedMsgCount().intValue());
-        Assert.assertEquals(1, this.listener.messages.size());
-        Assert.assertTrue(this.channel.isActive());
-        Mockito.verify(this.channel, Mockito.times(1)).close();
+        assertTrue(listener.up);
+
+        session.handleMessage(kaMsg);
+        assertEquals(1, session.getMessages().getReceivedMsgCount().intValue());
+
+        session.handleMessage(new PcreqBuilder().build());
+        assertEquals(2, session.getMessages().getReceivedMsgCount().intValue());
+        assertEquals(1, listener.messages.size());
+        assertThat(listener.messages.get(0), instanceOf(Pcreq.class));
+        assertEquals(2, session.getMessages().getReceivedMsgCount().intValue());
+
+        session.handleMessage(closeMsg);
+        assertEquals(3, session.getMessages().getReceivedMsgCount().intValue());
+        assertEquals(1, listener.messages.size());
+        assertTrue(channel.isActive());
+        verify(channel).close();
     }
 
     @Test
     public void testAttemptSecondSession() {
-        this.session.handleMessage(this.openMsg);
-        Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
-        Assert.assertEquals(1, this.msgsSend.size());
-        Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
-        final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
-        final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
-        Assert.assertEquals(
-            PCEPErrors.ATTEMPT_2ND_SESSION, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
+        session.handleMessage(openMsg);
+        assertEquals(1, session.getMessages().getReceivedMsgCount().intValue());
+        assertEquals(1, msgsSend.size());
+        final var pcErr = msgsSend.get(0);
+        assertThat(pcErr, instanceOf(Pcerr.class));
+        final ErrorObject errorObj = ((Pcerr) pcErr).getPcerrMessage().getErrors().get(0).getErrorObject();
+        assertEquals(PCEPErrors.ATTEMPT_2ND_SESSION, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
     }
 
     @Test
     public void testClosedByNode() {
-        this.session.handleMessage(this.closeMsg);
-        Mockito.verify(this.channel).close();
+        session.handleMessage(closeMsg);
+        verify(channel).close();
     }
 
     @Test
     public void testCapabilityNotSupported() {
-        this.session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
-        Assert.assertEquals(2, this.msgsSend.size());
-        Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
-        final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
-        final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
-        Assert.assertEquals(
-            PCEPErrors.CAPABILITY_NOT_SUPPORTED, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
-        Assert.assertEquals(1, this.session.getMessages().getUnknownMsgReceived().intValue());
+        session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
+        assertEquals(2, msgsSend.size());
+        final var pcErr = msgsSend.get(0);
+        assertThat(pcErr, instanceOf(Pcerr.class));
+        final ErrorObject errorObj = ((Pcerr) pcErr).getPcerrMessage().getErrors().get(0).getErrorObject();
+        assertEquals(PCEPErrors.CAPABILITY_NOT_SUPPORTED, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
+        assertEquals(1, session.getMessages().getUnknownMsgReceived().intValue());
         // exceeded max. unknown messages count - terminate session
-        Assert.assertTrue(this.msgsSend.get(1) instanceof CloseMessage);
-        final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(1);
-        Assert.assertEquals(TerminationReason.TOO_MANY_UNKNOWN_MSGS,
-            TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
-        Mockito.verify(this.channel, Mockito.times(1)).close();
+        final var closeMsg = msgsSend.get(1);
+        assertThat(closeMsg, instanceOf(CloseMessage.class));
+
+        assertEquals(TerminationReason.TOO_MANY_UNKNOWN_MSGS,
+            TerminationReason.forValue(((CloseMessage) closeMsg).getCCloseMessage().getCClose().getReason()));
+        verify(channel).close();
     }
 
     @Test
     public void testEndoOfInput() {
-        Assert.assertTrue(this.listener.up);
-        this.session.endOfInput();
-        Assert.assertFalse(this.listener.up);
+        assertTrue(listener.up);
+        session.endOfInput();
+        assertFalse(listener.up);
     }
 
     @Test
     public void testCloseSessionWithReason() {
-        this.session.close(TerminationReason.UNKNOWN);
-        Assert.assertEquals(1, this.msgsSend.size());
-        Assert.assertTrue(this.msgsSend.get(0) instanceof CloseMessage);
-        final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(0);
-        Assert.assertEquals(TerminationReason.UNKNOWN,
-            TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
-        Mockito.verify(this.channel, Mockito.times(1)).close();
+        session.close(TerminationReason.UNKNOWN);
+        assertEquals(1, msgsSend.size());
+        final var closeMsg = msgsSend.get(0);
+        assertThat(closeMsg, instanceOf(CloseMessage.class));
+        assertEquals(TerminationReason.UNKNOWN,
+            TerminationReason.forValue(((CloseMessage) closeMsg).getCCloseMessage().getCClose().getReason()));
+        verify(channel).close();
     }
 
     @Test
     public void testSessionStatistics() {
-        this.session.handleMessage(Util.createErrorMessage(PCEPErrors.LSP_RSVP_ERROR, null));
-        Assert.assertEquals(this.ipAddress, this.session.getPeerPref().getIpAddress());
-        final PeerPref peerPref = this.session.getPeerPref();
-        Assert.assertEquals(this.ipAddress, peerPref.getIpAddress());
-        Assert.assertEquals(DEADTIMER, peerPref.getDeadtimer());
-        Assert.assertEquals(KEEP_ALIVE, peerPref.getKeepalive());
-        Assert.assertEquals(0, peerPref.getSessionId().intValue());
-        final LocalPref localPref = this.session.getLocalPref();
-        Assert.assertEquals(this.ipAddress, localPref.getIpAddress());
-        Assert.assertEquals(DEADTIMER, localPref.getDeadtimer());
-        Assert.assertEquals(KEEP_ALIVE, localPref.getKeepalive());
-        Assert.assertEquals(0, localPref.getSessionId().intValue());
-        final Messages msgs = this.session.getMessages();
-        Assert.assertEquals(1, msgs.getReceivedMsgCount().longValue());
-        Assert.assertEquals(0, msgs.getSentMsgCount().longValue());
-        Assert.assertEquals(0, msgs.getUnknownMsgReceived().longValue());
+        session.handleMessage(Util.createErrorMessage(PCEPErrors.LSP_RSVP_ERROR, null));
+        assertEquals(ipAddress, session.getPeerPref().getIpAddress());
+        final PeerPref peerPref = session.getPeerPref();
+        assertEquals(ipAddress, peerPref.getIpAddress());
+        assertEquals(DEADTIMER, peerPref.getDeadtimer());
+        assertEquals(KEEP_ALIVE, peerPref.getKeepalive());
+        assertEquals(0, peerPref.getSessionId().intValue());
+        final LocalPref localPref = session.getLocalPref();
+        assertEquals(ipAddress, localPref.getIpAddress());
+        assertEquals(DEADTIMER, localPref.getDeadtimer());
+        assertEquals(KEEP_ALIVE, localPref.getKeepalive());
+        assertEquals(0, localPref.getSessionId().intValue());
+        final Messages msgs = session.getMessages();
+        assertEquals(1, msgs.getReceivedMsgCount().longValue());
+        assertEquals(0, msgs.getSentMsgCount().longValue());
+        assertEquals(0, msgs.getUnknownMsgReceived().longValue());
         final ErrorMessages errMsgs = msgs.getErrorMessages();
-        Assert.assertEquals(1, errMsgs.getReceivedErrorMsgCount().intValue());
-        Assert.assertEquals(0, errMsgs.getSentErrorMsgCount().intValue());
-        Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorType(), errMsgs.getLastReceivedError().getErrorType());
-        Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorValue(), errMsgs.getLastReceivedError().getErrorValue());
-
-        this.session.sendMessage(Util.createErrorMessage(PCEPErrors.UNKNOWN_PLSP_ID, null));
-        final Messages msgs2 = this.session.getMessages();
-        Assert.assertEquals(1, msgs2.getReceivedMsgCount().longValue());
-        Assert.assertEquals(1, msgs2.getSentMsgCount().longValue());
-        Assert.assertEquals(0, msgs2.getUnknownMsgReceived().longValue());
+        assertEquals(1, errMsgs.getReceivedErrorMsgCount().intValue());
+        assertEquals(0, errMsgs.getSentErrorMsgCount().intValue());
+        assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorType(), errMsgs.getLastReceivedError().getErrorType());
+        assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorValue(), errMsgs.getLastReceivedError().getErrorValue());
+
+        session.sendMessage(Util.createErrorMessage(PCEPErrors.UNKNOWN_PLSP_ID, null));
+        final Messages msgs2 = session.getMessages();
+        assertEquals(1, msgs2.getReceivedMsgCount().longValue());
+        assertEquals(1, msgs2.getSentMsgCount().longValue());
+        assertEquals(0, msgs2.getUnknownMsgReceived().longValue());
         final ErrorMessages errMsgs2 = msgs2.getErrorMessages();
-        Assert.assertEquals(1, errMsgs2.getReceivedErrorMsgCount().intValue());
-        Assert.assertEquals(1, errMsgs2.getSentErrorMsgCount().intValue());
-        Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorType(), errMsgs2.getLastSentError().getErrorType());
-        Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorValue(), errMsgs2.getLastSentError().getErrorValue());
+        assertEquals(1, errMsgs2.getReceivedErrorMsgCount().intValue());
+        assertEquals(1, errMsgs2.getSentErrorMsgCount().intValue());
+        assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorType(), errMsgs2.getLastSentError().getErrorType());
+        assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorValue(), errMsgs2.getLastSentError().getErrorValue());
     }
 
     @Test
     public void testExceptionCaught() {
-        Assert.assertFalse(this.session.isClosed());
-        Assert.assertTrue(this.listener.up);
-        this.session.exceptionCaught(null, new Throwable("PCEP exception."));
-        Assert.assertFalse(this.listener.up);
-        Assert.assertTrue(this.session.isClosed());
+        assertFalse(session.isClosed());
+        assertTrue(listener.up);
+        session.exceptionCaught(null, new Throwable("PCEP exception."));
+        assertFalse(listener.up);
+        assertTrue(session.isClosed());
     }
 
     @Test
-    @SuppressWarnings({"checkstyle:IllegalCatch","checkstyle:EmptyBlock"})
     public void testSessionRecoveryOnException() {
-        this.listener = new SimpleExceptionSessionListener();
-        this.session = Mockito.spy(new PCEPSessionImpl(this.listener, 0, this.channel,
-                this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen()));
-        Mockito.verify(this.session, Mockito.never()).handleException(ArgumentMatchers.any());
-        Mockito.verify(this.session, Mockito.never()).sendMessage(ArgumentMatchers.any());
-        Mockito.verify(this.session, Mockito.never()).closeChannel();
-        try {
-            this.session.sessionUp();
-            Assert.fail();  // expect the exception to be populated
-        } catch (final RuntimeException ignored) {
-        }
-        Assert.assertFalse(this.listener.up);
-        Mockito.verify(this.session).handleException(ArgumentMatchers.any());
-        Mockito.verify(this.session).sendMessage(ArgumentMatchers.any(CloseMessage.class));
-        Mockito.verify(this.session).closeChannel();
+        listener = new SimpleExceptionSessionListener();
+        session = spy(new PCEPSessionImpl(listener, 0, channel, openMsg.getOpenMessage().getOpen(),
+            openMsg.getOpenMessage().getOpen()));
+        verify(session, never()).handleException(any());
+        verify(session, never()).sendMessage(any());
+        verify(session, never()).closeChannel();
+
+        final var ex = assertThrows(UnsupportedOperationException.class, session::sessionUp);
+        assertEquals("Mocked runtime exception.", ex.getMessage());
+
+        assertFalse(listener.up);
+        verify(session).handleException(any());
+        verify(session).sendMessage(any(CloseMessage.class));
+        verify(session).closeChannel();
     }
 
     private static class SimpleExceptionSessionListener extends SimpleSessionListener {
         @Override
         public synchronized void onSessionUp(final PCEPSession session) {
             super.onSessionUp(session);
-            throw new RuntimeException("Mocked runtime exception.");
+            throw new UnsupportedOperationException("Mocked runtime exception.");
         }
     }
 }