45cbb85fa1ba2e90bbeed96352914bf15e80ece8
[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.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;
21
22 import com.google.common.base.Strings;
23 import com.google.common.collect.Lists;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
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;
37
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest(Strings.class)
40 public class NetconfCommandsImplCallsTest {
41
42     @Mock
43     private NetconfCommands netconfCommands;
44
45     @Before
46     public void setUp() {
47         initMocks(this);
48     }
49
50     @Test
51     public void testConnectDeviceCommand() throws Exception {
52         NetconfConnectDeviceCommand netconfConnectDeviceCommand =
53                 new NetconfConnectDeviceCommand(netconfCommands);
54         netconfConnectDeviceCommand.doExecute();
55         verify(netconfCommands, times(0)).connectDevice(any(), any());
56
57         netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777");
58
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());
64     }
65
66     @Test
67     public void testDisconnectDeviceCommand() throws Exception {
68         NetconfDisconnectDeviceCommand netconfDisconnectDeviceCommand =
69                 new NetconfDisconnectDeviceCommand(netconfCommands);
70         netconfDisconnectDeviceCommand.doExecute();
71
72         verify(netconfCommands, times(0)).disconnectDevice(any(), any());
73
74         netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands, "deviceId", null, null);
75
76         doReturn(true).when(netconfCommands).disconnectDevice(any());
77         netconfDisconnectDeviceCommand.doExecute();
78
79         verify(netconfCommands, times(1)).disconnectDevice(any());
80
81         netconfDisconnectDeviceCommand =
82                 new NetconfDisconnectDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
83
84         doReturn(true).when(netconfCommands).disconnectDevice(any(), any());
85         netconfDisconnectDeviceCommand.doExecute();
86
87         verify(netconfCommands, times(1)).disconnectDevice(any(), any());
88     }
89
90     @Test
91     public void testListDeviceCommand() throws Exception {
92         final NetconfListDevicesCommand netconfListDeviceCommand = new NetconfListDevicesCommand(netconfCommands);
93         doReturn(getDeviceHashMap()).when(netconfCommands).listDevices();
94
95         netconfListDeviceCommand.doExecute();
96
97         verify(netconfCommands, times(1)).listDevices();
98     }
99
100     @Test
101     public void testShowDeviceCommand() throws Exception {
102         NetconfShowDeviceCommand netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands);
103         netconfShowDeviceCommand.doExecute();
104
105         verify(netconfCommands, times(0)).showDevice(any());
106
107         netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, "deviceId", null, null);
108
109         doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any());
110         netconfShowDeviceCommand.doExecute();
111
112         verify(netconfCommands, times(1)).showDevice(any());
113
114         netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
115
116         doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any(), any());
117         netconfShowDeviceCommand.doExecute();
118
119         verify(netconfCommands, times(1)).showDevice(any(), any());
120     }
121
122     @Test
123     @SuppressWarnings("unchecked")
124     public void testUpdateDeviceCommand() throws Exception {
125         final NetconfUpdateDeviceCommand netconfUpdateDeviceCommand =
126                 new NetconfUpdateDeviceCommand(netconfCommands, "192.168.1.1");
127
128         final ArgumentCaptor<HashMap> hashMapArgumentCaptor = ArgumentCaptor.forClass(HashMap.class);
129
130         doReturn("").when(netconfCommands).updateDevice(anyString(), anyString(), anyString(), any());
131
132         netconfUpdateDeviceCommand.doExecute();
133
134         verify(netconfCommands, times(1)).updateDevice(anyString(), anyString(), anyString(),
135                 hashMapArgumentCaptor.capture());
136
137         assertTrue(hashMapArgumentCaptor.getValue().containsKey(NetconfConsoleConstants.NETCONF_IP));
138         assertEquals("192.168.1.1", hashMapArgumentCaptor.getValue().get(NetconfConsoleConstants.NETCONF_IP));
139     }
140
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);
149         return devices;
150     }
151
152 }