Thanuj Ravindranath
Committed by Gerrit Code Review

Unit testing code for the Netconf Controller.

Change-Id: Id6e4097768562888e76b1a101b2d85ee9e6841d9
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.netconf.ctl;
18 +
19 +import org.easymock.EasyMock;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.packet.IpAddress;
24 +import org.onosproject.net.DeviceId;
25 +import org.onosproject.netconf.NetconfDevice;
26 +import org.onosproject.netconf.NetconfDeviceFactory;
27 +import org.onosproject.netconf.NetconfDeviceInfo;
28 +import org.onosproject.netconf.NetconfDeviceListener;
29 +import org.onosproject.netconf.NetconfDeviceOutputEvent;
30 +import org.onosproject.netconf.NetconfDeviceOutputEventListener;
31 +import org.onosproject.netconf.NetconfException;
32 +import org.onosproject.netconf.NetconfSession;
33 +
34 +import java.lang.reflect.Field;
35 +import java.util.Map;
36 +import java.util.Optional;
37 +
38 +import static org.hamcrest.Matchers.*;
39 +import static org.junit.Assert.*;
40 +
41 +/**
42 + * Unit tests for the Netconf controller implementation test.
43 + */
44 +public class NetconfControllerImplTest {
45 + NetconfControllerImpl ctrl;
46 +
47 + //DeviceInfo
48 + NetconfDeviceInfo deviceInfo1;
49 + NetconfDeviceInfo deviceInfo2;
50 + NetconfDeviceInfo badDeviceInfo3;
51 +
52 + //Devices & DeviceId
53 + NetconfDevice device1;
54 + DeviceId deviceId1;
55 + NetconfDevice device2;
56 + DeviceId deviceId2;
57 +
58 + //Events
59 + NetconfDeviceOutputEvent eventForDeviceInfo1;
60 + NetconfDeviceOutputEvent eventForDeviceInfo2;
61 +
62 + private Map<DeviceId, NetconfDevice> reflectedDeviceMap;
63 + private NetconfDeviceOutputEventListener reflectedDownListener;
64 +
65 + //Test Device IP addresses and ports
66 + private static final String DEVICE_1_IP = "10.10.10.11";
67 + private static final String DEVICE_2_IP = "10.10.10.12";
68 + private static final String BAD_DEVICE_IP = "10.10.10.13";
69 +
70 + private static final int DEVICE_1_PORT = 11;
71 + private static final int DEVICE_2_PORT = 12;
72 + private static final int BAD_DEVICE_PORT = 13;
73 +
74 +
75 + @Before
76 + public void setUp() throws Exception {
77 + ctrl = new NetconfControllerImpl();
78 + ctrl.deviceFactory = new TestNetconfDeviceFactory();
79 +
80 + //Creating mock devices
81 + deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
82 + deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
83 + badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
84 +
85 + device1 = new TestNetconfDevice(deviceInfo1);
86 + deviceId1 = deviceInfo1.getDeviceId();
87 + device2 = new TestNetconfDevice(deviceInfo2);
88 + deviceId2 = deviceInfo2.getDeviceId();
89 +
90 + //Adding to the map for testing get device calls.
91 + Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
92 + field1.setAccessible(true);
93 + reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
94 + reflectedDeviceMap.put(deviceId1, device1);
95 + reflectedDeviceMap.put(deviceId2, device2);
96 +
97 + //Creating mock events for testing NetconfDeviceOutputEventListener
98 + Field field2 = ctrl.getClass().getDeclaredField("downListener");
99 + field2.setAccessible(true);
100 + reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
101 +
102 + eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
103 + null, Optional.of(1), deviceInfo1);
104 + eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
105 + null, Optional.of(2), deviceInfo2);
106 + }
107 +
108 + @After
109 + public void tearDown() {
110 + ctrl.deactivate();
111 + }
112 +
113 + /**
114 + * Test to add DeviceListeners,
115 + * and also to check whether the netconfDeviceListeners set is
116 + * updating or not which was present in NetconfControllerImpl class.
117 + */
118 + @Test
119 + public void testAddRemoveDeviceListener() {
120 + NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class);
121 + NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class);
122 + NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class);
123 +
124 + ctrl.addDeviceListener(deviceListener1);
125 + ctrl.addDeviceListener(deviceListener2);
126 + ctrl.addDeviceListener(deviceListener3);
127 + assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
128 + assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
129 + deviceListener2, deviceListener3));
130 +
131 + ctrl.removeDeviceListener(deviceListener1);
132 + assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
133 + assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3));
134 + }
135 +
136 + @Test
137 + public void testGetNetconfDevice() {
138 + NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
139 + assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
140 +
141 + NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
142 + assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
143 + }
144 +
145 + @Test
146 + public void testGetNetconfDeviceWithIPPort() {
147 + NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
148 + assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip());
149 +
150 + NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
151 + assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip());
152 + }
153 +
154 + /**
155 + * Check for bad device connection. In this case the device map shouldn't get modified.
156 + */
157 + @Test(expected = NetconfException.class)
158 + public void testConnectBadDevice() throws Exception {
159 + reflectedDeviceMap.clear();
160 + try {
161 + ctrl.connectDevice(badDeviceInfo3);
162 + } finally {
163 + assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
164 + }
165 + }
166 +
167 + /**
168 + * Check for correct device connection. In this case the device map get modified.
169 + */
170 + @Test
171 + public void testConnectCorrectDevice() throws Exception {
172 + reflectedDeviceMap.clear();
173 + ctrl.connectDevice(deviceInfo1);
174 + ctrl.connectDevice(deviceInfo2);
175 + assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1));
176 + assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
177 + assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
178 + }
179 +
180 +
181 + /**
182 + * Check for connect devices already added to the map.
183 + */
184 + @Test
185 + public void testConnectAlreadyExistingDevice() throws Exception {
186 + NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1);
187 + NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2);
188 + assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
189 + deviceInfo1.getDeviceId());
190 + assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
191 + deviceInfo2.getDeviceId());
192 + }
193 +
194 + /**
195 + * Check for removeDevice exception.
196 + */
197 + @Test
198 + public void testRemoveDevice() throws Exception {
199 + ctrl.removeDevice(deviceInfo1);
200 + assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
201 + }
202 +
203 + /**
204 + * Test to get the connected device map.
205 + */
206 + @Test
207 + public void testGetDevicesMap() {
208 + assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
209 + }
210 +
211 + /**
212 + * Test to check whether the DeviceDownEventListener removes the device from the map when session
213 + * for a particular device getting closed.
214 + */
215 + @Test
216 + public void testDeviceDownEventListener() throws Exception {
217 + reflectedDeviceMap.clear();
218 + ctrl.connectDevice(deviceInfo1);
219 + boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
220 + assertFalse("Irrelevant Device Event", result1);
221 + assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
222 + reflectedDownListener.event(eventForDeviceInfo1);
223 + assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
224 + ctrl.connectDevice(deviceInfo2);
225 + boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
226 + assertTrue("Irrelevant Device Event", result2);
227 + assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
228 + reflectedDownListener.event(eventForDeviceInfo2);
229 + assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
230 + }
231 +
232 + /**
233 + * Mock NetconfDeviceFactory class.
234 + */
235 + private class TestNetconfDeviceFactory implements NetconfDeviceFactory {
236 +
237 + @Override
238 + public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
239 + return new TestNetconfDevice(netconfDeviceInfo);
240 + }
241 + }
242 +
243 + /**
244 + * Mock NetconfDeviceImpl class, used for creating test devices.
245 + */
246 + protected class TestNetconfDevice implements NetconfDevice {
247 + private NetconfDeviceInfo netconfDeviceInfo;
248 + private boolean deviceState = false;
249 + private NetconfSession netconfSession;
250 +
251 + public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
252 + netconfDeviceInfo = deviceInfo;
253 + if (netconfDeviceInfo.ip() != badDeviceInfo3.ip()) {
254 + netconfSession = EasyMock.createMock(NetconfSession.class);
255 + deviceState = true;
256 + } else {
257 + throw new NetconfException("Cannot create Connection and Session");
258 + }
259 + }
260 +
261 + @Override
262 + public boolean isActive() {
263 + return deviceState;
264 + }
265 +
266 + @Override
267 + public NetconfSession getSession() {
268 + return netconfSession;
269 + }
270 +
271 + @Override
272 + public void disconnect() {
273 + deviceState = false;
274 + netconfSession = null;
275 + }
276 +
277 + @Override
278 + public NetconfDeviceInfo getDeviceInfo() {
279 + return netconfDeviceInfo;
280 + }
281 +
282 + }
283 +}
...@@ -53,6 +53,11 @@ ...@@ -53,6 +53,11 @@
53 <artifactId>onos-api</artifactId> 53 <artifactId>onos-api</artifactId>
54 <version>${project.version}</version> 54 <version>${project.version}</version>
55 </dependency> 55 </dependency>
56 + <dependency>
57 + <groupId>org.easymock</groupId>
58 + <artifactId>easymock</artifactId>
59 + <scope>test</scope>
60 + </dependency>
56 </dependencies> 61 </dependencies>
57 62
58 </project> 63 </project>
......