Merge pull request #406 from effy/dev1

test codes for network graph updates
diff --git a/src/test/java/net/onrc/onos/graph/GraphDBConnectionTest.java b/src/test/java/net/onrc/onos/graph/GraphDBConnectionTest.java
new file mode 100644
index 0000000..bee936d
--- /dev/null
+++ b/src/test/java/net/onrc/onos/graph/GraphDBConnectionTest.java
@@ -0,0 +1,229 @@
+/**
+ * 
+ */
+package net.onrc.onos.graph;
+
+import static org.junit.Assert.*;
+import static org.easymock.EasyMock.expect;
+import static org.powermock.api.easymock.PowerMock.*;
+
+import java.util.*;
+
+import net.onrc.onos.graph.GraphDBOperation;
+
+import org.easymock.IAnswer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import com.thinkaurelius.titan.core.TitanFactory;
+import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
+import com.tinkerpop.frames.FramedGraph;
+
+/**
+ * @author Toshio Koide
+ *
+ */
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({
+	GraphDBConnection.class,
+	GraphDBOperation.class,
+	TitanFactory.class,
+	EventTransactionalGraph.class})
+public class GraphDBConnectionTest {
+	private static TitanGraph graph = null;
+	private static EventTransactionalGraph<TitanGraph> eg = null;
+	private static Boolean isGraphOpen = false;
+	/**
+	 * @throws java.lang.Exception
+	 */
+	@Before
+	public void setUp() throws Exception {
+	}
+
+	/**
+	 * @throws java.lang.Exception
+	 */
+	@After
+	public void tearDown() throws Exception {
+	}
+
+	
+	private void expectDBConnectionAvailable() throws Exception {
+		isGraphOpen = false;
+		
+		// create mock objects
+		mockStatic(TitanFactory.class);
+		mockStatic(EventTransactionalGraph.class);
+		graph = createMock(TitanGraph.class);
+		eg = createMock(EventTransactionalGraph.class);
+		
+		// setup expectations
+		expect(graph.isOpen()).andAnswer(new IAnswer<Boolean>() {
+			@Override
+			public Boolean answer() throws Throwable {
+				return isGraphOpen;
+			}
+		}).anyTimes();
+		expect(TitanFactory.open("/path/to/dummy")).andAnswer(new IAnswer<TitanGraph>() {
+			@Override
+			public TitanGraph answer() throws Throwable {
+				isGraphOpen = true;
+				return graph;
+			}
+		}).anyTimes();
+		expect(graph.getIndexedKeys(Vertex.class)).andReturn(new TreeSet<String>());
+		graph.createKeyIndex("dpid", Vertex.class);
+		graph.createKeyIndex("port_id", Vertex.class);
+		graph.createKeyIndex("type", Vertex.class);
+		graph.createKeyIndex("dl_addr", Vertex.class);
+		graph.createKeyIndex("flow_id", Vertex.class);
+		graph.createKeyIndex("flow_entry_id", Vertex.class);
+		graph.createKeyIndex("switch_state", Vertex.class);
+		graph.commit();
+		expectNew(EventTransactionalGraph.class, graph).andReturn(eg);
+	}
+	
+	/**
+	 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#getInstance(java.lang.String)}.
+	 * @throws Exception
+	 */
+	@Test
+	public final void testGetInstance() throws Exception {
+		// setup expectations
+		expectDBConnectionAvailable();
+		
+		// start the test
+		replayAll();
+		GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
+
+		// verify the test
+		verifyAll();
+		assertNotNull(conn);
+	}
+
+	/**
+	 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#getFramedGraph()}.
+	 * @throws Exception
+	 */
+	@Test
+	public final void testGetFramedGraph() throws Exception {
+		// setup expectations
+		expectDBConnectionAvailable();
+		
+		// start the test
+		replayAll();
+		GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
+		FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+		
+		// verify the test
+		verifyAll();
+		assertNotNull(fg);
+		assertEquals(graph, fg.getBaseGraph());
+	}
+
+	/**
+	 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#addEventListener(net.onrc.onos.graph.LocalGraphChangedListener)}.
+	 * @throws Exception 
+	 */
+	@Test
+	public final void testAddEventListener() throws Exception {
+		// instantiate required objects
+		LocalGraphChangedListener listener = new LocalTopologyEventListener(null);
+
+		// setup expectations
+		expectDBConnectionAvailable();
+		eg.addListener(listener);
+		
+		// start the test
+		replayAll();
+		GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
+		conn.addEventListener(listener);
+		
+		// verify the test
+		verifyAll();
+	}
+
+	/**
+	 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#isValid()}.
+	 * @throws Exception 
+	 */
+	@Test
+	public final void testIsValid() throws Exception {
+		// setup expectations
+		expectDBConnectionAvailable();
+
+		// start the test
+		replayAll();
+		GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
+		Boolean result = conn.isValid();
+		
+		// verify the test
+		verifyAll();
+		assertTrue(result);
+	}
+
+	/**
+	 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#commit()}.
+	 * @throws Exception 
+	 */
+	@Test
+	public final void testCommit() throws Exception {
+		// setup expectations
+		expectDBConnectionAvailable();
+		graph.commit();
+		
+		// start the test
+		replayAll();
+		GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
+		conn.commit();
+
+		// verify the test
+		verifyAll();
+	}
+
+	/**
+	 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#rollback()}.
+	 * @throws Exception 
+	 */
+	@Test
+	public final void testRollback() throws Exception {
+		// setup expectations
+		expectDBConnectionAvailable();
+		graph.rollback();
+		
+		// start the test
+		replayAll();
+		GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
+		conn.rollback();
+
+		// verify the test
+		verifyAll();
+	}
+
+	/**
+	 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#close()}.
+	 * @throws Exception 
+	 */
+	@Test
+	public final void testClose() throws Exception {
+		// setup expectations
+		expectDBConnectionAvailable();
+		graph.commit();
+		
+		// start the test
+		replayAll();
+		GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
+		conn.close();
+
+		// verify the test
+		verifyAll();
+	}
+
+}
diff --git a/src/test/java/net/onrc/onos/util/GraphDBOperationTest.java b/src/test/java/net/onrc/onos/graph/GraphDBOperationTest.java
similarity index 97%
rename from src/test/java/net/onrc/onos/util/GraphDBOperationTest.java
rename to src/test/java/net/onrc/onos/graph/GraphDBOperationTest.java
index f85cc4f..e99ca81 100644
--- a/src/test/java/net/onrc/onos/util/GraphDBOperationTest.java
+++ b/src/test/java/net/onrc/onos/graph/GraphDBOperationTest.java
@@ -1,7 +1,7 @@
 /**
  * 
  */
