Refactor NETCONF node defaults
[netconf.git] / netconf / callhome-provider / src / test / java / org / opendaylight / netconf / callhome / mount / CallHomeMountSessionContextTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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.netconf.callhome.mount;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.net.InetAddresses;
19 import java.net.Inet4Address;
20 import java.net.InetSocketAddress;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.netconf.api.NetconfMessage;
24 import org.opendaylight.netconf.api.NetconfTerminationReason;
25 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
26 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
27 import org.opendaylight.netconf.callhome.protocol.TransportType;
28 import org.opendaylight.netconf.client.NetconfClientSession;
29 import org.opendaylight.netconf.client.NetconfClientSessionListener;
30
31 public class CallHomeMountSessionContextTest {
32     private Inet4Address someAddressIpv4;
33     private InetSocketAddress someSocketAddress;
34     private CallHomeChannelActivator mockActivator;
35     private CallHomeMountSessionContext.CloseCallback mockCallback;
36     private CallHomeMountSessionContext instance;
37     private CallHomeProtocolSessionContext mockProtocol;
38
39     @Before
40     public void setup() {
41         someAddressIpv4 = (Inet4Address) InetAddresses.forString("1.2.3.4");
42         someSocketAddress = new InetSocketAddress(someAddressIpv4, 123);
43
44         mockProtocol = mock(CallHomeProtocolSessionContext.class);
45         mockActivator = mock(CallHomeChannelActivator.class);
46         mockCallback = mock(CallHomeMountSessionContext.CloseCallback.class);
47         doReturn(someSocketAddress).when(mockProtocol).getRemoteAddress();
48         doReturn(TransportType.SSH).when(mockProtocol).getTransportType();
49
50         instance = new CallHomeMountSessionContext("test",mockProtocol, mockActivator, mockCallback);
51     }
52
53     @Test
54     public void configNodeCanBeCreated() {
55         assertNotNull(instance.getConfigNode());
56     }
57
58     @Test
59     public void activationOfListenerSupportsSessionUp() {
60         // given
61         when(mockActivator.activate(any(NetconfClientSessionListener.class)))
62             .thenAnswer(invocationOnMock -> {
63                 NetconfClientSession mockSession = mock(NetconfClientSession.class);
64
65                 Object arg = invocationOnMock.getArguments()[0];
66                 ((NetconfClientSessionListener) arg).onSessionUp(mockSession);
67                 return null;
68             });
69
70         NetconfClientSessionListener mockListener = mock(NetconfClientSessionListener.class);
71         // when
72         mockActivator.activate(mockListener);
73         // then
74         verify(mockListener, times(1)).onSessionUp(any(NetconfClientSession.class));
75     }
76
77     @Test
78     public void activationOfListenerSupportsSessionTermination() {
79         // given
80         when(mockActivator.activate(any(NetconfClientSessionListener.class)))
81                 .thenAnswer(invocationOnMock -> {
82                     NetconfClientSession mockSession = mock(NetconfClientSession.class);
83                     NetconfTerminationReason mockReason = mock(NetconfTerminationReason.class);
84
85                     Object arg = invocationOnMock.getArguments()[0];
86                     ((NetconfClientSessionListener) arg).onSessionTerminated(mockSession, mockReason);
87                     return null;
88                 });
89
90         NetconfClientSessionListener mockListener = mock(NetconfClientSessionListener.class);
91         // when
92         mockActivator.activate(mockListener);
93         // then
94         verify(mockListener, times(1)).onSessionTerminated(any(NetconfClientSession.class),
95                 any(NetconfTerminationReason.class));
96     }
97
98     @Test
99     public void activationOfListenerSupportsSessionDown() {
100         // given
101         when(mockActivator.activate(any(NetconfClientSessionListener.class)))
102                 .thenAnswer(invocationOnMock -> {
103                     NetconfClientSession mockSession = mock(NetconfClientSession.class);
104                     Exception mockException = mock(Exception.class);
105
106                     Object arg = invocationOnMock.getArguments()[0];
107                     ((NetconfClientSessionListener) arg).onSessionDown(mockSession, mockException);
108                     return null;
109                 });
110         // given
111         NetconfClientSessionListener mockListener = mock(NetconfClientSessionListener.class);
112         // when
113         mockActivator.activate(mockListener);
114         // then
115         verify(mockListener, times(1)).onSessionDown(any(NetconfClientSession.class),
116                 any(Exception.class));
117     }
118
119     @Test
120     public void activationOfListenerSupportsSessionMessages() {
121         // given
122         when(mockActivator.activate(any(NetconfClientSessionListener.class)))
123                 .thenAnswer(invocationOnMock -> {
124                     NetconfClientSession mockSession = mock(NetconfClientSession.class);
125                     NetconfMessage mockMsg = mock(NetconfMessage.class);
126
127                     Object arg = invocationOnMock.getArguments()[0];
128                     ((NetconfClientSessionListener) arg).onMessage(mockSession, mockMsg);
129                     return null;
130                 });
131         // given
132         NetconfClientSessionListener mockListener = mock(NetconfClientSessionListener.class);
133         // when
134         mockActivator.activate(mockListener);
135         // then
136         verify(mockListener, times(1)).onMessage(any(NetconfClientSession.class),
137                 any(NetconfMessage.class));
138     }
139 }