Bug-730: Added PCEPSessionImpl test 43/10143/3
authorMilos Fabian <milfabia@cisco.com>
Thu, 21 Aug 2014 15:30:18 +0000 (17:30 +0200)
committerMilos Fabian <milfabia@cisco.com>
Mon, 25 Aug 2014 07:40:25 +0000 (09:40 +0200)
Change-Id: I338bd27ebacf55a6a2507b8aab792501faf1d219
Signed-off-by: Milos Fabian <milfabia@cisco.com>
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/AbstractPCEPSessionTest.java [new file with mode: 0644]
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/FiniteStateMachineTest.java
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImplTest.java [new file with mode: 0644]

diff --git a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/AbstractPCEPSessionTest.java b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/AbstractPCEPSessionTest.java
new file mode 100644 (file)
index 0000000..4eb9f9d
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.protocol.pcep.impl;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+import com.google.common.collect.Lists;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.DefaultChannelPromise;
+import io.netty.channel.EventLoop;
+import io.netty.util.concurrent.ScheduledFuture;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import org.junit.Before;
+import org.mockito.Mock;
+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.pcep.message.rev131007.Keepalive;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
+import org.opendaylight.yangtools.yang.binding.Notification;
+
+public class AbstractPCEPSessionTest {
+
+    @Mock
+    protected Channel channel;
+
+    @Mock
+    private EventLoop eventLoop;
+
+    @Mock
+    private ScheduledFuture<?> future;
+
+    @Mock
+    private ChannelPipeline pipeline;
+
+    @Mock
+    private SocketAddress address;
+
+    protected final List<Notification> msgsSend = Lists.newArrayList();
+
+    protected Open openMsg;
+
+    protected Keepalive kaMsg;
+
+    protected SimpleSessionListener listener;
+
+    @Before
+    public final void setUp() {
+        MockitoAnnotations.initMocks(this);
+        final ChannelFuture future = new DefaultChannelPromise(this.channel);
+        doAnswer(new Answer<Object>() {
+            @Override
+            public Object answer(final InvocationOnMock invocation) {
+                final Object[] args = invocation.getArguments();
+                AbstractPCEPSessionTest.this.msgsSend.add((Notification) args[0]);
+                return future;
+            }
+        }).when(this.channel).writeAndFlush(any(Notification.class));
+        doReturn("TestingChannel").when(this.channel).toString();
+        doReturn(this.pipeline).when(this.channel).pipeline();
+        doReturn(this.address).when(this.channel).localAddress();
+        doReturn(this.address).when(this.channel).remoteAddress();
+        doReturn(this.eventLoop).when(this.channel).eventLoop();
+        doReturn(true).when(this.future).cancel(false);
+        doReturn(this.future).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
+        doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
+        doReturn(true).when(this.channel).isActive();
+        doReturn(mock(ChannelFuture.class)).when(this.channel).close();
+        doReturn(InetSocketAddress.createUnresolved("127.0.0.1", 4189)).when(this.channel).remoteAddress();
+        doReturn(InetSocketAddress.createUnresolved("127.0.0.1", 4189)).when(this.channel).localAddress();
+        this.openMsg = new OpenBuilder().setOpenMessage(
+                new OpenMessageBuilder().setOpen(
+                        new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer(
+                                (short) 45).setKeepalive((short) 15).build()).build()).build();
+        this.kaMsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
+
+        this.listener = new SimpleSessionListener();
+    }
+
+}
index e8b8966419538e0c3c5aae6d411567e3590cedca..d65ee1e1d82e8580422099832608266f809cd0f3 100644 (file)
@@ -9,102 +9,30 @@ package org.opendaylight.protocol.pcep.impl;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 
-import com.google.common.collect.Lists;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelFuture;
-import io.netty.channel.ChannelHandler;
-import io.netty.channel.ChannelPipeline;
-import io.netty.channel.DefaultChannelPromise;
-import io.netty.channel.EventLoop;
 import io.netty.util.concurrent.DefaultPromise;
 import io.netty.util.concurrent.GlobalEventExecutor;
