Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.devicemanager.internal; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.util.Arrays; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import net.floodlightcontroller.core.internal.TestDatabaseManager; |
| 9 | import net.floodlightcontroller.devicemanager.IDevice; |
| 10 | import net.floodlightcontroller.devicemanager.SwitchPort; |
| 11 | import net.floodlightcontroller.devicemanager.internal.Device; |
| 12 | import net.floodlightcontroller.packet.IPv4; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame^] | 13 | import net.onrc.onos.graph.GraphDBConnection; |
| 14 | import net.onrc.onos.graph.GraphDBOperation; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 15 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 16 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 17 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 18 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 19 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 21 | |
| 22 | import org.easymock.EasyMock; |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | import org.openflow.util.HexString; |
| 28 | import org.powermock.api.easymock.PowerMock; |
| 29 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | import org.powermock.modules.junit4.PowerMockRunner; |
| 32 | |
| 33 | import com.thinkaurelius.titan.core.TitanFactory; |
| 34 | import com.thinkaurelius.titan.core.TitanGraph; |
| 35 | |
| 36 | //Add Powermock preparation |
| 37 | @RunWith(PowerMockRunner.class) |
| 38 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class}) |
| 39 | public class DeviceStorageImplTestBB { |
| 40 | protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
| 41 | |
| 42 | String conf; |
| 43 | private GraphDBConnection conn = null; |
| 44 | private GraphDBOperation ope = null; |
| 45 | private TitanGraph titanGraph = null; |
| 46 | IDeviceStorage deviceImpl = null; |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() throws Exception { |
| 50 | |
| 51 | deviceImpl = new DeviceStorageImpl(); |
| 52 | conf = "/dummy/path/to/db"; |
| 53 | |
| 54 | // Make mock cassandra DB |
| 55 | // Replace TitanFactory.open() to return mock DB |
| 56 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 57 | TestDatabaseManager.populateTestData(titanGraph); |
| 58 | PowerMock.mockStatic(TitanFactory.class); |
| 59 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 60 | PowerMock.replay(TitanFactory.class); |
| 61 | |
| 62 | conn = GraphDBConnection.getInstance(conf); |
| 63 | ope = new GraphDBOperation(conn); |
| 64 | |
| 65 | deviceImpl.init(conf); |
| 66 | } |
| 67 | |
| 68 | @After |
| 69 | public void tearDown() throws Exception { |
| 70 | titanGraph.shutdown(); |
| 71 | TestDatabaseManager.deleteTestDatabase(); |
| 72 | |
| 73 | deviceImpl.close(); |
| 74 | deviceImpl = null; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Desc: |
| 79 | * Test method for addDevice method. |
| 80 | * Codition: |
| 81 | * N/A |
| 82 | * Expect: |
| 83 | * Get proper IDeviceObject |
| 84 | * Check the IDeviceObject properties set |
| 85 | */ |
| 86 | @Test |
| 87 | public void testAddDevice() { |
| 88 | try |
| 89 | { |
| 90 | //Make mockDevice |
| 91 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 92 | // Mac addr for test device. |
| 93 | String macAddr = "99:99:99:99:99:99"; |
| 94 | // IP addr for test device |
| 95 | String ip = "192.168.100.1"; |
| 96 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 97 | Integer[] ipaddrs = {ipInt}; |
| 98 | // Mac addr for attached switch |
| 99 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 100 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 101 | // Port number for attached switch |
| 102 | short portNum = 2; |
| 103 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 104 | SwitchPort[] sps = {sp1}; |
| 105 | |
| 106 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 107 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 108 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 109 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 110 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 111 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 112 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 113 | |
| 114 | EasyMock.replay(mockDev); |
| 115 | |
| 116 | //Add the device |
| 117 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 118 | assertNotNull(obj); |
| 119 | |
| 120 | //Test to take a Device from DB correctly |
| 121 | IDeviceObject devObj1 = ope.searchDevice(macAddr); |
| 122 | assertEquals(macAddr, devObj1.getMACAddress()); |
| 123 | |
| 124 | //Test to take a attached sw from DB correctly |
| 125 | for(ISwitchObject sw1: devObj1.getSwitch()) |
| 126 | { |
| 127 | String swMacFromDB = sw1.getDPID(); |
| 128 | assertEquals(switchMacAddr, swMacFromDB); |
| 129 | } |
| 130 | |
| 131 | //Test to take a IP addr from DB |
| 132 | //TodoForGettingIPaddr. There may be bug in the test class. |
| 133 | String ipFromDB = devObj1.getIPAddress(); |
| 134 | String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(","); |
| 135 | List<String> ipsList = Arrays.asList(ipsFromDB); |
| 136 | assertTrue(ipsList.contains(ip)); |
| 137 | |
| 138 | //Test to take a attached port from DB |
| 139 | for(IPortObject port : devObj1.getAttachedPorts()) |
| 140 | { |
| 141 | |
| 142 | //In this implementing, the object was not set the port. So it must be null. |
| 143 | if(port.getNumber() != null) |
| 144 | { |
| 145 | String portNumFromDB = port.getNumber().toString(); |
| 146 | assertEquals(String.valueOf(portNum), portNumFromDB); |
| 147 | } |
| 148 | } |
| 149 | } catch(Exception e) { |
| 150 | fail(e.getMessage()); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Desc: |
| 156 | * Test method for addDevice method. |
| 157 | * Condition: |
| 158 | * Already added device is existed. |
| 159 | * Expect: |
| 160 | * Get proper IDeviceObject still. |
| 161 | * Check the IDeviceObject properties set. |
| 162 | */ |
| 163 | @Test |
| 164 | public void testAddDeviceExisting() { |
| 165 | try |
| 166 | { |
| 167 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 168 | String macAddr = "99:99:99:99:99:99"; |
| 169 | String ip = "192.168.100.1"; |
| 170 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 171 | Integer[] ipaddrs = {ipInt}; |
| 172 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 173 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 174 | short portNum = 2; |
| 175 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 176 | SwitchPort[] sps = {sp1}; |
| 177 | |
| 178 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 179 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 180 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 181 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 182 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 183 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 184 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 185 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 186 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 187 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 188 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 189 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 190 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 191 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 192 | EasyMock.replay(mockDev); |
| 193 | |
| 194 | //Add the device |
| 195 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 196 | assertNotNull(obj); |
| 197 | |
| 198 | //Test to take a Device from DB correctly |
| 199 | IDeviceObject devObj1 = ope.searchDevice(macAddr); |
| 200 | assertEquals(macAddr, devObj1.getMACAddress()); |
| 201 | |
| 202 | //Add the same device |
| 203 | IDeviceObject obj2 = deviceImpl.addDevice(mockDev); |
| 204 | assertNotNull(obj2); |
| 205 | |
| 206 | IDeviceObject devObj2 = ope.searchDevice(macAddr); |
| 207 | assertEquals(macAddr, devObj2.getMACAddress()); |
| 208 | |
| 209 | //Test to take a attached port from DB |
| 210 | for(IPortObject port : devObj2.getAttachedPorts()) |
| 211 | { |
| 212 | //In this implementing, the object was not set the port. So it must be null. |
| 213 | if(port.getNumber() != null) |
| 214 | { |
| 215 | |
| 216 | String portNumFromDB = port.getNumber().toString(); |
| 217 | assertEquals(String.valueOf(portNum), portNumFromDB); |
| 218 | |
| 219 | ISwitchObject sw = port.getSwitch(); |
| 220 | String str = sw.getDPID(); |
| 221 | log.debug(""); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | String ipFromDB = devObj2.getIPAddress(); |
| 226 | String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(","); |
| 227 | List<String> ipsList = Arrays.asList(ipsFromDB); |
| 228 | assertTrue(ipsList.contains(ip)); |
| 229 | |
| 230 | //Test to take a attached port from DB |
| 231 | for(IPortObject port : devObj2.getAttachedPorts()) |
| 232 | { |
| 233 | |
| 234 | //In this implementing, the object was not set the port. So it must be null. |
| 235 | if(port.getNumber() != null) |
| 236 | { |
| 237 | String portNumFromDB = port.getNumber().toString(); |
| 238 | assertEquals(String.valueOf(portNum), portNumFromDB); |
| 239 | } |
| 240 | } |
| 241 | } catch(Exception e) { |
| 242 | fail(e.getMessage()); |
| 243 | } |
| 244 | } |
| 245 | /** |
| 246 | * Desc: |
| 247 | * Test method for updateDevice method. |
| 248 | * Condition: |
| 249 | * The mac address and attachment point are the same. |
| 250 | * All of the other parameter are different. |
| 251 | * Expect: |
| 252 | * Changed parameters are set properly. |
| 253 | */ |
| 254 | //@Ignore |
| 255 | @Test |
| 256 | public void testUpdateDevice() { |
| 257 | try |
| 258 | { |
| 259 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 260 | String macAddr = "99:99:99:99:99:99"; |
| 261 | String ip = "192.168.100.1"; |
| 262 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 263 | Integer[] ipaddrs = {ipInt}; |
| 264 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 265 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 266 | short portNum = 2; |
| 267 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 268 | SwitchPort[] sps = {sp1}; |
| 269 | |
| 270 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 271 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 272 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 273 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 274 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 275 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 276 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 277 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 278 | EasyMock.replay(mockDev); |
| 279 | |
| 280 | //Dev2 (attached port is the same) |
| 281 | IDevice mockDev2 = EasyMock.createMock(Device.class); |
| 282 | String macAddr2 = "99:aa:aa:aa:aa:aa"; |
| 283 | Integer ip2 = IPv4.toIPv4Address("192.168.100.2"); |
| 284 | Integer[] ipaddrs2 = {ip2}; |
| 285 | |
| 286 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 287 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 288 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 289 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 290 | EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2); |
| 291 | EasyMock.expect(mockDev2.getAttachmentPoints()).andReturn(sps); |
| 292 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 293 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 294 | EasyMock.replay(mockDev2); |
| 295 | |
| 296 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 297 | assertNotNull(obj); |
| 298 | |
| 299 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 300 | assertEquals(macAddr, dev1.getMACAddress()); |
| 301 | |
| 302 | //update theDevice |
| 303 | deviceImpl.updateDevice(mockDev2); |
| 304 | IDeviceObject dev2 = ope.searchDevice(macAddr2); |
| 305 | assertEquals(macAddr2, dev2.getMACAddress()); |
| 306 | IPortObject iport = ope.searchPort(switchMacAddr, portNum); |
| 307 | |
| 308 | //Test to take a attached port from DB |
| 309 | for(IDeviceObject dev : iport.getDevices()) |
| 310 | { |
| 311 | String macAddrFromDB = dev.getMACAddress(); |
| 312 | if(macAddr2.equals(macAddrFromDB)){ |
| 313 | //Nothing to do |
| 314 | } |
| 315 | else{ |
| 316 | fail("notFoundTheDeviceOnThePort"); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | } catch(Exception e) { |
| 321 | fail(e.getMessage()); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Desc: |
| 327 | * Test method for testRemoveDevice method. |
| 328 | * Condition: |
| 329 | * 1. Unregistered IDeviceObject argument is put. |
| 330 | * Expect: |
| 331 | * 1. Nothing happen when unregistered IDeviceObject is put |
| 332 | * 2. IDeviceObject will be removed. |
| 333 | */ |
| 334 | //@Ignore |
| 335 | @Test |
| 336 | public void testRemoveDevice() { |
| 337 | try |
| 338 | { |
| 339 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 340 | String macAddr = "99:99:99:99:99:99"; |
| 341 | String ip = "192.168.100.1"; |
| 342 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 343 | Integer[] ipaddrs = {ipInt}; |
| 344 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 345 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 346 | short portNum = 2; |
| 347 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 348 | SwitchPort[] sps = {sp1}; |
| 349 | |
| 350 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 351 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 352 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 353 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 354 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 355 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 356 | |
| 357 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 358 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 359 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 360 | EasyMock.replay(mockDev); |
| 361 | |
| 362 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 363 | assertNotNull(obj); |
| 364 | |
| 365 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 366 | assertEquals(macAddr, dev1.getMACAddress()); |
| 367 | |
| 368 | deviceImpl.removeDevice(mockDev); |
| 369 | IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr); |
| 370 | assertNull(dev); |
| 371 | |
| 372 | } catch(Exception e) { |
| 373 | fail(e.getMessage()); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Desc: |
| 379 | * Test method for getDeviceByMac |
| 380 | * Condition: |
| 381 | * 1. Unregistered mac address argument is set |
| 382 | * Expect: |
| 383 | * 1.Nothing happen when you put unregistered mac address |
| 384 | * 2.Get the proper IDeviceObject. |
| 385 | * 3.Check the IDeviceObject properties set. |
| 386 | */ |
| 387 | //@Ignore |
| 388 | @Test |
| 389 | public void testGetDeviceByMac() { |
| 390 | try |
| 391 | { |
| 392 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 393 | String macAddr = "99:99:99:99:99:99"; |
| 394 | String ip = "192.168.100.1"; |
| 395 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 396 | Integer[] ipaddrs = {ipInt}; |
| 397 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 398 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 399 | short portNum = 2; |
| 400 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 401 | SwitchPort[] sps = {sp1}; |
| 402 | |
| 403 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 404 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 405 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 406 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 407 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 408 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 409 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 410 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 411 | EasyMock.replay(mockDev); |
| 412 | |
| 413 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 414 | assertNotNull(obj); |
| 415 | |
| 416 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 417 | assertEquals(macAddr, dev1.getMACAddress()); |
| 418 | |
| 419 | IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr); |
| 420 | assertNotNull(dev); |
| 421 | assertEquals(macAddr, dev.getMACAddress()); |
| 422 | |
| 423 | } catch(Exception e) { |
| 424 | fail(e.getMessage()); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Desc: |
| 430 | * Test method for getDeviceByIP method. |
| 431 | * Condition: |
| 432 | * 1. Unregistered ip address argument is set |
| 433 | * Expect: |
| 434 | * 1. Nothing happen when you put unregistered mac address |
| 435 | * 2. Get the proper IDeviceObject. |
| 436 | * 3. Check the IDeviceObject properties set. |
| 437 | */ |
| 438 | //@Ignore |
| 439 | @Test |
| 440 | public void testGetDeviceByIP() { |
| 441 | try |
| 442 | { |
| 443 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 444 | String macAddr = "99:99:99:99:99:99"; |
| 445 | String ip = "192.168.100.1"; |
| 446 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 447 | Integer[] ipaddrs = {ipInt}; |
| 448 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 449 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 450 | short portNum = 2; |
| 451 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 452 | SwitchPort[] sps = {sp1}; |
| 453 | |
| 454 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 455 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 456 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 457 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 458 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 459 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 460 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 461 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 462 | EasyMock.replay(mockDev); |
| 463 | |
| 464 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 465 | assertNotNull(obj); |
| 466 | |
| 467 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 468 | assertEquals(macAddr, dev1.getMACAddress()); |
| 469 | |
| 470 | IDeviceObject dev = deviceImpl.getDeviceByIP(ip); |
| 471 | assertNotNull(dev); |
| 472 | String ipFromDB = dev.getIPAddress(); |
| 473 | String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(","); |
| 474 | List<String> ipsList = Arrays.asList(ipsFromDB); |
| 475 | assertTrue(ipsList.contains(ip)); |
| 476 | |
| 477 | } catch(Exception e) { |
| 478 | fail(e.getMessage()); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Desc: |
| 484 | * Test method for testChangeDeviceAttachmentsIDevice |
| 485 | * Condition: |
| 486 | * 1. Unexisting attachment point argument is set |
| 487 | * Expect: |
| 488 | * 1. Unexisting attachment point is ignored, so nothing happen. |
| 489 | * 2. Change the attachment point. |
| 490 | */ |
| 491 | //@Ignore |
| 492 | @Test |
| 493 | public void testChangeDeviceAttachmentsIDevice() { |
| 494 | try |
| 495 | { |
| 496 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 497 | String macAddr = "99:99:99:99:99:99"; |
| 498 | String ip = "192.168.100.1"; |
| 499 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 500 | Integer[] ipaddrs = {ipInt}; |
| 501 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 502 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 503 | short portNum = 2; |
| 504 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 505 | SwitchPort[] sps = {sp1}; |
| 506 | |
| 507 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 508 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 509 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 510 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 511 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 512 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 513 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 514 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 515 | EasyMock.replay(mockDev); |
| 516 | |
| 517 | //Dev2 |
| 518 | IDevice mockDev2 = EasyMock.createMock(Device.class); |
| 519 | String switchMacAddr2 = "00:00:00:00:00:00:0a:02"; |
| 520 | long lSwitchMacAddr2 = HexString.toLong(switchMacAddr2); |
| 521 | short portNum2 = 2; |
| 522 | SwitchPort sp2 = new SwitchPort(lSwitchMacAddr2, portNum2); |
| 523 | SwitchPort sps2[] = {sp2}; |
| 524 | |
| 525 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 526 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 527 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 528 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 529 | EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs); |
| 530 | EasyMock.expect(mockDev2.getAttachmentPoints()).andReturn(sps2); |
| 531 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 532 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 533 | EasyMock.replay(mockDev2); |
| 534 | |
| 535 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 536 | assertNotNull(obj); |
| 537 | |
| 538 | deviceImpl.changeDeviceAttachments(mockDev2); |
| 539 | |
| 540 | IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr); |
| 541 | assertNotNull(dev); |
| 542 | |
| 543 | for(ISwitchObject sw1: dev.getSwitch()) |
| 544 | { |
| 545 | String swMacFromDB = sw1.getDPID(); |
| 546 | assertEquals(switchMacAddr2, swMacFromDB); |
| 547 | } |
| 548 | } catch(Exception e) { |
| 549 | fail(e.getMessage()); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | //@Ignore |
| 554 | @Test |
| 555 | public void testChangeDeviceAttachmentsIDeviceIDeviceObject() { |
| 556 | //It is tested by the testChangeDeviceAttachmentsIDevice |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Desc: |
| 561 | * Test method for testChangeDeviceIPv4Address |
| 562 | * Condition: |
| 563 | * N/A |
| 564 | * Expect: |
| 565 | * 1. Check correctly changed the ipadress |
| 566 | */ |
| 567 | //@Ignore |
| 568 | @Test |
| 569 | public void testChangeDeviceIPv4Address() { |
| 570 | try |
| 571 | { |
| 572 | //Dev1 |
| 573 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 574 | String macAddr = "99:99:99:99:99:99"; |
| 575 | String ip = "192.168.100.1"; |
| 576 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 577 | Integer[] ipaddrs = {ipInt}; |
| 578 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 579 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 580 | short portNum = 2; |
| 581 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 582 | SwitchPort[] sps = {sp1}; |
| 583 | |
| 584 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 585 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 586 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 587 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 588 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 589 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 590 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 591 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 592 | EasyMock.replay(mockDev); |
| 593 | |
| 594 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 595 | assertNotNull(obj); |
| 596 | |
| 597 | IDevice mockDev2 = EasyMock.createMock(Device.class); |
| 598 | String ip2 = "192.168.100.2"; |
| 599 | Integer ipInt2 = IPv4.toIPv4Address(ip2); |
| 600 | Integer[] ipaddrs2 = {ipInt2}; |
| 601 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 602 | EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2); |
| 603 | EasyMock.replay(mockDev2); |
| 604 | |
| 605 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 606 | assertEquals(macAddr, dev1.getMACAddress()); |
| 607 | String ipFromDB = dev1.getIPAddress(); |
| 608 | String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(","); |
| 609 | List<String> ipsList = Arrays.asList(ipsFromDB); |
| 610 | assertTrue(ipsList.contains(ip)); |
| 611 | |
| 612 | deviceImpl.changeDeviceIPv4Address(mockDev2); |
| 613 | |
| 614 | IDeviceObject dev2 = ope.searchDevice(macAddr); |
| 615 | assertEquals(macAddr, dev2.getMACAddress()); |
| 616 | String ipFromDB2 = dev2.getIPAddress(); |
| 617 | String[] ipsFromDB2 = ipFromDB2.replace("[", "").replace("]", "").split(","); |
| 618 | List<String> ipsList2 = Arrays.asList(ipsFromDB2); |
| 619 | assertTrue(ipsList2.contains(ip2)); |
| 620 | } |
| 621 | catch(Exception e) { |
| 622 | fail(e.getMessage()); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | } |