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