-import io.netty.util.concurrent.ScheduledFuture;
-import java.net.InetSocketAddress;
-import java.net.SocketAddress;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
-public class FiniteStateMachineTest {
+public class FiniteStateMachineTest extends AbstractPCEPSessionTest {
 
     private DefaultPCEPSessionNegotiator serverSession;
 
-    @Mock
-    private Channel clientListener;
-
-    @Mock
-    private EventLoop eventLoop;
-
-    @Mock
-    private ScheduledFuture<?> future;
-
-    @Mock
-    private ChannelPipeline pipeline;
-
-    @Mock
-    private SocketAddress address;
-
-    private final List<Notification> receivedMsgs = Lists.newArrayList();
-
-    private Open openmsg;
-
-    private Keepalive kamsg;
-
     @Before
-    public void setUp() {
-        MockitoAnnotations.initMocks(this);
+    public void setup() {
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open localPrefs = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setKeepalive(
                 (short) 1).build();
-        this.serverSession = new DefaultPCEPSessionNegotiator(new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE), this.clientListener, new SimpleSessionListener(), (short) 1, 20, localPrefs);
-        final ChannelFuture future = new DefaultChannelPromise(this.clientListener);
-        doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(final InvocationOnMock invocation) {
-                final Object[] args = invocation.getArguments();
-                FiniteStateMachineTest.this.receivedMsgs.add((Notification) args[0]);
-                return future;
-            }
-        }).when(this.clientListener).writeAndFlush(any(Notification.class));
-        doReturn("TestingChannel").when(this.clientListener).toString();
-        doReturn(this.pipeline).when(this.clientListener).pipeline();
-        doReturn(this.address).when(this.clientListener).localAddress();
-        doReturn(this.address).when(this.clientListener).remoteAddress();
-        doReturn(this.eventLoop).when(this.clientListener).eventLoop();
-        doReturn(true).when(this.future).cancel(false);
-        doReturn(this.future).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
-        doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
-        doReturn(true).when(this.clientListener).isActive();
-        doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
-        doReturn(InetSocketAddress.createUnresolved("127.0.0.1", 4189)).when(this.clientListener).remoteAddress();
-        doReturn(InetSocketAddress.createUnresolved("127.0.0.1", 4189)).when(this.clientListener).localAddress();
-        this.openmsg = new OpenBuilder().setOpenMessage(
-                new OpenMessageBuilder().setOpen(
-                        new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer(
-                                (short) 45).setKeepalive((short) 15).build()).build()).build();
-        this.kamsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
+        this.serverSession = new DefaultPCEPSessionNegotiator(new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE), this.channel, this.listener, (short) 1, 20, localPrefs);
     }
 
     /**
@@ -116,12 +44,12 @@ public class FiniteStateMachineTest {
     @Test
     public void testSessionCharsAccBoth() throws Exception {
         this.serverSession.channelActive(null);
-        assertEquals(1, this.receivedMsgs.size());
-        assertTrue(this.receivedMsgs.get(0) instanceof Open);
-        this.serverSession.handleMessage(this.openmsg);
-        assertEquals(2, this.receivedMsgs.size());
-        assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
-        this.serverSession.handleMessage(this.kamsg);
+        assertEquals(1, this.msgsSend.size());
+        assertTrue(this.msgsSend.get(0) instanceof Open);
+        this.serverSession.handleMessage(this.openMsg);
+        assertEquals(2, this.msgsSend.size());
+        assertTrue(this.msgsSend.get(1) instanceof Keepalive);
+        this.serverSession.handleMessage(this.kaMsg);
         assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.Finished);
     }
 
@@ -133,16 +61,16 @@ public class FiniteStateMachineTest {
     @Test
     public void testSessionCharsAccMe() throws Exception {
         this.serverSession.channelActive(null);
-        assertEquals(1, this.receivedMsgs.size());
-        assertTrue(this.receivedMsgs.get(0) instanceof Open);
-        final Open remote = (Open) this.receivedMsgs.get(0);
-        this.serverSession.handleMessage(this.openmsg);
-        assertEquals(2, this.receivedMsgs.size());
-        assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
+        assertEquals(1, this.msgsSend.size());
+        assertTrue(this.msgsSend.get(0) instanceof Open);
+        final Open remote = (Open) this.msgsSend.get(0);
+        this.serverSession.handleMessage(this.openMsg);
+        assertEquals(2, this.msgsSend.size());
+        assertTrue(this.msgsSend.get(1) instanceof Keepalive);
         this.serverSession.handleMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR, remote.getOpenMessage().getOpen()));
-        assertEquals(3, this.receivedMsgs.size());
-        assertTrue(this.receivedMsgs.get(2) instanceof Open);
-        this.serverSession.handleMessage(this.kamsg);
+        assertEquals(3, this.msgsSend.size());
+        assertTrue(this.msgsSend.get(2) instanceof Open);
+        this.serverSession.handleMessage(this.kaMsg);
         assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.Finished);
     }
 
@@ -154,10 +82,10 @@ public class FiniteStateMachineTest {
     @Test
     public void testErrorOneOne() throws Exception {
         this.serverSession.channelActive(null);
-        assertEquals(1, this.receivedMsgs.size());
-        assertTrue(this.receivedMsgs.get(0) instanceof Open);
-        this.serverSession.handleMessage(this.kamsg);
-        for (final Notification m : this.receivedMsgs) {
+        assertEquals(1, this.msgsSend.size());
+        assertTrue(this.msgsSend.get(0) instanceof Open);
+        this.serverSession.handleMessage(this.kaMsg);
+        for (final Notification m : this.msgsSend) {
             if (m instanceof Pcerr) {
                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
@@ -174,11 +102,11 @@ public class FiniteStateMachineTest {
     @Test
     public void testErrorOneSeven() throws Exception {
         this.serverSession.channelActive(null);
-        assertEquals(1, this.receivedMsgs.size());
-        assertTrue(this.receivedMsgs.get(0) instanceof Open);
-        this.serverSession.handleMessage(this.openmsg);
+        assertEquals(1, this.msgsSend.size());
+        assertTrue(this.msgsSend.get(0) instanceof Open);
+        this.serverSession.handleMessage(this.openMsg);
         Thread.sleep(1000);
-        for (final Notification m : this.receivedMsgs) {
+        for (final Notification m : this.msgsSend) {
             if (m instanceof Pcerr) {
                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
@@ -198,10 +126,10 @@ public class FiniteStateMachineTest {
     @Ignore
     public void testErrorOneTwo() throws InterruptedException {
         this.serverSession.channelActive(null);
-        assertEquals(1, this.receivedMsgs.size());
-        assertTrue(this.receivedMsgs.get(0) instanceof OpenMessage);
+        assertEquals(1, this.msgsSend.size());
+        assertTrue(this.msgsSend.get(0) instanceof OpenMessage);
         Thread.sleep(60 * 1000);
-        for (final Notification m : this.receivedMsgs) {
+        for (final Notification m : this.msgsSend) {
             if (m instanceof Pcerr) {
                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
@@ -214,7 +142,7 @@ public class FiniteStateMachineTest {
     @Ignore
     public void testUnknownMessage() throws InterruptedException {
         final SimpleSessionListener client = new SimpleSessionListener();
-        final PCEPSessionImpl s = new PCEPSessionImpl(client, 5, this.clientListener, this.openmsg.getOpenMessage().getOpen(), this.openmsg.getOpenMessage().getOpen());
+        final PCEPSessionImpl s = new PCEPSessionImpl(client, 5, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
         assertEquals(1, s.getUnknownMessagesTimes().size());
         Thread.sleep(10000);
diff --git a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImplTest.java b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImplTest.java
new file mode 100644 (file)
index 0000000..4b65334
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.protocol.pcep.impl;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.opendaylight.protocol.pcep.TerminationReason;
+import org.opendaylight.protocol.pcep.spi.PCEPErrors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.CloseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcreq;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcreqBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
+
+public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
+
+    private PCEPSessionImpl session;
+
+    @Before
+    public void setup() {
+        this.session = new PCEPSessionImpl(this.listener, 0, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
+        this.session.sessionUp();
+    }
+
+    @After
+    public void tearDown() {
+        this.session.tearDown();
+    }
+
+    @Test
+    public void testPcepSessionImpl() throws InterruptedException {
+        Assert.assertTrue(this.listener.up);
+
+        Assert.assertEquals(45, this.session.getDeadTimerValue().intValue());
+        Assert.assertEquals(15, this.session.getKeepAliveTimerValue().intValue());
+        this.session.handleMessage(this.kaMsg);
+        Assert.assertEquals(1, this.session.getReceivedMsgCount().intValue());
+
+        this.session.handleMessage(new PcreqBuilder().build());
+        Assert.assertEquals(2, this.session.getReceivedMsgCount().intValue());
+        Assert.assertEquals(1, this.listener.messages.size());
+        Assert.assertTrue(this.listener.messages.get(0) instanceof Pcreq);
+
+        this.session.handleMessage(new CloseBuilder().build());
+        Assert.assertEquals(3, this.session.getReceivedMsgCount().intValue());
+        Assert.assertEquals(1, this.listener.messages.size());
+        Assert.assertTrue(this.channel.isActive());
+        Mockito.verify(this.channel, Mockito.times(1)).close();
+    }
+
+    @Test
+    public void testAttemptSecondSession() {
+        this.session.handleMessage(this.openMsg);
+        Assert.assertEquals(1, this.session.getReceivedMsgCount().intValue());
+        Assert.assertEquals(1, this.msgsSend.size());
+        Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
+        final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
+        final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
+        Assert.assertEquals(PCEPErrors.ATTEMPT_2ND_SESSION, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
+    }
+
+    @Test
+    public void testCapabilityNotSupported() {
+        this.session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
+        Assert.assertEquals(2, this.msgsSend.size());
+        Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
+        final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
+        final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
+        Assert.assertEquals(PCEPErrors.CAPABILITY_NOT_SUPPORTED, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
+        Assert.assertEquals(1, this.session.getUnknownMessagesTimes().size());
+        // exceeded max. unknown messages count - terminate session
+        Assert.assertTrue(this.msgsSend.get(1) instanceof CloseMessage);
+        final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(1);
+        Assert.assertEquals(TerminationReason.TooManyUnknownMsg, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
+        Mockito.verify(this.channel, Mockito.times(1)).close();
+    }
+
+    @Test
+    public void testEndoOfInput() {
+        Assert.assertTrue(this.listener.up);
+        this.session.endOfInput();
+        Assert.assertFalse(this.listener.up);
+    }
+
+    @Test
+    public void voidTestCloseSessionWithReason() {
+        this.session.close(TerminationReason.Unknown);
+        Assert.assertEquals(1, this.msgsSend.size());
+        Assert.assertTrue(this.msgsSend.get(0) instanceof CloseMessage);
+        final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(0);
+        Assert.assertEquals(TerminationReason.Unknown, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
+        Mockito.verify(this.channel, Mockito.times(1)).close();
+    }
+}