blob: d01754645f91e9e0b218ed0c3023319a06882500 [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;
yoshi2fd4c7e2013-11-22 15:47:55 -080049 String dbStore;
Teruab4b01a2013-06-20 10:09:57 -070050 DeviceStorageImpl deviceImpl;
51 private GraphDBConnection mockConn;
52 private GraphDBOperation mockOpe;
53
54 @Before
55 public void setUp() throws Exception {
Teru4fd58642013-06-21 07:54:49 -070056 deviceImpl = new DeviceStorageImpl();
57 conf = "/dummy/path/to/db";
yoshi2fd4c7e2013-11-22 15:47:55 -080058 dbStore = "dummyStore";
Teru4fd58642013-06-21 07:54:49 -070059
Teruab4b01a2013-06-20 10:09:57 -070060 PowerMock.mockStatic(GraphDBConnection.class);
61 mockConn = createMock(GraphDBConnection.class);
62 PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class));
63 EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn);
64 PowerMock.replay(GraphDBConnection.class);
65
66 //PowerMock.mockStatic(GraphDBOperation.class);
67 mockOpe = PowerMock.createMock(GraphDBOperation.class);
Teru4fd58642013-06-21 07:54:49 -070068 PowerMock.expectNew(GraphDBOperation.class, new Class<?>[]{String.class}, conf).andReturn(mockOpe);
Jonathan Hart56d15772013-11-03 19:09:59 -080069 //mockOpe.close();
Teruab4b01a2013-06-20 10:09:57 -070070 PowerMock.replay(GraphDBOperation.class);
71 // Replace the conf to dummy conf
72 // String conf = "/tmp/cassandra.titan";
Teru4fd58642013-06-21 07:54:49 -070073
yoshitomo03c0d9d2013-11-23 14:58:11 -080074 deviceImpl.init(dbStore, conf);
Teru4fd58642013-06-21 07:54:49 -070075
Teruab4b01a2013-06-20 10:09:57 -070076 }
77
78 @After
79 public void tearDown() throws Exception {
Teru4fd58642013-06-21 07:54:49 -070080 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -070081 }
82
Jonathan Hart56d15772013-11-03 19:09:59 -080083 private IPortObject getMockPort(long dpid, short port) {
84 IPortObject mockPortObject = createMock(IPortObject.class);
85 expect(mockPortObject.getNumber()).andReturn(port).anyTimes();
86 expect(mockPortObject.getDesc()).andReturn("test port").anyTimes();
87 return mockPortObject;
Teruab4b01a2013-06-20 10:09:57 -070088 }
89
Jonathan Hart56d15772013-11-03 19:09:59 -080090 private IDevice getMockDevice(String strMacAddress, long attachmentDpid,
91 short attachmentPort, int ipv4Address) {
92 IDevice mockIDevice = createMock(IDevice.class);
93
94
95 long longMacAddress = HexString.toLong(strMacAddress);
96
97 SwitchPort[] attachmentSwitchPorts = {new SwitchPort(attachmentDpid, attachmentPort)};
98
99 expect(mockIDevice.getMACAddress()).andReturn(longMacAddress).anyTimes();
100 expect(mockIDevice.getMACAddressString()).andReturn(strMacAddress).anyTimes();
101 expect(mockIDevice.getAttachmentPoints()).andReturn(attachmentSwitchPorts).anyTimes();
102 expect(mockIDevice.getIPv4Addresses()).andReturn(new Integer[] {ipv4Address}).anyTimes();
103
104 replay(mockIDevice);
105
106 return mockIDevice;
107 }
108
Teruab4b01a2013-06-20 10:09:57 -0700109 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800110 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700111 * Test method for addDevice method.
Jonathan Hart56d15772013-11-03 19:09:59 -0800112 * Condition:
113 * The device does not already exist in the database
Teruab4b01a2013-06-20 10:09:57 -0700114 * Expect:
115 * Get proper IDeviceObject
116 */
Teruab4b01a2013-06-20 10:09:57 -0700117 @Test
118 public void testAddNewDevice() {
Jonathan Hart56d15772013-11-03 19:09:59 -0800119 String strMacAddress = "99:99:99:99:99:99";
120 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
121 short attachmentPort = 2;
122 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
Teruab4b01a2013-06-20 10:09:57 -0700123
Jonathan Hart56d15772013-11-03 19:09:59 -0800124 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
125
126 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
127 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
128 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
129
130 expect(mockOpe.searchDevice(strMacAddress)).andReturn(null);
131 expect(mockOpe.newDevice()).andReturn(mockDeviceObject);
132 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.<IPortObject>emptyList());
133 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
134 mockPortObject.setDevice(mockDeviceObject);
135 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null);
136 expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
137 mockDeviceObject.addIpv4Address(mockIpv4Address);
138 expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address));
139 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
140
141 mockDeviceObject.setMACAddress(strMacAddress);
142 mockDeviceObject.setType("device");
143 mockDeviceObject.setState("ACTIVE");
144 mockOpe.commit();
145
146 replay(mockDeviceObject);
147 replay(mockPortObject);
148 replay(mockIpv4Address);
149 replay(mockOpe);
150
151 IDeviceObject addedObject = deviceImpl.addDevice(device);
152 assertNotNull(addedObject);
153
154 verify(mockDeviceObject);
Teruab4b01a2013-06-20 10:09:57 -0700155 }
156
157 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800158 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700159 * Test method for addDevice method.
160 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800161 * The device already exists in the database.
Teruab4b01a2013-06-20 10:09:57 -0700162 * Expect:
163 * Get proper IDeviceObject still.
164 * Check the IDeviceObject properties set expectedly.
165 */
Teruab4b01a2013-06-20 10:09:57 -0700166 @Test
Jonathan Hart56d15772013-11-03 19:09:59 -0800167 public void testAddExistingDevice() {
168 String strMacAddress = "99:99:99:99:99:99";
169 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
170 short attachmentPort = 2;
171 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
Teruab4b01a2013-06-20 10:09:57 -0700172
Jonathan Hart56d15772013-11-03 19:09:59 -0800173 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
174
175 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
176 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
177 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
178
179 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
180 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singleton(mockPortObject));
181 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
182 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
183 expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address));
184 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
185
186 mockDeviceObject.setMACAddress(strMacAddress);
187 mockDeviceObject.setType("device");
188 mockDeviceObject.setState("ACTIVE");
189 mockOpe.commit();
190
191 replay(mockDeviceObject);
192 replay(mockPortObject);
193 replay(mockIpv4Address);
194 replay(mockOpe);
195
196 IDeviceObject addedObject = deviceImpl.addDevice(device);
197 assertNotNull(addedObject);
198
199 verify(mockDeviceObject);
Teruab4b01a2013-06-20 10:09:57 -0700200 }
201
202 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800203 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700204 * Test method for updateDevice method.
Jonathan Hart56d15772013-11-03 19:09:59 -0800205 * NB. this is the same test as testAddExistingDevice
Teruab4b01a2013-06-20 10:09:57 -0700206 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800207 * The MAC address and attachment point are the same.
Teruab4b01a2013-06-20 10:09:57 -0700208 * All of the other parameter are different.
209 * Expect:
210 * Changed parameters are set expectedly.
211 */
Teruab4b01a2013-06-20 10:09:57 -0700212 @Test
Jonathan Hart56d15772013-11-03 19:09:59 -0800213 public void testAddUpdateDevice() {
214 String strMacAddress = "99:99:99:99:99:99";
215 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
216 short attachmentPort = 2;
217 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
218
219 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
220
221 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
222 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
223 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
224
225 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
226 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singleton(mockPortObject));
227 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
228 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
229 expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address));
230 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
231
232 mockDeviceObject.setMACAddress(strMacAddress);
233 mockDeviceObject.setType("device");
234 mockDeviceObject.setState("ACTIVE");
235 mockOpe.commit();
236
237 replay(mockDeviceObject);
238 replay(mockPortObject);
239 replay(mockIpv4Address);
240 replay(mockOpe);
241
242 IDeviceObject addedObject = deviceImpl.updateDevice(device);
243 assertNotNull(addedObject);
244
245 verify(mockDeviceObject);
yoshi0fee3de2013-11-23 09:13:37 -0800246
Teruab4b01a2013-06-20 10:09:57 -0700247 }
248
249 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800250 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700251 * Test method for testRemoveDevice method.
252 * Condition:
253 * 1. Unregistered IDeviceObject argument is put.
254 * Expect:
255 * 1. Nothing happen when unregistered IDeviceObject is put
256 * 2. IDeviceObject will be removed.
257 */
Teruab4b01a2013-06-20 10:09:57 -0700258 @Test
259 public void testRemoveDevice() {
Jonathan Hart56d15772013-11-03 19:09:59 -0800260 String strMacAddress = "99:99:99:99:99:99";
261 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
262 short attachmentPort = 2;
263 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
264
265 IIpv4Address ipv4AddressObject = createMock(IIpv4Address.class);
266 IDeviceObject deviceObject = createMock(IDeviceObject.class);
267 expect(deviceObject.getIpv4Addresses()).andReturn(Collections.singleton(ipv4AddressObject));
Jonathan Hartfb698512013-11-19 17:11:29 -0800268 expect(deviceObject.getMACAddress()).andReturn(strMacAddress);
Jonathan Hart56d15772013-11-03 19:09:59 -0800269 replay(deviceObject);
270
271 expect(mockOpe.searchDevice(strMacAddress)).andReturn(deviceObject);
272 mockOpe.removeIpv4Address(ipv4AddressObject);
273 mockOpe.removeDevice(deviceObject);
274 mockOpe.commit();
275 replay(mockOpe);
276
277 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
Teruab4b01a2013-06-20 10:09:57 -0700278
Jonathan Hart56d15772013-11-03 19:09:59 -0800279 deviceImpl.removeDevice(device);
280
281 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700282 }
283
284 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800285 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700286 * Test method for getDeviceByMac
287 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800288 * 1. Unregistered MAC address argument is set
Teruab4b01a2013-06-20 10:09:57 -0700289 * Expect:
Jonathan Hart56d15772013-11-03 19:09:59 -0800290 * 1.Nothing happen when you put unregistered MAC address
Teruab4b01a2013-06-20 10:09:57 -0700291 * 2.Get the proper IDeviceObject.
292 * 3.Check the IDeviceObject properties set expectedly.
293 */
Teruab4b01a2013-06-20 10:09:57 -0700294 @Test
295 public void testGetDeviceByMac() {
Jonathan Hart56d15772013-11-03 19:09:59 -0800296 String mac = "99:99:99:99:99:99";
297
298 IDeviceObject mockDevice = createMock(IDeviceObject.class);
299
300 expect(mockOpe.searchDevice(mac)).andReturn(mockDevice);
301
302 replay(mockOpe);
303
304 IDeviceObject result = deviceImpl.getDeviceByMac(mac);
305 assertNotNull(result);
306
307 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700308 }
Jonathan Hart56d15772013-11-03 19:09:59 -0800309
Teruab4b01a2013-06-20 10:09:57 -0700310 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800311 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700312 * Test method for getDeviceByIP method.
313 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800314 * 1. Unregistered IP address argument is set
Teruab4b01a2013-06-20 10:09:57 -0700315 * Expect:
Jonathan Hart56d15772013-11-03 19:09:59 -0800316 * 1. Nothing happen when you put unregistered IP address
Teruab4b01a2013-06-20 10:09:57 -0700317 * 2. Get the proper IDeviceObject.
318 * 3. Check the IDeviceObject properties set expectedly.
319 */
Teruab4b01a2013-06-20 10:09:57 -0700320 @Test
321 public void testGetDeviceByIP() {
Jonathan Hart56d15772013-11-03 19:09:59 -0800322 int nonExistingIp = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.10.50"));
323 int existingIp = InetAddresses.coerceToInteger(InetAddresses.forString("10.5.12.128"));
324
325 IDeviceObject mockDevice = createMock(IDeviceObject.class);
326 IIpv4Address mockExistingIp = createMock(IIpv4Address.class);
327 expect(mockExistingIp.getDevice()).andReturn(mockDevice);
328
329 expect(mockOpe.searchIpv4Address(nonExistingIp)).andReturn(null);
330 expect(mockOpe.searchIpv4Address(existingIp)).andReturn(mockExistingIp);
331
332 replay(mockExistingIp);
333 replay(mockOpe);
334
335 IDeviceObject result = deviceImpl.getDeviceByIP(nonExistingIp);
336 assertNull(result);
337
338 result = deviceImpl.getDeviceByIP(existingIp);
339 assertNotNull(result);
340
341 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700342 }
343
344 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800345 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700346 * Test method for testChangeDeviceAttachmentsIDevice
347 * Condition:
Jonathan Hart56d15772013-11-03 19:09:59 -0800348 * 1. The device is not currently attached to any point.
Teruab4b01a2013-06-20 10:09:57 -0700349 * Expect:
Jonathan Hart56d15772013-11-03 19:09:59 -0800350 * 1. Nothing happen when you put nonexistent attachment point.
Teruab4b01a2013-06-20 10:09:57 -0700351 * 2. Set the attachment point expectedly;
352 */
Teruab4b01a2013-06-20 10:09:57 -0700353 @Test
Jonathan Hart56d15772013-11-03 19:09:59 -0800354 public void testChangeDeviceAttachementsWhenUnattached() {
355 String strMacAddress = "99:99:99:99:99:99";
356 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
357 short attachmentPort = 2;
358 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
359
360 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
361
362 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
363 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
364
365 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
366 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.<IPortObject>emptyList());
367 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
368 mockPortObject.setDevice(mockDeviceObject);
369 mockOpe.commit();
370
371 replay(mockDeviceObject);
372 replay(mockPortObject);
373 replay(mockOpe);
374
375 deviceImpl.changeDeviceAttachments(device);
376
377 verify(mockDeviceObject);
378 verify(mockPortObject);
379 verify(mockOpe);
380 }
Teruab4b01a2013-06-20 10:09:57 -0700381
Jonathan Hart56d15772013-11-03 19:09:59 -0800382 /**
383 * Description:
384 * Test method for testChangeDeviceAttachmentsIDevice
385 * Condition:
386 * 1. The device is currently attached to a switch, but this attachment point
387 * has now changed.
388 * Expect:
389 * 1. The device should be removed from the old attachment point.
390 * 2. Set the attachment point expectedly;
391 */
392 @Test
393 public void testChangeDeviceAttachementsWhenAttached() {
394 String strMacAddress = "99:99:99:99:99:99";
395 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
396 short attachmentPort = 2;
397 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
398
399 //Details for the port the device will be moved from
400 long alreadyAttachedDpid = HexString.toLong("00:00:00:00:00:00:0b:01");
401 short alreadyAttachedPort = 5;
402
403 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
404
405 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
406 IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
407 IPortObject alreadyAttachedPortObject = getMockPort(alreadyAttachedDpid, alreadyAttachedPort);
408
409 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
410 expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singletonList(alreadyAttachedPortObject));
411 expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
412 mockPortObject.setDevice(mockDeviceObject);
413 alreadyAttachedPortObject.removeDevice(mockDeviceObject);
414 mockOpe.commit();
415
416 replay(mockDeviceObject);
417 replay(alreadyAttachedPortObject);
418 replay(mockPortObject);
419 replay(mockOpe);
420
421 deviceImpl.changeDeviceAttachments(device);
422
423 verify(mockDeviceObject);
424 verify(alreadyAttachedPortObject);
425 verify(mockPortObject);
426 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700427 }
428
Teru4fd58642013-06-21 07:54:49 -0700429 @Ignore
Teruab4b01a2013-06-20 10:09:57 -0700430 @Test
431 public void testChangeDeviceAttachmentsIDeviceIDeviceObject() {
432 //It is tested by the testChangeDeviceAttachmentsIDevice
Teruab4b01a2013-06-20 10:09:57 -0700433 }
434
435 /**
Jonathan Hart56d15772013-11-03 19:09:59 -0800436 * Description:
Teruab4b01a2013-06-20 10:09:57 -0700437 * Test method for testChangeDeviceIPv4Address
438 * Condition:
439 * N/A
440 * Expect:
Jonathan Hart56d15772013-11-03 19:09:59 -0800441 * 1. Set the IP address expectedly.
Teruab4b01a2013-06-20 10:09:57 -0700442 */
Teruab4b01a2013-06-20 10:09:57 -0700443 @Test
Jonathan Hart56d15772013-11-03 19:09:59 -0800444 public void testChangeDeviceIpv4Address() {
445 String strMacAddress = "99:99:99:99:99:99";
446 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
447 short attachmentPort = 2;
448 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
449
450 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
451
452 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
453 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
454
455 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
456 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null);
457 expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
458 mockDeviceObject.addIpv4Address(mockIpv4Address);
459 expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singletonList(mockIpv4Address));
460 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
461 mockOpe.commit();
462
463 replay(mockDeviceObject);
464 replay(mockIpv4Address);
465 replay(mockOpe);
466
467 deviceImpl.changeDeviceIPv4Address(device);
468
469 verify(mockDeviceObject);
470 verify(mockIpv4Address);
471 verify(mockOpe);
472 }
473
474 /**
475 * Description:
476 * Test method for testChangeDeviceIPv4Address
477 * Condition:
478 * 1. The device had an old IP address which has now changed.
479 * Expect:
480 * 1. The old IP address should be removed from the device.
481 * 2. Set the IP address expectedly.
482 */
483 @Test
484 public void testChangeDeviceIpv4AddressAndRemoveExisting() {
485 String strMacAddress = "99:99:99:99:99:99";
486 long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
487 short attachmentPort = 2;
488 int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
489
490 IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
491
492 IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
493
494 IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
495 IIpv4Address mockDeletingIpv4Address = createMock(IIpv4Address.class);
496 List<IIpv4Address> ipv4Vertices = new ArrayList<IIpv4Address>(2);
497 ipv4Vertices.add(mockIpv4Address);
498 ipv4Vertices.add(mockDeletingIpv4Address);
499
500 expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
501 expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null);
502 expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
Jonathan Hartfb698512013-11-19 17:11:29 -0800503 expect(mockIpv4Address.getDevice()).andReturn(null);
Jonathan Hart56d15772013-11-03 19:09:59 -0800504 mockDeviceObject.addIpv4Address(mockIpv4Address);
505 expect(mockDeviceObject.getIpv4Addresses()).andReturn(ipv4Vertices);
506 expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
507 expect(mockDeletingIpv4Address.getIpv4Address()).andReturn(1);
508 mockDeviceObject.removeIpv4Address(mockDeletingIpv4Address);
509 mockOpe.commit();
510
511 replay(mockDeviceObject);
512 replay(mockIpv4Address);
513 replay(mockOpe);
514
515 deviceImpl.changeDeviceIPv4Address(device);
516
517 verify(mockDeviceObject);
518 verify(mockIpv4Address);
519 verify(mockOpe);
Teruab4b01a2013-06-20 10:09:57 -0700520 }
521
522}