blob: 552cb9c4ec67bfe0b72e0fe7e0dfb3abfb699ae2 [file] [log] [blame]
Teruab4b01a2013-06-20 10:09:57 -07001package net.onrc.onos.ofcontroller.devicemanager.internal;
2
3import static org.junit.Assert.*;
4
5import java.util.Arrays;
6import java.util.List;
7
8import net.floodlightcontroller.core.internal.TestDatabaseManager;
9import net.floodlightcontroller.devicemanager.IDevice;
10import net.floodlightcontroller.devicemanager.SwitchPort;
11import net.floodlightcontroller.devicemanager.internal.Device;
12import net.floodlightcontroller.packet.IPv4;
Pankaj Berde38646d62013-06-21 11:34:04 -070013import net.onrc.onos.graph.GraphDBConnection;
14import net.onrc.onos.graph.GraphDBOperation;
Teru4fd58642013-06-21 07:54:49 -070015import net.onrc.onos.ofcontroller.core.IDeviceStorage;
Teruab4b01a2013-06-20 10:09:57 -070016import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
17import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
18import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Teru4fd58642013-06-21 07:54:49 -070019import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Teruab4b01a2013-06-20 10:09:57 -070020import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
Teruab4b01a2013-06-20 10:09:57 -070021
22import org.easymock.EasyMock;
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.openflow.util.HexString;
28import org.powermock.api.easymock.PowerMock;
29import org.powermock.core.classloader.annotations.PrepareForTest;
30import org.slf4j.LoggerFactory;
31import org.powermock.modules.junit4.PowerMockRunner;
32
33import com.thinkaurelius.titan.core.TitanFactory;
34import com.thinkaurelius.titan.core.TitanGraph;
35
36//Add Powermock preparation
37@RunWith(PowerMockRunner.class)
38@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
39public 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();
Yuta HIGUCHIb63f94f2013-10-14 10:01:45 -0700217 assertEquals(String.valueOf(portNum), portNumFromDB);
Teruab4b01a2013-06-20 10:09:57 -0700218
Teruab4b01a2013-06-20 10:09:57 -0700219 }
220 }
221
222 String ipFromDB = devObj2.getIPAddress();
223 String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(",");
224 List<String> ipsList = Arrays.asList(ipsFromDB);
225 assertTrue(ipsList.contains(ip));
226
227 //Test to take a attached port from DB
228 for(IPortObject port : devObj2.getAttachedPorts())
229 {
230
231 //In this implementing, the object was not set the port. So it must be null.
232 if(port.getNumber() != null)
233 {
234 String portNumFromDB = port.getNumber().toString();
235 assertEquals(String.valueOf(portNum), portNumFromDB);
236 }
237 }
238 } catch(Exception e) {
239 fail(e.getMessage());
240 }
241 }
242 /**
243 * Desc:
244 * Test method for updateDevice method.
245 * Condition:
246 * The mac address and attachment point are the same.
247 * All of the other parameter are different.
248 * Expect:
249 * Changed parameters are set properly.
250 */
251 //@Ignore
252 @Test
253 public void testUpdateDevice() {
254 try
255 {
256 IDevice mockDev = EasyMock.createMock(Device.class);
257 String macAddr = "99:99:99:99:99:99";
258 String ip = "192.168.100.1";
259 Integer ipInt = IPv4.toIPv4Address(ip);
260 Integer[] ipaddrs = {ipInt};
261 String switchMacAddr = "00:00:00:00:00:00:0a:01";
262 long switchMacAddrL = HexString.toLong(switchMacAddr);
263 short portNum = 2;
264 SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
265 SwitchPort[] sps = {sp1};
266
267 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
268 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
269 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
270 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
271 EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
272 EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
273 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
274 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
275 EasyMock.replay(mockDev);
276
277 //Dev2 (attached port is the same)
278 IDevice mockDev2 = EasyMock.createMock(Device.class);
279 String macAddr2 = "99:aa:aa:aa:aa:aa";
280 Integer ip2 = IPv4.toIPv4Address("192.168.100.2");
281 Integer[] ipaddrs2 = {ip2};
282
283 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
284 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
285 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
286 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
287 EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2);
288 EasyMock.expect(mockDev2.getAttachmentPoints()).andReturn(sps);
289 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
290 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
291 EasyMock.replay(mockDev2);
292
293 IDeviceObject obj = deviceImpl.addDevice(mockDev);
294 assertNotNull(obj);
295
296 IDeviceObject dev1 = ope.searchDevice(macAddr);
297 assertEquals(macAddr, dev1.getMACAddress());
298
299 //update theDevice
300 deviceImpl.updateDevice(mockDev2);
301 IDeviceObject dev2 = ope.searchDevice(macAddr2);
302 assertEquals(macAddr2, dev2.getMACAddress());
303 IPortObject iport = ope.searchPort(switchMacAddr, portNum);
304
305 //Test to take a attached port from DB
306 for(IDeviceObject dev : iport.getDevices())
307 {
308 String macAddrFromDB = dev.getMACAddress();
309 if(macAddr2.equals(macAddrFromDB)){
310 //Nothing to do
311 }
312 else{
313 fail("notFoundTheDeviceOnThePort");
314 }
315 }
316
317 } catch(Exception e) {
318 fail(e.getMessage());
319 }
320 }
321
322 /**
323 * Desc:
324 * Test method for testRemoveDevice method.
325 * Condition:
326 * 1. Unregistered IDeviceObject argument is put.
327 * Expect:
328 * 1. Nothing happen when unregistered IDeviceObject is put
329 * 2. IDeviceObject will be removed.
330 */
331 //@Ignore
332 @Test
333 public void testRemoveDevice() {
334 try
335 {
336 IDevice mockDev = EasyMock.createMock(Device.class);
337 String macAddr = "99:99:99:99:99:99";
338 String ip = "192.168.100.1";
339 Integer ipInt = IPv4.toIPv4Address(ip);
340 Integer[] ipaddrs = {ipInt};
341 String switchMacAddr = "00:00:00:00:00:00:0a:01";
342 long switchMacAddrL = HexString.toLong(switchMacAddr);
343 short portNum = 2;
344 SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
345 SwitchPort[] sps = {sp1};
346
347 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
348 EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
349 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
350 EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
351 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
352 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
353
354 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
355 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
356 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
357 EasyMock.replay(mockDev);
358
359 IDeviceObject obj = deviceImpl.addDevice(mockDev);
360 assertNotNull(obj);
361
362 IDeviceObject dev1 = ope.searchDevice(macAddr);
363 assertEquals(macAddr, dev1.getMACAddress());
364
365 deviceImpl.removeDevice(mockDev);
366 IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
367 assertNull(dev);
368
369 } catch(Exception e) {
370 fail(e.getMessage());
371 }
372 }
373
374 /**
375 * Desc:
376 * Test method for getDeviceByMac
377 * Condition:
378 * 1. Unregistered mac address argument is set
379 * Expect:
380 * 1.Nothing happen when you put unregistered mac address
381 * 2.Get the proper IDeviceObject.
382 * 3.Check the IDeviceObject properties set.
383 */
384 //@Ignore
385 @Test
386 public void testGetDeviceByMac() {
387 try
388 {
389 IDevice mockDev = EasyMock.createMock(Device.class);
390 String macAddr = "99:99:99:99:99:99";
391 String ip = "192.168.100.1";
392 Integer ipInt = IPv4.toIPv4Address(ip);
393 Integer[] ipaddrs = {ipInt};
394 String switchMacAddr = "00:00:00:00:00:00:0a:01";
395 long switchMacAddrL = HexString.toLong(switchMacAddr);
396 short portNum = 2;
397 SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
398 SwitchPort[] sps = {sp1};
399
400 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
401 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
402 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
403 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
404 EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
405 EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
406 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
407 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
408 EasyMock.replay(mockDev);
409
410 IDeviceObject obj = deviceImpl.addDevice(mockDev);
411 assertNotNull(obj);
412
413 IDeviceObject dev1 = ope.searchDevice(macAddr);
414 assertEquals(macAddr, dev1.getMACAddress());
415
416 IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
417 assertNotNull(dev);
418 assertEquals(macAddr, dev.getMACAddress());
419
420 } catch(Exception e) {
421 fail(e.getMessage());
422 }
423 }
424
425 /**
426 * Desc:
427 * Test method for getDeviceByIP method.
428 * Condition:
429 * 1. Unregistered ip address argument is set
430 * Expect:
431 * 1. Nothing happen when you put unregistered mac address
432 * 2. Get the proper IDeviceObject.
433 * 3. Check the IDeviceObject properties set.
434 */
435 //@Ignore
436 @Test
437 public void testGetDeviceByIP() {
438 try
439 {
440 IDevice mockDev = EasyMock.createMock(Device.class);
441 String macAddr = "99:99:99:99:99:99";
442 String ip = "192.168.100.1";
443 Integer ipInt = IPv4.toIPv4Address(ip);
444 Integer[] ipaddrs = {ipInt};
445 String switchMacAddr = "00:00:00:00:00:00:0a:01";
446 long switchMacAddrL = HexString.toLong(switchMacAddr);
447 short portNum = 2;
448 SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
449 SwitchPort[] sps = {sp1};
450
451 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
452 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
453 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
454 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
455 EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
456 EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
457 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
458 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
459 EasyMock.replay(mockDev);
460
461 IDeviceObject obj = deviceImpl.addDevice(mockDev);
462 assertNotNull(obj);
463
464 IDeviceObject dev1 = ope.searchDevice(macAddr);
465 assertEquals(macAddr, dev1.getMACAddress());
466
467 IDeviceObject dev = deviceImpl.getDeviceByIP(ip);
468 assertNotNull(dev);
469 String ipFromDB = dev.getIPAddress();
470 String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(",");
471 List<String> ipsList = Arrays.asList(ipsFromDB);
472 assertTrue(ipsList.contains(ip));
473
474 } catch(Exception e) {
475 fail(e.getMessage());
476 }
477 }
478
479 /**
480 * Desc:
481 * Test method for testChangeDeviceAttachmentsIDevice
482 * Condition:
483 * 1. Unexisting attachment point argument is set
484 * Expect:
485 * 1. Unexisting attachment point is ignored, so nothing happen.
486 * 2. Change the attachment point.
487 */
488 //@Ignore
489 @Test
490 public void testChangeDeviceAttachmentsIDevice() {
491 try
492 {
493 IDevice mockDev = EasyMock.createMock(Device.class);
494 String macAddr = "99:99:99:99:99:99";
495 String ip = "192.168.100.1";
496 Integer ipInt = IPv4.toIPv4Address(ip);
497 Integer[] ipaddrs = {ipInt};
498 String switchMacAddr = "00:00:00:00:00:00:0a:01";
499 long switchMacAddrL = HexString.toLong(switchMacAddr);
500 short portNum = 2;
501 SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
502 SwitchPort[] sps = {sp1};
503
504 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
505 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
506 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
507 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
508 EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
509 EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
510 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
511 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
512 EasyMock.replay(mockDev);
513
514 //Dev2
515 IDevice mockDev2 = EasyMock.createMock(Device.class);
516 String switchMacAddr2 = "00:00:00:00:00:00:0a:02";
517 long lSwitchMacAddr2 = HexString.toLong(switchMacAddr2);
518 short portNum2 = 2;
519 SwitchPort sp2 = new SwitchPort(lSwitchMacAddr2, portNum2);
520 SwitchPort sps2[] = {sp2};
521
522 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
523 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
524 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
525 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
526 EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs);
527 EasyMock.expect(mockDev2.getAttachmentPoints()).andReturn(sps2);
528 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
529 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
530 EasyMock.replay(mockDev2);
531
532 IDeviceObject obj = deviceImpl.addDevice(mockDev);
533 assertNotNull(obj);
534
535 deviceImpl.changeDeviceAttachments(mockDev2);
536
537 IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
538 assertNotNull(dev);
539
540 for(ISwitchObject sw1: dev.getSwitch())
541 {
542 String swMacFromDB = sw1.getDPID();
543 assertEquals(switchMacAddr2, swMacFromDB);
544 }
545 } catch(Exception e) {
546 fail(e.getMessage());
547 }
548 }
549
550 //@Ignore
551 @Test
552 public void testChangeDeviceAttachmentsIDeviceIDeviceObject() {
553 //It is tested by the testChangeDeviceAttachmentsIDevice
554 }
555
556 /**
557 * Desc:
558 * Test method for testChangeDeviceIPv4Address
559 * Condition:
560 * N/A
561 * Expect:
562 * 1. Check correctly changed the ipadress
563 */
564 //@Ignore
565 @Test
566 public void testChangeDeviceIPv4Address() {
567 try
568 {
569 //Dev1
570 IDevice mockDev = EasyMock.createMock(Device.class);
571 String macAddr = "99:99:99:99:99:99";
572 String ip = "192.168.100.1";
573 Integer ipInt = IPv4.toIPv4Address(ip);
574 Integer[] ipaddrs = {ipInt};
575 String switchMacAddr = "00:00:00:00:00:00:0a:01";
576 long switchMacAddrL = HexString.toLong(switchMacAddr);
577 short portNum = 2;
578 SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
579 SwitchPort[] sps = {sp1};
580
581 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
582 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
583 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
584 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
585 EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
586 EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
587 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
588 EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
589 EasyMock.replay(mockDev);
590
591 IDeviceObject obj = deviceImpl.addDevice(mockDev);
592 assertNotNull(obj);
593
594 IDevice mockDev2 = EasyMock.createMock(Device.class);
595 String ip2 = "192.168.100.2";
596 Integer ipInt2 = IPv4.toIPv4Address(ip2);
597 Integer[] ipaddrs2 = {ipInt2};
598 EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
599 EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2);
600 EasyMock.replay(mockDev2);
601
602 IDeviceObject dev1 = ope.searchDevice(macAddr);
603 assertEquals(macAddr, dev1.getMACAddress());
604 String ipFromDB = dev1.getIPAddress();
605 String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(",");
606 List<String> ipsList = Arrays.asList(ipsFromDB);
607 assertTrue(ipsList.contains(ip));
608
609 deviceImpl.changeDeviceIPv4Address(mockDev2);
610
611 IDeviceObject dev2 = ope.searchDevice(macAddr);
612 assertEquals(macAddr, dev2.getMACAddress());
613 String ipFromDB2 = dev2.getIPAddress();
614 String[] ipsFromDB2 = ipFromDB2.replace("[", "").replace("]", "").split(",");
615 List<String> ipsList2 = Arrays.asList(ipsFromDB2);
616 assertTrue(ipsList2.contains(ip2));
617 }
618 catch(Exception e) {
619 fail(e.getMessage());
620 }
621 }
622
623}