Fix failing unit tests 10/40910/3
authorMilos Fabian <milfabia@cisco.com>
Tue, 28 Jun 2016 06:21:27 +0000 (08:21 +0200)
committerMilos Fabian <milfabia@cisco.com>
Tue, 28 Jun 2016 08:04:50 +0000 (10:04 +0200)
Fixed failing BmpMockSessionTest's unit test. Used mocked channel instead of spying embded channel.
Use immediate reconnecting strategy in BGPDispatcherImplTest

Change-Id: Ibf8ccb13b31be70e3cd2e9c08f667c46bfa58f13
Signed-off-by: Milos Fabian <milfabia@cisco.com>
bgp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockSessionTest.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java

index 349aa8ab7792cc7d580436342c8f34fd52b9b2b8..5f0a350fd2c1c84b1abb91a912225b20952508eb 100644 (file)
@@ -14,15 +14,20 @@ import static org.junit.Assert.assertTrue;
 
 import com.google.common.collect.Lists;
 import com.google.common.net.InetAddresses;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.ChannelOutboundHandlerAdapter;
-import io.netty.channel.ChannelPromise;
-import io.netty.channel.embedded.EmbeddedChannel;
+import io.netty.channel.ChannelPipeline;
+import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.GenericFutureListener;
 import java.net.InetSocketAddress;
 import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.InitiationMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.PeerUp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.RouteMonitoringMessage;
@@ -34,29 +39,37 @@ public class BmpMockSessionTest {
     private static final InetSocketAddress LOCAL_ADDRESS = new InetSocketAddress(InetAddresses.forString("127.0.0.2"), 0);
 
     private ChannelHandlerContext context;
-    private EmbeddedChannel channel;
+    private Channel channel;
     private BmpMockSession session;
+    private List<Notification> messages;
 
     @Before
     public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        this.messages = Lists.newArrayList();
         this.session = new BmpMockSession(1, 1, 1);
-        this.channel = Mockito.spy(new EmbeddedChannel());
+        this.channel = Mockito.mock(Channel.class);
         Mockito.doReturn(REMOTE_ADDRESS).when(this.channel).remoteAddress();
         Mockito.doReturn(LOCAL_ADDRESS).when(this.channel).localAddress();
-        this.channel.pipeline().addLast(this.session);
+        final ChannelPipeline pipeline = Mockito.mock(ChannelPipeline.class);
+        Mockito.doReturn(pipeline).when(this.channel).pipeline();
+        Mockito.doAnswer(new Answer<Void>() {
+            @Override
+            public Void answer(final InvocationOnMock invocation) throws Throwable {
+                messages.add((Notification) invocation.getArguments()[0]);
+                return null;
+            }
+        }).when(this.channel).writeAndFlush(Mockito.any());
+        final ChannelFuture channelFuture = Mockito.mock(ChannelFuture.class);
+        Mockito.doReturn(null).when(channelFuture).addListener(Mockito.<GenericFutureListener<? extends Future<? super Void>>>any());
+        Mockito.doReturn(channelFuture).when(this.channel).closeFuture();
+        Mockito.doReturn(channelFuture).when(this.channel).close();
         this.context = Mockito.mock(ChannelHandlerContext.class);
         Mockito.doReturn(this.channel).when(this.context).channel();
     }
 
     @Test
     public void testBmpMockSession() throws Exception {
-        final List<Notification> messages = Lists.newArrayList();
-        this.channel.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
-            @Override
-            public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
-                messages.add((Notification) msg);
-            }
-        });
         this.session.channelActive(this.context);
 
         assertEquals(REMOTE_ADDRESS.getAddress(), this.session.getRemoteAddress());
index 32aa23a0b1ebff56aea1473de4661ee198322b2e..d00914fb64a88c3e555267505edb4f6807de4646 100644 (file)
@@ -31,6 +31,7 @@ import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionPr
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
+import org.opendaylight.protocol.framework.ReconnectImmediatelyStrategy;
 import org.opendaylight.protocol.framework.ReconnectStrategy;
 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
@@ -124,7 +125,7 @@ public class BGPDispatcherImplTest {
     private static final class ReconnectStrategyFctImpl implements ReconnectStrategyFactory {
         @Override
         public ReconnectStrategy createReconnectStrategy() {
-            return new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, TIMEOUT);
+            return new ReconnectImmediatelyStrategy(GlobalEventExecutor.INSTANCE, TIMEOUT);
         }
 
     }