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.Matchers.any;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.MockitoAnnotations.initMocks;
21 import com.google.common.collect.Lists;
22 import java.util.HashMap;
23 import java.util.List;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mock;
29 import org.opendaylight.netconf.console.api.NetconfCommands;
30 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
32 public class NetconfCommandsImplCallsTest {
35 private NetconfCommands netconfCommands;
43 public void testConnectDeviceCommand() throws Exception {
44 NetconfConnectDeviceCommand netconfConnectDeviceCommand =
45 new NetconfConnectDeviceCommand(netconfCommands);
46 netconfConnectDeviceCommand.doExecute();
47 verify(netconfCommands, times(0)).connectDevice(any(), any());
49 netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777");
51 netconfConnectDeviceCommand.doExecute();
52 doNothing().when(netconfCommands).connectDevice(any(), any());
53 verify(netconfCommands, times(1)).connectDevice(any(), any());
57 public void testDisconnectDeviceCommand() throws Exception {
58 NetconfDisconnectDeviceCommand netconfDisconnectDeviceCommand =
59 new NetconfDisconnectDeviceCommand(netconfCommands);
60 netconfDisconnectDeviceCommand.doExecute();
62 verify(netconfCommands, times(0)).connectDevice(any(), any());
64 netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands, "deviceId", null, null);
66 doReturn(true).when(netconfCommands).disconnectDevice(any());
67 netconfDisconnectDeviceCommand.doExecute();
69 verify(netconfCommands, times(1)).disconnectDevice(any());
71 netconfDisconnectDeviceCommand =
72 new NetconfDisconnectDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
74 doReturn(true).when(netconfCommands).disconnectDevice(any(), any());
75 netconfDisconnectDeviceCommand.doExecute();
77 verify(netconfCommands, times(1)).disconnectDevice(any(), any());
81 public void testListDeviceCommand() throws Exception {
82 final NetconfListDevicesCommand netconfListDeviceCommand = new NetconfListDevicesCommand(netconfCommands);
83 doReturn(getDeviceHashMap()).when(netconfCommands).listDevices();
85 netconfListDeviceCommand.doExecute();
87 verify(netconfCommands, times(1)).listDevices();
91 public void testShowDeviceCommand() throws Exception {
92 NetconfShowDeviceCommand netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands);
93 netconfShowDeviceCommand.doExecute();
95 verify(netconfCommands, times(0)).showDevice(any());
97 netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, "deviceId", null, null);
99 doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any());
100 netconfShowDeviceCommand.doExecute();
102 verify(netconfCommands, times(1)).showDevice(any());
104 netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
106 doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any(), any());
107 netconfShowDeviceCommand.doExecute();
109 verify(netconfCommands, times(1)).showDevice(any(), any());
113 @SuppressWarnings("unchecked")
114 public void testUpdateDeviceCommand() throws Exception {
115 final NetconfUpdateDeviceCommand netconfUpdateDeviceCommand =
116 new NetconfUpdateDeviceCommand(netconfCommands, "192.168.1.1");
118 final ArgumentCaptor<HashMap> hashMapArgumentCaptor = ArgumentCaptor.forClass(HashMap.class);
120 doReturn("").when(netconfCommands).updateDevice(anyString(), anyString(), anyString(), any());
122 netconfUpdateDeviceCommand.doExecute();
124 verify(netconfCommands, times(1)).updateDevice(anyString(), anyString(), anyString(),
125 hashMapArgumentCaptor.capture());
127 assertTrue(hashMapArgumentCaptor.getValue().containsKey(NetconfConsoleConstants.NETCONF_IP));
128 assertEquals("192.168.1.1", hashMapArgumentCaptor.getValue().get(NetconfConsoleConstants.NETCONF_IP));
131 private HashMap getDeviceHashMap() {
132 final HashMap<String, Map<String, List<String>>> devices = new HashMap<>();
133 final HashMap<String, List<String>> deviceMap = new HashMap<>();
134 deviceMap.put(NetconfConsoleConstants.NETCONF_IP, Lists.newArrayList("192.168.1.1"));
135 deviceMap.put(NetconfConsoleConstants.NETCONF_PORT, Lists.newArrayList("7777"));
136 deviceMap.put(NetconfConsoleConstants.STATUS, Lists.newArrayList("connecting"));
137 deviceMap.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Lists.newArrayList("cap1", "cap2", "cap3"));
138 devices.put("device", deviceMap);