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 |
Jonathan Hart | fb69851 | 2013-11-19 17:11:29 -0800 | [diff] [blame] | 41 | @Ignore //TODO broken 11/19/13, should fix |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 42 | @RunWith(PowerMockRunner.class) |
| 43 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, DeviceStorageImpl.class}) |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 44 | public class DeviceStorageImplTest{ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 45 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 46 | protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 47 | |
| 48 | String conf; |
| 49 | DeviceStorageImpl deviceImpl; |
| 50 | private GraphDBConnection mockConn; |
| 51 | private GraphDBOperation mockOpe; |
| 52 | |
| 53 | @Before |
| 54 | public void setUp() throws Exception { |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 55 | deviceImpl = new DeviceStorageImpl(); |
| 56 | conf = "/dummy/path/to/db"; |
| 57 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 58 | 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); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 66 | PowerMock.expectNew(GraphDBOperation.class, new Class<?>[]{String.class}, conf).andReturn(mockOpe); |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 67 | //mockOpe.close(); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 68 | PowerMock.replay(GraphDBOperation.class); |
| 69 | // Replace the conf to dummy conf |
| 70 | // String conf = "/tmp/cassandra.titan"; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 71 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 72 | deviceImpl.init(conf); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 73 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @After |
| 77 | public void tearDown() throws Exception { |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 78 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 81 | 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; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 88 | 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 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 107 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 108 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 109 | * Test method for addDevice method. |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 110 | * Condition: |
| 111 | * The device does not already exist in the database |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 112 | * Expect: |
| 113 | * Get proper IDeviceObject |
| 114 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 115 | @Test |
| 116 | public void testAddNewDevice() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 117 | 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")); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 121 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 122 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 156 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 157 | * Test method for addDevice method. |
| 158 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 159 | * The device already exists in the database. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 160 | * Expect: |
| 161 | * Get proper IDeviceObject still. |
| 162 | * Check the IDeviceObject properties set expectedly. |
| 163 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 164 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 165 | 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")); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 170 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 171 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 201 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 202 | * Test method for updateDevice method. |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 203 | * NB. this is the same test as testAddExistingDevice |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 204 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 205 | * The MAC address and attachment point are the same. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 206 | * All of the other parameter are different. |
| 207 | * Expect: |
| 208 | * Changed parameters are set expectedly. |
| 209 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 210 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 211 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 247 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 248 | * 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 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 255 | @Test |
| 256 | public void testRemoveDevice() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 257 | 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 Hart | fb69851 | 2013-11-19 17:11:29 -0800 | [diff] [blame] | 265 | expect(deviceObject.getMACAddress()).andReturn(strMacAddress); |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 266 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 275 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 276 | deviceImpl.removeDevice(device); |
| 277 | |
| 278 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 282 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 283 | * Test method for getDeviceByMac |
| 284 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 285 | * 1. Unregistered MAC address argument is set |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 286 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 287 | * 1.Nothing happen when you put unregistered MAC address |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 288 | * 2.Get the proper IDeviceObject. |
| 289 | * 3.Check the IDeviceObject properties set expectedly. |
| 290 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 291 | @Test |
| 292 | public void testGetDeviceByMac() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 293 | 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); |
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 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 307 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 308 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 309 | * Test method for getDeviceByIP method. |
| 310 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 311 | * 1. Unregistered IP address argument is set |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 312 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 313 | * 1. Nothing happen when you put unregistered IP address |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 314 | * 2. Get the proper IDeviceObject. |
| 315 | * 3. Check the IDeviceObject properties set expectedly. |
| 316 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 317 | @Test |
| 318 | public void testGetDeviceByIP() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 319 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 342 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 343 | * Test method for testChangeDeviceAttachmentsIDevice |
| 344 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 345 | * 1. The device is not currently attached to any point. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 346 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 347 | * 1. Nothing happen when you put nonexistent attachment point. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 348 | * 2. Set the attachment point expectedly; |
| 349 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 350 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 351 | 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 | } |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 378 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 379 | /** |
| 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 426 | @Ignore |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 427 | @Test |
| 428 | public void testChangeDeviceAttachmentsIDeviceIDeviceObject() { |
| 429 | //It is tested by the testChangeDeviceAttachmentsIDevice |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 433 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 434 | * Test method for testChangeDeviceIPv4Address |
| 435 | * Condition: |
| 436 | * N/A |
| 437 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 438 | * 1. Set the IP address expectedly. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 439 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 440 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 441 | 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 Hart | fb69851 | 2013-11-19 17:11:29 -0800 | [diff] [blame] | 500 | expect(mockIpv4Address.getDevice()).andReturn(null); |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 501 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | } |