Cleanup eclipse warnings
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionImplTest.java
index 02b816518a75280f470cf021b9dfe1a6482d23c0..0245ce31450156de3d34d8d8d4807cd5912d3305 100644 (file)
@@ -13,6 +13,7 @@ import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkIdleState;
 
 import com.google.common.collect.Lists;
 import io.netty.channel.Channel;
@@ -21,8 +22,6 @@ import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelPipeline;
 import io.netty.channel.EventLoop;
 import io.netty.channel.embedded.EmbeddedChannel;
-import io.netty.util.concurrent.Future;
-import io.netty.util.concurrent.GenericFutureListener;
 import io.netty.util.concurrent.GlobalEventExecutor;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
@@ -36,15 +35,14 @@ import org.mockito.Matchers;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.protocol.bgp.rib.spi.State;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
@@ -113,104 +111,104 @@ public class BGPSessionImplTest {
         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capa).build());
 
         final ChannelFuture f = mock(ChannelFuture.class);
-        doReturn(null).when(f).addListener(Mockito.<GenericFutureListener<? extends Future<? super Void>>>any());
-
-        doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(final InvocationOnMock invocation) {
-                final Object[] args = invocation.getArguments();
-                BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
-                return f;
-            }
+        doReturn(null).when(f).addListener(Mockito.any());
+
+        doAnswer(invocation -> {
+            final Object[] args = invocation.getArguments();
+            BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
+            return f;
         }).when(this.speakerListener).writeAndFlush(Mockito.any(Notification.class));
         doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
         doReturn(true).when(this.speakerListener).isActive();
-        doAnswer(new Answer<Void>() {
-            @Override
-            public Void answer(final InvocationOnMock invocation) throws Throwable {
-                final Runnable command = (Runnable) invocation.getArguments()[0];
-                final long delay = (long) invocation.getArguments()[1];
-                final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
-                GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
-                return null;
-            }
+        doAnswer(invocation -> {
+            final Runnable command = (Runnable) invocation.getArguments()[0];
+            final long delay = (long) invocation.getArguments()[1];
+            final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
+            GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
+            return null;
         }).when(this.eventLoop).schedule(Mockito.any(Runnable.class), Mockito.any(long.class), Mockito.any(TimeUnit.class));
         doReturn("TestingChannel").when(this.speakerListener).toString();
+        doReturn(true).when(this.speakerListener).isWritable();
         doReturn(new InetSocketAddress(InetAddress.getByName(BGP_ID.getValue()), 179)).when(this.speakerListener).remoteAddress();
         doReturn(new InetSocketAddress(InetAddress.getByName(LOCAL_IP), LOCAL_PORT)).when(this.speakerListener).localAddress();
         doReturn(this.pipeline).when(this.speakerListener).pipeline();
         doReturn(this.pipeline).when(this.pipeline).replace(Mockito.any(ChannelHandler.class), Mockito.any(String.class), Mockito.any(ChannelHandler.class));
         doReturn(null).when(this.pipeline).replace(Matchers.<Class<ChannelHandler>>any(), Mockito.any(String.class), Mockito.any(ChannelHandler.class));
         doReturn(this.pipeline).when(this.pipeline).addLast(Mockito.any(ChannelHandler.class));
-        doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
+        final ChannelFuture futureChannel = mock(ChannelFuture.class);
+        doReturn(null).when(futureChannel).addListener(Mockito.any());
+        doReturn(futureChannel).when(this.speakerListener).close();
         this.listener = new SimpleSessionListener();
         this.bgpSession = new BGPSessionImpl(this.listener, this.speakerListener, this.classicOpen, this.classicOpen.getHoldTimer(), null);
+        this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
     }
 
     @Test
     public void testBGPSession() throws BGPDocumentedException {
         this.bgpSession.sessionUp();
-        assertEquals(BGPSessionImpl.State.UP, this.bgpSession.getState());
+        assertEquals(State.UP, this.bgpSession.getState());
         assertEquals(AS_NUMBER, this.bgpSession.getAsNumber());
         assertEquals(BGP_ID, this.bgpSession.getBgpId());
         assertEquals(1, this.bgpSession.getAdvertisedTableTypes().size());
-        assertTrue(this.listener.up);
+        Assert.assertEquals(State.UP, this.listener.getState());
+
         //test stats
-        final BgpSessionState state = this.bgpSession.getBgpSesionState();
+        final BgpSessionState state = this.bgpSession.getBgpSessionState();
         assertEquals(HOLD_TIMER, state.getHoldtimeCurrent().intValue());
         assertEquals(1, state.getKeepaliveCurrent().intValue());
-        assertEquals(BGPSessionImpl.State.UP.name(), state.getSessionState());
-        assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getAddress());
-        assertEquals(AS_NUMBER.getValue(), state.getPeerPreferences().getAs());
-        assertTrue(state.getPeerPreferences().getBgpExtendedMessageCapability());
-        assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getBgpId());
-        assertEquals(1, state.getPeerPreferences().getAdvertizedTableTypes().size());
-        assertEquals(HOLD_TIMER, state.getPeerPreferences().getHoldtime().intValue());
-        assertTrue(state.getPeerPreferences().getFourOctetAsCapability().booleanValue());
-        assertTrue(state.getPeerPreferences().getBgpExtendedMessageCapability().booleanValue());
-        assertTrue(state.getPeerPreferences().getGrCapability());
-        assertEquals(LOCAL_IP, state.getSpeakerPreferences().getAddress());
-        assertEquals(LOCAL_PORT, state.getSpeakerPreferences().getPort().intValue());
-        assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
-        assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
+        assertEquals(State.UP.name(), state.getSessionState());
+        assertEquals(BGP_ID.getValue(), new String(state.getLocalPeerPreferences().getHost().getValue()));
+        assertEquals(AS_NUMBER.getValue(), state.getLocalPeerPreferences().getAs().getValue());
+        assertTrue(state.getLocalPeerPreferences().getBgpExtendedMessageCapability());
+        assertEquals(BGP_ID.getValue(), state.getLocalPeerPreferences().getBgpId().getValue());
+        assertEquals(1, state.getLocalPeerPreferences().getAdvertizedTableTypes().size());
+        assertEquals(HOLD_TIMER, state.getLocalPeerPreferences().getHoldtimer().intValue());
+        assertTrue(state.getLocalPeerPreferences().getFourOctetAsCapability().booleanValue());
+        assertTrue(state.getLocalPeerPreferences().getBgpExtendedMessageCapability().booleanValue());
+        assertTrue(state.getLocalPeerPreferences().getGrCapability());
+        assertEquals(LOCAL_IP, new String(state.getRemotePeerPreferences().getHost().getValue()));
+        assertEquals(LOCAL_PORT, state.getRemotePeerPreferences().getPort().getValue().intValue());
+        assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
+        assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().getValue().longValue());
 
         this.bgpSession.handleMessage(new UpdateBuilder().build());
         assertEquals(1, this.listener.getListMsg().size());
         assertTrue(this.listener.getListMsg().get(0) instanceof Update);
