Move shaded components to third-party/
[netconf.git] / netconf / netconf-console / src / test / java / org / opendaylight / netconf / console / commands / NetconfCommandsImplCallsTest.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.console.commands;
10
11 import static junit.framework.TestCase.assertEquals;
12 import static junit.framework.TestCase.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.isNull;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.netconf.console.api.NetconfCommands;
29 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
30
31 @RunWith(MockitoJUnitRunner.StrictStubs.class)
32 public class NetconfCommandsImplCallsTest {
33
34     @Mock
35     private NetconfCommands netconfCommands;
36
37     @Test
38     public void testConnectDeviceCommand() throws Exception {
39         NetconfConnectDeviceCommand netconfConnectDeviceCommand =
40                 new NetconfConnectDeviceCommand(netconfCommands);
41         netconfConnectDeviceCommand.execute();
42         verify(netconfCommands, times(0)).connectDevice(any(), any());
43
44         netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777", "user",
45             "pass");
46
47         netconfConnectDeviceCommand.execute();
48         verify(netconfCommands, times(1)).connectDevice(any(), any());
49     }
50
51     @Test
52     public void testDisconnectDeviceCommand() throws Exception {
53         NetconfDisconnectDeviceCommand netconfDisconnectDeviceCommand =
54                 new NetconfDisconnectDeviceCommand(netconfCommands);
55         netconfDisconnectDeviceCommand.execute();
56
57         verify(netconfCommands, times(0)).disconnectDevice(any(), any());
58
59         netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands, "deviceId", null, null);
60
61         doReturn(true).when(netconfCommands).disconnectDevice(any());
62         netconfDisconnectDeviceCommand.execute();
63
64         verify(netconfCommands, times(1)).disconnectDevice(any());
65
66         netconfDisconnectDeviceCommand =
67                 new NetconfDisconnectDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
68
69         doReturn(true).when(netconfCommands).disconnectDevice(any(), any());
70         netconfDisconnectDeviceCommand.execute();
71
72         verify(netconfCommands, times(1)).disconnectDevice(any(), any());
73     }
74
75     @Test
76     public void testListDeviceCommand() throws Exception {
77         final NetconfListDevicesCommand netconfListDeviceCommand = new NetconfListDevicesCommand(netconfCommands);
78         doReturn(getDeviceHashMap()).when(netconfCommands).listDevices();
79
80         netconfListDeviceCommand.execute();
81
82         verify(netconfCommands, times(1)).listDevices();
83     }
84
85     @Test
86     public void testShowDeviceCommand() throws Exception {
87         NetconfShowDeviceCommand netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands);
88         netconfShowDeviceCommand.execute();
89
90         verify(netconfCommands, times(0)).showDevice(any());
91
92         netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, "deviceId", null, null);
93
94         doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any());
95         netconfShowDeviceCommand.execute();
96
97         verify(netconfCommands, times(1)).showDevice(any());
98
99         netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
100
101         doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any(), any());
102         netconfShowDeviceCommand.execute();
103
104         verify(netconfCommands, times(1)).showDevice(any(), any());
105     }
106
107     @Test
108     @SuppressWarnings("unchecked")
109     public void testUpdateDeviceCommand() throws Exception {
110         final NetconfUpdateDeviceCommand netconfUpdateDeviceCommand =
111                 new NetconfUpdateDeviceCommand(netconfCommands, "192.168.1.1");
112
113         final ArgumentCaptor<HashMap> hashMapArgumentCaptor = ArgumentCaptor.forClass(HashMap.class);
114
115         doReturn("").when(netconfCommands).updateDevice(isNull(), isNull(), isNull(), any(HashMap.class));
116
117         netconfUpdateDeviceCommand.execute();
118
119         verify(netconfCommands, times(1)).updateDevice(isNull(), isNull(), isNull(),
120                 hashMapArgumentCaptor.capture());
121
122         assertTrue(hashMapArgumentCaptor.getValue().containsKey(NetconfConsoleConstants.NETCONF_IP));
123         assertEquals("192.168.1.1", hashMapArgumentCaptor.getValue().get(NetconfConsoleConstants.NETCONF_IP));
124     }
125
126     private static HashMap<String, Map<String, List<String>>> getDeviceHashMap() {
127         final HashMap<String, Map<String, List<String>>> devices = new HashMap<>();
128         final HashMap<String, List<String>> deviceMap = new HashMap<>();
129         deviceMap.put(NetconfConsoleConstants.NETCONF_IP, Arrays.asList("192.168.1.1"));
130         deviceMap.put(NetconfConsoleConstants.NETCONF_PORT, Arrays.asList("7777"));
131         deviceMap.put(NetconfConsoleConstants.STATUS, Arrays.asList("connecting"));
132         deviceMap.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Arrays.asList("cap1", "cap2", "cap3"));
133         devices.put("device", deviceMap);
134         return devices;
135     }
136
137 }