Add unit test files
diff --git a/src/test/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImplTest.java b/src/test/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImplTest.java
new file mode 100644
index 0000000..daac980
--- /dev/null
+++ b/src/test/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImplTest.java
@@ -0,0 +1,768 @@
+package net.onrc.onos.ofcontroller.core.internal;
+
+import static org.easymock.EasyMock.*;
+
+import net.onrc.onos.ofcontroller.core.ISwitchStorage;
+import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
+import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
+import net.onrc.onos.util.GraphDBConnection;
+import net.onrc.onos.util.GraphDBOperation;
+import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.openflow.protocol.OFPhysicalPort;
+import org.openflow.protocol.OFPhysicalPort.OFPortState;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.slf4j.LoggerFactory;
+
+import com.thinkaurelius.titan.core.TitanFactory;
+import com.thinkaurelius.titan.core.TitanGraph;
+
+//Add Powermock preparation
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
+public class SwitchStorageImplTest {
+
+ protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
+
+ String conf;
+ private GraphDBConnection mockConn = null;
+ private GraphDBOperation mockOpe = null;
+ private GraphDBOperation realOpe = null;
+ private TitanGraph titanGraph = null;
+ ISwitchStorage swSt = null;
+
+ @Before
+ public void setUp() throws Exception {
+
+ swSt = new SwitchStorageImpl();
+ conf = "/dummy/path/to/db";
+
+ // Make mock cassandra DB
+ // Replace TitanFactory.open() to return mock DB
+
+ PowerMock.mockStatic(GraphDBConnection.class);
+ mockConn = createMock(GraphDBConnection.class);
+ PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class));
+ EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn);
+ PowerMock.replay(GraphDBConnection.class);
+
+ PowerMock.mockStatic(GraphDBOperation.class);
+ mockOpe = PowerMock.createStrictMock(GraphDBOperation.class);
+ PowerMock.expectNew(GraphDBOperation.class, mockConn).andReturn(mockOpe);
+ PowerMock.replay(GraphDBOperation.class);
+ // Replace the conf to dummy conf
+ // String conf = "/tmp/cassandra.titan";
+
+
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ swSt.close();
+ swSt = null;
+
+ }
+
+ /**
+ * Desc:
+ * Test method for addSwitch method.
+ * Condition:
+ * Normal
+ * Expect:
+ * Call SwitchStorageImpl.addSwitch func with proper properties.
+ */
+ @Ignore
+ @Test
+ public void testAddSwitch() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ String state = "ACTIVE";
+
+ //Mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ }
+
+ /**
+ * Desc:
+ * Test method for addSwitch method.
+ * Condition:
+ * The switch is already existing.
+ * Expect:
+ * Call SwitchStorageImpl.addSwitch func with proper properties.
+ */
+ //@Ignore
+ @Test
+ public void testAddSwitchExisting() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ String state = "ACTIVE";
+
+ //Mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ mockISw.setState(state);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.addSwitch(dpid);
+ }
+
+ /**
+ * Desc:
+ * Test method for addSwitch method.
+ * Condition:
+ * The switch construction is fail and return null
+ * Expect:
+ * Write the status as info log.
+ */
+ //@Ignore
+ @Test
+ public void testAddSwitchAbnormal() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(null);
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ }
+
+ /**
+ * Desc:
+ * Test method for addSwitch method.
+ * Condition:
+ * Tthrow runtimeException.
+ * Expect:
+ * The rollback method is called.
+ */
+ //@Ignore
+ @Test
+ public void testAddSwitchException() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ String state = "ACTIVE";
+
+ //Mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expectLastCall().andThrow(new RuntimeException());
+ mockOpe.rollback();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ }
+
+ /**
+ * Desc:
+ * Test method for updateSwitch method.
+ * Condition:
+ * SwitchState : INACTIVE
+ * DMOPERATION : UPDATE
+ * Expect:
+ * Should call addSwitch function and commit.
+ */
+ //@Ignore
+ @Test
+ public void testUpdateUPDATE() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ SwitchState stateINACTIVE = SwitchState.INACTIVE;
+ DM_OPERATION opUPDATE = DM_OPERATION.UPDATE;
+
+ //Mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState("ACTIVE");
+ mockISw.setState(stateINACTIVE.toString());
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.update(dpid, stateINACTIVE, opUPDATE);
+ }
+
+ /**
+ * Desc:
+ * Test method for updateSwitch method.
+ * Condition:
+ * SwitchState : INACTIVE
+ * DMOPERATION : CREATE
+ * Expect:
+ * Should call addSwitch function and commit.
+ */
+ //@Ignore
+ @Test
+ public void testUpdateCREATE() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ SwitchState stateINACTIVE = SwitchState.INACTIVE;
+ DM_OPERATION opCREATE = DM_OPERATION.CREATE;
+
+ //Mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState("ACTIVE");
+ mockISw.setState(stateINACTIVE.toString());
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.update(dpid, stateINACTIVE, opCREATE);
+ }
+
+ /**
+ * Desc:
+ * Test method for updateSwitch method.
+ * Condition:
+ * SwitchState : INACTIVE
+ * DMOPERATION : INSERT
+ * Expect:
+ * Should call addSwitch function and commit.
+ */
+ //@Ignore
+ @Test
+ public void testUpdateINSERT() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ SwitchState stateINACTIVE = SwitchState.INACTIVE;
+ DM_OPERATION opINSERT = DM_OPERATION.INSERT;
+
+ //Mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState("ACTIVE");
+ mockISw.setState(stateINACTIVE.toString());
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.update(dpid, stateINACTIVE, opINSERT);
+ }
+
+ /**
+ * Desc:
+ * Test method for updateSwitch method.
+ * Condition:
+ * SwitchState : ACTIVE
+ * DMOPERATION : DELETE
+ * Expect:
+ * Should call removeSwitch function and commit.
+ */
+ //@Ignore
+ @Test
+ public void testUpdateDELETE() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ SwitchState stateACTIVE = SwitchState.ACTIVE;
+ DM_OPERATION opDELETE = DM_OPERATION.DELETE;
+
+ //Mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(stateACTIVE.toString());
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ mockOpe.removeSwitch(mockISw);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.update(dpid, stateACTIVE, opDELETE);
+ }
+
+ /**
+ * Desc:
+ * Test method for deleteSwitch method.
+ * Condition:
+ * The switch is existing.
+ * Expect:
+ * Should call removeSwitch function and commit.
+ */
+ //@Ignore
+ @Test
+ public void testDeleteSwitch() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ String state = "ACTIVE";
+
+ //Mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ mockOpe.removeSwitch(mockISw);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.deleteSwitch(dpid);
+
+ //Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator();
+ //assertFalse(it.hasNext());
+ }
+
+ /**
+ * Desc:
+ * Test method for deleteSwitch method.
+ * Condition:
+ * The commit func throw exception.
+ * Expect:
+ * Should call rollback.
+ */
+ //@Ignore
+ @Test
+ public void testDeleteSwitchException() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ String state = "ACTIVE";
+ String type = "";
+
+ //Mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ mockOpe.removeSwitch(mockISw);
+ mockOpe.commit();
+ expectLastCall().andThrow(new RuntimeException());
+ mockOpe.rollback();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.deleteSwitch(dpid);
+ }
+
+ /**
+ * Desc:
+ * Test method for addPort method.
+ * Condition:
+ * port is existing.
+ * Expect:
+ * Should call addPort and commit.
+ */
+ //@Ignore
+ @Test
+ public void testAddPort() {
+ String dpid = "00:00:00:00:00:00:0a:01";
+ short portNumber = 5;
+ String state = "ACTIVE";
+ String name = "port 5 at SEA switch";
+
+ OFPhysicalPort portToAdd = new OFPhysicalPort();
+ portToAdd.setName(name);
+ portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
+ portToAdd.setPortNumber(portNumber);
+ portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
+
+ //Expectation of mock Port
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setState(state);
+ mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
+ mockIPort.setDesc(name);
+ replay(mockIPort);
+
+ //Expectation of mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ mockISw.addPort(mockIPort);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
+ expect(mockOpe.newPort(portNumber)).andReturn(mockIPort);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.addPort(dpid, portToAdd);
+ }
+
+ /**
+ * Desc:
+ * Test method for addPort method.
+ * Condition:
+ * Port status is down.
+ * Expect:
+ * Should call removePort and commit.
+ */
+ //@Ignore
+ @Test
+ public void testAddPortWithPortLinkDown() {
+ String dpid = "00:00:00:00:00:00:0a:01";
+ short portNumber = 5;
+ String state = "ACTIVE";
+ String name = "port 5 at SEA switch";
+
+ OFPhysicalPort portToAdd = new OFPhysicalPort();
+ portToAdd.setName(name);
+ portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
+ portToAdd.setPortNumber(portNumber);
+ portToAdd.setState(OFPortState.OFPPS_LINK_DOWN.getValue());
+
+ //Expectation of mock Port
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setState(state);
+ mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
+ mockIPort.setDesc(name);
+ replay(mockIPort);
+
+ //Expectation of mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ mockISw.removePort(mockIPort);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
+ mockOpe.removePort(mockIPort);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.addPort(dpid, portToAdd);
+ }
+
+ /**
+ * Desc:
+ * Test method for addPort method.
+ * Condition:
+ * The switch is not existing.
+ * Expect:
+ * Nothing happens.
+ */
+ //@Ignore
+ @Test
+ public void testAddPortAbnormalNoSwitch() {
+ String dpid = "00:00:00:00:00:00:0a:01";
+ short portNumber = 5;
+ String state = "ACTIVE";
+ String name = "port 5 at SEA switch";
+
+ OFPhysicalPort portToAdd = new OFPhysicalPort();
+ portToAdd.setName(name);
+ portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
+ portToAdd.setPortNumber(portNumber);
+ portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
+
+ //Expectation of mock Port
+ IPortObject mockIPort = createStrictMock(IPortObject.class);
+ replay(mockIPort);
+
+ //Expectation of mock Switch
+ ISwitchObject mockISw = createStrictMock(ISwitchObject.class);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addPort(dpid, portToAdd);
+ }
+
+ /**
+ * Desc:
+ * Test method for addPort method.
+ * Condition:
+ * port is not existing.
+ * Expect:
+ * Should call addPort and commit.
+ */
+ //@Ignore
+ @Test
+ public void testAddPortAbnormalNoPort() {
+ String dpid = "00:00:00:00:00:00:0a:01";
+ short portNumber = 5;
+ String state = "ACTIVE";
+ String name = "port 5 at SEA switch";
+
+ OFPhysicalPort portToAdd = new OFPhysicalPort();
+ portToAdd.setName(name);
+ portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
+ portToAdd.setPortNumber(portNumber);
+ portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
+
+ //Expectation of mock Port
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setState(state);
+ mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
+ mockIPort.setDesc(name);
+ replay(mockIPort);
+
+ //Expectation of mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ mockISw.addPort(mockIPort);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
+ expect(mockOpe.newPort(portNumber)).andReturn(null);
+ mockOpe.rollback();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.addPort(dpid, portToAdd);
+ }
+
+ /**
+ * Desc:
+ * Test method for addPort method.
+ * Condition:
+ * commit throw the exception.
+ * Expect:
+ * Should call rollback.
+ */
+ //@Ignore
+ @Test
+ public void testAddPortWithException() {
+ String dpid = "00:00:00:00:00:00:0a:01";
+ short portNumber = 5;
+ String state = "ACTIVE";
+ String name = "port 5 at SEA switch";
+
+ OFPhysicalPort portToAdd = new OFPhysicalPort();
+ portToAdd.setName(name);
+ portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
+ portToAdd.setPortNumber(portNumber);
+ portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
+
+ //Expectation of mock Port
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setState(state);
+ mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
+ mockIPort.setDesc(name);
+ replay(mockIPort);
+
+ //Expectation of mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ mockISw.addPort(mockIPort);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
+ expect(mockOpe.newPort(portNumber)).andReturn(mockIPort);
+ mockOpe.commit();
+ expectLastCall().andThrow(new RuntimeException());
+ mockOpe.rollback();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.addPort(dpid, portToAdd);
+ }
+
+ /**
+ * Desc:
+ * Test method for deletePort method.
+ * Condition:
+ * port is existing.
+ * Expect:
+ * Should call removePort and commit.
+ */
+ //@Ignore
+ @Test
+ public void testDeletePort() {
+ String dpid = "00:00:00:00:00:00:0a:01";
+ short portNumber = 5;
+ String state = "ACTIVE";
+ String name = "port 5 at SEA switch";
+
+ OFPhysicalPort portToAdd = new OFPhysicalPort();
+ portToAdd.setName(name);
+ portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
+ portToAdd.setPortNumber(portNumber);
+ portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
+
+ //Expectation of mock Port
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setState(state);
+ mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
+ mockIPort.setDesc(name);
+ replay(mockIPort);
+
+ //Expectation of mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ mockISw.addPort(mockIPort);
+ mockISw.removePort(mockIPort);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
+ expect(mockOpe.newPort(portNumber)).andReturn(mockIPort);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
+ mockOpe.removePort(mockIPort);
+ mockOpe.commit();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.addPort(dpid, portToAdd);
+ swSt.deletePort(dpid, portNumber);
+ }
+
+ /**
+ * Desc:
+ * Test method for addPort method.
+ * Condition:
+ * commit throws the exception.
+ * Expect:
+ * Should call rollback.
+ */
+ //@Ignore
+ @Test
+ public void testDeletePortException() {
+ String dpid = "00:00:00:00:00:00:0a:01";
+ short portNumber = 5;
+ String state = "ACTIVE";
+ String name = "port 5 at SEA switch";
+
+ OFPhysicalPort portToAdd = new OFPhysicalPort();
+ portToAdd.setName(name);
+ portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
+ portToAdd.setPortNumber(portNumber);
+ portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
+
+ //Expectation of mock Port
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setState(state);
+ mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
+ mockIPort.setDesc(name);
+ replay(mockIPort);
+
+ //Expectation of mock Switch
+ ISwitchObject mockISw = createMock(ISwitchObject.class);
+ mockISw.setState(state);
+ mockISw.addPort(mockIPort);
+ mockISw.removePort(mockIPort);
+ replay(mockISw);
+
+ //Expectation of mock operation.
+ expect(mockOpe.searchSwitch(dpid)).andReturn(null);
+ expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
+ expect(mockOpe.newPort(portNumber)).andReturn(mockIPort);
+ mockOpe.commit();
+ expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
+ expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
+ mockOpe.removePort(mockIPort);
+ expectLastCall().andThrow(new RuntimeException());
+ mockOpe.rollback();
+ mockOpe.close();
+ replay(mockOpe);
+
+ swSt.init(conf);
+ swSt.addSwitch(dpid);
+ swSt.addPort(dpid, portToAdd);
+ swSt.deletePort(dpid, portNumber);
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImplTestBB.java b/src/test/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImplTestBB.java
new file mode 100644
index 0000000..78594b5
--- /dev/null
+++ b/src/test/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImplTestBB.java
@@ -0,0 +1,328 @@
+package net.onrc.onos.ofcontroller.core.internal;
+
+import static org.junit.Assert.*;
+
+import net.floodlightcontroller.core.internal.TestDatabaseManager;
+import net.onrc.onos.ofcontroller.core.ISwitchStorage;
+import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
+import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
+import net.onrc.onos.util.GraphDBConnection;
+import net.onrc.onos.util.GraphDBOperation;
+import net.onrc.onos.ofcontroller.core.INetMapStorage;
+import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.openflow.protocol.OFPhysicalPort;
+import org.openflow.protocol.OFPhysicalPort.OFPortState;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.slf4j.LoggerFactory;
+
+import com.thinkaurelius.titan.core.TitanFactory;
+import com.thinkaurelius.titan.core.TitanGraph;
+
+//Add Powermock preparation
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
+public class SwitchStorageImplTestBB {
+
+ protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
+
+ String conf;
+ private GraphDBConnection conn = null;
+ private GraphDBOperation ope = null;
+ private TitanGraph titanGraph = null;
+ ISwitchStorage swSt = null;
+
+ @Before
+ public void setUp() throws Exception {
+
+ swSt = new SwitchStorageImpl();
+ conf = "/dummy/path/to/db";
+
+ // Make mock cassandra DB
+ // Replace TitanFactory.open() to return mock DB
+ titanGraph = TestDatabaseManager.getTestDatabase();
+ TestDatabaseManager.populateTestData(titanGraph);
+ PowerMock.mockStatic(TitanFactory.class);
+ EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
+ PowerMock.replay(TitanFactory.class);
+
+ conn = GraphDBConnection.getInstance(conf);
+ ope = new GraphDBOperation(conn);
+
+ swSt.init(conf);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+
+ titanGraph.shutdown();
+ TestDatabaseManager.deleteTestDatabase();
+
+ swSt.close();
+ swSt = null;
+ }
+
+ /**
+ * Desc:
+ * Test method for addSwitch method.
+ * Condition:
+ * Normal
+ * Expect:
+ * 1. Switch should be generated.
+ * 2. The status of switch should be ACTIVE
+ */
+ //@Ignore
+ @Test
+ public void testAddSwitch() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+
+ ISwitchObject sw = ope.searchSwitch(dpid);
+ assertTrue(sw == null);
+ swSt.addSwitch(dpid);
+ ISwitchObject sw2 = ope.searchSwitch(dpid);
+ assertTrue(sw2 != null);
+ assertEquals(sw2.getState(), "ACTIVE");
+ }
+
+ /**
+ * Desc:
+ * Test method for addSwitch method.
+ * Condition:
+ * The switch status is INACTIVE.
+ * Expect:
+ * 1. Switch should be existing.
+ * 2. The status of switch should be ACTIVE
+ */
+ //@Ignore
+ @Test
+ public void testAddSwitchExisting() {
+ String dpid = "00:00:00:00:00:00:0a:06";
+
+ swSt.update(dpid, SwitchState.INACTIVE, DM_OPERATION.UPDATE);
+ ISwitchObject sw = ope.searchSwitch(dpid);
+ assertTrue(sw != null);
+ assertEquals(sw.getState(), SwitchState.INACTIVE.toString());
+ swSt.addSwitch(dpid);
+ ISwitchObject sw2 = ope.searchSwitch(dpid);
+ assertTrue(sw2 != null);
+ assertEquals(sw2.getState(), SwitchState.ACTIVE.toString());
+ }
+
+ /**
+ * Desc:
+ * Test method for testUpdate method.
+ * Condition:
+ * We will create a swith with this method.
+ * Expect:
+ * 1. Switch should be created.
+ * 2. The status of switch should be INACTIVE.
+ */
+ //@Ignore
+ @Test
+ public void testUpdate() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+ SwitchState state = ISwitchStorage.SwitchState.INACTIVE;
+ DM_OPERATION dmope = INetMapStorage.DM_OPERATION.CREATE;
+
+ ISwitchObject sw = ope.searchSwitch(dpid);
+ assertTrue(sw == null);
+ swSt.update(dpid, state, dmope);
+ ISwitchObject sw2 = ope.searchSwitch(dpid);
+ assertTrue(sw2 != null);
+ assertEquals(sw2.getState(), state.toString());
+ }
+
+ /**
+ * Desc:
+ * Test method for testUpdate method.
+ * Condition:
+ * The switch is existing.
+ * Expect:
+ * 1. Switch should be deleted.
+ */
+ //@Ignore
+ @Test
+ public void testUpdateWithDELETE() {
+ String dpid = "00:00:00:00:00:00:0a:06";
+ SwitchState state = ISwitchStorage.SwitchState.ACTIVE;
+ DM_OPERATION dmope = INetMapStorage.DM_OPERATION.DELETE;
+
+ ISwitchObject sw = ope.searchSwitch(dpid);
+ assertTrue(sw != null);
+ swSt.update(dpid, state, dmope);
+ ISwitchObject sw2 = ope.searchSwitch(dpid);
+ assertTrue(sw2 == null);
+ }
+
+ /**
+ * Desc:
+ * Test method for delete switch method.
+ * Condition:
+ * The switch is existing.
+ * Expect:
+ * 1. Switch should be deleted.
+ */
+ //@Ignore
+ @Test
+ public void testDeleteSwitch() {
+ String dpid = "00:00:00:00:00:00:0a:06";
+
+ ISwitchObject sw = ope.searchSwitch(dpid);
+ assertTrue(sw != null);
+ swSt.deleteSwitch(dpid);
+ ISwitchObject sw2 = ope.searchSwitch(dpid);
+ assertTrue(sw2 == null);
+ }
+
+ /**
+ * Desc:
+ * Test method for delete switch method.
+ * Condition:
+ * The switch is not existing.
+ * Expect:
+ * Nothing happends.
+ */
+ //@Ignore
+ @Test
+ public void testDeleteNonExistingSwitch() {
+ String dpid = "00:00:00:00:00:00:0a:07";
+
+ ISwitchObject sw = ope.searchSwitch(dpid);
+ assertTrue(sw == null);
+ swSt.deleteSwitch(dpid);
+ ISwitchObject sw2 = ope.searchSwitch(dpid);
+ assertTrue(sw2 == null);
+ }
+
+ /**
+ * Desc:
+ * Test method for delete port method.
+ * Condition:
+ * The port is existing.
+ * Expect:
+ * Delete the port.
+ */
+ //@Ignore
+ @Test
+ public void testDeletePort() {
+ String dpid = "00:00:00:00:00:00:0a:06";
+ short portNumber = 3;
+
+ IPortObject portObj1 = ope.searchPort(dpid, portNumber);
+ assertTrue(portObj1 != null);
+ swSt.deletePort(dpid, portNumber);
+ IPortObject portObj2 = ope.searchPort(dpid, portNumber);
+ assertTrue(portObj2 == null);
+ }
+
+ /**
+ * Desc:
+ * Test method for delete port method.
+ * Condition:
+ * The port is not existing.
+ * Expect:
+ * Nothing happens.
+ */
+ //@Ignore
+ @Test
+ public void testDeleteNonExistingPort() {
+ String dpid = "00:00:00:00:00:00:0a:06";
+ short portNumber = 4;
+
+ IPortObject portObj1 = ope.searchPort(dpid, portNumber);
+ assertTrue(portObj1 == null);
+ swSt.deletePort(dpid, portNumber);
+ IPortObject portObj2 = ope.searchPort(dpid, portNumber);
+ assertTrue(portObj2 == null);
+ }
+
+ /**
+ * Desc:
+ * Test method for add method.
+ * Condition:
+ * The port is not existing.
+ * Expect:
+ * The port should be added.
+ */
+ //@Ignore
+ @Test
+ public void testAddPort() {
+ String dpid = "00:00:00:00:00:00:0a:06";
+ short portNumber = 4;
+ String name = "port 4 at ATL Switch";
+ int state = OFPortState.OFPPS_STP_FORWARD.getValue();
+ OFPhysicalPort port = new OFPhysicalPort();
+ port.setPortNumber(portNumber);
+ port.setName(name);
+ port.setState(state);
+
+ ISwitchObject sw = ope.searchSwitch(dpid);
+ assertTrue(sw != null);
+ swSt.addPort(dpid, port);
+ IPortObject portObj = ope.searchPort(dpid, portNumber);
+ assertTrue(portObj != null);
+ }
+
+ /**
+ * Desc:
+ * Test method for add method.
+ * Condition:
+ * The port is existing.
+ * Expect:
+ * Nothing happens.
+ */
+ //@Ignore
+ @Test
+ public void testAddExistingPort() {
+ String dpid = "00:00:00:00:00:00:0a:06";
+ short portNumber = 3;
+ String name = "port 3 at ATL Switch";
+ int state = OFPortState.OFPPS_STP_FORWARD.getValue();
+ OFPhysicalPort port = new OFPhysicalPort();
+ port.setPortNumber(portNumber);
+ port.setName(name);
+ port.setState(state);
+
+ ISwitchObject sw = ope.searchSwitch(dpid);
+ assertTrue(sw != null);
+ swSt.addPort(dpid, port);
+ IPortObject portObj = ope.searchPort(dpid, portNumber);
+ assertTrue(portObj != null);
+ }
+
+ /**
+ * Desc:
+ * Test method for add method.
+ * Condition:
+ * The port status is down.
+ * Expect:
+ * Delete the port.
+ */
+ //@Ignore
+ @Test
+ public void testAddDownPort() {
+ String dpid = "00:00:00:00:00:00:0a:06";
+ short portNumber = 3;
+ String name = "port 3 at ATL Switch";
+ int state = OFPortState.OFPPS_LINK_DOWN.getValue();
+ OFPhysicalPort port = new OFPhysicalPort();
+ port.setPortNumber(portNumber);
+ port.setName(name);
+ port.setState(state);
+
+ ISwitchObject sw = ope.searchSwitch(dpid);
+ assertTrue(sw != null);
+ swSt.addPort(dpid, port);
+ IPortObject portObj = ope.searchPort(dpid, portNumber);
+ assertTrue(portObj == null);
+ }
+}
diff --git a/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTest.java b/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTest.java
new file mode 100644
index 0000000..5309667
--- /dev/null
+++ b/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTest.java
@@ -0,0 +1,833 @@
+package net.onrc.onos.ofcontroller.devicemanager.internal;
+
+import static org.junit.Assert.*;
+import static org.easymock.EasyMock.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
+import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
+import net.onrc.onos.ofcontroller.devicemanager.internal.DeviceStorageImpl;
+import net.onrc.onos.util.GraphDBConnection;
+import net.onrc.onos.util.GraphDBOperation;
+import net.floodlightcontroller.devicemanager.IDevice;
+import net.floodlightcontroller.devicemanager.SwitchPort;
+import net.floodlightcontroller.devicemanager.internal.Device;
+import net.floodlightcontroller.packet.IPv4;
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.openflow.util.HexString;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import com.thinkaurelius.titan.core.TitanFactory;
+
+//Add Powermock preparation
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, DeviceStorageImpl.class})
+public class DeviceStorageImplTest{ //extends FloodlightTestCase{
+
+ protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
+
+ String conf;
+ DeviceStorageImpl deviceImpl;
+ private GraphDBConnection mockConn;
+ private GraphDBOperation mockOpe;
+
+ @Before
+ public void setUp() throws Exception {
+
+ PowerMock.mockStatic(GraphDBConnection.class);
+ mockConn = createMock(GraphDBConnection.class);
+ PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class));
+ EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn);
+ PowerMock.replay(GraphDBConnection.class);
+
+ //PowerMock.mockStatic(GraphDBOperation.class);
+ mockOpe = PowerMock.createMock(GraphDBOperation.class);
+ PowerMock.expectNew(GraphDBOperation.class, mockConn).andReturn(mockOpe);
+ PowerMock.replay(GraphDBOperation.class);
+ // Replace the conf to dummy conf
+ // String conf = "/tmp/cassandra.titan";
+ conf = "/dummy/path/to/db";
+
+ deviceImpl = new DeviceStorageImpl();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ deviceImpl.close();
+ deviceImpl = null;
+ }
+
+ private String makeIPStringFromArray(Integer[] ipaddresses){
+ String multiIntString = "";
+ for(Integer intValue : ipaddresses)
+ {
+ if (multiIntString == null || multiIntString.isEmpty()){
+ multiIntString = "[" + IPv4.fromIPv4Address(intValue);
+ }
+ else{
+ multiIntString += "," + IPv4.fromIPv4Address(intValue);
+ }
+ }
+ return multiIntString + "]";
+ }
+
+
+ /**
+ * Desc:
+ * Test method for addDevice method.
+ * Codition:
+ * N/A
+ * Expect:
+ * Get proper IDeviceObject
+ */
+ //@Ignore
+ @Test
+ public void testAddNewDevice() {
+ try
+ {
+ //Make mockDevice
+ IDevice mockDev = createMock(Device.class);
+ // Mac addr for test device.
+ String macAddr = "99:99:99:99:99:99";
+ // IP addr for test device
+ String ip = "192.168.100.1";
+ Integer[] ipaddrs = {IPv4.toIPv4Address(ip)};
+ // Mac addr for attached switch
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ // Port number for attached switch
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs).anyTimes();
+ expect(mockDev.getAttachmentPoints()).andReturn(sps).anyTimes();
+ replay(mockDev);
+
+ //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setNumber(portNum);
+ mockIPort.setType("port");
+ String iPortDesc = "port 1 at SEA Switch";
+ expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
+ expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
+ replay(mockIPort);
+
+ //Make Iterator for mockIport
+ List<IPortObject> portList = new ArrayList<IPortObject>();
+ portList.add(mockIPort);
+
+ //Expectation for mockIDeviceObject
+ IDeviceObject mockIDev = createMock(IDeviceObject.class);
+ expect(mockIDev.getAttachedPorts()).andReturn(portList);
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ replay(mockIDev);
+
+ //Expectation for mockOpe
+ expect(mockOpe.searchDevice(macAddr)).andReturn(null);
+ expect(mockOpe.newDevice()).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+
+ replay(mockOpe);
+
+ deviceImpl.init(conf);
+
+ //Add the device
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ verify(mockIDev);
+ verify(mockOpe);
+
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for addDevice method.
+ * Condition:
+ * Already added device is existed.
+ * Expect:
+ * Get proper IDeviceObject still.
+ * Check the IDeviceObject properties set expectedly.
+ */
+ //@Ignore
+ @Test
+ public void testAddDeviceExisting() {
+ try
+ {
+ IDevice mockDev = createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer[] ipaddrs = {IPv4.toIPv4Address(ip)};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs).times(2);
+ expect(mockDev.getAttachmentPoints()).andReturn(sps).times(2);
+ replay(mockDev);
+
+ //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setNumber(portNum);
+ mockIPort.setType("port");
+ String iPortDesc = "port 1 at SEA Switch";
+ expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
+ expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
+ replay(mockIPort);
+
+ //Make Iterator for mockIport
+ List<IPortObject> portList = new ArrayList<IPortObject>();
+ portList.add(mockIPort);
+
+ //Expectation for mockIDeviceObject
+ IDeviceObject mockIDev = createMock(IDeviceObject.class);
+ expect(mockIDev.getAttachedPorts()).andReturn(portList).anyTimes();
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ replay(mockIDev);
+
+ //Expectation for mockOpe
+ expect(mockOpe.searchDevice(macAddr)).andReturn(null);
+ expect(mockOpe.newDevice()).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+ expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+ replay(mockOpe);
+
+ deviceImpl.init(conf);
+
+ //Add the device
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ //Add the same device
+ IDeviceObject obj2 = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj2);
+
+ verify(mockIDev);
+ verify(mockOpe);
+
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for updateDevice method.
+ * Condition:
+ * The mac address and attachment point are the same.
+ * All of the other parameter are different.
+ * Expect:
+ * Changed parameters are set expectedly.
+ */
+ //@Ignore
+ @Test
+ public void testUpdateDevice() {
+ try
+ {
+ IDevice mockDev = createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ replay(mockDev);
+
+ //Dev2 (attached port is the same)
+ IDevice mockDev2 = createMock(Device.class);
+ String ip2 = "192.168.100.2";
+ Integer ipInt2 = IPv4.toIPv4Address(ip2);
+ Integer[] ipaddrs2 = {ipInt2};
+
+ expect(mockDev2.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2);
+ expect(mockDev2.getAttachmentPoints()).andReturn(sps);
+ replay(mockDev2);
+
+ //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setNumber(portNum);
+ mockIPort.setType("port");
+ String iPortDesc = "port 1 at SEA Switch";
+ expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
+ expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
+ replay(mockIPort);
+
+ //Make Iterator for mockIport
+ List<IPortObject> portList = new ArrayList<IPortObject>();
+ portList.add(mockIPort);
+
+ //Expectation for mockIDeviceObject
+ IDeviceObject mockIDev = createMock(IDeviceObject.class);
+ expect(mockIDev.getAttachedPorts()).andReturn(portList).anyTimes();
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs2));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ replay(mockIDev);
+
+ //Expectation for mockOpe
+ expect(mockOpe.searchDevice(macAddr)).andReturn(null);
+ expect(mockOpe.newDevice()).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+ expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+ replay(mockOpe);
+
+ deviceImpl.init(conf);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ //update theDevice
+ IDeviceObject obj2 = deviceImpl.updateDevice(mockDev2);
+ assertNotNull(obj2);
+
+ verify(mockIDev);
+ verify(mockOpe);
+
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for testRemoveDevice method.
+ * Condition:
+ * 1. Unregistered IDeviceObject argument is put.
+ * Expect:
+ * 1. Nothing happen when unregistered IDeviceObject is put
+ * 2. IDeviceObject will be removed.
+ */
+ //@Ignore
+ @Test
+ public void testRemoveDevice() {
+ try
+ {
+ IDevice mockDev = createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ replay(mockDev);
+
+ //Dev2 (attached port is the same)
+ IDevice mockDev2 = createMock(Device.class);
+ String macAddr2 = "33:33:33:33:33:33";
+ expect(mockDev2.getMACAddressString()).andReturn(macAddr2).anyTimes();
+ expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs);
+ expect(mockDev2.getAttachmentPoints()).andReturn(sps);
+ replay(mockDev2);
+
+ //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setNumber(portNum);
+ mockIPort.setType("port");
+ String iPortDesc = "port 1 at SEA Switch";
+ expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
+ expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
+ replay(mockIPort);
+
+ //Make Iterator for mockIport
+ List<IPortObject> portList = new ArrayList<IPortObject>();
+ portList.add(mockIPort);
+
+ //Expectation for mockIDeviceObject
+ IDeviceObject mockIDev = createMock(IDeviceObject.class);
+ expect(mockIDev.getAttachedPorts()).andReturn(portList);
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ replay(mockIDev);
+
+ //Expectation for mockOpe
+ expect(mockOpe.searchDevice(macAddr)).andReturn(null);
+ expect(mockOpe.newDevice()).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+ expect(mockOpe.searchDevice(macAddr2)).andReturn(null);
+ expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
+ expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
+ mockOpe.removeDevice(mockIDev);
+ mockOpe.commit();
+ expect(mockOpe.searchDevice(macAddr)).andReturn(null);
+ replay(mockOpe);
+
+ deviceImpl.init(conf);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ deviceImpl.removeDevice(mockDev2);
+ IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
+ assertNotNull(dev);
+
+ deviceImpl.removeDevice(mockDev);
+ IDeviceObject dev2 = deviceImpl.getDeviceByMac(macAddr);
+ assertNull(dev2);
+
+ verify(mockIDev);
+ verify(mockOpe);
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for getDeviceByMac
+ * Condition:
+ * 1. Unregistered mac address argument is set
+ * Expect:
+ * 1.Nothing happen when you put unregistered mac address
+ * 2.Get the proper IDeviceObject.
+ * 3.Check the IDeviceObject properties set expectedly.
+ */
+ //@Ignore
+ @Test
+ public void testGetDeviceByMac() {
+ try
+ {
+ IDevice mockDev = createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ String dummyMac = "33:33:33:33:33:33";
+
+ expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ replay(mockDev);
+
+ //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setNumber(portNum);
+ mockIPort.setType("port");
+ String iPortDesc = "port 1 at SEA Switch";
+ expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
+ expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
+ replay(mockIPort);
+
+ //Make Iterator for mockIport
+ List<IPortObject> portList = new ArrayList<IPortObject>();
+ portList.add(mockIPort);
+
+ //Expectation for mockIDeviceObject
+ IDeviceObject mockIDev = createMock(IDeviceObject.class);
+ expect(mockIDev.getAttachedPorts()).andReturn(portList);
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ replay(mockIDev);
+
+ //Expectation for mockOpe
+ expect(mockOpe.searchDevice(macAddr)).andReturn(null);
+ expect(mockOpe.newDevice()).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+ expect(mockOpe.searchDevice(dummyMac)).andReturn(null);
+ expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
+ replay(mockOpe);
+
+ deviceImpl.init(conf);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ IDeviceObject dummyDev = deviceImpl.getDeviceByMac(dummyMac);
+ assertNull(dummyDev);
+
+ IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
+ assertNotNull(dev);
+
+ verify(mockIDev);
+ verify(mockOpe);
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for getDeviceByIP method.
+ * Condition:
+ * 1. Unregistered ip address argument is set
+ * Expect:
+ * 1. Nothing happen when you put unregistered mac address
+ * 2. Get the proper IDeviceObject.
+ * 3. Check the IDeviceObject properties set expectedly.
+ */
+ //@Ignore
+ @Test
+ public void testGetDeviceByIP() {
+ try
+ {
+ IDevice mockDev = createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ String ip2 = "192.168.100.2";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer ipInt2 = IPv4.toIPv4Address(ip2);
+ Integer[] ipaddrs = {ipInt, ipInt2};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ String dummyIP = "222.222.222.222";
+
+ expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ replay(mockDev);
+
+ //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setNumber(portNum);
+ mockIPort.setType("port");
+ String iPortDesc = "port 1 at SEA Switch";
+ expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
+ expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
+ replay(mockIPort);
+
+ //Make Iterator for mockIport
+ List<IPortObject> portList = new ArrayList<IPortObject>();
+ portList.add(mockIPort);
+
+ //Expectation for mockIDeviceObject
+ IDeviceObject mockIDev = createMock(IDeviceObject.class);
+ expect(mockIDev.getAttachedPorts()).andReturn(portList);
+ expect(mockIDev.getIPAddress()).andReturn(makeIPStringFromArray(ipaddrs)).times(2);
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ replay(mockIDev);
+
+
+ //Make mock Iterator for IDeviceObject
+ List<IDeviceObject> deviceList = new ArrayList<IDeviceObject>();
+ deviceList.add(mockIDev);
+
+ //Expectation for mockOpe
+ expect(mockOpe.searchDevice(macAddr)).andReturn(null);
+ expect(mockOpe.newDevice()).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+ expect(mockOpe.getDevices()).andReturn(deviceList).times(2);
+ replay(mockOpe);
+
+ deviceImpl.init(conf);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ IDeviceObject dummyDev = deviceImpl.getDeviceByIP(dummyIP);
+ assertNull(dummyDev);
+
+ IDeviceObject dev = deviceImpl.getDeviceByIP(ip);
+ assertNotNull(dev);
+
+ verify(mockIDev);
+ verify(mockOpe);
+
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for testChangeDeviceAttachmentsIDevice
+ * Condition:
+ * 1. Unexisting attachment point argument is set
+ * Expect:
+ * 1. Nothing happen when you put unexisting attachment point.
+ * 2. Set the attachment point expectedly;
+ */
+ //@Ignore
+ @Test
+ public void testChangeDeviceAttachmentsIDevice() {
+ try
+ {
+ IDevice mockDev = createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ replay(mockDev);
+
+ //Dev2
+ IDevice mockDev2 = createMock(Device.class);
+ String switchMacAddr2 = "00:00:00:00:00:00:0a:02";
+ long lSwitchMacAddr2 = HexString.toLong(switchMacAddr2);
+ short portNum2 = 2;
+ SwitchPort sp2 = new SwitchPort(lSwitchMacAddr2, portNum2);
+ SwitchPort sps2[] = {sp2};
+
+ expect(mockDev2.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs);
+ expect(mockDev2.getAttachmentPoints()).andReturn(sps2);
+ replay(mockDev2);
+
+ //Dev3
+ IDevice mockDev3 = createMock(Device.class);
+ String switchMacAddr3 = "00:00:00:00:00:00:00:00";
+ long lSwitchMacAddr3 = HexString.toLong(switchMacAddr3);
+ short portNum3 = 1;
+ SwitchPort sp3 = new SwitchPort(lSwitchMacAddr3, portNum3);
+ SwitchPort sps3[] = {sp3};
+
+ expect(mockDev3.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev3.getIPv4Addresses()).andReturn(ipaddrs);
+ expect(mockDev3.getAttachmentPoints()).andReturn(sps3);
+ replay(mockDev3);
+
+ IDeviceObject mockIDev = createMock(IDeviceObject.class);
+
+ //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setNumber(portNum);
+ mockIPort.setType("port");
+ String iPortDesc = "port 1 at SEA Switch";
+ expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
+ expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
+ mockIPort.removeDevice(mockIDev);
+ mockIPort.removeDevice(mockIDev);
+ replay(mockIPort);
+
+ //Make Iterator for mockIport
+ List<IPortObject> portList = new ArrayList<IPortObject>();
+ portList.add(mockIPort);
+
+ //Expectation for mockIDeviceObject
+ expect(mockIDev.getAttachedPorts()).andReturn(portList).anyTimes();
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ replay(mockIDev);
+
+ //Mock IPortObject 2 with dpid "00:00:00:00:00:00:0a:02" and port "2"
+ IPortObject mockIPort2 = createMock(IPortObject.class);
+ mockIPort2.setNumber(portNum2);
+ mockIPort2.setType("port");
+ String iPortDesc2 = "port 2 at LAX Switch";
+ expect(mockIPort2.getNumber()).andReturn(portNum2).anyTimes();
+ expect(mockIPort2.getDesc()).andReturn(iPortDesc2).anyTimes();
+ mockIPort2.setDevice(mockIDev);
+ replay(mockIPort2);
+
+ //Make Iterator for mockIport
+ List<IPortObject> portList2 = new ArrayList<IPortObject>();
+ portList2.add(mockIPort2);
+
+ //Mock IPortObject 3 with dpid "00:00:00:00:00:00:00:00" and port "1"
+ IPortObject mockIPort3 = createMock(IPortObject.class);
+ mockIPort3.setNumber(portNum3);
+ mockIPort3.setType("port");
+ String iPortDesc3 = "n/a";
+ expect(mockIPort3.getNumber()).andReturn(portNum3).anyTimes();
+ expect(mockIPort3.getDesc()).andReturn(iPortDesc3).anyTimes();
+ mockIPort3.setDevice(mockIDev);
+ replay(mockIPort3);
+
+ //Expectation for mockOpe
+ expect(mockOpe.searchDevice(macAddr)).andReturn(null);
+ expect(mockOpe.newDevice()).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+ expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr2, portNum2)).andReturn(mockIPort2);
+ mockOpe.commit();
+ expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr3, portNum3)).andReturn(null);
+ mockOpe.commit();
+ replay(mockOpe);
+
+ deviceImpl.init(conf);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ deviceImpl.changeDeviceAttachments(mockDev2);
+
+ deviceImpl.changeDeviceAttachments(mockDev3);
+
+ verify(mockIDev);
+ verify(mockOpe);
+
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ //@Ignore
+ @Test
+ public void testChangeDeviceAttachmentsIDeviceIDeviceObject() {
+ //It is tested by the testChangeDeviceAttachmentsIDevice
+ deviceImpl.init(conf);
+ }
+
+ /**
+ * Desc:
+ * Test method for testChangeDeviceIPv4Address
+ * Condition:
+ * N/A
+ * Expect:
+ * 1. Set the ipadress expectedly.
+ */
+ //@Ignore
+ @Test
+ public void testChangeDeviceIPv4Address() {
+ try
+ {
+ //Dev1
+ IDevice mockDev = createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
+ expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ replay(mockDev);
+
+ //Dev2
+ IDevice mockDev2 = createMock(Device.class);
+ String ip2 = "192.168.100.2";
+ Integer ipInt2 = IPv4.toIPv4Address(ip2);
+ Integer[] ipaddrs2 = {ipInt2};
+ expect(mockDev2.getMACAddressString()).andReturn(macAddr);
+ expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2);
+ replay(mockDev2);
+
+ //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
+ IPortObject mockIPort = createMock(IPortObject.class);
+ mockIPort.setNumber(portNum);
+ mockIPort.setType("port");
+ String iPortDesc = "port 1 at SEA Switch";
+ expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
+ expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
+ replay(mockIPort);
+
+ //Make Iterator for mockIport
+ List<IPortObject> portList = new ArrayList<IPortObject>();
+ portList.add(mockIPort);
+
+ //Expectation for mockIDeviceObject
+ IDeviceObject mockIDev = createMock(IDeviceObject.class);
+ expect(mockIDev.getAttachedPorts()).andReturn(portList);
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
+ mockIDev.setMACAddress(macAddr);
+ mockIDev.setType("device");
+ mockIDev.setState("ACTIVE");
+ mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs2));
+ replay(mockIDev);
+
+ //Expectation for mockOpe
+ expect(mockOpe.searchDevice(macAddr)).andReturn(null);
+ expect(mockOpe.newDevice()).andReturn(mockIDev);
+ expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
+ mockOpe.commit();
+ expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
+ mockOpe.commit();
+ replay(mockOpe);
+
+ deviceImpl.init(conf);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ deviceImpl.changeDeviceIPv4Address(mockDev2);
+
+ verify(mockIDev);
+ verify(mockOpe);
+
+ }
+ catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+}
diff --git a/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTestBB.java b/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTestBB.java
new file mode 100644
index 0000000..8ec42be
--- /dev/null
+++ b/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTestBB.java
@@ -0,0 +1,625 @@
+package net.onrc.onos.ofcontroller.devicemanager.internal;
+
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+import java.util.List;
+
+import net.floodlightcontroller.core.internal.TestDatabaseManager;
+import net.floodlightcontroller.devicemanager.IDevice;
+import net.floodlightcontroller.devicemanager.SwitchPort;
+import net.floodlightcontroller.devicemanager.internal.Device;
+import net.floodlightcontroller.packet.IPv4;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
+import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
+import net.onrc.onos.ofcontroller.devicemanager.IDeviceStorage;
+import net.onrc.onos.util.GraphDBConnection;
+import net.onrc.onos.util.GraphDBOperation;
+
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.openflow.util.HexString;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.slf4j.LoggerFactory;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import com.thinkaurelius.titan.core.TitanFactory;
+import com.thinkaurelius.titan.core.TitanGraph;
+
+//Add Powermock preparation
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
+public class DeviceStorageImplTestBB {
+ protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
+
+ String conf;
+ private GraphDBConnection conn = null;
+ private GraphDBOperation ope = null;
+ private TitanGraph titanGraph = null;
+ IDeviceStorage deviceImpl = null;
+
+ @Before
+ public void setUp() throws Exception {
+
+ deviceImpl = new DeviceStorageImpl();
+ conf = "/dummy/path/to/db";
+
+ // Make mock cassandra DB
+ // Replace TitanFactory.open() to return mock DB
+ titanGraph = TestDatabaseManager.getTestDatabase();
+ TestDatabaseManager.populateTestData(titanGraph);
+ PowerMock.mockStatic(TitanFactory.class);
+ EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
+ PowerMock.replay(TitanFactory.class);
+
+ conn = GraphDBConnection.getInstance(conf);
+ ope = new GraphDBOperation(conn);
+
+ deviceImpl.init(conf);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ titanGraph.shutdown();
+ TestDatabaseManager.deleteTestDatabase();
+
+ deviceImpl.close();
+ deviceImpl = null;
+ }
+
+ /**
+ * Desc:
+ * Test method for addDevice method.
+ * Codition:
+ * N/A
+ * Expect:
+ * Get proper IDeviceObject
+ * Check the IDeviceObject properties set
+ */
+ @Test
+ public void testAddDevice() {
+ try
+ {
+ //Make mockDevice
+ IDevice mockDev = EasyMock.createMock(Device.class);
+ // Mac addr for test device.
+ String macAddr = "99:99:99:99:99:99";
+ // IP addr for test device
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ // Mac addr for attached switch
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ // Port number for attached switch
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+
+ EasyMock.replay(mockDev);
+
+ //Add the device
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ //Test to take a Device from DB correctly
+ IDeviceObject devObj1 = ope.searchDevice(macAddr);
+ assertEquals(macAddr, devObj1.getMACAddress());
+
+ //Test to take a attached sw from DB correctly
+ for(ISwitchObject sw1: devObj1.getSwitch())
+ {
+ String swMacFromDB = sw1.getDPID();
+ assertEquals(switchMacAddr, swMacFromDB);
+ }
+
+ //Test to take a IP addr from DB
+ //TodoForGettingIPaddr. There may be bug in the test class.
+ String ipFromDB = devObj1.getIPAddress();
+ String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(",");
+ List<String> ipsList = Arrays.asList(ipsFromDB);
+ assertTrue(ipsList.contains(ip));
+
+ //Test to take a attached port from DB
+ for(IPortObject port : devObj1.getAttachedPorts())
+ {
+
+ //In this implementing, the object was not set the port. So it must be null.
+ if(port.getNumber() != null)
+ {
+ String portNumFromDB = port.getNumber().toString();
+ assertEquals(String.valueOf(portNum), portNumFromDB);
+ }
+ }
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for addDevice method.
+ * Condition:
+ * Already added device is existed.
+ * Expect:
+ * Get proper IDeviceObject still.
+ * Check the IDeviceObject properties set.
+ */
+ @Test
+ public void testAddDeviceExisting() {
+ try
+ {
+ IDevice mockDev = EasyMock.createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.replay(mockDev);
+
+ //Add the device
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ //Test to take a Device from DB correctly
+ IDeviceObject devObj1 = ope.searchDevice(macAddr);
+ assertEquals(macAddr, devObj1.getMACAddress());
+
+ //Add the same device
+ IDeviceObject obj2 = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj2);
+
+ IDeviceObject devObj2 = ope.searchDevice(macAddr);
+ assertEquals(macAddr, devObj2.getMACAddress());
+
+ //Test to take a attached port from DB
+ for(IPortObject port : devObj2.getAttachedPorts())
+ {
+ //In this implementing, the object was not set the port. So it must be null.
+ if(port.getNumber() != null)
+ {
+
+ String portNumFromDB = port.getNumber().toString();
+ assertEquals(String.valueOf(portNum), portNumFromDB);
+
+ ISwitchObject sw = port.getSwitch();
+ String str = sw.getDPID();
+ log.debug("");
+ }
+ }
+
+ String ipFromDB = devObj2.getIPAddress();
+ String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(",");
+ List<String> ipsList = Arrays.asList(ipsFromDB);
+ assertTrue(ipsList.contains(ip));
+
+ //Test to take a attached port from DB
+ for(IPortObject port : devObj2.getAttachedPorts())
+ {
+
+ //In this implementing, the object was not set the port. So it must be null.
+ if(port.getNumber() != null)
+ {
+ String portNumFromDB = port.getNumber().toString();
+ assertEquals(String.valueOf(portNum), portNumFromDB);
+ }
+ }
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+ /**
+ * Desc:
+ * Test method for updateDevice method.
+ * Condition:
+ * The mac address and attachment point are the same.
+ * All of the other parameter are different.
+ * Expect:
+ * Changed parameters are set properly.
+ */
+ //@Ignore
+ @Test
+ public void testUpdateDevice() {
+ try
+ {
+ IDevice mockDev = EasyMock.createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.replay(mockDev);
+
+ //Dev2 (attached port is the same)
+ IDevice mockDev2 = EasyMock.createMock(Device.class);
+ String macAddr2 = "99:aa:aa:aa:aa:aa";
+ Integer ip2 = IPv4.toIPv4Address("192.168.100.2");
+ Integer[] ipaddrs2 = {ip2};
+
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
+ EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2);
+ EasyMock.expect(mockDev2.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2);
+ EasyMock.replay(mockDev2);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ IDeviceObject dev1 = ope.searchDevice(macAddr);
+ assertEquals(macAddr, dev1.getMACAddress());
+
+ //update theDevice
+ deviceImpl.updateDevice(mockDev2);
+ IDeviceObject dev2 = ope.searchDevice(macAddr2);
+ assertEquals(macAddr2, dev2.getMACAddress());
+ IPortObject iport = ope.searchPort(switchMacAddr, portNum);
+
+ //Test to take a attached port from DB
+ for(IDeviceObject dev : iport.getDevices())
+ {
+ String macAddrFromDB = dev.getMACAddress();
+ if(macAddr2.equals(macAddrFromDB)){
+ //Nothing to do
+ }
+ else{
+ fail("notFoundTheDeviceOnThePort");
+ }
+ }
+
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for testRemoveDevice method.
+ * Condition:
+ * 1. Unregistered IDeviceObject argument is put.
+ * Expect:
+ * 1. Nothing happen when unregistered IDeviceObject is put
+ * 2. IDeviceObject will be removed.
+ */
+ //@Ignore
+ @Test
+ public void testRemoveDevice() {
+ try
+ {
+ IDevice mockDev = EasyMock.createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.replay(mockDev);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ IDeviceObject dev1 = ope.searchDevice(macAddr);
+ assertEquals(macAddr, dev1.getMACAddress());
+
+ deviceImpl.removeDevice(mockDev);
+ IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
+ assertNull(dev);
+
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for getDeviceByMac
+ * Condition:
+ * 1. Unregistered mac address argument is set
+ * Expect:
+ * 1.Nothing happen when you put unregistered mac address
+ * 2.Get the proper IDeviceObject.
+ * 3.Check the IDeviceObject properties set.
+ */
+ //@Ignore
+ @Test
+ public void testGetDeviceByMac() {
+ try
+ {
+ IDevice mockDev = EasyMock.createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.replay(mockDev);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ IDeviceObject dev1 = ope.searchDevice(macAddr);
+ assertEquals(macAddr, dev1.getMACAddress());
+
+ IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
+ assertNotNull(dev);
+ assertEquals(macAddr, dev.getMACAddress());
+
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for getDeviceByIP method.
+ * Condition:
+ * 1. Unregistered ip address argument is set
+ * Expect:
+ * 1. Nothing happen when you put unregistered mac address
+ * 2. Get the proper IDeviceObject.
+ * 3. Check the IDeviceObject properties set.
+ */
+ //@Ignore
+ @Test
+ public void testGetDeviceByIP() {
+ try
+ {
+ IDevice mockDev = EasyMock.createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.replay(mockDev);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ IDeviceObject dev1 = ope.searchDevice(macAddr);
+ assertEquals(macAddr, dev1.getMACAddress());
+
+ IDeviceObject dev = deviceImpl.getDeviceByIP(ip);
+ assertNotNull(dev);
+ String ipFromDB = dev.getIPAddress();
+ String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(",");
+ List<String> ipsList = Arrays.asList(ipsFromDB);
+ assertTrue(ipsList.contains(ip));
+
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Desc:
+ * Test method for testChangeDeviceAttachmentsIDevice
+ * Condition:
+ * 1. Unexisting attachment point argument is set
+ * Expect:
+ * 1. Unexisting attachment point is ignored, so nothing happen.
+ * 2. Change the attachment point.
+ */
+ //@Ignore
+ @Test
+ public void testChangeDeviceAttachmentsIDevice() {
+ try
+ {
+ IDevice mockDev = EasyMock.createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.replay(mockDev);
+
+ //Dev2
+ IDevice mockDev2 = EasyMock.createMock(Device.class);
+ String switchMacAddr2 = "00:00:00:00:00:00:0a:02";
+ long lSwitchMacAddr2 = HexString.toLong(switchMacAddr2);
+ short portNum2 = 2;
+ SwitchPort sp2 = new SwitchPort(lSwitchMacAddr2, portNum2);
+ SwitchPort sps2[] = {sp2};
+
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev2.getAttachmentPoints()).andReturn(sps2);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
+ EasyMock.replay(mockDev2);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ deviceImpl.changeDeviceAttachments(mockDev2);
+
+ IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
+ assertNotNull(dev);
+
+ for(ISwitchObject sw1: dev.getSwitch())
+ {
+ String swMacFromDB = sw1.getDPID();
+ assertEquals(switchMacAddr2, swMacFromDB);
+ }
+ } catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ //@Ignore
+ @Test
+ public void testChangeDeviceAttachmentsIDeviceIDeviceObject() {
+ //It is tested by the testChangeDeviceAttachmentsIDevice
+ }
+
+ /**
+ * Desc:
+ * Test method for testChangeDeviceIPv4Address
+ * Condition:
+ * N/A
+ * Expect:
+ * 1. Check correctly changed the ipadress
+ */
+ //@Ignore
+ @Test
+ public void testChangeDeviceIPv4Address() {
+ try
+ {
+ //Dev1
+ IDevice mockDev = EasyMock.createMock(Device.class);
+ String macAddr = "99:99:99:99:99:99";
+ String ip = "192.168.100.1";
+ Integer ipInt = IPv4.toIPv4Address(ip);
+ Integer[] ipaddrs = {ipInt};
+ String switchMacAddr = "00:00:00:00:00:00:0a:01";
+ long switchMacAddrL = HexString.toLong(switchMacAddr);
+ short portNum = 2;
+ SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
+ SwitchPort[] sps = {sp1};
+
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
+ EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr);
+ EasyMock.replay(mockDev);
+
+ IDeviceObject obj = deviceImpl.addDevice(mockDev);
+ assertNotNull(obj);
+
+ IDevice mockDev2 = EasyMock.createMock(Device.class);
+ String ip2 = "192.168.100.2";
+ Integer ipInt2 = IPv4.toIPv4Address(ip2);
+ Integer[] ipaddrs2 = {ipInt2};
+ EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr);
+ EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2);
+ EasyMock.replay(mockDev2);
+
+ IDeviceObject dev1 = ope.searchDevice(macAddr);
+ assertEquals(macAddr, dev1.getMACAddress());
+ String ipFromDB = dev1.getIPAddress();
+ String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(",");
+ List<String> ipsList = Arrays.asList(ipsFromDB);
+ assertTrue(ipsList.contains(ip));
+
+ deviceImpl.changeDeviceIPv4Address(mockDev2);
+
+ IDeviceObject dev2 = ope.searchDevice(macAddr);
+ assertEquals(macAddr, dev2.getMACAddress());
+ String ipFromDB2 = dev2.getIPAddress();
+ String[] ipsFromDB2 = ipFromDB2.replace("[", "").replace("]", "").split(",");
+ List<String> ipsList2 = Arrays.asList(ipsFromDB2);
+ assertTrue(ipsList2.contains(ip2));
+ }
+ catch(Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+}