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