BUG 5634 : Implement concurrent message limit
[netconf.git] / netconf / netconf-topology / src / test / java / org / opendaylight / netconf / topology / pipeline / ClusteredNetconfDeviceCommunicatorTest.java
1 /*
2  * Copyright (c) 2016 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.netconf.topology.pipeline;
10
11 import static org.mockito.Matchers.eq;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15
16 import java.net.InetSocketAddress;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.opendaylight.netconf.api.NetconfTerminationReason;
24 import org.opendaylight.netconf.client.NetconfClientSession;
25 import org.opendaylight.netconf.client.NetconfClientSessionListener;
26 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
27
28 public class ClusteredNetconfDeviceCommunicatorTest {
29
30     private static final RemoteDeviceId REMOTE_DEVICE_ID = new RemoteDeviceId("testing-device", new InetSocketAddress(9999));
31
32     @Mock
33     private ClusteredNetconfDevice remoteDevice;
34
35     @Mock
36     private EntityOwnershipService ownershipService;
37
38     @Mock
39     private NetconfClientSession session;
40
41     @Mock
42     private NetconfClientSessionListener listener1;
43
44     @Mock
45     private NetconfClientSessionListener listener2;
46
47     @Mock
48     private EntityOwnershipListenerRegistration ownershipListenerRegistration;
49
50     private ClusteredNetconfDeviceCommunicator communicator;
51
52
53     @Before
54     public void setUp() throws Exception {
55         MockitoAnnotations.initMocks(this);
56
57         doReturn(ownershipListenerRegistration).when(ownershipService).registerListener(
58                 "netconf-node/" + REMOTE_DEVICE_ID.getName(), remoteDevice);
59
60         communicator = new ClusteredNetconfDeviceCommunicator(REMOTE_DEVICE_ID, remoteDevice, ownershipService, 10);
61         communicator.registerNetconfClientSessionListener(listener1);
62         communicator.registerNetconfClientSessionListener(listener2);
63     }
64
65     @Test
66     public void testOnSessionDown() {
67         communicator.onSessionUp(session);
68
69         Exception exception = mock(Exception.class);
70         communicator.onSessionDown(session, exception);
71
72         verify(ownershipListenerRegistration).close();
73
74         verify(listener1).onSessionDown(eq(session), eq(exception));
75         verify(listener2).onSessionDown(eq(session), eq(exception));
76     }
77
78     @Test
79     public void testOnSessionUp() {
80         communicator.onSessionUp(session);
81
82         verify(ownershipService).registerListener("netconf-node/" + REMOTE_DEVICE_ID.getName(), remoteDevice);
83
84         verify(listener1).onSessionUp(eq(session));
85         verify(listener2).onSessionUp(eq(session));
86     }
87
88     @Test
89     public void testOnSessionTerminated() {
90         communicator.onSessionUp(session);
91
92         NetconfTerminationReason reason = mock(NetconfTerminationReason.class);
93         communicator.onSessionTerminated(session, reason);
94
95         verify(ownershipListenerRegistration).close();
96
97         verify(listener1).onSessionTerminated(eq(session), eq(reason));
98         verify(listener2).onSessionTerminated(eq(session), eq(reason));
99     }
100 }