Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.ofcontroller.core; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.util.HashMap; |
| 6 | |
| 7 | import net.onrc.onos.graph.GraphDBConnection; |
| 8 | import net.onrc.onos.graph.GraphDBOperation; |
| 9 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
| 12 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 13 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 14 | import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl; |
| 15 | import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager; |
| 16 | import org.easymock.EasyMock; |
| 17 | import org.junit.After; |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | import org.junit.runner.RunWith; |
| 21 | import org.powermock.api.easymock.PowerMock; |
| 22 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | import org.powermock.modules.junit4.PowerMockRunner; |
| 25 | |
| 26 | import com.thinkaurelius.titan.core.TitanFactory; |
| 27 | import com.thinkaurelius.titan.core.TitanGraph; |
| 28 | |
| 29 | //Add Powermock preparation |
| 30 | @RunWith(PowerMockRunner.class) |
| 31 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class}) |
| 32 | public class INetMapTopologyObjectsIDeviceObjectTest { |
| 33 | |
| 34 | //The test network is ./titan/schema/test-network.xml |
| 35 | |
| 36 | protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
| 37 | |
| 38 | String conf; |
| 39 | private GraphDBConnection conn = null; |
| 40 | private GraphDBOperation ope = null; |
| 41 | private TitanGraph titanGraph = null; |
| 42 | |
| 43 | @Before |
| 44 | public void setUp() throws Exception { |
| 45 | conf = "/dummy/path/to/db"; |
| 46 | |
| 47 | // Make mock cassandra DB |
| 48 | // Replace TitanFactory.open() to return mock DB |
| 49 | TestDatabaseManager.deleteTestDatabase(); |
| 50 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 51 | //TestDatabaseManager.populateTestData(titanGraph); |
| 52 | PowerMock.mockStatic(TitanFactory.class); |
| 53 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 54 | PowerMock.replay(TitanFactory.class); |
| 55 | |
| 56 | conn = GraphDBConnection.getInstance(conf); |
| 57 | ope = new GraphDBOperation(conn); |
| 58 | } |
| 59 | |
| 60 | @After |
| 61 | public void tearDown() throws Exception { |
| 62 | titanGraph.shutdown(); |
| 63 | TestDatabaseManager.deleteTestDatabase(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Desc: |
| 68 | * Test method for get and set MacAddress method. |
| 69 | * Condition: |
| 70 | * N/A |
| 71 | * Expect: |
| 72 | * 1. Should set the mac address. |
| 73 | * 2. Should get the mac address. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testSetGetMacAddress() { |
| 77 | String macaddr = "00:00:00:00:00:00:0a:07"; |
| 78 | IDeviceObject devObj = ope.newDevice(); |
| 79 | devObj.setMACAddress(macaddr); |
| 80 | assertEquals(devObj.getMACAddress(), macaddr); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Desc: |
| 85 | * Test method for get and set IPAddress method. |
| 86 | * Condition: |
| 87 | * N/A |
| 88 | * Expect: |
| 89 | * 1. Should set the ip address. |
| 90 | * 2. Should get the ip address. |
| 91 | */ |
| 92 | @Test |
| 93 | public void testSetGetIPAddress() { |
| 94 | String ipaddr = "192.168.0.1"; |
| 95 | IDeviceObject devObj = ope.newDevice(); |
| 96 | devObj.setIPAddress(ipaddr); |
| 97 | assertEquals(devObj.getIPAddress(), ipaddr); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Desc: |
| 102 | * Test method for get attached port. |
| 103 | * Condition: |
| 104 | * N/A |
| 105 | * Expect: |
| 106 | * 1. Should get the attached ports. |
| 107 | */ |
| 108 | @Test |
| 109 | public void testGetAttachedPort() { |
| 110 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 111 | Short number = 1; |
| 112 | Short number2 = 2; |
| 113 | IPortObject portObj = ope.newPort(dpid, number); |
| 114 | IPortObject portObj2 = ope.newPort(dpid, number2); |
| 115 | |
| 116 | String ipaddr = "192.168.0.1"; |
| 117 | IDeviceObject devObj = ope.newDevice(); |
| 118 | |
| 119 | portObj.setDevice(devObj); |
| 120 | portObj2.setDevice(devObj); |
| 121 | |
| 122 | HashMap<Short, IPortObject> portObjectList = new HashMap<Short, IPortObject>(); |
| 123 | for(IPortObject port : devObj.getAttachedPorts()) |
| 124 | { |
| 125 | portObjectList.put(port.getNumber(), port); |
| 126 | } |
| 127 | |
| 128 | assertTrue(portObjectList.containsValue(portObj)); |
| 129 | assertTrue(portObjectList.containsValue(portObj2)); |
| 130 | |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Desc: |
| 135 | * Test method for set and remove host port method. |
| 136 | * Condition: |
| 137 | * N/A |
| 138 | * Expect: |
| 139 | * 1. Should set host port from the device. |
| 140 | * 2. Should remove host port from the device. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testSetRemoveHostPort() { |
| 144 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 145 | Short number = 1; |
| 146 | Short number2 = 2; |
| 147 | IPortObject portObj = ope.newPort(dpid, number); |
| 148 | IPortObject portObj2 = ope.newPort(dpid, number2); |
| 149 | |
| 150 | String ipaddr = "192.168.0.1"; |
| 151 | IDeviceObject devObj = ope.newDevice(); |
| 152 | |
| 153 | devObj.setHostPort(portObj); |
| 154 | |
| 155 | HashMap<String, IDeviceObject> portObjectList = new HashMap<String, IDeviceObject>(); |
| 156 | for(IDeviceObject dev : portObj.getDevices()) |
| 157 | { |
| 158 | portObjectList.put(dev.getMACAddress(), dev); |
| 159 | } |
| 160 | assertTrue(portObjectList.containsValue(devObj)); |
| 161 | |
| 162 | devObj.removeHostPort(portObj); |
| 163 | |
| 164 | HashMap<String, IDeviceObject> portObjectList2 = new HashMap<String, IDeviceObject>(); |
| 165 | for(IDeviceObject dev : portObj.getDevices()) |
| 166 | { |
| 167 | portObjectList2.put(dev.getMACAddress(), dev); |
| 168 | } |
| 169 | assertTrue(!portObjectList2.containsValue(devObj)); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Desc: |
| 174 | * Test method for getSwitch method. |
| 175 | * Condition: |
| 176 | * N/A |
| 177 | * Expect: |
| 178 | * 1. Should get the switch connected to the device. |
| 179 | */ |
| 180 | @Test |
| 181 | public void testGetSwitches() { |
| 182 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 183 | String dpid2 = "00:00:00:00:00:00:0a:08"; |
| 184 | Short number = 1; |
| 185 | Short number2 = 2; |
| 186 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 187 | ISwitchObject swObj2 = ope.newSwitch(dpid2); |
| 188 | IPortObject portObj = ope.newPort(dpid, number); |
| 189 | IPortObject portObj2 = ope.newPort(dpid2, number2); |
| 190 | swObj.addPort(portObj); |
| 191 | swObj2.addPort(portObj2); |
| 192 | IDeviceObject devObj = ope.newDevice(); |
| 193 | portObj.setDevice(devObj); |
| 194 | portObj2.setDevice(devObj); |
| 195 | |
| 196 | HashMap<String, ISwitchObject> switchObjectList = new HashMap<String, ISwitchObject>(); |
| 197 | for(ISwitchObject sw : devObj.getSwitch()) |
| 198 | { |
| 199 | switchObjectList.put(sw.getDPID(), sw); |
| 200 | } |
| 201 | assertTrue(switchObjectList.containsValue(swObj)); |
| 202 | assertTrue(switchObjectList.containsValue(swObj2)); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | } |