Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / netconf-client / src / test / java / org / opendaylight / controller / netconf / client / SimpleNetconfClientSessionListenerTest.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.controller.netconf.client;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.anyObject;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelFuture;
21 import io.netty.util.concurrent.Future;
22 import java.util.Set;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.internal.util.collections.Sets;
26 import org.opendaylight.controller.netconf.api.NetconfMessage;
27 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
28
29 public class SimpleNetconfClientSessionListenerTest {
30
31     private Channel channel;
32     private ChannelFuture channelFuture;
33     Set<String> caps;
34     private NetconfHelloMessage helloMessage;
35     private NetconfMessage message;
36     private NetconfClientSessionListener sessionListener;
37     private NetconfClientSession clientSession;
38
39     @Before
40     public void setUp() throws Exception {
41         channel = mock(Channel.class);
42         channelFuture = mock(ChannelFuture.class);
43         doReturn(channelFuture).when(channel).writeAndFlush(anyObject());
44         caps = Sets.newSet("a", "b");
45         helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
46         message = new NetconfMessage(helloMessage.getDocument());
47         sessionListener = mock(NetconfClientSessionListener.class);
48         clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
49     }
50
51     @Test
52     public void testSessionDown() throws Exception {
53         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
54         Future<NetconfMessage> promise = simpleListener.sendRequest(message);
55         simpleListener.onSessionUp(clientSession);
56         verify(channel, times(1)).writeAndFlush(anyObject());
57
58         simpleListener.onSessionDown(clientSession, new Exception());
59         assertFalse(promise.isSuccess());
60     }
61
62     @Test
63     public void testSendRequest() throws Exception {
64         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
65         Future<NetconfMessage> promise = simpleListener.sendRequest(message);
66         simpleListener.onSessionUp(clientSession);
67         verify(channel, times(1)).writeAndFlush(anyObject());
68
69         simpleListener.sendRequest(message);
70         assertFalse(promise.isSuccess());
71     }
72
73     @Test
74     public void testOnMessage() throws Exception {
75         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
76         Future<NetconfMessage> promise = simpleListener.sendRequest(message);
77         simpleListener.onSessionUp(clientSession);
78         verify(channel, times(1)).writeAndFlush(anyObject());
79
80         simpleListener.onMessage(clientSession, message);
81         assertTrue(promise.isSuccess());
82     }
83 }