package org.opendaylight.protocol.bmp.mock;
-import static org.opendaylight.protocol.bmp.mock.BmpMockTest.waitFutureComplete;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
+import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
import com.google.common.base.Optional;
import com.google.common.net.InetAddresses;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import java.net.InetSocketAddress;
-import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
-import org.mockito.Mockito;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
+import org.opendaylight.protocol.bmp.impl.session.DefaultBmpSessionFactory;
import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
import org.opendaylight.protocol.util.InetSocketAddressUtil;
public class BmpMockDispatcherTest {
- private final BmpMessageRegistry registry = Mockito.mock(BmpMessageRegistry.class);
- private final BmpSessionFactory sessionFactory = Mockito.mock(BmpSessionFactory.class);
- private final BmpSessionListenerFactory slf = Mockito.mock(BmpSessionListenerFactory.class);
+ private final BmpSessionFactory sessionFactory = new DefaultBmpSessionFactory();
+ private final BmpMockSessionListener sl = new BmpMockSessionListener();
+ @Mock
+ private BmpMessageRegistry registry;
+ @Mock
+ private BmpSessionListenerFactory slf;
+ private BmpMockDispatcher bmpMockDispatcher;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ doReturn(this.sl).when(this.slf).getSessionListener();
+ this.bmpMockDispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory);
+ }
@Test
- public void testCreateClient() throws InterruptedException {
- final BmpMockDispatcher dispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory);
+ public void testCreateClient() throws Exception {
final int port = InetSocketAddressUtil.getRandomPort();
final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
- final BmpDispatcherImpl serverDispatcher = new BmpDispatcherImpl(
+
+ final BmpDispatcherImpl bmpDispatcher = new BmpDispatcherImpl(
new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory);
- final ChannelFuture futureServer = serverDispatcher
+ final ChannelFuture futureServer = bmpDispatcher
.createServer(serverAddr, this.slf, Optional.absent());
- waitFutureComplete(futureServer);
- final ChannelFuture channelFuture = dispatcher.createClient(InetSocketAddressUtil
+ waitFutureSuccess(futureServer);
+
+ final ChannelFuture channelFuture = this.bmpMockDispatcher.createClient(InetSocketAddressUtil
.getRandomLoopbackInetSocketAddress(0), serverAddr);
- waitFutureComplete(channelFuture);
+ waitFutureSuccess(channelFuture);
final Channel channel = channelFuture.sync().channel();
- Assert.assertTrue(channel.isActive());
+ assertTrue(channel.isActive());
+ checkEquals(() -> assertTrue(this.sl.getStatus()));
channel.close();
- serverDispatcher.close();
+ bmpDispatcher.close();
+ checkEquals(() -> assertFalse(this.sl.getStatus()));
+
+ final BmpDispatcherImpl bmpDispatcher2 = new BmpDispatcherImpl(
+ new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory);
+ final ChannelFuture futureServer2 = bmpDispatcher2
+ .createServer(serverAddr, this.slf, Optional.absent());
+ waitFutureSuccess(futureServer2);
+ checkEquals(() -> assertTrue(this.sl.getStatus()));
+
+ bmpDispatcher2.close();
+ this.bmpMockDispatcher.close();
+ checkEquals(() -> assertFalse(this.sl.getStatus()));
}
@Test
- public void testCreateServer() throws InterruptedException {
- final BmpMockDispatcher dispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory);
+ public void testCreateServer() throws Exception {
final int port = InetSocketAddressUtil.getRandomPort();
- final BmpDispatcherImpl serverDispatcher = new BmpDispatcherImpl(
+ final BmpDispatcherImpl bmpDispatcher = new BmpDispatcherImpl(
new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory);
- final ChannelFuture futureServer = dispatcher.createServer(
+ final ChannelFuture futureServer = this.bmpMockDispatcher.createServer(
new InetSocketAddress(InetAddresses.forString("0.0.0.0"), port));
- waitFutureComplete(futureServer);
- final ChannelFuture channelFuture = serverDispatcher.createClient(
+ waitFutureSuccess(futureServer);
+ final ChannelFuture channelFuture = bmpDispatcher.createClient(
InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port), this.slf, Optional.absent());
- waitFutureComplete(channelFuture);
+ waitFutureSuccess(channelFuture);
final Channel channel = channelFuture.sync().channel();
- Assert.assertTrue(channel.isActive());
+ assertTrue(channel.isActive());
+ checkEquals(() -> assertTrue(this.sl.getStatus()));
+ assertTrue(futureServer.channel().isActive());
channel.close();
- serverDispatcher.close();
+
+ bmpDispatcher.close();
+ this.bmpMockDispatcher.close();
+ checkEquals(() -> assertFalse(this.sl.getStatus()));
}
}