Merge "Migrate restconf to mockito ArgumentMatchers"
[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
9 package org.opendaylight.netconf.callhome.mount;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.net.InetAddresses;
20 import java.net.Inet4Address;
21 import java.net.InetSocketAddress;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.netconf.api.NetconfMessage;
25 import org.opendaylight.netconf.api.NetconfTerminationReason;
26 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
27 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
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
49         instance = new CallHomeMountSessionContext("test",mockProtocol, mockActivator, mockCallback);
50     }
51
52     @Test
53     public void configNodeCanBeCreated() {
54         assertNotNull(instance.getConfigNode());
55     }
56
57     @Test
58     public void activationOfListenerSupportsSessionUp() {
59         // given
60         when(mockActivator.activate(any(NetconfClientSessionListener.class)))
61             .thenAnswer(invocationOnMock -> {
62                 NetconfClientSession mockSession = mock(NetconfClientSession.class);
63
64                 Object arg = invocationOnMock.getArguments()[0];
65                 ((NetconfClientSessionListener) arg).onSessionUp(mockSession);
66                 return null;
67             });
68
69         NetconfClientSessionListener mockListener = mock(NetconfClientSessionListener.class);
70         // when
71         mockActivator.activate(mockListener);
72         // then
73         verify(mockListener, times(1)).onSessionUp(any(NetconfClientSession.class));
74     }
75
76     @Test
77     public void activationOfListenerSupportsSessionTermination() {
78         // given
79         when(mockActivator.activate(any(NetconfClientSessionListener.class)))
80                 .thenAnswer(invocationOnMock -> {
81                     NetconfClientSession mockSession = mock(NetconfClientSession.class);
82                     NetconfTerminationReason mockReason = mock(NetconfTerminationReason.class);
83
84                     Object arg = invocationOnMock.getArguments()[0];
85                     ((NetconfClientSessionListener) arg).onSessionTerminated(mockSession, mockReason);
86                     return null;
87                 });
88
89         NetconfClientSessionListener mockListener = mock(NetconfClientSessionListener.class);
90         // when
91         mockActivator.activate(mockListener);
92         // then
93         verify(mockListener, times(1)).onSessionTerminated(any(NetconfClientSession.class),
94                 any(NetconfTerminationReason.class));
95     }
96
97     @Test
98     public void activationOfListenerSupportsSessionDown() {
99         // given
100         when(mockActivator.activate(any(NetconfClientSessionListener.class)))
101                 .thenAnswer(invocationOnMock -> {
102                     NetconfClientSession mockSession = mock(NetconfClientSession.class);
103                     Exception mockException = mock(Exception.class);
104
105                     Object arg = invocationOnMock.getArguments()[0];
106                     ((NetconfClientSessionListener) arg).onSessionDown(mockSession, mockException);
107                     return null;
108                 });
109         // given
110         NetconfClientSessionListener mockListener = mock(NetconfClientSessionListener.class);
111         // when
112         mockActivator.activate(mockListener);
113         // then
114         verify(mockListener, times(1)).onSessionDown(any(NetconfClientSession.class),
115                 any(Exception.class));
116     }
117
118     @Test
119     public void activationOfListenerSupportsSessionMessages() {
120         // given
121         when(mockActivator.activate(any(NetconfClientSessionListener.class)))
122                 .thenAnswer(invocationOnMock -> {
123                     NetconfClientSession mockSession = mock(NetconfClientSession.class);
124                     NetconfMessage mockMsg = mock(NetconfMessage.class);
125
126                     Object arg = invocationOnMock.getArguments()[0];
127                     ((NetconfClientSessionListener) arg).onMessage(mockSession, mockMsg);
128                     return null;
129                 });
130         // given
131         NetconfClientSessionListener mockListener = mock(NetconfClientSessionListener.class);
132         // when
133         mockActivator.activate(mockListener);
134         // then
135         verify(mockListener, times(1)).onMessage(any(NetconfClientSession.class),
136                 any(NetconfMessage.class));
137     }
138 }