Dhruv Dhody | b585012 | 2016-02-17 17:51:19 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Dhruv Dhody | b585012 | 2016-02-17 17:51:19 +0530 | [diff] [blame] | 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 | package org.onosproject.ospf.controller.impl; |
| 17 | |
| 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.Ip4Address; |
| 24 | import org.onosproject.net.driver.DriverService; |
| 25 | import org.onosproject.ospf.controller.OspfAgent; |
| 26 | import org.onosproject.ospf.controller.OspfArea; |
| 27 | import org.onosproject.ospf.controller.OspfInterface; |
| 28 | import org.onosproject.ospf.controller.OspfProcess; |
| 29 | import org.onosproject.ospf.controller.OspfRouter; |
| 30 | import org.onosproject.ospf.controller.area.OspfAreaImpl; |
| 31 | import org.onosproject.ospf.controller.area.OspfInterfaceImpl; |
| 32 | import org.onosproject.ospf.controller.area.OspfProcessImpl; |
| 33 | |
| 34 | import java.nio.channels.Channel; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.HashMap; |
| 37 | import java.util.List; |
| 38 | import java.util.Map; |
| 39 | |
| 40 | import static org.hamcrest.CoreMatchers.*; |
| 41 | import static org.hamcrest.MatcherAssert.assertThat; |
| 42 | |
| 43 | /** |
| 44 | * Unit test class for Controller. |
| 45 | */ |
| 46 | public class ControllerTest { |
| 47 | private Controller controller; |
| 48 | private Map<String, Long> maps; |
| 49 | private OspfAgent ospfAgent; |
| 50 | private DriverService driverService; |
| 51 | private List<Channel> connectedChannels; |
| 52 | private List<OspfProcess> process; |
| 53 | private OspfProcess ospfProcess; |
| 54 | private OspfArea ospfArea; |
| 55 | private OspfInterface ospfInterface; |
| 56 | private List<OspfInterface> ospfInterfaces; |
| 57 | private List<OspfArea> ospfAreas; |
| 58 | private List<OspfProcess> ospfProcesses; |
| 59 | private OspfProcess ospfProcess1; |
| 60 | private OspfArea ospfArea1; |
| 61 | private OspfRouter ospfRouter; |
| 62 | |
| 63 | @Before |
| 64 | public void setUp() throws Exception { |
| 65 | controller = new Controller(); |
| 66 | maps = new HashMap<String, Long>(); |
| 67 | ospfProcess = new OspfProcessImpl(); |
| 68 | ospfArea = new OspfAreaImpl(); |
| 69 | ospfInterface = new OspfInterfaceImpl(); |
| 70 | ospfInterfaces = new ArrayList(); |
| 71 | ospfInterface.setIpAddress(Ip4Address.valueOf("1.1.1.1")); |
| 72 | ospfInterfaces.add(ospfInterface); |
| 73 | ospfArea.setAreaId(Ip4Address.valueOf("2.2.2.2")); |
| 74 | ospfArea.setInterfacesLst(ospfInterfaces); |
| 75 | ospfProcess.setProcessId("10.10.10.10"); |
| 76 | ospfAreas = new ArrayList(); |
| 77 | ospfAreas.add(ospfArea); |
| 78 | ospfProcess.setAreas(ospfAreas); |
| 79 | ospfProcesses = new ArrayList(); |
| 80 | ospfProcesses.add(ospfProcess); |
| 81 | ospfProcess1 = new OspfProcessImpl(); |
| 82 | ospfProcess1.setProcessId("11.11.11.11"); |
| 83 | ospfArea1 = new OspfAreaImpl(); |
| 84 | ospfArea1.setAreaId(Ip4Address.valueOf("2.2.2.2")); |
| 85 | ospfArea1.setInterfacesLst(ospfInterfaces); |
| 86 | ospfAreas.add(ospfArea1); |
| 87 | ospfProcess1.setAreas(ospfAreas); |
| 88 | ospfProcesses.add(ospfProcess1); |
| 89 | connectedChannels = new ArrayList<>(); |
| 90 | } |
| 91 | |
| 92 | @After |
| 93 | public void tearDown() throws Exception { |
| 94 | controller = null; |
| 95 | maps.clear(); |
| 96 | connectedChannels.clear(); |
| 97 | controller = null; |
| 98 | maps = null; |
| 99 | ospfAgent = null; |
| 100 | driverService = null; |
| 101 | connectedChannels = null; |
| 102 | process = null; |
| 103 | ospfProcess = null; |
| 104 | ospfArea = null; |
| 105 | ospfInterface = null; |
| 106 | ospfInterfaces = null; |
| 107 | ospfAreas = null; |
| 108 | ospfProcesses = null; |
| 109 | ospfProcess1 = null; |
| 110 | ospfArea1 = null; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tests getAllConfiguredProcesses() method. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testGetAllConfiguredProcesses() throws Exception { |
| 118 | process = controller.getAllConfiguredProcesses(); |
| 119 | assertThat(process, is(nullValue())); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Tests addDeviceDetails() method. |
| 124 | */ |
| 125 | @Test(expected = Exception.class) |
| 126 | public void testAddDeviceDetails() throws Exception { |
| 127 | controller.addDeviceDetails(new OspfRouterImpl()); |
| 128 | assertThat(controller, is(notNullValue())); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests removeDeviceDetails() method. |
| 133 | */ |
| 134 | @Test(expected = Exception.class) |
| 135 | public void testRemoveDeviceDetails() throws Exception { |
| 136 | controller.removeDeviceDetails(new OspfRouterImpl()); |
| 137 | assertThat(controller, is(notNullValue())); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Tests init() method. |
| 142 | */ |
| 143 | @Test |
| 144 | public void testInit() throws Exception { |
| 145 | controller.init(); |
| 146 | assertThat(controller.systemStartTime, is(notNullValue())); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Tests start() method. |
| 151 | */ |
| 152 | @Test |
| 153 | public void testStart() throws Exception { |
| 154 | ospfAgent = EasyMock.createMock(OspfAgent.class); |
| 155 | controller.start(ospfAgent, driverService); |
| 156 | controller.updateConfig(ospfProcesses); |
| 157 | assertThat(controller, is(notNullValue())); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Tests stop() method. |
| 162 | */ |
| 163 | @Test(expected = Exception.class) |
| 164 | public void testStop() throws Exception { |
| 165 | controller.start(ospfAgent, driverService); |
| 166 | controller.stop(); |
| 167 | assertThat(controller, is(notNullValue())); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Tests deleteInterfaceFromArea() method. |
| 172 | */ |
| 173 | @Test |
| 174 | public void testDeleteInterfaceFromArea() throws Exception { |
| 175 | controller.updateConfig(ospfProcesses); |
| 176 | assertThat(controller.deleteInterfaceFromArea("10.10.10.10", "2.2.2.2", "1.1.1.1"), is(true)); |
| 177 | assertThat(controller.deleteInterfaceFromArea("10.10.10.10", "2.2.2.2", "5.5.5.5"), is(false)); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests checkArea() method. |
| 182 | */ |
| 183 | @Test |
| 184 | public void testCheckArea() throws Exception { |
| 185 | controller.updateConfig(ospfProcesses); |
| 186 | assertThat(controller.checkArea("10.10.10.10", "2.2.2.2"), is(true)); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Tests checkArea() method. |
| 191 | */ |
| 192 | @Test |
| 193 | public void testCheckArea1() throws Exception { |
| 194 | controller.updateConfig(ospfProcesses); |
| 195 | assertThat(controller.checkArea("10.10.10.10", "111.111.111.111"), is(false)); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Tests checkProcess() method. |
| 200 | */ |
| 201 | @Test |
| 202 | public void testCheckProcess() throws Exception { |
| 203 | controller.updateConfig(ospfProcesses); |
| 204 | assertThat(controller.checkProcess("3.3.3.3"), is(false)); |
| 205 | assertThat(controller.checkProcess("1.1.1.1"), is(false)); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Tests checkInterface() method. |
| 210 | */ |
| 211 | @Test |
| 212 | public void testCheckInterface() throws Exception { |
| 213 | controller.updateConfig(ospfProcesses); |
| 214 | assertThat(controller.checkInterface("10.10.10.10", "2.2.2.2", "1.1.1.1"), is(true)); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Tests updateAreaInProcess() method. |
| 219 | */ |
| 220 | @Test |
| 221 | public void testUpdateAreaInProcess() throws Exception { |
| 222 | controller.updateConfig(ospfProcesses); |
| 223 | controller.updateAreaInProcess("10.10.10.10", "2.2.2.2", ospfArea); |
| 224 | assertThat(controller, is(notNullValue())); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Tests updateConfig() method. |
| 229 | */ |
| 230 | @Test |
| 231 | public void testUpdateConfig() throws Exception { |
| 232 | controller.updateConfig(ospfProcesses); |
| 233 | controller.updateConfig(ospfProcesses); |
| 234 | controller.updateConfig(ospfProcesses); |
| 235 | assertThat(controller, is(notNullValue())); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Tests updateConfig() method. |
| 240 | */ |
| 241 | @Test |
| 242 | public void testUpdateConfig2() throws Exception { |
| 243 | controller.updateConfig(ospfProcesses); |
| 244 | controller.updateConfig(ospfProcesses); |
| 245 | assertThat(controller, is(notNullValue())); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Tests updateConfig() method. |
| 250 | */ |
| 251 | @Test |
| 252 | public void testUpdateConfig1() throws Exception { |
| 253 | ospfProcess = new OspfProcessImpl(); |
| 254 | ospfArea = new OspfAreaImpl(); |
| 255 | ospfInterface = new OspfInterfaceImpl(); |
| 256 | ospfInterfaces = new ArrayList(); |
| 257 | ospfInterface.setIpAddress(Ip4Address.valueOf("10.10.10.5")); |
| 258 | ospfInterfaces.add(ospfInterface); |
| 259 | ospfArea.setAreaId(Ip4Address.valueOf("2.2.2.2")); |
| 260 | ospfArea.setInterfacesLst(ospfInterfaces); |
| 261 | ospfProcess.setProcessId("10.10.10.10"); |
| 262 | ospfAreas = new ArrayList(); |
| 263 | ospfAreas.add(ospfArea); |
| 264 | ospfProcess.setAreas(ospfAreas); |
| 265 | ospfProcesses = new ArrayList(); |
| 266 | ospfProcesses.add(ospfProcess); |
| 267 | controller.updateConfig(ospfProcesses); |
| 268 | assertThat(controller, is(notNullValue())); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Tests deleteConfig() method. |
| 273 | */ |
| 274 | @Test(expected = Exception.class) |
| 275 | public void testDeleteConfig() throws Exception { |
| 276 | controller.updateConfig(ospfProcesses); |
| 277 | controller.deleteConfig(ospfProcesses, "INTERFACE"); |
| 278 | assertThat(controller, is(notNullValue())); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Tests deleteConfig() method. |
| 283 | */ |
| 284 | @Test(expected = Exception.class) |
| 285 | public void testDeleteConfig1() throws Exception { |
| 286 | controller.updateConfig(ospfProcesses); |
| 287 | controller.deleteConfig(ospfProcesses, "AREA"); |
| 288 | assertThat(controller, is(notNullValue())); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Tests deleteConfig() method. |
| 293 | */ |
| 294 | |
| 295 | @Test |
| 296 | public void testDeleteConfig2() throws Exception { |
| 297 | controller.updateConfig(ospfProcesses); |
| 298 | controller.deleteConfig(ospfProcesses, "PROCESS"); |
| 299 | assertThat(controller, is(notNullValue())); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Tests deleteConfig() method. |
| 304 | */ |
| 305 | @Test |
| 306 | public void testDeleteConfig3() throws Exception { |
| 307 | ospfProcesses = new ArrayList(); |
| 308 | controller.deleteConfig(ospfProcesses, "PROCESS"); |
| 309 | assertThat(controller, is(notNullValue())); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Tests deleteConfig() method. |
| 314 | */ |
| 315 | @Test |
| 316 | public void testDeleteConfig4() throws Exception { |
| 317 | controller.updateConfig(ospfProcesses); |
| 318 | controller.deleteConfig(ospfProcesses, "PROCESS"); |
| 319 | controller.updateConfig(ospfProcesses); |
| 320 | assertThat(controller, is(notNullValue())); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Tests deleteProcessWhenExists() method. |
| 325 | */ |
| 326 | @Test |
| 327 | public void testDeleteProcessWhenExists() throws Exception { |
| 328 | controller.updateConfig(ospfProcesses); |
| 329 | controller.deleteProcessWhenExists(ospfProcesses, "PROCESS"); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Tests addLinkDetails() method. |
| 334 | */ |
| 335 | @Test |
| 336 | public void testAddLinkDetails() throws Exception { |
| 337 | ospfAgent = EasyMock.createMock(OspfAgent.class); |
| 338 | controller.start(ospfAgent, driverService); |
| 339 | ospfRouter = new OspfRouterImpl(); |
| 340 | controller.addLinkDetails(ospfRouter, new OspfLinkTedImpl()); |
| 341 | assertThat(controller, is(notNullValue())); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Tests removeLinkDetails() method. |
| 346 | */ |
| 347 | @Test |
| 348 | public void testRemoveLinkDetails() throws Exception { |
| 349 | ospfAgent = EasyMock.createMock(OspfAgent.class); |
| 350 | controller.start(ospfAgent, driverService); |
| 351 | ospfRouter = new OspfRouterImpl(); |
| 352 | controller.addLinkDetails(ospfRouter, new OspfLinkTedImpl()); |
| 353 | controller.removeLinkDetails(ospfRouter); |
| 354 | assertThat(controller, is(notNullValue())); |
| 355 | } |
| 356 | } |