-        assertEquals(1, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
-        assertEquals(1, state.getMessagesStats().getUpdateMsgs().getReceived().getCount().longValue());
-        assertEquals(0, state.getMessagesStats().getUpdateMsgs().getSent().getCount().longValue());
+        assertEquals(1, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
+        assertEquals(1, state.getMessagesStats().getUpdateMsgs().getReceived().getCount().getValue().longValue());
+        assertEquals(0, state.getMessagesStats().getUpdateMsgs().getSent().getCount().getValue().longValue());
 
         this.bgpSession.handleMessage(new KeepaliveBuilder().build());
         this.bgpSession.handleMessage(new KeepaliveBuilder().build());
-        assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
-        assertEquals(2, state.getMessagesStats().getKeepAliveMsgs().getReceived().getCount().longValue());
-        assertEquals(0, state.getMessagesStats().getKeepAliveMsgs().getSent().getCount().longValue());
+        assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
+        assertEquals(2, state.getMessagesStats().getKeepAliveMsgs().getReceived().getCount().getValue().longValue());
+        assertEquals(0, state.getMessagesStats().getKeepAliveMsgs().getSent().getCount().getValue().longValue());
 
         this.bgpSession.close();
-        assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
+        assertEquals(State.IDLE, this.bgpSession.getState());
         assertEquals(1, this.receivedMsgs.size());
         assertTrue(this.receivedMsgs.get(0) instanceof Notify);
         final Notify error = (Notify) this.receivedMsgs.get(0);
         assertEquals(BGPError.CEASE.getCode(), error.getErrorCode().shortValue());
         assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode().shortValue());
         Mockito.verify(this.speakerListener).close();
-        assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
-        assertEquals(1, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
-        assertEquals(1, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
-        assertEquals(BGPError.CEASE.getCode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getCode().shortValue());
-        assertEquals(BGPError.CEASE.getSubcode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getSubCode().shortValue());
-
-        this.bgpSession.resetSessionStats();
-        assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
-        assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
-        assertEquals(0, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
+        assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
+        assertEquals(1, state.getMessagesStats().getTotalMsgs().getSent().getCount().getValue().longValue());
+        assertEquals(1, state.getMessagesStats().getErrorMsgs().getErrorSentTotal().getCount().getValue().longValue());
+        assertEquals(1, state.getMessagesStats().getErrorMsgs().getErrorSent().get(0).getCount().getValue().longValue());
+        assertEquals(BGPError.CEASE.getCode(), state.getMessagesStats().getErrorMsgs().getErrorSent().get(0).getErrorCode().shortValue());
+        assertEquals(BGPError.CEASE.getSubcode(), state.getMessagesStats().getErrorMsgs().getErrorSent().get(0).getErrorSubcode().shortValue());
+
+        this.bgpSession.resetBgpSessionStats();
+        assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
+        assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().getValue().longValue());
+        assertEquals(0, state.getMessagesStats().getErrorMsgs().getErrorSentTotal().getCount().getValue().longValue());
     }
 
     @Test
     public void testHandleOpenMsg() throws BGPDocumentedException {
         this.bgpSession.handleMessage(this.classicOpen);
-        Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
+        Assert.assertEquals(State.IDLE, this.bgpSession.getState());
         Assert.assertEquals(1, this.receivedMsgs.size());
         Assert.assertTrue(this.receivedMsgs.get(0) instanceof Notify);
         final Notify error = (Notify) this.receivedMsgs.get(0);
@@ -222,26 +220,26 @@ public class BGPSessionImplTest {
     @Test
     public void testHandleNotifyMsg() throws BGPDocumentedException {
         this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode()).setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
-        assertEquals(1, this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCount().longValue());
-        assertEquals(BGPError.BAD_BGP_ID.getCode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCode().shortValue());
-        assertEquals(BGPError.BAD_BGP_ID.getSubcode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getSubCode().shortValue());
-        Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
+        assertEquals(1, this.bgpSession.getBgpSessionState().getMessagesStats().getErrorMsgs().getErrorReceivedTotal().getCount().getValue().longValue());
+        assertEquals(1, this.bgpSession.getBgpSessionState().getMessagesStats().getErrorMsgs().getErrorReceived().get(0).getCount().getValue().longValue());
+        assertEquals(BGPError.BAD_BGP_ID.getCode(), this.bgpSession.getBgpSessionState().getMessagesStats().getErrorMsgs().getErrorReceived().get(0).getErrorCode().shortValue());
+        assertEquals(BGPError.BAD_BGP_ID.getSubcode(), this.bgpSession.getBgpSessionState().getMessagesStats().getErrorMsgs().getErrorReceived().get(0).getErrorSubcode().shortValue());
+        Assert.assertEquals(State.IDLE, this.bgpSession.getState());
         Mockito.verify(this.speakerListener).close();
     }
 
     @Test
-    public void testEndOfInput() {
+    public void testEndOfInput() throws InterruptedException {
         this.bgpSession.sessionUp();
-        Assert.assertFalse(this.listener.down);
+        Assert.assertEquals(State.UP, this.listener.getState());
         this.bgpSession.endOfInput();
-        Assert.assertTrue(this.listener.down);
+        checkIdleState(this.listener);
     }
 
     @Test
     public void testHoldTimerExpire() throws InterruptedException {
         this.bgpSession.sessionUp();
-        Thread.sleep(3500);
-        Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
+        checkIdleState(this.listener);
         Assert.assertEquals(3, this.receivedMsgs.size());
         Assert.assertTrue(this.receivedMsgs.get(2) instanceof Notify);
         final Notify error = (Notify) this.receivedMsgs.get(2);