blob: 44573bcb90094a638cd62af0c4c9549a1dfd4ffd [file] [log] [blame]
Teruab4b01a2013-06-20 10:09:57 -07001package net.onrc.onos.ofcontroller.devicemanager.internal;
2
HIGUCHI Yuta5fdd9152013-06-17 15:48:09 -07003import static org.easymock.EasyMock.createMock;
4import static org.easymock.EasyMock.expect;
5import static org.easymock.EasyMock.replay;
6import static org.easymock.EasyMock.verify;
7import static org.junit.Assert.assertNotNull;
8import static org.junit.Assert.assertNull;
Teruab4b01a2013-06-20 10:09:57 -07009
10import java.util.ArrayList;
Jonathan Hart56d15772013-11-03 19:09:59 -080011import java.util.Collections;
Teruab4b01a2013-06-20 10:09:57 -070012import java.util.List;
13
Teruab4b01a2013-06-20 10:09:57 -070014import net.floodlightcontroller.devicemanager.IDevice;
15import net.floodlightcontroller.devicemanager.SwitchPort;
Pankaj Berde38646d62013-06-21 11:34:04 -070016import net.onrc.onos.graph.GraphDBConnection;
17import net.onrc.onos.graph.GraphDBOperation;
HIGUCHI Yuta5fdd9152013-06-17 15:48:09 -070018import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hart56d15772013-11-03 19:09:59 -080019import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
HIGUCHI Yuta5fdd9152013-06-17 15:48:09 -070020import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
21import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
22import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
Jonathan Hart56d15772013-11-03 19:09:59 -080023
Teruab4b01a2013-06-20 10:09:57 -070024import org.easymock.EasyMock;
25import org.junit.After;
26import org.junit.Before;
Teru4fd58642013-06-21 07:54:49 -070027import org.junit.Ignore;
Teruab4b01a2013-06-20 10:09:57 -070028import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.openflow.util.HexString;
31import org.powermock.api.easymock.PowerMock;
32import org.powermock.core.classloader.annotations.PrepareForTest;
33import org.powermock.modules.junit4.PowerMockRunner;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
Jonathan Hart56d15772013-11-03 19:09:59 -080036
37import com.google.common.net.InetAddresses;
Teruab4b01a2013-06-20 10:09:57 -070038import com.thinkaurelius.titan.core.TitanFactory;
39
40//Add Powermock preparation
Jonathan Hartfb698512013-11-19 17:11:29 -080041@Ignore //TODO broken 11/19/13, should fix
Teruab4b01a2013-06-20 10:09:57 -070042@RunWith(PowerMockRunner.class)
43@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, DeviceStorageImpl.class})
Jonathan Hart56d15772013-11-03 19:09:59 -080044public class DeviceStorageImplTest{
Teruab4b01a2013-06-20 10:09:57 -070045
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070046 protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Teruab4b01a2013-06-20 10:09:57 -070047
48 String conf;
49 DeviceStorageImpl deviceImpl;
50 private GraphDBConnection mockConn;
51 private GraphDBOperation mockOpe;
52
53 @Before
54 public void setUp() throws Exception {
Teru4fd58642013-06-21 07:54:49 -070055 deviceImpl = new DeviceStorageImpl();
56 conf = "/dummy/path/to/db";
57
Teruab4b01a2013-06-20 10:09:57 -070058 PowerMock.mockStatic(GraphDBConnection.class);
59 mockConn = createMock(GraphDBConnection.class);
60 PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class));
61 EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn);
62 PowerMock.replay(GraphDBConnection.class);
63
64 //PowerMock.mockStatic(GraphDBOperation.class);
65 mockOpe = PowerMock.createMock(GraphDBOperation.class);
Teru4fd58642013-06-21 07:54:49 -070066 PowerMock.expectNew(GraphDBOperation.class, new Class<?>[]{String.class}, conf).andReturn(mockOpe);
Jonathan Hart56d15772013-11-03 19:09:59 -080067 //mockOpe.close();
Teruab4b01a2013-06-20 10:09:57 -070068 PowerMock.replay(GraphDBOperation.class);
69 // Replace the conf to dummy conf
70 // String conf = "/tmp/cassandra.titan";
Teru4fd58642013-06-21 07:54:49 -070071
Jonathan Hart56d15772013-11-03 19:09:59 -080072 deviceImpl.init(conf);
Teru4fd58642013-06-21 07:54:49 -070073
Teruab4b01a2013-06-20 10:09:57 -070074 }
75
76 @After
77 public void tearDown() throws Exception {
Teru4fd58642013-06-21 07:54:49 -070078 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -070079 }
80
Jonathan Hart56d15772013-11-03 19:09:59 -080081 private IPortObject getMockPort(long dpid, short port) {
82 IPortObject mockPortObject = createMock(IPortObject.class);
83 expect(mockPortObject.getNumber()).andReturn(port).anyTimes();
84 expect(mockPortObject.getDesc()).andReturn("test port").anyTimes();
85 return mockPortObject;
Teruab4b01a2013-06-20 10:09:57 -070086 }
87
Jonathan Hart56d15772013-11-03 19:09:59 -080088 private IDevice getMockDevice(String strMacAddress, long attachmentDpid,
89 short attachmentPort, int ipv4Address) {
90 IDevice mockIDevice = createMock(IDevice.class);
91
92
93 long longMacAddress = HexString.toLong(strMacAddress);
94
95 SwitchPort[] attachmentSwitchPorts = {new SwitchPort(attachmentDpid, attachmentPort)};
96
97 expect(mockIDevice.getMACAddress()).andReturn(longMacAddress).anyTimes();
98 expect(mockIDevice.getMACAddressString()).andReturn(strMacAddress).anyTimes();
99 expect(mockIDevice.getAttachmentPoints()).andReturn(attachmentSwitchPorts).anyTimes();
100 expect(mockIDevice.getIPv4Addresses()).andReturn(new Integer[] {ipv4Address}).anyTimes();
101
102 replay(mockIDevice);
103
104 return mockIDevice;
105 }
106
Teruab4b01a2013-06-20 10:09:57 -0700107 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800108 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700109 * Test method for addDevice method.
Jonathan Hart56d15772013-11-03 19:09:59 -0800110 * Condition:
111 * The device does not already exist in the database
Teruab4b01a2013-06-20 10:09:57 -0700112 * Expect:
113 * Get proper IDeviceObject
114 */
Teruab4b01a2013-06-20 10:09:57 -0700115 @Test
116 public void testAddNewDevice() {
Jonathan Hart56d15772013-11-03 19:09:59 -0800117 String strMacAddress = "99:99:99:99:99:99";
118 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
119 short attachmentPort = 2;
120 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
Teruab4b01a2013-06-20 10:09:57 -0700121
Jonathan Hart56d15772013-11-03 19:09:59 -0800122 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
123
124 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
125 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
126 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
127
128 expect(mockOpe.searchDevice(strMacAddress)).andReturn(null);
129 expect(mockOpe.newDevice()).andReturn(mockDeviceObject);
130 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.<IPortObject>emptyList());
131 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
132 mockPortObject.setDevice(mockDeviceObject);
133 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null);
134 expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
135 mockDeviceObject.addIpv4Address(mockIpv4Address);
136 expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address));
137 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
138
139 mockDeviceObject.setMACAddress(strMacAddress);
140 mockDeviceObject.setType("device");
141 mockDeviceObject.setState("ACTIVE");
142 mockOpe.commit();
143
144 replay(mockDeviceObject);
145 replay(mockPortObject);
146 replay(mockIpv4Address);
147 replay(mockOpe);
148
149 IDeviceObject addedObject = deviceImpl.addDevice(device);
150 assertNotNull(addedObject);
151
152 verify(mockDeviceObject);
Teruab4b01a2013-06-20 10:09:57 -0700153 }
154
155 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800156 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700157 * Test method for addDevice method.
158 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800159 * The device already exists in the database.
Teruab4b01a2013-06-20 10:09:57 -0700160 * Expect:
161 * Get proper IDeviceObject still.
162 * Check the IDeviceObject properties set expectedly.
163 */
Teruab4b01a2013-06-20 10:09:57 -0700164 @Test
Jonathan Hart56d15772013-11-03 19:09:59 -0800165 public void testAddExistingDevice() {
166 String strMacAddress = "99:99:99:99:99:99";
167 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
168 short attachmentPort = 2;
169 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
Teruab4b01a2013-06-20 10:09:57 -0700170
Jonathan Hart56d15772013-11-03 19:09:59 -0800171 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
172
173 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
174 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
175 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
176
177 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
178 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singleton(mockPortObject));
179 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
180 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
181 expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address));
182 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
183
184 mockDeviceObject.setMACAddress(strMacAddress);
185 mockDeviceObject.setType("device");
186 mockDeviceObject.setState("ACTIVE");
187 mockOpe.commit();
188
189 replay(mockDeviceObject);
190 replay(mockPortObject);
191 replay(mockIpv4Address);
192 replay(mockOpe);
193
194 IDeviceObject addedObject = deviceImpl.addDevice(device);
195 assertNotNull(addedObject);
196
197 verify(mockDeviceObject);
Teruab4b01a2013-06-20 10:09:57 -0700198 }
199
200 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800201 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700202 * Test method for updateDevice method.
Jonathan Hart56d15772013-11-03 19:09:59 -0800203 * NB. this is the same test as testAddExistingDevice
Teruab4b01a2013-06-20 10:09:57 -0700204 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800205 * The MAC address and attachment point are the same.
Teruab4b01a2013-06-20 10:09:57 -0700206 * All of the other parameter are different.
207 * Expect:
208 * Changed parameters are set expectedly.
209 */
Teruab4b01a2013-06-20 10:09:57 -0700210 @Test
Jonathan Hart56d15772013-11-03 19:09:59 -0800211 public void testAddUpdateDevice() {
212 String strMacAddress = "99:99:99:99:99:99";
213 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
214 short attachmentPort = 2;
215 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
216
217 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
218
219 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
220 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
221 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
222
223 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
224 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singleton(mockPortObject));
225 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
226 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
227 expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address));
228 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
229
230 mockDeviceObject.setMACAddress(strMacAddress);
231 mockDeviceObject.setType("device");
232 mockDeviceObject.setState("ACTIVE");
233 mockOpe.commit();
234
235 replay(mockDeviceObject);
236 replay(mockPortObject);
237 replay(mockIpv4Address);
238 replay(mockOpe);
239
240 IDeviceObject addedObject = deviceImpl.updateDevice(device);
241 assertNotNull(addedObject);
242
243 verify(mockDeviceObject);
Teruab4b01a2013-06-20 10:09:57 -0700244 }
245
246 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800247 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700248 * Test method for testRemoveDevice method.
249 * Condition:
250 * 1. Unregistered IDeviceObject argument is put.
251 * Expect:
252 * 1. Nothing happen when unregistered IDeviceObject is put
253 * 2. IDeviceObject will be removed.
254 */
Teruab4b01a2013-06-20 10:09:57 -0700255 @Test
256 public void testRemoveDevice() {
Jonathan Hart56d15772013-11-03 19:09:59 -0800257 String strMacAddress = "99:99:99:99:99:99";
258 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
259 short attachmentPort = 2;
260 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
261
262 IIpv4Address ipv4AddressObject = createMock(IIpv4Address.class);
263 IDeviceObject deviceObject = createMock(IDeviceObject.class);
264 expect(deviceObject.getIpv4Addresses()).andReturn(Collections.singleton(ipv4AddressObject));
Jonathan Hartfb698512013-11-19 17:11:29 -0800265 expect(deviceObject.getMACAddress()).andReturn(strMacAddress);
Jonathan Hart56d15772013-11-03 19:09:59 -0800266 replay(deviceObject);
267
268 expect(mockOpe.searchDevice(strMacAddress)).andReturn(deviceObject);
269 mockOpe.removeIpv4Address(ipv4AddressObject);
270 mockOpe.removeDevice(deviceObject);
271 mockOpe.commit();
272 replay(mockOpe);
273
274 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
Teruab4b01a2013-06-20 10:09:57 -0700275
Jonathan Hart56d15772013-11-03 19:09:59 -0800276 deviceImpl.removeDevice(device);
277
278 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700279 }
280
281 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800282 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700283 * Test method for getDeviceByMac
284 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800285 * 1. Unregistered MAC address argument is set
Teruab4b01a2013-06-20 10:09:57 -0700286 * Expect:
Jonathan Hart56d15772013-11-03 19:09:59 -0800287 * 1.Nothing happen when you put unregistered MAC address
Teruab4b01a2013-06-20 10:09:57 -0700288 * 2.Get the proper IDeviceObject.
289 * 3.Check the IDeviceObject properties set expectedly.
290 */
Teruab4b01a2013-06-20 10:09:57 -0700291 @Test
292 public void testGetDeviceByMac() {
Jonathan Hart56d15772013-11-03 19:09:59 -0800293 String mac = "99:99:99:99:99:99";
294
295 IDeviceObject mockDevice = createMock(IDeviceObject.class);
296
297 expect(mockOpe.searchDevice(mac)).andReturn(mockDevice);
298
299 replay(mockOpe);
300
301 IDeviceObject result = deviceImpl.getDeviceByMac(mac);
302 assertNotNull(result);
303
304 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700305 }
Jonathan Hart56d15772013-11-03 19:09:59 -0800306
Teruab4b01a2013-06-20 10:09:57 -0700307 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800308 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700309 * Test method for getDeviceByIP method.
310 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800311 * 1. Unregistered IP address argument is set
Teruab4b01a2013-06-20 10:09:57 -0700312 * Expect:
Jonathan Hart56d15772013-11-03 19:09:59 -0800313 * 1. Nothing happen when you put unregistered IP address
Teruab4b01a2013-06-20 10:09:57 -0700314 * 2. Get the proper IDeviceObject.
315 * 3. Check the IDeviceObject properties set expectedly.
316 */
Teruab4b01a2013-06-20 10:09:57 -0700317 @Test
318 public void testGetDeviceByIP() {
Jonathan Hart56d15772013-11-03 19:09:59 -0800319 int nonExistingIp = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.10.50"));
320 int existingIp = InetAddresses.coerceToInteger(InetAddresses.forString("10.5.12.128"));
321
322 IDeviceObject mockDevice = createMock(IDeviceObject.class);
323 IIpv4Address mockExistingIp = createMock(IIpv4Address.class);
324 expect(mockExistingIp.getDevice()).andReturn(mockDevice);
325
326 expect(mockOpe.searchIpv4Address(nonExistingIp)).andReturn(null);
327 expect(mockOpe.searchIpv4Address(existingIp)).andReturn(mockExistingIp);
328
329 replay(mockExistingIp);
330 replay(mockOpe);
331
332 IDeviceObject result = deviceImpl.getDeviceByIP(nonExistingIp);
333 assertNull(result);
334
335 result = deviceImpl.getDeviceByIP(existingIp);
336 assertNotNull(result);
337
338 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700339 }
340
341 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800342 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700343 * Test method for testChangeDeviceAttachmentsIDevice
344 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800345 * 1. The device is not currently attached to any point.
Teruab4b01a2013-06-20 10:09:57 -0700346 * Expect:
Jonathan Hart56d15772013-11-03 19:09:59 -0800347 * 1. Nothing happen when you put nonexistent attachment point.
Teruab4b01a2013-06-20 10:09:57 -0700348 * 2. Set the attachment point expectedly;
349 */
Teruab4b01a2013-06-20 10:09:57 -0700350 @Test
Jonathan Hart56d15772013-11-03 19:09:59 -0800351 public void testChangeDeviceAttachementsWhenUnattached() {
352 String strMacAddress = "99:99:99:99:99:99";
353 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
354 short attachmentPort = 2;
355 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
356
357 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
358
359 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
360 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
361
362 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
363 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.<IPortObject>emptyList());
364 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
365 mockPortObject.setDevice(mockDeviceObject);
366 mockOpe.commit();
367
368 replay(mockDeviceObject);
369 replay(mockPortObject);
370 replay(mockOpe);
371
372 deviceImpl.changeDeviceAttachments(device);
373
374 verify(mockDeviceObject);
375 verify(mockPortObject);
376 verify(mockOpe);
377 }
Teruab4b01a2013-06-20 10:09:57 -0700378
Jonathan Hart56d15772013-11-03 19:09:59 -0800379 /**
380 * Description:
381 * Test method for testChangeDeviceAttachmentsIDevice
382 * Condition:
383 * 1. The device is currently attached to a switch, but this attachment point
384 * has now changed.
385 * Expect:
386 * 1. The device should be removed from the old attachment point.
387 * 2. Set the attachment point expectedly;
388 */
389 @Test
390 public void testChangeDeviceAttachementsWhenAttached() {
391 String strMacAddress = "99:99:99:99:99:99";
392 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
393 short attachmentPort = 2;
394 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
395
396 //Details for the port the device will be moved from
397 long alreadyAttachedDpid = HexString.toLong("00:00:00:00:00:00:0b:01");
398 short alreadyAttachedPort = 5;
399
400 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
401
402 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
403 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
404 IPortObject alreadyAttachedPortObject = getMockPort(alreadyAttachedDpid, alreadyAttachedPort);
405
406 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
407 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singletonList(alreadyAttachedPortObject));
408 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
409 mockPortObject.setDevice(mockDeviceObject);
410 alreadyAttachedPortObject.removeDevice(mockDeviceObject);
411 mockOpe.commit();
412
413 replay(mockDeviceObject);
414 replay(alreadyAttachedPortObject);
415 replay(mockPortObject);
416 replay(mockOpe);
417
418 deviceImpl.changeDeviceAttachments(device);
419
420 verify(mockDeviceObject);
421 verify(alreadyAttachedPortObject);
422 verify(mockPortObject);
423 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700424 }
425
Teru4fd58642013-06-21 07:54:49 -0700426 @Ignore
Teruab4b01a2013-06-20 10:09:57 -0700427 @Test
428 public void testChangeDeviceAttachmentsIDeviceIDeviceObject() {
429 //It is tested by the testChangeDeviceAttachmentsIDevice
Teruab4b01a2013-06-20 10:09:57 -0700430 }
431
432 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800433 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700434 * Test method for testChangeDeviceIPv4Address
435 * Condition:
436 * N/A
437 * Expect:
Jonathan Hart56d15772013-11-03 19:09:59 -0800438 * 1. Set the IP address expectedly.
Teruab4b01a2013-06-20 10:09:57 -0700439 */
Teruab4b01a2013-06-20 10:09:57 -0700440 @Test
Jonathan Hart56d15772013-11-03 19:09:59 -0800441 public void testChangeDeviceIpv4Address() {
442 String strMacAddress = "99:99:99:99:99:99";
443 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
444 short attachmentPort = 2;
445 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
446
447 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
448
449 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
450 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
451
452 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
453 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null);
454 expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
455 mockDeviceObject.addIpv4Address(mockIpv4Address);
456 expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singletonList(mockIpv4Address));
457 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
458 mockOpe.commit();
459
460 replay(mockDeviceObject);
461 replay(mockIpv4Address);
462 replay(mockOpe);
463
464 deviceImpl.changeDeviceIPv4Address(device);
465
466 verify(mockDeviceObject);
467 verify(mockIpv4Address);
468 verify(mockOpe);
469 }
470
471 /**
472 * Description:
473 * Test method for testChangeDeviceIPv4Address
474 * Condition:
475 * 1. The device had an old IP address which has now changed.
476 * Expect:
477 * 1. The old IP address should be removed from the device.
478 * 2. Set the IP address expectedly.
479 */
480 @Test
481 public void testChangeDeviceIpv4AddressAndRemoveExisting() {
482 String strMacAddress = "99:99:99:99:99:99";
483 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
484 short attachmentPort = 2;
485 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
486
487 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
488
489 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
490
491 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
492 IIpv4Address mockDeletingIpv4Address = createMock(IIpv4Address.class);
493 List<IIpv4Address> ipv4Vertices = new ArrayList<IIpv4Address>(2);
494 ipv4Vertices.add(mockIpv4Address);
495 ipv4Vertices.add(mockDeletingIpv4Address);
496
497 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
498 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null);
499 expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
Jonathan Hartfb698512013-11-19 17:11:29 -0800500 expect(mockIpv4Address.getDevice()).andReturn(null);
Jonathan Hart56d15772013-11-03 19:09:59 -0800501 mockDeviceObject.addIpv4Address(mockIpv4Address);
502 expect(mockDeviceObject.getIpv4Addresses()).andReturn(ipv4Vertices);
503 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
504 expect(mockDeletingIpv4Address.getIpv4Address()).andReturn(1);
505 mockDeviceObject.removeIpv4Address(mockDeletingIpv4Address);
506 mockOpe.commit();
507
508 replay(mockDeviceObject);
509 replay(mockIpv4Address);
510 replay(mockOpe);
511
512 deviceImpl.changeDeviceIPv4Address(device);
513
514 verify(mockDeviceObject);
515 verify(mockIpv4Address);
516 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700517 }
518
519}