Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.devicemanager.internal; |
| 2 | |
HIGUCHI Yuta | 5fdd915 | 2013-06-17 15:48:09 -0700 | [diff] [blame] | 3 | import static org.easymock.EasyMock.createMock; |
| 4 | import static org.easymock.EasyMock.expect; |
| 5 | import static org.easymock.EasyMock.replay; |
| 6 | import static org.easymock.EasyMock.verify; |
| 7 | import static org.junit.Assert.assertNotNull; |
| 8 | import static org.junit.Assert.assertNull; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 9 | |
| 10 | import java.util.ArrayList; |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 11 | import java.util.Collections; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 12 | import java.util.List; |
| 13 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 14 | import net.floodlightcontroller.devicemanager.IDevice; |
| 15 | import net.floodlightcontroller.devicemanager.SwitchPort; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 16 | import net.onrc.onos.graph.GraphDBConnection; |
| 17 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 5fdd915 | 2013-06-17 15:48:09 -0700 | [diff] [blame] | 18 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 19 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address; |
HIGUCHI Yuta | 5fdd915 | 2013-06-17 15:48:09 -0700 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 21 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
| 22 | import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl; |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 23 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 24 | import org.easymock.EasyMock; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 27 | import org.junit.Ignore; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.openflow.util.HexString; |
| 31 | import org.powermock.api.easymock.PowerMock; |
| 32 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 33 | import org.powermock.modules.junit4.PowerMockRunner; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 36 | |
| 37 | import com.google.common.net.InetAddresses; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 38 | import com.thinkaurelius.titan.core.TitanFactory; |
| 39 | |
| 40 | //Add Powermock preparation |
| 41 | @RunWith(PowerMockRunner.class) |
| 42 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, DeviceStorageImpl.class}) |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 43 | public class DeviceStorageImplTest{ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 44 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 45 | protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 46 | |
| 47 | String conf; |
| 48 | DeviceStorageImpl deviceImpl; |
| 49 | private GraphDBConnection mockConn; |
| 50 | private GraphDBOperation mockOpe; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() throws Exception { |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 54 | deviceImpl = new DeviceStorageImpl(); |
| 55 | conf = "/dummy/path/to/db"; |
| 56 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 57 | PowerMock.mockStatic(GraphDBConnection.class); |
| 58 | mockConn = createMock(GraphDBConnection.class); |
| 59 | PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class)); |
| 60 | EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn); |
| 61 | PowerMock.replay(GraphDBConnection.class); |
| 62 | |
| 63 | //PowerMock.mockStatic(GraphDBOperation.class); |
| 64 | mockOpe = PowerMock.createMock(GraphDBOperation.class); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 65 | PowerMock.expectNew(GraphDBOperation.class, new Class<?>[]{String.class}, conf).andReturn(mockOpe); |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 66 | //mockOpe.close(); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 67 | PowerMock.replay(GraphDBOperation.class); |
| 68 | // Replace the conf to dummy conf |
| 69 | // String conf = "/tmp/cassandra.titan"; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 70 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 71 | deviceImpl.init(conf); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 72 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @After |
| 76 | public void tearDown() throws Exception { |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 77 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 80 | private IPortObject getMockPort(long dpid, short port) { |
| 81 | IPortObject mockPortObject = createMock(IPortObject.class); |
| 82 | expect(mockPortObject.getNumber()).andReturn(port).anyTimes(); |
| 83 | expect(mockPortObject.getDesc()).andReturn("test port").anyTimes(); |
| 84 | return mockPortObject; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 87 | private IDevice getMockDevice(String strMacAddress, long attachmentDpid, |
| 88 | short attachmentPort, int ipv4Address) { |
| 89 | IDevice mockIDevice = createMock(IDevice.class); |
| 90 | |
| 91 | |
| 92 | long longMacAddress = HexString.toLong(strMacAddress); |
| 93 | |
| 94 | SwitchPort[] attachmentSwitchPorts = {new SwitchPort(attachmentDpid, attachmentPort)}; |
| 95 | |
| 96 | expect(mockIDevice.getMACAddress()).andReturn(longMacAddress).anyTimes(); |
| 97 | expect(mockIDevice.getMACAddressString()).andReturn(strMacAddress).anyTimes(); |
| 98 | expect(mockIDevice.getAttachmentPoints()).andReturn(attachmentSwitchPorts).anyTimes(); |
| 99 | expect(mockIDevice.getIPv4Addresses()).andReturn(new Integer[] {ipv4Address}).anyTimes(); |
| 100 | |
| 101 | replay(mockIDevice); |
| 102 | |
| 103 | return mockIDevice; |
| 104 | } |
| 105 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 106 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 107 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 108 | * Test method for addDevice method. |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 109 | * Condition: |
| 110 | * The device does not already exist in the database |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 111 | * Expect: |
| 112 | * Get proper IDeviceObject |
| 113 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 114 | @Test |
| 115 | public void testAddNewDevice() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 116 | String strMacAddress = "99:99:99:99:99:99"; |
| 117 | long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01"); |
| 118 | short attachmentPort = 2; |
| 119 | int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1")); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 120 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 121 | IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address); |
| 122 | |
| 123 | IDeviceObject mockDeviceObject = createMock(IDeviceObject.class); |
| 124 | IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort); |
| 125 | IIpv4Address mockIpv4Address = createMock(IIpv4Address.class); |
| 126 | |
| 127 | expect(mockOpe.searchDevice(strMacAddress)).andReturn(null); |
| 128 | expect(mockOpe.newDevice()).andReturn(mockDeviceObject); |
| 129 | expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.<IPortObject>emptyList()); |
| 130 | expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject); |
| 131 | mockPortObject.setDevice(mockDeviceObject); |
| 132 | expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null); |
| 133 | expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address); |
| 134 | mockDeviceObject.addIpv4Address(mockIpv4Address); |
| 135 | expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address)); |
| 136 | expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address); |
| 137 | |
| 138 | mockDeviceObject.setMACAddress(strMacAddress); |
| 139 | mockDeviceObject.setType("device"); |
| 140 | mockDeviceObject.setState("ACTIVE"); |
| 141 | mockOpe.commit(); |
| 142 | |
| 143 | replay(mockDeviceObject); |
| 144 | replay(mockPortObject); |
| 145 | replay(mockIpv4Address); |
| 146 | replay(mockOpe); |
| 147 | |
| 148 | IDeviceObject addedObject = deviceImpl.addDevice(device); |
| 149 | assertNotNull(addedObject); |
| 150 | |
| 151 | verify(mockDeviceObject); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 155 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 156 | * Test method for addDevice method. |
| 157 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 158 | * The device already exists in the database. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 159 | * Expect: |
| 160 | * Get proper IDeviceObject still. |
| 161 | * Check the IDeviceObject properties set expectedly. |
| 162 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 163 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 164 | public void testAddExistingDevice() { |
| 165 | String strMacAddress = "99:99:99:99:99:99"; |
| 166 | long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01"); |
| 167 | short attachmentPort = 2; |
| 168 | int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1")); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 169 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 170 | IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address); |
| 171 | |
| 172 | IDeviceObject mockDeviceObject = createMock(IDeviceObject.class); |
| 173 | IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort); |
| 174 | IIpv4Address mockIpv4Address = createMock(IIpv4Address.class); |
| 175 | |
| 176 | expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject); |
| 177 | expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singleton(mockPortObject)); |
| 178 | expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject); |
| 179 | expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(mockIpv4Address); |
| 180 | expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address)); |
| 181 | expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address); |
| 182 | |
| 183 | mockDeviceObject.setMACAddress(strMacAddress); |
| 184 | mockDeviceObject.setType("device"); |
| 185 | mockDeviceObject.setState("ACTIVE"); |
| 186 | mockOpe.commit(); |
| 187 | |
| 188 | replay(mockDeviceObject); |
| 189 | replay(mockPortObject); |
| 190 | replay(mockIpv4Address); |
| 191 | replay(mockOpe); |
| 192 | |
| 193 | IDeviceObject addedObject = deviceImpl.addDevice(device); |
| 194 | assertNotNull(addedObject); |
| 195 | |
| 196 | verify(mockDeviceObject); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 200 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 201 | * Test method for updateDevice method. |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 202 | * NB. this is the same test as testAddExistingDevice |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 203 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 204 | * The MAC address and attachment point are the same. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 205 | * All of the other parameter are different. |
| 206 | * Expect: |
| 207 | * Changed parameters are set expectedly. |
| 208 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 209 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 210 | public void testAddUpdateDevice() { |
| 211 | String strMacAddress = "99:99:99:99:99:99"; |
| 212 | long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01"); |
| 213 | short attachmentPort = 2; |
| 214 | int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1")); |
| 215 | |
| 216 | IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address); |
| 217 | |
| 218 | IDeviceObject mockDeviceObject = createMock(IDeviceObject.class); |
| 219 | IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort); |
| 220 | IIpv4Address mockIpv4Address = createMock(IIpv4Address.class); |
| 221 | |
| 222 | expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject); |
| 223 | expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singleton(mockPortObject)); |
| 224 | expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject); |
| 225 | expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(mockIpv4Address); |
| 226 | expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address)); |
| 227 | expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address); |
| 228 | |
| 229 | mockDeviceObject.setMACAddress(strMacAddress); |
| 230 | mockDeviceObject.setType("device"); |
| 231 | mockDeviceObject.setState("ACTIVE"); |
| 232 | mockOpe.commit(); |
| 233 | |
| 234 | replay(mockDeviceObject); |
| 235 | replay(mockPortObject); |
| 236 | replay(mockIpv4Address); |
| 237 | replay(mockOpe); |
| 238 | |
| 239 | IDeviceObject addedObject = deviceImpl.updateDevice(device); |
| 240 | assertNotNull(addedObject); |
| 241 | |
| 242 | verify(mockDeviceObject); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 246 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 247 | * Test method for testRemoveDevice method. |
| 248 | * Condition: |
| 249 | * 1. Unregistered IDeviceObject argument is put. |
| 250 | * Expect: |
| 251 | * 1. Nothing happen when unregistered IDeviceObject is put |
| 252 | * 2. IDeviceObject will be removed. |
| 253 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 254 | @Test |
| 255 | public void testRemoveDevice() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 256 | String strMacAddress = "99:99:99:99:99:99"; |
| 257 | long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01"); |
| 258 | short attachmentPort = 2; |
| 259 | int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1")); |
| 260 | |
| 261 | IIpv4Address ipv4AddressObject = createMock(IIpv4Address.class); |
| 262 | IDeviceObject deviceObject = createMock(IDeviceObject.class); |
| 263 | expect(deviceObject.getIpv4Addresses()).andReturn(Collections.singleton(ipv4AddressObject)); |
| 264 | replay(deviceObject); |
| 265 | |
| 266 | expect(mockOpe.searchDevice(strMacAddress)).andReturn(deviceObject); |
| 267 | mockOpe.removeIpv4Address(ipv4AddressObject); |
| 268 | mockOpe.removeDevice(deviceObject); |
| 269 | mockOpe.commit(); |
| 270 | replay(mockOpe); |
| 271 | |
| 272 | IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 273 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 274 | deviceImpl.removeDevice(device); |
| 275 | |
| 276 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 280 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 281 | * Test method for getDeviceByMac |
| 282 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 283 | * 1. Unregistered MAC address argument is set |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 284 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 285 | * 1.Nothing happen when you put unregistered MAC address |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 286 | * 2.Get the proper IDeviceObject. |
| 287 | * 3.Check the IDeviceObject properties set expectedly. |
| 288 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 289 | @Test |
| 290 | public void testGetDeviceByMac() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 291 | String mac = "99:99:99:99:99:99"; |
| 292 | |
| 293 | IDeviceObject mockDevice = createMock(IDeviceObject.class); |
| 294 | |
| 295 | expect(mockOpe.searchDevice(mac)).andReturn(mockDevice); |
| 296 | |
| 297 | replay(mockOpe); |
| 298 | |
| 299 | IDeviceObject result = deviceImpl.getDeviceByMac(mac); |
| 300 | assertNotNull(result); |
| 301 | |
| 302 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 303 | } |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 304 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 305 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 306 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 307 | * Test method for getDeviceByIP method. |
| 308 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 309 | * 1. Unregistered IP address argument is set |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 310 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 311 | * 1. Nothing happen when you put unregistered IP address |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 312 | * 2. Get the proper IDeviceObject. |
| 313 | * 3. Check the IDeviceObject properties set expectedly. |
| 314 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 315 | @Test |
| 316 | public void testGetDeviceByIP() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 317 | int nonExistingIp = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.10.50")); |
| 318 | int existingIp = InetAddresses.coerceToInteger(InetAddresses.forString("10.5.12.128")); |
| 319 | |
| 320 | IDeviceObject mockDevice = createMock(IDeviceObject.class); |
| 321 | IIpv4Address mockExistingIp = createMock(IIpv4Address.class); |
| 322 | expect(mockExistingIp.getDevice()).andReturn(mockDevice); |
| 323 | |
| 324 | expect(mockOpe.searchIpv4Address(nonExistingIp)).andReturn(null); |
| 325 | expect(mockOpe.searchIpv4Address(existingIp)).andReturn(mockExistingIp); |
| 326 | |
| 327 | replay(mockExistingIp); |
| 328 | replay(mockOpe); |
| 329 | |
| 330 | IDeviceObject result = deviceImpl.getDeviceByIP(nonExistingIp); |
| 331 | assertNull(result); |
| 332 | |
| 333 | result = deviceImpl.getDeviceByIP(existingIp); |
| 334 | assertNotNull(result); |
| 335 | |
| 336 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 340 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 341 | * Test method for testChangeDeviceAttachmentsIDevice |
| 342 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 343 | * 1. The device is not currently attached to any point. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 344 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 345 | * 1. Nothing happen when you put nonexistent attachment point. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 346 | * 2. Set the attachment point expectedly; |
| 347 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 348 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 349 | public void testChangeDeviceAttachementsWhenUnattached() { |
| 350 | String strMacAddress = "99:99:99:99:99:99"; |
| 351 | long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01"); |
| 352 | short attachmentPort = 2; |
| 353 | int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1")); |
| 354 | |
| 355 | IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address); |
| 356 | |
| 357 | IDeviceObject mockDeviceObject = createMock(IDeviceObject.class); |
| 358 | IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort); |
| 359 | |
| 360 | expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject); |
| 361 | expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.<IPortObject>emptyList()); |
| 362 | expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject); |
| 363 | mockPortObject.setDevice(mockDeviceObject); |
| 364 | mockOpe.commit(); |
| 365 | |
| 366 | replay(mockDeviceObject); |
| 367 | replay(mockPortObject); |
| 368 | replay(mockOpe); |
| 369 | |
| 370 | deviceImpl.changeDeviceAttachments(device); |
| 371 | |
| 372 | verify(mockDeviceObject); |
| 373 | verify(mockPortObject); |
| 374 | verify(mockOpe); |
| 375 | } |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 376 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 377 | /** |
| 378 | * Description: |
| 379 | * Test method for testChangeDeviceAttachmentsIDevice |
| 380 | * Condition: |
| 381 | * 1. The device is currently attached to a switch, but this attachment point |
| 382 | * has now changed. |
| 383 | * Expect: |
| 384 | * 1. The device should be removed from the old attachment point. |
| 385 | * 2. Set the attachment point expectedly; |
| 386 | */ |
| 387 | @Test |
| 388 | public void testChangeDeviceAttachementsWhenAttached() { |
| 389 | String strMacAddress = "99:99:99:99:99:99"; |
| 390 | long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01"); |
| 391 | short attachmentPort = 2; |
| 392 | int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1")); |
| 393 | |
| 394 | //Details for the port the device will be moved from |
| 395 | long alreadyAttachedDpid = HexString.toLong("00:00:00:00:00:00:0b:01"); |
| 396 | short alreadyAttachedPort = 5; |
| 397 | |
| 398 | IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address); |
| 399 | |
| 400 | IDeviceObject mockDeviceObject = createMock(IDeviceObject.class); |
| 401 | IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort); |
| 402 | IPortObject alreadyAttachedPortObject = getMockPort(alreadyAttachedDpid, alreadyAttachedPort); |
| 403 | |
| 404 | expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject); |
| 405 | expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singletonList(alreadyAttachedPortObject)); |
| 406 | expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject); |
| 407 | mockPortObject.setDevice(mockDeviceObject); |
| 408 | alreadyAttachedPortObject.removeDevice(mockDeviceObject); |
| 409 | mockOpe.commit(); |
| 410 | |
| 411 | replay(mockDeviceObject); |
| 412 | replay(alreadyAttachedPortObject); |
| 413 | replay(mockPortObject); |
| 414 | replay(mockOpe); |
| 415 | |
| 416 | deviceImpl.changeDeviceAttachments(device); |
| 417 | |
| 418 | verify(mockDeviceObject); |
| 419 | verify(alreadyAttachedPortObject); |
| 420 | verify(mockPortObject); |
| 421 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 424 | @Ignore |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 425 | @Test |
| 426 | public void testChangeDeviceAttachmentsIDeviceIDeviceObject() { |
| 427 | //It is tested by the testChangeDeviceAttachmentsIDevice |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 431 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 432 | * Test method for testChangeDeviceIPv4Address |
| 433 | * Condition: |
| 434 | * N/A |
| 435 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 436 | * 1. Set the IP address expectedly. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 437 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 438 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 439 | public void testChangeDeviceIpv4Address() { |
| 440 | String strMacAddress = "99:99:99:99:99:99"; |
| 441 | long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01"); |
| 442 | short attachmentPort = 2; |
| 443 | int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1")); |
| 444 | |
| 445 | IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address); |
| 446 | |
| 447 | IDeviceObject mockDeviceObject = createMock(IDeviceObject.class); |
| 448 | IIpv4Address mockIpv4Address = createMock(IIpv4Address.class); |
| 449 | |
| 450 | expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject); |
| 451 | expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null); |
| 452 | expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address); |
| 453 | mockDeviceObject.addIpv4Address(mockIpv4Address); |
| 454 | expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singletonList(mockIpv4Address)); |
| 455 | expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address); |
| 456 | mockOpe.commit(); |
| 457 | |
| 458 | replay(mockDeviceObject); |
| 459 | replay(mockIpv4Address); |
| 460 | replay(mockOpe); |
| 461 | |
| 462 | deviceImpl.changeDeviceIPv4Address(device); |
| 463 | |
| 464 | verify(mockDeviceObject); |
| 465 | verify(mockIpv4Address); |
| 466 | verify(mockOpe); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Description: |
| 471 | * Test method for testChangeDeviceIPv4Address |
| 472 | * Condition: |
| 473 | * 1. The device had an old IP address which has now changed. |
| 474 | * Expect: |
| 475 | * 1. The old IP address should be removed from the device. |
| 476 | * 2. Set the IP address expectedly. |
| 477 | */ |
| 478 | @Test |
| 479 | public void testChangeDeviceIpv4AddressAndRemoveExisting() { |
| 480 | String strMacAddress = "99:99:99:99:99:99"; |
| 481 | long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01"); |
| 482 | short attachmentPort = 2; |
| 483 | int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1")); |
| 484 | |
| 485 | IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address); |
| 486 | |
| 487 | IDeviceObject mockDeviceObject = createMock(IDeviceObject.class); |
| 488 | |
| 489 | IIpv4Address mockIpv4Address = createMock(IIpv4Address.class); |
| 490 | IIpv4Address mockDeletingIpv4Address = createMock(IIpv4Address.class); |
| 491 | List<IIpv4Address> ipv4Vertices = new ArrayList<IIpv4Address>(2); |
| 492 | ipv4Vertices.add(mockIpv4Address); |
| 493 | ipv4Vertices.add(mockDeletingIpv4Address); |
| 494 | |
| 495 | expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject); |
| 496 | expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null); |
| 497 | expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address); |
| 498 | mockDeviceObject.addIpv4Address(mockIpv4Address); |
| 499 | expect(mockDeviceObject.getIpv4Addresses()).andReturn(ipv4Vertices); |
| 500 | expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address); |
| 501 | expect(mockDeletingIpv4Address.getIpv4Address()).andReturn(1); |
| 502 | mockDeviceObject.removeIpv4Address(mockDeletingIpv4Address); |
| 503 | mockOpe.commit(); |
| 504 | |
| 505 | replay(mockDeviceObject); |
| 506 | replay(mockIpv4Address); |
| 507 | replay(mockOpe); |
| 508 | |
| 509 | deviceImpl.changeDeviceIPv4Address(device); |
| 510 | |
| 511 | verify(mockDeviceObject); |
| 512 | verify(mockIpv4Address); |
| 513 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | } |