Check for early message send failures
[netconf.git] / netconf / netconf-client / src / test / java / org / opendaylight / netconf / client / NetconfClientSessionNegotiatorTest.java
index b25de514c7f8acb8ff43fbea61c8a51c6f98e3aa..dd308eba2ea4d3269e8ac2edfdc2606fe15de516 100644 (file)
@@ -5,7 +5,6 @@
  * 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.client;
 
 import static org.junit.Assert.assertEquals;
@@ -19,7 +18,6 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableSet;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelHandler;
@@ -36,12 +34,10 @@ import io.netty.util.Timer;
 import io.netty.util.concurrent.GenericFutureListener;
 import io.netty.util.concurrent.Promise;
 import java.io.InputStream;
+import java.util.Optional;
 import java.util.Set;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.internal.util.collections.Sets;
-import org.opendaylight.netconf.api.NetconfClientSessionPreferences;
-import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
 import org.opendaylight.netconf.api.xml.XmlUtil;
@@ -56,7 +52,6 @@ import org.opendaylight.netconf.util.test.XmlFileLoader;
 import org.w3c.dom.Document;
 
 public class NetconfClientSessionNegotiatorTest {
-
     private NetconfHelloMessage helloMessage;
     private ChannelPipeline pipeline;
     private ChannelPromise future;
@@ -64,8 +59,8 @@ public class NetconfClientSessionNegotiatorTest {
     private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
 
     @Before
-    public void setUp() throws Exception {
-        helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional.absent());
+    public void setUp() {
+        helloMessage = NetconfHelloMessage.createClientHello(Set.of("exi:1.0"), Optional.empty());
         pipeline = mockChannelPipeline();
         future = mockChannelFuture();
         channel = mockChannel();
@@ -130,15 +125,15 @@ public class NetconfClientSessionNegotiatorTest {
 
     private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(
             final Promise<NetconfClientSession> promise,
-            final NetconfMessage startExi) {
+            final NetconfStartExiMessage startExi) {
         ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
-        NetconfClientSessionPreferences preferences = new NetconfClientSessionPreferences(helloMessage, startExi);
         doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
 
         long timeout = 10L;
         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
         Timer timer = new HashedWheelTimer();
-        return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
+        return new NetconfClientSessionNegotiator(helloMessage, startExi, promise, channel, timer, sessionListener,
+            timeout, 16384);
     }
 
     private static NetconfHelloMessage createHelloMsg(final String name) throws Exception {
@@ -155,34 +150,30 @@ public class NetconfClientSessionNegotiatorTest {
     }
 
     @Test
-    public void testNetconfClientSessionNegotiator() throws NetconfDocumentedException {
+    public void testNetconfClientSessionNegotiator() throws Exception {
         Promise<NetconfClientSession> promise = mock(Promise.class);
         doReturn(promise).when(promise).setSuccess(any());
         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
 
         negotiator.channelActive(null);
-        Set<String> caps = Sets.newSet("a", "b");
-        NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
-        negotiator.handleMessage(helloServerMessage);
+        doReturn(null).when(future).cause();
+        negotiator.handleMessage(NetconfHelloMessage.createServerHello(Set.of("a", "b"), 10));
         verify(promise).setSuccess(any());
     }
 
     @Test
     public void testNegotiatorWhenChannelActiveHappenAfterHandleMessage() throws Exception {
-        Promise promise = mock(Promise.class);
+        Promise<NetconfClientSession> promise = mock(Promise.class);
         doReturn(false).when(promise).isDone();
         doReturn(promise).when(promise).setSuccess(any());
         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
-        Set<String> caps = Sets.newSet("a", "b");
-        NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
 
-        negotiator.handleMessage(helloServerMessage);
+        doReturn(null).when(future).cause();
+        negotiator.handleMessage(NetconfHelloMessage.createServerHello(Set.of("a", "b"), 10));
         negotiator.channelActive(null);
-
         verify(promise).setSuccess(any());
     }
 
-
     @Test
     public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
         Promise<NetconfClientSession> promise = mock(Promise.class);
@@ -190,9 +181,8 @@ public class NetconfClientSessionNegotiatorTest {
         doReturn(promise).when(promise).setSuccess(any());
         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
 
+        doReturn(null).when(future).cause();
         negotiator.channelActive(null);
-        Set<String> caps = Sets.newSet("exi:1.0");
-        NetconfHelloMessage message = NetconfHelloMessage.createServerHello(caps, 10);
 
         doAnswer(invocationOnMock -> {
             channelInboundHandlerAdapter = invocationOnMock.getArgument(2);
@@ -201,7 +191,7 @@ public class NetconfClientSessionNegotiatorTest {
 
         ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
         doReturn(pipeline).when(handlerContext).pipeline();
-        negotiator.handleMessage(message);
+        negotiator.handleMessage(NetconfHelloMessage.createServerHello(Set.of("exi:1.0"), 10));
         Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
         channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
 
@@ -213,7 +203,7 @@ public class NetconfClientSessionNegotiatorTest {
 
     @Test
     public void testNetconfClientSessionNegotiatorGetCached() throws Exception {
-        Promise promise = mock(Promise.class);
+        Promise<NetconfClientSession> promise = mock(Promise.class);
         doReturn(promise).when(promise).setSuccess(any());
         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);