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