2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.console.commands;
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;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.List;
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;
33 public class NetconfCommandsImplCallsTest {
36 private NetconfCommands netconfCommands;
44 public void testConnectDeviceCommand() throws Exception {
45 NetconfConnectDeviceCommand netconfConnectDeviceCommand =
46 new NetconfConnectDeviceCommand(netconfCommands);
47 netconfConnectDeviceCommand.execute();
48 verify(netconfCommands, times(0)).connectDevice(any(), any());
50 netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777", "user",
53 netconfConnectDeviceCommand.execute();
54 doNothing().when(netconfCommands).connectDevice(any(), any());
55 verify(netconfCommands, times(1)).connectDevice(any(), any());
59 public void testDisconnectDeviceCommand() throws Exception {
60 NetconfDisconnectDeviceCommand netconfDisconnectDeviceCommand =
61 new NetconfDisconnectDeviceCommand(netconfCommands);
62 netconfDisconnectDeviceCommand.execute();
64 verify(netconfCommands, times(0)).disconnectDevice(any(), any());
66 netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands, "deviceId", null, null);
68 doReturn(true).when(netconfCommands).disconnectDevice(any());
69 netconfDisconnectDeviceCommand.execute();
71 verify(netconfCommands, times(1)).disconnectDevice(any());
73 netconfDisconnectDeviceCommand =
74 new NetconfDisconnectDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
76 doReturn(true).when(netconfCommands).disconnectDevice(any(), any());
77 netconfDisconnectDeviceCommand.execute();
79 verify(netconfCommands, times(1)).disconnectDevice(any(), any());
83 public void testListDeviceCommand() throws Exception {
84 final NetconfListDevicesCommand netconfListDeviceCommand = new NetconfListDevicesCommand(netconfCommands);
85 doReturn(getDeviceHashMap()).when(netconfCommands).listDevices();
87 netconfListDeviceCommand.execute();
89 verify(netconfCommands, times(1)).listDevices();
93 public void testShowDeviceCommand() throws Exception {
94 NetconfShowDeviceCommand netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands);
95 netconfShowDeviceCommand.execute();
97 verify(netconfCommands, times(0)).showDevice(any());
99 netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, "deviceId", null, null);
101 doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any());
102 netconfShowDeviceCommand.execute();
104 verify(netconfCommands, times(1)).showDevice(any());
106 netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
108 doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any(), any());
109 netconfShowDeviceCommand.execute();
111 verify(netconfCommands, times(1)).showDevice(any(), any());
115 @SuppressWarnings("unchecked")
116 public void testUpdateDeviceCommand() throws Exception {
117 final NetconfUpdateDeviceCommand netconfUpdateDeviceCommand =
118 new NetconfUpdateDeviceCommand(netconfCommands, "192.168.1.1");
120 final ArgumentCaptor<HashMap> hashMapArgumentCaptor = ArgumentCaptor.forClass(HashMap.class);
122 doReturn("").when(netconfCommands).updateDevice(anyString(), anyString(), anyString(), any());
124 netconfUpdateDeviceCommand.execute();
126 verify(netconfCommands, times(1)).updateDevice(isNull(), isNull(), isNull(),
127 hashMapArgumentCaptor.capture());
129 assertTrue(hashMapArgumentCaptor.getValue().containsKey(NetconfConsoleConstants.NETCONF_IP));
130 assertEquals("192.168.1.1", hashMapArgumentCaptor.getValue().get(NetconfConsoleConstants.NETCONF_IP));
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);