Bump versions to 0.21.7-SNAPSHOT
[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 package org.opendaylight.protocol.pcep.impl;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doAnswer;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelHandler;
18 import io.netty.channel.ChannelPipeline;
19 import io.netty.channel.DefaultChannelPromise;
20 import io.netty.channel.EventLoop;
21 import io.netty.util.concurrent.GenericFutureListener;
22 import io.netty.util.concurrent.ScheduledFuture;
23 import java.net.InetSocketAddress;
24 import java.net.SocketAddress;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.TimeUnit;
28 import org.junit.Before;
29 import org.mockito.Mock;
30 import org.opendaylight.protocol.util.InetSocketAddressUtil;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Close;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.CloseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Keepalive;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.KeepaliveBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Open;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.OpenBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Starttls;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.StarttlsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.message.CCloseMessageBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CCloseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.keepalive.message.KeepaliveMessageBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.message.OpenMessageBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.start.tls.message.StartTlsMessageBuilder;
44 import org.opendaylight.yangtools.yang.binding.Notification;
45 import org.opendaylight.yangtools.yang.common.Uint8;
46
47 public class AbstractPCEPSessionTest {
48     protected static final Uint8 KEEP_ALIVE = Uint8.valueOf(15);
49     protected static final Uint8 DEADTIMER = Uint8.valueOf(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 = new ArrayList<>();
72
73     protected Open openMsg;
74
75     protected Close closeMsg;
76
77     protected Starttls startTlsMsg;
78
79     protected Keepalive kaMsg;
80
81     protected SimpleSessionListener listener;
82
83     @SuppressWarnings("unchecked")
84     @Before
85     public final void setUp() {
86         final ChannelFuture cfuture = new DefaultChannelPromise(channel);
87         doAnswer(invocation -> {
88             final Object[] args = invocation.getArguments();
89             AbstractPCEPSessionTest.this.msgsSend.add((Notification<?>) args[0]);
90             return cfuture;
91         }).when(channel).writeAndFlush(any(Notification.class));
92         doReturn(channelFuture).when(channel).closeFuture();
93         doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
94         doReturn("TestingChannel").when(channel).toString();
95         doReturn(pipeline).when(channel).pipeline();
96         doReturn(address).when(channel).localAddress();
97         doReturn(address).when(channel).remoteAddress();
98         doReturn(eventLoop).when(channel).eventLoop();
99         doReturn(true).when(future).cancel(false);
100         doReturn(future).when(eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
101         doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), any(String.class),
102             any(ChannelHandler.class));
103         doReturn(pipeline).when(pipeline).addFirst(any(ChannelHandler.class));
104         doReturn(true).when(channel).isActive();
105         doReturn(mock(ChannelFuture.class)).when(channel).close();
106         doReturn(new InetSocketAddress(ipAddress, port)).when(channel).remoteAddress();
107         doReturn(new InetSocketAddress(ipAddress, port)).when(channel).localAddress();
108         openMsg = new OpenBuilder()
109                 .setOpenMessage(new OpenMessageBuilder()
110                     .setOpen(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
111                         .open.object.OpenBuilder()
112                         .setDeadTimer(DEADTIMER)
113                         .setKeepalive(KEEP_ALIVE)
114                         .setSessionId(Uint8.ZERO)
115                         .build())
116                     .build())
117                 .build();
118         kaMsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
119         startTlsMsg = new StarttlsBuilder().setStartTlsMessage(new StartTlsMessageBuilder().build()).build();
120         closeMsg = new CloseBuilder().setCCloseMessage(new CCloseMessageBuilder()
121             .setCClose(new CCloseBuilder().setReason(Uint8.valueOf(6)).build()).build()).build();
122
123         listener = new SimpleSessionListener();
124     }
125
126 }