Add trial code for GraphDBConnection to pass UnitTest.
diff --git a/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImplTest.java b/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImplTest.java
index 079715a..39221ab 100644
--- a/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImplTest.java
+++ b/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImplTest.java
@@ -7,6 +7,9 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
+
+import junit.framework.Assert;
import net.floodlightcontroller.core.INetMapStorage.DM_OPERATION;
import net.floodlightcontroller.core.internal.TestDatabaseManager;
@@ -20,6 +23,8 @@
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.openflow.util.HexString;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -27,8 +32,12 @@
import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.Direction;
+import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.gremlin.java.GremlinPipeline;
+import com.tinkerpop.pipes.PipeFunction;
+import com.tinkerpop.pipes.transform.PathPipe;
@RunWith(PowerMockRunner.class)
@PrepareForTest({TitanFactory.class})
@@ -75,19 +84,17 @@
TestDatabaseManager.deleteTestDatabase();
}
- // TODO: remove @Ignore after UPDATE method will be implemented
+ // TODO: remove @Ignore after UPDATE method is implemented
@Ignore @Test
public void testUpdate_UpdateSingleLink() {
Link linkToUpdate= createExistingLink();
- LinkInfo infoToUpdate = new LinkInfo(
- System.currentTimeMillis(),
- System.currentTimeMillis(),
- System.currentTimeMillis(),
- 0, 0);
+ long currentTime = System.currentTimeMillis();
+ LinkInfo infoToUpdate = createFeasibleLinkInfo(currentTime);
+ LinkInfo infoToVerify = createFeasibleLinkInfo(currentTime);
linkStorage.update(linkToUpdate, infoToUpdate, ILinkStorage.DM_OPERATION.UPDATE);
- // TODO: get LinkInfo from titanGraph and verify
+ doTestLinkHasStateOf(linkToUpdate, infoToVerify);
}
@Test
@@ -141,18 +148,7 @@
linkStorage.update(linkToDelete, DM_OPERATION.DELETE);
doTestLinkIsNotInGraph(linkToVerify);
}
-
- // TODO: remove @Ignore after UPDATE method will be implemented
- @Ignore @Test
- public void testUpdate_UpdateLinks(){
- List<Link> linksToUpdate= createExistingLinks();
-
- // TODO: Who calls update method like this way? Remove this test if unneeded.
- linkStorage.update(linksToUpdate, ILinkStorage.DM_OPERATION.UPDATE);
-
- // TODO: verification of update result
- }
-
+
@Test
public void testUpdate_CreateLinks(){
List<Link> linksToCreate = createFeasibleLinks();
@@ -247,19 +243,17 @@
doTestLinkIsNotInGraph(createExistingLink());
}
+ // TODO: remove @Ignore after UPDATE method is implemented
@Ignore @Test
public void testAddOrUpdateLink_Update() {
Link linkToUpdate= createExistingLink();
- LinkInfo infoToUpdate = new LinkInfo(
- System.currentTimeMillis(),
- System.currentTimeMillis(),
- System.currentTimeMillis(),
- 0, 0);
+ long currentTime = System.currentTimeMillis();
+ LinkInfo infoToUpdate = createFeasibleLinkInfo(currentTime);
+ LinkInfo infoToVerify = createFeasibleLinkInfo(currentTime);
linkStorage.addOrUpdateLink(linkToUpdate, infoToUpdate, ILinkStorage.DM_OPERATION.UPDATE);
- // TODO: get LinkInfo from titanGraph and verify
-
+ doTestLinkHasStateOf(linkToUpdate, infoToVerify);
}
@Test
@@ -421,7 +415,6 @@
short src_port = link.getSrcPort();
short dst_port = link.getDstPort();
- GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
Iterator<Vertex> it = titanGraph.getVertices("dpid", src_dpid).iterator();
// Test if just one switch is found in the graph
@@ -429,6 +422,7 @@
Vertex sw = it.next();
assertFalse(it.hasNext());
+ GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
pipe.start(sw).out("on").has("number", src_port).out("link").has("number", dst_port).in("on").has("dpid", dst_dpid);
// Test if just one link is found in the graph
@@ -447,7 +441,6 @@
short src_port = link.getSrcPort();
short dst_port = link.getDstPort();
- GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
Iterator<Vertex> it = titanGraph.getVertices("dpid", src_dpid).iterator();
// Test if just one switch is found in the graph
@@ -455,11 +448,76 @@
Vertex sw = it.next();
assertFalse(it.hasNext());
+ GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
pipe.start(sw).out("on").has("number", src_port).out("link").has("number", dst_port).in("on").has("dpid", dst_dpid);
// Test if no link is found in the graph
assertFalse(pipe.hasNext());
}
+
+ /**
+ * Test if titanGraph has specific Link with specific LinkInfo
+ * @param link
+ */
+ private void doTestLinkHasStateOf(Link link, LinkInfo info) {
+ String src_dpid = HexString.toHexString(link.getSrc());
+ String dst_dpid = HexString.toHexString(link.getDst());
+ short src_port = link.getSrcPort();
+ short dst_port = link.getDstPort();
+
+ Iterator<Vertex> it = titanGraph.getVertices("dpid", src_dpid).iterator();
+
+ // Test if just one switch is found in the graph
+ assertTrue(it.hasNext());
+ Vertex sw = it.next();
+ assertFalse(it.hasNext());
+
+ GremlinPipeline<Vertex, Edge> pipe = new GremlinPipeline<Vertex, Edge>();
+ pipe.start(sw);
+ pipe.enablePath();
+ pipe.out("on").has("number", src_port).out("link").has("number", dst_port).in("on").has("dpid", dst_dpid)
+ .path().step(new PipeFunction<PathPipe<Vertex>, Edge>() {
+ @Override
+ public Edge compute(PathPipe<Vertex> pipepath) {
+ List<Vertex> V = pipepath.next();
+
+ Vertex port_src = V.get(1);
+ Vertex port_dst = V.get(2);
+
+ for(Edge e : port_src.getEdges(Direction.OUT)) {
+ if(e.getVertex(Direction.IN).equals(port_dst)) {
+ return e;
+ }
+ }
+
+ return null;
+ }
+ });
+
+ // Test if just one link is found in the graph
+ assertTrue(pipe.hasNext());
+ Edge edge = pipe.next();
+ assertTrue(edge != null);
+ assertFalse(pipe.hasNext());
+
+ // TODO: implement test code to check if update is correctly done.
+ int portStateSrc = edge.getVertex(Direction.OUT).getProperty("port_state");
+ int portStateDst = edge.getVertex(Direction.IN).getProperty("port_state");
+
+ assertTrue(portStateSrc == info.getSrcPortState());
+ assertTrue(portStateDst == info.getDstPortState());
+
+// long firstSeenTime = edge.getProperty("first_seen_time");
+// long lastLldpReceivedTime = edge.getProperty("last_lldp_received_time");
+// long lastBddpReceivedTime = edge.getProperty("last_bddp_received_time");
+ long firstSeenTime = edge.getVertex(Direction.OUT).getProperty("first_seen_time");
+ long lastLldpReceivedTime = edge.getVertex(Direction.OUT).getProperty("last_lldp_received_time");
+ long lastBddpReceivedTime = edge.getVertex(Direction.OUT).getProperty("last_bddp_received_time");
+ assertTrue(firstSeenTime == info.getFirstSeenTime());
+ assertTrue(lastLldpReceivedTime == info.getUnicastValidTime());
+ assertTrue(lastBddpReceivedTime == info.getMulticastValidTime());
+ }
+
//----------------- Creation of test data -----------------------
/**
@@ -511,5 +569,23 @@
links.add(new Link(Long.decode("0x0000000000000a01"), 4, Long.decode("0x0000000000000a02"), 1));
return links;
}
+
+ /**
+ * Returns new LinkInfo object with convenient values.
+ * @return LinkInfo object
+ */
+ private LinkInfo createFeasibleLinkInfo(long time) {
+ long time_first = time;
+ long time_last_lldp = time + 50;
+ long time_last_bddp = time + 100;
+ int state_src = OFPhysicalPort.OFPortState.OFPPS_STP_FORWARD.getValue();
+ int state_dst = OFPhysicalPort.OFPortState.OFPPS_STP_LISTEN.getValue();
+
+ return new LinkInfo(time_first,
+ time_last_lldp,
+ time_last_bddp,
+ state_src,
+ state_dst);
+ }
//---------------------------------------------------------------
}