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.BDDMockito.given;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
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 com.google.common.base.Strings;
23 import com.google.common.collect.Lists;
24 import java.util.HashMap;
25 import java.util.List;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mock;
32 import org.opendaylight.netconf.console.api.NetconfCommands;
33 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
34 import org.powermock.api.mockito.PowerMockito;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.modules.junit4.PowerMockRunner;
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest(Strings.class)
40 public class NetconfCommandsImplCallsTest {
43 private NetconfCommands netconfCommands;
51 public void testConnectDeviceCommand() throws Exception {
52 NetconfConnectDeviceCommand netconfConnectDeviceCommand =
53 new NetconfConnectDeviceCommand(netconfCommands);
54 netconfConnectDeviceCommand.doExecute();
55 verify(netconfCommands, times(0)).connectDevice(any(), any());
57 netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777");
59 PowerMockito.mockStatic(Strings.class);
60 given(Strings.isNullOrEmpty(any())).willReturn(false);
61 netconfConnectDeviceCommand.doExecute();
62 doNothing().when(netconfCommands).connectDevice(any(), any());
63 verify(netconfCommands, times(1)).connectDevice(any(), any());
67 public void testDisconnectDeviceCommand() throws Exception {
68 NetconfDisconnectDeviceCommand netconfDisconnectDeviceCommand =
69 new NetconfDisconnectDeviceCommand(netconfCommands);
70 netconfDisconnectDeviceCommand.doExecute();
72 verify(netconfCommands, times(0)).disconnectDevice(any(), any());
74 netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands, "deviceId", null, null);
76 doReturn(true).when(netconfCommands).disconnectDevice(any());
77 netconfDisconnectDeviceCommand.doExecute();
79 verify(netconfCommands, times(1)).disconnectDevice(any());
81 netconfDisconnectDeviceCommand =
82 new NetconfDisconnectDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
84 doReturn(true).when(netconfCommands).disconnectDevice(any(), any());
85 netconfDisconnectDeviceCommand.doExecute();
87 verify(netconfCommands, times(1)).disconnectDevice(any(), any());
91 public void testListDeviceCommand() throws Exception {
92 final NetconfListDevicesCommand netconfListDeviceCommand = new NetconfListDevicesCommand(netconfCommands);
93 doReturn(getDeviceHashMap()).when(netconfCommands).listDevices();
95 netconfListDeviceCommand.doExecute();
97 verify(netconfCommands, times(1)).listDevices();
101 public void testShowDeviceCommand() throws Exception {
102 NetconfShowDeviceCommand netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands);
103 netconfShowDeviceCommand.doExecute();
105 verify(netconfCommands, times(0)).showDevice(any());
107 netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, "deviceId", null, null);
109 doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any());
110 netconfShowDeviceCommand.doExecute();
112 verify(netconfCommands, times(1)).showDevice(any());
114 netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
116 doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any(), any());
117 netconfShowDeviceCommand.doExecute();
119 verify(netconfCommands, times(1)).showDevice(any(), any());
123 @SuppressWarnings("unchecked")
124 public void testUpdateDeviceCommand() throws Exception {
125 final NetconfUpdateDeviceCommand netconfUpdateDeviceCommand =
126 new NetconfUpdateDeviceCommand(netconfCommands, "192.168.1.1");
128 final ArgumentCaptor<HashMap> hashMapArgumentCaptor = ArgumentCaptor.forClass(HashMap.class);
130 doReturn("").when(netconfCommands).updateDevice(anyString(), anyString(), anyString(), any());
132 netconfUpdateDeviceCommand.doExecute();
134 verify(netconfCommands, times(1)).updateDevice(anyString(), anyString(), anyString(),
135 hashMapArgumentCaptor.capture());
137 assertTrue(hashMapArgumentCaptor.getValue().containsKey(NetconfConsoleConstants.NETCONF_IP));
138 assertEquals("192.168.1.1", hashMapArgumentCaptor.getValue().get(NetconfConsoleConstants.NETCONF_IP));
141 private HashMap getDeviceHashMap() {
142 final HashMap<String, Map<String, List<String>>> devices = new HashMap<>();
143 final HashMap<String, List<String>> deviceMap = new HashMap<>();
144 deviceMap.put(NetconfConsoleConstants.NETCONF_IP, Lists.newArrayList("192.168.1.1"));
145 deviceMap.put(NetconfConsoleConstants.NETCONF_PORT, Lists.newArrayList("7777"));
146 deviceMap.put(NetconfConsoleConstants.STATUS, Lists.newArrayList("connecting"));
147 deviceMap.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Lists.newArrayList("cap1", "cap2", "cap3"));
148 devices.put("device", deviceMap);