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; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 49 | String dbStore; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 50 | DeviceStorageImpl deviceImpl; |
| 51 | private GraphDBConnection mockConn; |
| 52 | private GraphDBOperation mockOpe; |
| 53 | |
| 54 | @Before |
| 55 | public void setUp() throws Exception { |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 56 | deviceImpl = new DeviceStorageImpl(); |
| 57 | conf = "/dummy/path/to/db"; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 58 | dbStore = "dummyStore"; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 59 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 60 | 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); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 68 | PowerMock.expectNew(GraphDBOperation.class, new Class<?>[]{String.class}, conf).andReturn(mockOpe); |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 69 | //mockOpe.close(); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 70 | PowerMock.replay(GraphDBOperation.class); |
| 71 | // Replace the conf to dummy conf |
| 72 | // String conf = "/tmp/cassandra.titan"; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 73 | |
yoshitomo | 03c0d9d | 2013-11-23 14:58:11 -0800 | [diff] [blame] | 74 | deviceImpl.init(dbStore, conf); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 75 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @After |
| 79 | public void tearDown() throws Exception { |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 80 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 83 | 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; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 90 | 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 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 109 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 110 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 111 | * Test method for addDevice method. |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 112 | * Condition: |
| 113 | * The device does not already exist in the database |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 114 | * Expect: |
| 115 | * Get proper IDeviceObject |
| 116 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 117 | @Test |
| 118 | public void testAddNewDevice() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 119 | 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")); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 123 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 124 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 158 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 159 | * Test method for addDevice method. |
| 160 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 161 | * The device already exists in the database. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 162 | * Expect: |
| 163 | * Get proper IDeviceObject still. |
| 164 | * Check the IDeviceObject properties set expectedly. |
| 165 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 166 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 167 | 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")); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 172 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 173 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 203 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 204 | * Test method for updateDevice method. |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 205 | * NB. this is the same test as testAddExistingDevice |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 206 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 207 | * The MAC address and attachment point are the same. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 208 | * All of the other parameter are different. |
| 209 | * Expect: |
| 210 | * Changed parameters are set expectedly. |
| 211 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 212 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 213 | 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); |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 246 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 250 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 251 | * 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 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 258 | @Test |
| 259 | public void testRemoveDevice() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 260 | 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 Hart | fb69851 | 2013-11-19 17:11:29 -0800 | [diff] [blame] | 268 | expect(deviceObject.getMACAddress()).andReturn(strMacAddress); |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 269 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 278 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 279 | deviceImpl.removeDevice(device); |
| 280 | |
| 281 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 285 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 286 | * Test method for getDeviceByMac |
| 287 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 288 | * 1. Unregistered MAC address argument is set |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 289 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 290 | * 1.Nothing happen when you put unregistered MAC address |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 291 | * 2.Get the proper IDeviceObject. |
| 292 | * 3.Check the IDeviceObject properties set expectedly. |
| 293 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 294 | @Test |
| 295 | public void testGetDeviceByMac() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 296 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 308 | } |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 309 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 310 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 311 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 312 | * Test method for getDeviceByIP method. |
| 313 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 314 | * 1. Unregistered IP address argument is set |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 315 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 316 | * 1. Nothing happen when you put unregistered IP address |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 317 | * 2. Get the proper IDeviceObject. |
| 318 | * 3. Check the IDeviceObject properties set expectedly. |
| 319 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 320 | @Test |
| 321 | public void testGetDeviceByIP() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 322 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 345 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 346 | * Test method for testChangeDeviceAttachmentsIDevice |
| 347 | * Condition: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 348 | * 1. The device is not currently attached to any point. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 349 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 350 | * 1. Nothing happen when you put nonexistent attachment point. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 351 | * 2. Set the attachment point expectedly; |
| 352 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 353 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 354 | 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 | } |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 381 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 382 | /** |
| 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 429 | @Ignore |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 430 | @Test |
| 431 | public void testChangeDeviceAttachmentsIDeviceIDeviceObject() { |
| 432 | //It is tested by the testChangeDeviceAttachmentsIDevice |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 436 | * Description: |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 437 | * Test method for testChangeDeviceIPv4Address |
| 438 | * Condition: |
| 439 | * N/A |
| 440 | * Expect: |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 441 | * 1. Set the IP address expectedly. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 442 | */ |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 443 | @Test |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 444 | 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 Hart | fb69851 | 2013-11-19 17:11:29 -0800 | [diff] [blame] | 503 | expect(mockIpv4Address.getDevice()).andReturn(null); |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 504 | 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); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | } |