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.isNull;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.List;
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;
31 @RunWith(MockitoJUnitRunner.StrictStubs.class)
32 public class NetconfCommandsImplCallsTest {
35 private NetconfCommands netconfCommands;
38 public void testConnectDeviceCommand() throws Exception {
39 NetconfConnectDeviceCommand netconfConnectDeviceCommand =
40 new NetconfConnectDeviceCommand(netconfCommands);
41 netconfConnectDeviceCommand.execute();
42 verify(netconfCommands, times(0)).connectDevice(any(), any());
44 netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777", "user",
47 netconfConnectDeviceCommand.execute();
48 verify(netconfCommands, times(1)).connectDevice(any(), any());
52 public void testDisconnectDeviceCommand() throws Exception {
53 NetconfDisconnectDeviceCommand netconfDisconnectDeviceCommand =
54 new NetconfDisconnectDeviceCommand(netconfCommands);
55 netconfDisconnectDeviceCommand.execute();
57 verify(netconfCommands, times(0)).disconnectDevice(any(), any());
59 netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands, "deviceId", null, null);
61 doReturn(true).when(netconfCommands).disconnectDevice(any());
62 netconfDisconnectDeviceCommand.execute();
64 verify(netconfCommands, times(1)).disconnectDevice(any());
66 netconfDisconnectDeviceCommand =
67 new NetconfDisconnectDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
69 doReturn(true).when(netconfCommands).disconnectDevice(any(), any());
70 netconfDisconnectDeviceCommand.execute();
72 verify(netconfCommands, times(1)).disconnectDevice(any(), any());
76 public void testListDeviceCommand() throws Exception {
77 final NetconfListDevicesCommand netconfListDeviceCommand = new NetconfListDevicesCommand(netconfCommands);
78 doReturn(getDeviceHashMap()).when(netconfCommands).listDevices();
80 netconfListDeviceCommand.execute();
82 verify(netconfCommands, times(1)).listDevices();
86 public void testShowDeviceCommand() throws Exception {
87 NetconfShowDeviceCommand netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands);
88 netconfShowDeviceCommand.execute();
90 verify(netconfCommands, times(0)).showDevice(any());
92 netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, "deviceId", null, null);
94 doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any());
95 netconfShowDeviceCommand.execute();
97 verify(netconfCommands, times(1)).showDevice(any());
99 netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
101 doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any(), any());
102 netconfShowDeviceCommand.execute();
104 verify(netconfCommands, times(1)).showDevice(any(), any());
108 @SuppressWarnings("unchecked")
109 public void testUpdateDeviceCommand() throws Exception {
110 final NetconfUpdateDeviceCommand netconfUpdateDeviceCommand =
111 new NetconfUpdateDeviceCommand(netconfCommands, "192.168.1.1");
113 final ArgumentCaptor<HashMap> hashMapArgumentCaptor = ArgumentCaptor.forClass(HashMap.class);
115 doReturn("").when(netconfCommands).updateDevice(isNull(), isNull(), isNull(), any(HashMap.class));
117 netconfUpdateDeviceCommand.execute();
119 verify(netconfCommands, times(1)).updateDevice(isNull(), isNull(), isNull(),
120 hashMapArgumentCaptor.capture());
122 assertTrue(hashMapArgumentCaptor.getValue().containsKey(NetconfConsoleConstants.NETCONF_IP));
123 assertEquals("192.168.1.1", hashMapArgumentCaptor.getValue().get(NetconfConsoleConstants.NETCONF_IP));
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);