Bug-2081: PCEP statistics
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / AbstractPCEPSessionTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.pcep.impl;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15
16 import com.google.common.collect.Lists;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.channel.ChannelHandler;
20 import io.netty.channel.ChannelPipeline;
21 import io.netty.channel.DefaultChannelPromise;
22 import io.netty.channel.EventLoop;
23 import io.netty.util.concurrent.ScheduledFuture;
24 import java.net.InetSocketAddress;
25 import java.net.SocketAddress;
26 import java.util.List;
27 import java.util.concurrent.TimeUnit;
28 import org.junit.Before;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
39 import org.opendaylight.yangtools.yang.binding.Notification;
40
41 public class AbstractPCEPSessionTest {
42
43     protected static final String IP_ADDRESS = "127.0.0.1";
44     protected static final short KEEP_ALIVE = 15;
45     protected static final short DEADTIMER = 40;
46
47     @Mock
48     protected Channel channel;
49
50     @Mock
51     private EventLoop eventLoop;
52
53     @Mock
54     private ScheduledFuture<?> future;
55
56     @Mock
57     private ChannelPipeline pipeline;
58
59     @Mock
60     private SocketAddress address;
61
62     protected final List<Notification> msgsSend = Lists.newArrayList();
63
64     protected Open openMsg;
65
66     protected Keepalive kaMsg;
67
68     protected SimpleSessionListener listener;
69
70     @Before
71     public final void setUp() {
72         MockitoAnnotations.initMocks(this);
73         final ChannelFuture future = new DefaultChannelPromise(this.channel);
74         doAnswer(new Answer<Object>() {
75             @Override
76             public Object answer(final InvocationOnMock invocation) {
77                 final Object[] args = invocation.getArguments();
78                 AbstractPCEPSessionTest.this.msgsSend.add((Notification) args[0]);
79                 return future;
80             }
81         }).when(this.channel).writeAndFlush(any(Notification.class));
82         doReturn("TestingChannel").when(this.channel).toString();
83         doReturn(this.pipeline).when(this.channel).pipeline();
84         doReturn(this.address).when(this.channel).localAddress();
85         doReturn(this.address).when(this.channel).remoteAddress();
86         doReturn(this.eventLoop).when(this.channel).eventLoop();
87         doReturn(true).when(this.future).cancel(false);
88         doReturn(this.future).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
89         doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
90         doReturn(true).when(this.channel).isActive();
91         doReturn(mock(ChannelFuture.class)).when(this.channel).close();
92         doReturn(new InetSocketAddress(IP_ADDRESS, 4189)).when(this.channel).remoteAddress();
93         doReturn(new InetSocketAddress(IP_ADDRESS, 4189)).when(this.channel).localAddress();
94         this.openMsg = new OpenBuilder().setOpenMessage(
95                 new OpenMessageBuilder().setOpen(
96                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer(
97                                 DEADTIMER).setKeepalive(KEEP_ALIVE).setSessionId((short) 0).build()).build()).build();
98         this.kaMsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
99
100         this.listener = new SimpleSessionListener();
101     }
102
103 }