Merge "Refactor netconf-util's configuration service"
[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 com.google.common.collect.Lists;
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.doExecute();
47         verify(netconfCommands, times(0)).connectDevice(any(), any());
48
49         netconfConnectDeviceCommand = new NetconfConnectDeviceCommand(netconfCommands, "192.168.1.1", "7777");
50
51         netconfConnectDeviceCommand.doExecute();
52         doNothing().when(netconfCommands).connectDevice(any(), any());
53         verify(netconfCommands, times(1)).connectDevice(any(), any());
54     }
55
56     @Test
57     public void testDisconnectDeviceCommand() throws Exception {
58         NetconfDisconnectDeviceCommand netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands);
59         netconfDisconnectDeviceCommand.doExecute();
60
61         verify(netconfCommands, times(0)).connectDevice(any(), any());
62
63         netconfDisconnectDeviceCommand = new NetconfDisconnectDeviceCommand(netconfCommands, "deviceId", null, null);
64
65         doReturn(true).when(netconfCommands).disconnectDevice(any());
66         netconfDisconnectDeviceCommand.doExecute();
67
68         verify(netconfCommands, times(1)).disconnectDevice(any());
69
70         netconfDisconnectDeviceCommand =
71                 new NetconfDisconnectDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
72
73         doReturn(true).when(netconfCommands).disconnectDevice(any(), any());
74         netconfDisconnectDeviceCommand.doExecute();
75
76         verify(netconfCommands, times(1)).disconnectDevice(any(), any());
77     }
78
79     @Test
80     public void testListDeviceCommand() throws Exception {
81         final NetconfListDevicesCommand netconfListDeviceCommand = new NetconfListDevicesCommand(netconfCommands);
82         doReturn(getDeviceHashMap()).when(netconfCommands).listDevices();
83
84         netconfListDeviceCommand.doExecute();
85
86         verify(netconfCommands, times(1)).listDevices();
87     }
88
89     @Test
90     public void testShowDeviceCommand() throws Exception {
91         NetconfShowDeviceCommand netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands);
92         netconfShowDeviceCommand.doExecute();
93
94         verify(netconfCommands, times(0)).showDevice(any());
95
96         netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, "deviceId", null, null);
97
98         doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any());
99         netconfShowDeviceCommand.doExecute();
100
101         verify(netconfCommands, times(1)).showDevice(any());
102
103         netconfShowDeviceCommand = new NetconfShowDeviceCommand(netconfCommands, null, "192.168.1.1", "7777");
104
105         doReturn(getDeviceHashMap()).when(netconfCommands).showDevice(any(), any());
106         netconfShowDeviceCommand.doExecute();
107
108         verify(netconfCommands, times(1)).showDevice(any(), any());
109     }
110
111     @Test
112     @SuppressWarnings("unchecked")
113     public void testUpdateDeviceCommand() throws Exception {
114         final NetconfUpdateDeviceCommand netconfUpdateDeviceCommand =
115                 new NetconfUpdateDeviceCommand(netconfCommands, "192.168.1.1");
116
117         final ArgumentCaptor<HashMap> hashMapArgumentCaptor = ArgumentCaptor.forClass(HashMap.class);
118
119         doReturn("").when(netconfCommands).updateDevice(anyString(), anyString(), anyString(), any());
120
121         netconfUpdateDeviceCommand.doExecute();
122
123         verify(netconfCommands, times(1)).updateDevice(anyString(), anyString(), anyString(), hashMapArgumentCaptor.capture());
124
125         assertTrue(hashMapArgumentCaptor.getValue().containsKey(NetconfConsoleConstants.NETCONF_IP));
126         assertEquals("192.168.1.1", hashMapArgumentCaptor.getValue().get(NetconfConsoleConstants.NETCONF_IP));
127     }
128
129     private HashMap getDeviceHashMap() {
130         final HashMap<String, Map<String, List<String>>> devices = new HashMap<>();
131         final HashMap<String, List<String>> deviceMap = new HashMap<>();
132         deviceMap.put(NetconfConsoleConstants.NETCONF_IP, Lists.newArrayList("192.168.1.1"));
133         deviceMap.put(NetconfConsoleConstants.NETCONF_PORT, Lists.newArrayList("7777"));
134         deviceMap.put(NetconfConsoleConstants.STATUS, Lists.newArrayList("connecting"));
135         deviceMap.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Lists.newArrayList("cap1", "cap2", "cap3"));
136         devices.put("device", deviceMap);
137         return devices;
138     }
139
140 }