Toshio Koide | e32a4ec | 2013-06-27 11:51:21 -0700 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package net.onrc.onos.graph; |
| 5 | |
| 6 | import static org.junit.Assert.*; |
| 7 | import static org.easymock.EasyMock.expect; |
| 8 | import static org.powermock.api.easymock.PowerMock.*; |
| 9 | |
| 10 | import java.util.*; |
| 11 | |
| 12 | import net.onrc.onos.graph.GraphDBOperation; |
| 13 | |
| 14 | import org.easymock.IAnswer; |
| 15 | import org.junit.After; |
| 16 | import org.junit.Before; |
| 17 | import org.junit.Test; |
| 18 | import org.junit.runner.RunWith; |
| 19 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 20 | import org.powermock.modules.junit4.PowerMockRunner; |
| 21 | |
| 22 | import com.thinkaurelius.titan.core.TitanFactory; |
| 23 | import com.thinkaurelius.titan.core.TitanGraph; |
| 24 | import com.tinkerpop.blueprints.Vertex; |
| 25 | import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph; |
| 26 | import com.tinkerpop.frames.FramedGraph; |
| 27 | |
| 28 | /** |
| 29 | * @author Toshio Koide |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | @RunWith(PowerMockRunner.class) |
| 34 | @PrepareForTest({ |
| 35 | GraphDBConnection.class, |
| 36 | GraphDBOperation.class, |
| 37 | TitanFactory.class, |
| 38 | EventTransactionalGraph.class}) |
| 39 | public class GraphDBConnectionTest { |
| 40 | private static TitanGraph graph = null; |
| 41 | private static EventTransactionalGraph<TitanGraph> eg = null; |
| 42 | private static Boolean isGraphOpen = false; |
| 43 | /** |
| 44 | * @throws java.lang.Exception |
| 45 | */ |
| 46 | @Before |
| 47 | public void setUp() throws Exception { |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @throws java.lang.Exception |
| 52 | */ |
| 53 | @After |
| 54 | public void tearDown() throws Exception { |
| 55 | } |
| 56 | |
| 57 | |
Yuta HIGUCHI | e2eb7fc | 2013-10-14 10:00:55 -0700 | [diff] [blame] | 58 | @SuppressWarnings("unchecked") |
Toshio Koide | e32a4ec | 2013-06-27 11:51:21 -0700 | [diff] [blame] | 59 | private void expectDBConnectionAvailable() throws Exception { |
| 60 | isGraphOpen = false; |
| 61 | |
| 62 | // create mock objects |
| 63 | mockStatic(TitanFactory.class); |
| 64 | mockStatic(EventTransactionalGraph.class); |
| 65 | graph = createMock(TitanGraph.class); |
Yuta HIGUCHI | e2eb7fc | 2013-10-14 10:00:55 -0700 | [diff] [blame] | 66 | eg = (EventTransactionalGraph<TitanGraph>)createMock(EventTransactionalGraph.class); |
Toshio Koide | e32a4ec | 2013-06-27 11:51:21 -0700 | [diff] [blame] | 67 | |
| 68 | // setup expectations |
| 69 | expect(graph.isOpen()).andAnswer(new IAnswer<Boolean>() { |
| 70 | @Override |
| 71 | public Boolean answer() throws Throwable { |
| 72 | return isGraphOpen; |
| 73 | } |
| 74 | }).anyTimes(); |
| 75 | expect(TitanFactory.open("/path/to/dummy")).andAnswer(new IAnswer<TitanGraph>() { |
| 76 | @Override |
| 77 | public TitanGraph answer() throws Throwable { |
| 78 | isGraphOpen = true; |
| 79 | return graph; |
| 80 | } |
| 81 | }).anyTimes(); |
| 82 | expect(graph.getIndexedKeys(Vertex.class)).andReturn(new TreeSet<String>()); |
| 83 | graph.createKeyIndex("dpid", Vertex.class); |
| 84 | graph.createKeyIndex("port_id", Vertex.class); |
| 85 | graph.createKeyIndex("type", Vertex.class); |
| 86 | graph.createKeyIndex("dl_addr", Vertex.class); |
| 87 | graph.createKeyIndex("flow_id", Vertex.class); |
| 88 | graph.createKeyIndex("flow_entry_id", Vertex.class); |
| 89 | graph.createKeyIndex("switch_state", Vertex.class); |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 90 | graph.createKeyIndex("ipv4_address", Vertex.class); |
Toshio Koide | e32a4ec | 2013-06-27 11:51:21 -0700 | [diff] [blame] | 91 | graph.commit(); |
| 92 | expectNew(EventTransactionalGraph.class, graph).andReturn(eg); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Test method for {@link net.onrc.onos.graph.GraphDBConnection#getInstance(java.lang.String)}. |
| 97 | * @throws Exception |
| 98 | */ |
| 99 | @Test |
| 100 | public final void testGetInstance() throws Exception { |
| 101 | // setup expectations |
| 102 | expectDBConnectionAvailable(); |
| 103 | |
| 104 | // start the test |
| 105 | replayAll(); |
| 106 | GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy"); |
| 107 | |
| 108 | // verify the test |
| 109 | verifyAll(); |
| 110 | assertNotNull(conn); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Test method for {@link net.onrc.onos.graph.GraphDBConnection#getFramedGraph()}. |
| 115 | * @throws Exception |
| 116 | */ |
| 117 | @Test |
| 118 | public final void testGetFramedGraph() throws Exception { |
| 119 | // setup expectations |
| 120 | expectDBConnectionAvailable(); |
| 121 | |
| 122 | // start the test |
| 123 | replayAll(); |
| 124 | GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy"); |
| 125 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 126 | |
| 127 | // verify the test |
| 128 | verifyAll(); |
| 129 | assertNotNull(fg); |
| 130 | assertEquals(graph, fg.getBaseGraph()); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Test method for {@link net.onrc.onos.graph.GraphDBConnection#addEventListener(net.onrc.onos.graph.LocalGraphChangedListener)}. |
| 135 | * @throws Exception |
| 136 | */ |
| 137 | @Test |
| 138 | public final void testAddEventListener() throws Exception { |
| 139 | // instantiate required objects |
| 140 | LocalGraphChangedListener listener = new LocalTopologyEventListener(null); |
| 141 | |
| 142 | // setup expectations |
| 143 | expectDBConnectionAvailable(); |
| 144 | eg.addListener(listener); |
| 145 | |
| 146 | // start the test |
| 147 | replayAll(); |
| 148 | GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy"); |
| 149 | conn.addEventListener(listener); |
| 150 | |
| 151 | // verify the test |
| 152 | verifyAll(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Test method for {@link net.onrc.onos.graph.GraphDBConnection#isValid()}. |
| 157 | * @throws Exception |
| 158 | */ |
| 159 | @Test |
| 160 | public final void testIsValid() throws Exception { |
| 161 | // setup expectations |
| 162 | expectDBConnectionAvailable(); |
| 163 | |
| 164 | // start the test |
| 165 | replayAll(); |
| 166 | GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy"); |
| 167 | Boolean result = conn.isValid(); |
| 168 | |
| 169 | // verify the test |
| 170 | verifyAll(); |
| 171 | assertTrue(result); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Test method for {@link net.onrc.onos.graph.GraphDBConnection#commit()}. |
| 176 | * @throws Exception |
| 177 | */ |
| 178 | @Test |
| 179 | public final void testCommit() throws Exception { |
| 180 | // setup expectations |
| 181 | expectDBConnectionAvailable(); |
| 182 | graph.commit(); |
| 183 | |
| 184 | // start the test |
| 185 | replayAll(); |
| 186 | GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy"); |
| 187 | conn.commit(); |
| 188 | |
| 189 | // verify the test |
| 190 | verifyAll(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Test method for {@link net.onrc.onos.graph.GraphDBConnection#rollback()}. |
| 195 | * @throws Exception |
| 196 | */ |
| 197 | @Test |
| 198 | public final void testRollback() throws Exception { |
| 199 | // setup expectations |
| 200 | expectDBConnectionAvailable(); |
| 201 | graph.rollback(); |
| 202 | |
| 203 | // start the test |
| 204 | replayAll(); |
| 205 | GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy"); |
| 206 | conn.rollback(); |
| 207 | |
| 208 | // verify the test |
| 209 | verifyAll(); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Test method for {@link net.onrc.onos.graph.GraphDBConnection#close()}. |
| 214 | * @throws Exception |
| 215 | */ |
| 216 | @Test |
| 217 | public final void testClose() throws Exception { |
| 218 | // setup expectations |
| 219 | expectDBConnectionAvailable(); |
| 220 | graph.commit(); |
| 221 | |
| 222 | // start the test |
| 223 | replayAll(); |
| 224 | GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy"); |
| 225 | conn.close(); |
| 226 | |
| 227 | // verify the test |
| 228 | verifyAll(); |
| 229 | } |
| 230 | |
| 231 | } |