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