Code Clean Up
[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.GenericFutureListener;
24 import io.netty.util.concurrent.ScheduledFuture;
25 import java.net.InetSocketAddress;
26 import java.net.SocketAddress;
27 import java.util.List;
28 import java.util.concurrent.TimeUnit;
29 import org.junit.Before;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
34 import org.opendaylight.protocol.util.InetSocketAddressUtil;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Starttls;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.StarttlsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.start.tls.message.StartTlsMessageBuilder;
44 import org.opendaylight.yangtools.yang.binding.Notification;
45
46 public class AbstractPCEPSessionTest {
47
48     protected static final short KEEP_ALIVE = 15;
49     protected static final short DEADTIMER = 40;
50
51     @Mock
52     protected Channel channel;
53
54     @Mock
55     private ChannelFuture channelFuture;
56
57     @Mock
58     private EventLoop eventLoop;
59
60     @Mock
61     private ScheduledFuture<?> future;
62
63     @Mock
64     private ChannelPipeline pipeline;
65
66     @Mock
67     private SocketAddress address;
68
69     protected final String ipAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
70     protected final int port = InetSocketAddressUtil.getRandomPort();
71     protected final List<Notification> msgsSend = Lists.newArrayList();
72
73     protected Open openMsg;
74
75     protected Starttls startTlsMsg;
76
77     protected Keepalive kaMsg;
78
79     protected SimpleSessionListener listener;
80
81     @Before
82     public final void setUp() {
83         MockitoAnnotations.initMocks(this);
84         final ChannelFuture future = new DefaultChannelPromise(this.channel);
85         doAnswer(invocation -> {
86             final Object[] args = invocation.getArguments();
87             AbstractPCEPSessionTest.this.msgsSend.add((Notification) args[0]);
88             return future;
89         }).when(this.channel).writeAndFlush(any(Notification.class));
90         doReturn(this.channelFuture).when(this.channel).closeFuture();
91         doReturn(this.channelFuture).when(this.channelFuture).addListener(any(GenericFutureListener.class));
92         doReturn("TestingChannel").when(this.channel).toString();
93         doReturn(this.pipeline).when(this.channel).pipeline();
94         doReturn(this.address).when(this.channel).localAddress();
95         doReturn(this.address).when(this.channel).remoteAddress();
96         doReturn(this.eventLoop).when(this.channel).eventLoop();
97         doReturn(true).when(this.future).cancel(false);
98         doReturn(this.future).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
99         doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
100         doReturn(this.pipeline).when(this.pipeline).addFirst(any(ChannelHandler.class));
101         doReturn(true).when(this.channel).isActive();
102         doReturn(mock(ChannelFuture.class)).when(this.channel).close();
103         doReturn(new InetSocketAddress(ipAddress, port)).when(this.channel).remoteAddress();
104         doReturn(new InetSocketAddress(ipAddress, port)).when(this.channel).localAddress();
105         this.openMsg = new OpenBuilder().setOpenMessage(
106                 new OpenMessageBuilder().setOpen(
107                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer(
108                                 DEADTIMER).setKeepalive(KEEP_ALIVE).setSessionId((short) 0).build()).build()).build();
109         this.kaMsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
110         this.startTlsMsg = new StarttlsBuilder().setStartTlsMessage(new StartTlsMessageBuilder().build()).build();
111
112
113         this.listener = new SimpleSessionListener();
114     }
115
116 }