blob: 397ed8810cb443208f7649f61c81061ec3115ea8 [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
Yuta HIGUCHIe2eb7fc2013-10-14 10:00:55 -070058 @SuppressWarnings("unchecked")
Toshio Koidee32a4ec2013-06-27 11:51:21 -070059 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 HIGUCHIe2eb7fc2013-10-14 10:00:55 -070066 eg = (EventTransactionalGraph<TitanGraph>)createMock(EventTransactionalGraph.class);
Toshio Koidee32a4ec2013-06-27 11:51:21 -070067
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);
90 graph.commit();
91 expectNew(EventTransactionalGraph.class, graph).andReturn(eg);
92 }
93
94 /**
95 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#getInstance(java.lang.String)}.
96 * @throws Exception
97 */
98 @Test
99 public final void testGetInstance() throws Exception {
100 // setup expectations
101 expectDBConnectionAvailable();
102
103 // start the test
104 replayAll();
105 GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
106
107 // verify the test
108 verifyAll();
109 assertNotNull(conn);
110 }
111
112 /**
113 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#getFramedGraph()}.
114 * @throws Exception
115 */
116 @Test
117 public final void testGetFramedGraph() throws Exception {
118 // setup expectations
119 expectDBConnectionAvailable();
120
121 // start the test
122 replayAll();
123 GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
124 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
125
126 // verify the test
127 verifyAll();
128 assertNotNull(fg);
129 assertEquals(graph, fg.getBaseGraph());
130 }
131
132 /**
133 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#addEventListener(net.onrc.onos.graph.LocalGraphChangedListener)}.
134 * @throws Exception
135 */
136 @Test
137 public final void testAddEventListener() throws Exception {
138 // instantiate required objects
139 LocalGraphChangedListener listener = new LocalTopologyEventListener(null);
140
141 // setup expectations
142 expectDBConnectionAvailable();
143 eg.addListener(listener);
144
145 // start the test
146 replayAll();
147 GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
148 conn.addEventListener(listener);
149
150 // verify the test
151 verifyAll();
152 }
153
154 /**
155 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#isValid()}.
156 * @throws Exception
157 */
158 @Test
159 public final void testIsValid() throws Exception {
160 // setup expectations
161 expectDBConnectionAvailable();
162
163 // start the test
164 replayAll();
165 GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
166 Boolean result = conn.isValid();
167
168 // verify the test
169 verifyAll();
170 assertTrue(result);
171 }
172
173 /**
174 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#commit()}.
175 * @throws Exception
176 */
177 @Test
178 public final void testCommit() throws Exception {
179 // setup expectations
180 expectDBConnectionAvailable();
181 graph.commit();
182
183 // start the test
184 replayAll();
185 GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
186 conn.commit();
187
188 // verify the test
189 verifyAll();
190 }
191
192 /**
193 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#rollback()}.
194 * @throws Exception
195 */
196 @Test
197 public final void testRollback() throws Exception {
198 // setup expectations
199 expectDBConnectionAvailable();
200 graph.rollback();
201
202 // start the test
203 replayAll();
204 GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
205 conn.rollback();
206
207 // verify the test
208 verifyAll();
209 }
210
211 /**
212 * Test method for {@link net.onrc.onos.graph.GraphDBConnection#close()}.
213 * @throws Exception
214 */
215 @Test
216 public final void testClose() throws Exception {
217 // setup expectations
218 expectDBConnectionAvailable();
219 graph.commit();
220
221 // start the test
222 replayAll();
223 GraphDBConnection conn = GraphDBConnection.getInstance("/path/to/dummy");
224 conn.close();
225
226 // verify the test
227 verifyAll();
228 }
229
230}