X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-client%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fclient%2FNetconfClientSessionNegotiatorTest.java;h=b61073d6744c0599edde7abc2c18ed63dd7d622d;hb=4377c8e7b1ac67c1fe1699adfd13fc10a3509f1f;hp=0b9bba0f6f76d9abc61bdbf33220bfe4efdfb66e;hpb=1c2c99dd2be77e78114c5b0eff0275734e329ec2;p=netconf.git diff --git a/netconf/netconf-client/src/test/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiatorTest.java b/netconf/netconf-client/src/test/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiatorTest.java index 0b9bba0f6f..b61073d674 100644 --- a/netconf/netconf-client/src/test/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiatorTest.java +++ b/netconf/netconf-client/src/test/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiatorTest.java @@ -5,22 +5,19 @@ * 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 junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Matchers.anyString; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; 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; @@ -30,22 +27,22 @@ import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelProgressivePromise; import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; +import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.ssl.SslHandler; import io.netty.util.HashedWheelTimer; 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.controller.config.util.xml.XmlUtil; -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; import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder; +import org.opendaylight.netconf.nettyutil.handler.NetconfEXIToMessageDecoder; import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder; import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder; import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters; @@ -55,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; @@ -63,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(); @@ -82,8 +78,8 @@ public class NetconfClientSessionNegotiatorTest { doReturn("").when(ret).toString(); doReturn(future).when(ret).newPromise(); doReturn(future).when(ret).close(); - doReturn(future).when(ret).writeAndFlush(anyObject()); - doReturn(future).when(ret).writeAndFlush(anyObject(), anyObject()); + doReturn(future).when(ret).writeAndFlush(any()); + doReturn(future).when(ret).writeAndFlush(any(), any()); doReturn(true).when(ret).isOpen(); doReturn(pipeline).when(ret).pipeline(); doReturn("").when(pipeline).toString(); @@ -107,10 +103,14 @@ public class NetconfClientSessionNegotiatorTest { doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class)); NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder(); - doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(), any(NetconfXMLToMessageDecoder - .class)); - doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(), any(NetconfClientSession - .class)); + doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(), + any(NetconfXMLToMessageDecoder.class)); + doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(), + any(NetconfClientSession.class)); + doReturn(null).when(pipeline).replace(anyString(), anyString(), + any(MessageToByteEncoder.class)); + doReturn(null).when(pipeline).replace(anyString(), anyString(), + any(NetconfEXIToMessageDecoder.class)); return pipeline; } @@ -118,22 +118,22 @@ public class NetconfClientSessionNegotiatorTest { final EventLoop eventLoop = mock(EventLoop.class); doReturn(eventLoop).when(channel).eventLoop(); doAnswer(invocation -> { - invocation.getArgumentAt(0, Runnable.class).run(); + invocation.getArgument(0).run(); return null; }).when(eventLoop).execute(any(Runnable.class)); } private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator( final Promise 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 { @@ -150,41 +150,49 @@ public class NetconfClientSessionNegotiatorTest { } @Test - public void testNetconfClientSessionNegotiator() throws NetconfDocumentedException { + public void testNetconfClientSessionNegotiator() throws Exception { + Promise promise = mock(Promise.class); + doReturn(promise).when(promise).setSuccess(any()); + NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null); + + negotiator.channelActive(null); + negotiator.handleMessage(NetconfHelloMessage.createServerHello(Set.of("a", "b"), 10)); + verify(promise).setSuccess(any()); + } + + @Test + public void testNegotiatorWhenChannelActiveHappenAfterHandleMessage() throws Exception { Promise promise = mock(Promise.class); - doReturn(promise).when(promise).setSuccess(anyObject()); + doReturn(false).when(promise).isDone(); + doReturn(promise).when(promise).setSuccess(any()); NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null); + negotiator.handleMessage(NetconfHelloMessage.createServerHello(Set.of("a", "b"), 10)); negotiator.channelActive(null); - Set caps = Sets.newSet("a", "b"); - NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10); - negotiator.handleMessage(helloServerMessage); - verify(promise).setSuccess(anyObject()); + verify(promise).setSuccess(any()); } @Test public void testNetconfClientSessionNegotiatorWithEXI() throws Exception { Promise promise = mock(Promise.class); NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(EXIParameters.empty(), "msg-id"); - doReturn(promise).when(promise).setSuccess(anyObject()); + doReturn(promise).when(promise).setSuccess(any()); NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage); negotiator.channelActive(null); - Set caps = Sets.newSet("exi:1.0"); - NetconfHelloMessage message = NetconfHelloMessage.createServerHello(caps, 10); doAnswer(invocationOnMock -> { - channelInboundHandlerAdapter = (ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2]; + channelInboundHandlerAdapter = invocationOnMock.getArgument(2); return null; }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class)); 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)); - verify(promise).setSuccess(anyObject()); + verify(promise).setSuccess(any()); // two calls for exiMessage, 2 for hello message verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class)); @@ -192,8 +200,8 @@ public class NetconfClientSessionNegotiatorTest { @Test public void testNetconfClientSessionNegotiatorGetCached() throws Exception { - Promise promise = mock(Promise.class); - doReturn(promise).when(promise).setSuccess(anyObject()); + Promise promise = mock(Promise.class); + doReturn(promise).when(promise).setSuccess(any()); NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class); NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);