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