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