-package net.onrc.onos.util;
+package net.onrc.onos.graph;
 
 import static org.junit.Assert.*;
 
@@ -95,11 +95,12 @@
 	public final void testNewSwitch() {
 		assertNull(op.searchSwitch("123"));
 
-		op.newSwitch("123");		
+		ISwitchObject sw = op.newSwitch("123");
+		assertEquals(sw.getDPID(), "123");
 		op.commit();
 
-		ISwitchObject sw = op.searchSwitch("123");
-		assertNotNull(op);
+		sw = op.searchSwitch("123");
+		assertNotNull(sw);
 		assertEquals("123", sw.getDPID());
 	}
 
@@ -232,7 +233,7 @@
 	public final void testNewPort() {
 		assertFalse(testdb.getVertices("type", "port").iterator().hasNext());
 		
-		IPortObject port = op.newPort((short) 10);
+		IPortObject port = op.newPort("1", (short) 10);
 		assertTrue(port.getNumber() == 10);
 		op.commit();
 		
@@ -250,12 +251,12 @@
 		IPortObject port;
 		
 		sw = op.newSwitch("1");
-		sw.addPort(op.newPort((short) 1));
-		sw.addPort(op.newPort((short) 2));
+		sw.addPort(op.newPort("1", (short) 1));
+		sw.addPort(op.newPort("1", (short) 2));
 		
 		sw = op.newSwitch("2");
-		sw.addPort(op.newPort((short) 1));
-		sw.addPort(op.newPort((short) 2));
+		sw.addPort(op.newPort("2", (short) 1));
+		sw.addPort(op.newPort("2", (short) 2));
 
 		op.commit();
 
@@ -300,8 +301,8 @@
 		IPortObject port;
 		
 		sw = op.newSwitch("1");
-		sw.addPort(op.newPort((short) 1));
-		sw.addPort(op.newPort((short) 2));
+		sw.addPort(op.newPort("1", (short) 1));
+		sw.addPort(op.newPort("1", (short) 2));
 		
 		op.commit();