2 * Copyright (c) 2015 Inocybe Technologies and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.ovsdb.southbound;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Matchers.eq;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.spy;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 import static org.powermock.reflect.Whitebox.getField;
25 import com.google.common.util.concurrent.ListenableFuture;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.opendaylight.ovsdb.lib.LockAquisitionCallback;
38 import org.opendaylight.ovsdb.lib.LockStolenCallback;
39 import org.opendaylight.ovsdb.lib.MonitorCallBack;
40 import org.opendaylight.ovsdb.lib.MonitorHandle;
41 import org.opendaylight.ovsdb.lib.OvsdbClient;
42 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
43 import org.opendaylight.ovsdb.lib.message.TableUpdates;
44 import org.opendaylight.ovsdb.lib.operations.OperationResult;
45 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
46 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
47 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
48 import org.opendaylight.ovsdb.southbound.ovsdb.transact.BridgeOperationalState;
49 import org.opendaylight.ovsdb.southbound.ovsdb.transact.DataChangeEvent;
50 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactCommand;
51 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactInvoker;
52 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.powermock.reflect.Whitebox;
60 @RunWith(MockitoJUnitRunner.class)
61 public class OvsdbConnectionInstanceTest {
63 @Mock private OvsdbConnectionInstance ovsdbConnectionInstance;
64 @Mock private OvsdbClient client;
65 @Mock private ConnectionInfo connectionInfo;
66 @Mock private TransactionInvoker txInvoker;
67 @Mock private MonitorCallBack callback;
68 @Mock private ConnectionInfo key;
69 @Mock private InstanceIdentifier<Node> instanceIdentifier;
70 private Map<DatabaseSchema,TransactInvoker> transactInvokers;
73 public void setUp() throws Exception {
74 ovsdbConnectionInstance = spy(new OvsdbConnectionInstance(key, client, txInvoker, instanceIdentifier));
78 @SuppressWarnings({ "unchecked", "rawtypes" })
79 public void testTransact() throws Exception {
80 transactInvokers = new HashMap();
82 // init instance variables
83 TransactInvoker transactInvoker1 = mock(TransactInvoker.class);
84 TransactInvoker transactInvoker2 = mock(TransactInvoker.class);
85 transactInvokers.put(mock(DatabaseSchema.class), transactInvoker1);
86 transactInvokers.put(mock(DatabaseSchema.class), transactInvoker2);
87 getField(OvsdbConnectionInstance.class, "transactInvokers").set(ovsdbConnectionInstance , transactInvokers);
89 TransactCommand command = mock(TransactCommand.class);
90 ovsdbConnectionInstance.transact(command, mock(BridgeOperationalState.class), mock(DataChangeEvent.class),
91 mock(InstanceIdentifierCodec.class));
92 verify(transactInvoker1).invoke(any(TransactCommand.class), any(BridgeOperationalState.class),
93 any(DataChangeEvent.class), any(InstanceIdentifierCodec.class));
94 verify(transactInvoker2).invoke(any(TransactCommand.class), any(BridgeOperationalState.class),
95 any(DataChangeEvent.class), any(InstanceIdentifierCodec.class));
99 @SuppressWarnings("unchecked")
100 public void testRegisterCallbacks() throws Exception {
101 InstanceIdentifierCodec instanceIdentifierCodec = mock(InstanceIdentifierCodec.class);
103 // callback not null case
104 getField(OvsdbConnectionInstance.class, "callback").set(ovsdbConnectionInstance , callback);
105 ovsdbConnectionInstance.registerCallbacks(instanceIdentifierCodec);
106 verify(ovsdbConnectionInstance, times(0)).getDatabases();
108 // callback null case
109 getField(OvsdbConnectionInstance.class, "callback").set(ovsdbConnectionInstance , null);
110 List<String> databases = new ArrayList<>();
111 databases.add("Open_vSwitch");
114 ListenableFuture<DatabaseSchema> listenableDbSchema = mock(ListenableFuture.class);
115 DatabaseSchema dbSchema = mock(DatabaseSchema.class);
116 doReturn(listenableDbSchema).when(ovsdbConnectionInstance).getSchema(anyString());
117 doReturn(dbSchema).when(listenableDbSchema).get();
119 doNothing().when(ovsdbConnectionInstance).monitorTables(anyString(), any(DatabaseSchema.class));
120 ovsdbConnectionInstance.registerCallbacks(instanceIdentifierCodec);
121 verify(ovsdbConnectionInstance, times(1)).monitorTables(anyString(), any(DatabaseSchema.class));
125 @SuppressWarnings({ "unchecked", "rawtypes" })
126 public void testCreateTransactInvokers() throws Exception {
127 // transactInvokers not null case
128 transactInvokers = new HashMap();
129 getField(OvsdbConnectionInstance.class, "transactInvokers").set(ovsdbConnectionInstance , transactInvokers);
130 ovsdbConnectionInstance.createTransactInvokers();
131 verify(ovsdbConnectionInstance, times(0)).getSchema(anyString());
133 // transactInvokers null case
134 getField(OvsdbConnectionInstance.class, "transactInvokers").set(ovsdbConnectionInstance , null);
136 ListenableFuture<DatabaseSchema> listenableDbSchema = mock(ListenableFuture.class);
137 DatabaseSchema dbSchema = mock(DatabaseSchema.class);
138 doReturn(listenableDbSchema).when(ovsdbConnectionInstance).getSchema(anyString());
139 when(listenableDbSchema.get()).thenReturn(dbSchema);
141 ovsdbConnectionInstance.createTransactInvokers();
142 verify(ovsdbConnectionInstance).getSchema(anyString());
144 Map<DatabaseSchema, TransactInvoker> testTransactInvokers = Whitebox.getInternalState(ovsdbConnectionInstance,
146 assertEquals("Error, size of the hashmap is incorrect", 1, testTransactInvokers.size());
150 @SuppressWarnings("unchecked")
151 public void testMonitorAllTables() throws Exception {
152 Set<String> tables = new HashSet<>();
153 tables.add("Open_vSwitch");
155 DatabaseSchema dbSchema = mock(DatabaseSchema.class);
156 when(dbSchema.getTables()).thenReturn(tables);
157 GenericTableSchema tableSchema = mock(GenericTableSchema.class);
158 when(dbSchema.table(anyString(), eq(GenericTableSchema.class))).thenReturn(tableSchema);
160 Set<String> columns = new HashSet<>();
161 columns.add("columnName1");
162 columns.add("columnName2");
163 columns.add("_version");
164 columns.add("statistics");
165 when(tableSchema.getColumns()).thenReturn(columns);
167 TableUpdates tableUpdates = mock(TableUpdates.class);
168 doReturn(tableUpdates).when(ovsdbConnectionInstance).monitor(any(DatabaseSchema.class), any(List.class),
169 any(MonitorCallBack.class));
170 getField(OvsdbConnectionInstance.class, "callback").set(ovsdbConnectionInstance, callback);
171 doNothing().when(callback).update(any(TableUpdates.class), any(DatabaseSchema.class));
173 Whitebox.invokeMethod(ovsdbConnectionInstance, "monitorTables", "database", dbSchema);
174 verify(ovsdbConnectionInstance, times(1)).monitorTables(anyString(), any(DatabaseSchema.class));
177 @SuppressWarnings({ "unchecked" })
179 public void testOvsdbConnectionInstance() throws Exception {
180 getField(OvsdbConnectionInstance.class, "client").set(ovsdbConnectionInstance, client);
182 // test getDatabases()
183 ListenableFuture<List<String>> listenableFuture = mock(ListenableFuture.class);
184 when(client.getDatabases()).thenReturn(listenableFuture);
185 assertEquals("Error, did not return correct ListenableFuture<List<String>> object", listenableFuture,
186 ovsdbConnectionInstance.getDatabases());
187 verify(client).getDatabases();
190 ListenableFuture<DatabaseSchema> futureDatabaseSchema = mock(ListenableFuture.class);
191 when(client.getSchema(anyString())).thenReturn(futureDatabaseSchema);
192 assertEquals("Error, did not return correct ListenableFuture<DatabaseSchema> object", futureDatabaseSchema,
193 ovsdbConnectionInstance.getSchema(anyString()));
194 verify(client).getSchema(anyString());
196 // test transactBuilder()
197 TransactionBuilder transactionBuilder = mock(TransactionBuilder.class);
198 doReturn(transactionBuilder).when(client).transactBuilder(any(DatabaseSchema.class));
199 assertEquals("Error, did not return correct TransactionBuilder object", transactionBuilder,
200 ovsdbConnectionInstance.transactBuilder(mock(DatabaseSchema.class)));
201 verify(client).transactBuilder(any(DatabaseSchema.class));
204 ListenableFuture<List<OperationResult>> futureOperationResult = mock(ListenableFuture.class);
205 doReturn(futureOperationResult).when(client).transact(any(DatabaseSchema.class), any(List.class));
206 assertEquals("Error, did not return correct ListenableFuture<List<OperationResult>> object",
207 futureOperationResult, ovsdbConnectionInstance.transact(mock(DatabaseSchema.class), mock(List.class)));
208 verify(client).transact(any(DatabaseSchema.class), any(List.class));
211 TableUpdates tableUpdates = mock(TableUpdates.class);
212 doReturn(tableUpdates).when(client).monitor(any(DatabaseSchema.class), any(List.class),
213 any(MonitorCallBack.class));
214 assertEquals("Error, did not return correct TableUpdates object", tableUpdates, ovsdbConnectionInstance
215 .monitor(mock(DatabaseSchema.class), mock(List.class), mock(MonitorCallBack.class)));
216 verify(client).monitor(any(DatabaseSchema.class), any(List.class), any(MonitorCallBack.class));
218 // test cancelMonitor()
219 doNothing().when(client).cancelMonitor(any(MonitorHandle.class));
220 MonitorHandle monitorHandle = mock(MonitorHandle.class);
221 ovsdbConnectionInstance.cancelMonitor(monitorHandle);
222 verify(client).cancelMonitor(any(MonitorHandle.class));
225 doNothing().when(client).lock(anyString(), any(LockAquisitionCallback.class), any(LockStolenCallback.class));
226 LockAquisitionCallback lockAquisitionCallback = mock(LockAquisitionCallback.class);
227 LockStolenCallback lockStolenCallback = mock(LockStolenCallback.class);
228 ovsdbConnectionInstance.lock("lockId", lockAquisitionCallback, lockStolenCallback);
229 verify(client).lock(anyString(), any(LockAquisitionCallback.class), any(LockStolenCallback.class));
232 ListenableFuture<Boolean> futureBoolean = mock(ListenableFuture.class);
233 when(client.steal(anyString())).thenReturn(futureBoolean);
234 assertEquals("Error, did not return correct ListenableFuture<Boolean> object", futureBoolean,
235 ovsdbConnectionInstance.steal(anyString()));
236 verify(client).steal(anyString());
239 when(client.unLock(anyString())).thenReturn(futureBoolean);
240 assertEquals("Error, did not return correct ListenableFuture<Boolean> object", futureBoolean,
241 ovsdbConnectionInstance.unLock(anyString()));
242 verify(client).unLock(anyString());
245 when(client.isActive()).thenReturn(true);
246 assertEquals("Error, does not match isActive()", true, ovsdbConnectionInstance.isActive());
247 verify(client).isActive();
250 doNothing().when(client).disconnect();
251 ovsdbConnectionInstance.disconnect();
252 verify(client).disconnect();
254 // test getDatabaseSchema()
255 DatabaseSchema databaseSchema = mock(DatabaseSchema.class);
256 when(client.getDatabaseSchema(anyString())).thenReturn(databaseSchema);
257 assertEquals("Error, did not return correct DatabaseSchema object", databaseSchema,
258 ovsdbConnectionInstance.getDatabaseSchema(anyString()));
259 verify(client).getDatabaseSchema(anyString());
261 // test getConnectionInfo()
262 OvsdbConnectionInfo ovsdbConnectionInfo = mock(OvsdbConnectionInfo.class);
263 when(client.getConnectionInfo()).thenReturn(ovsdbConnectionInfo);
264 assertEquals("Error, did not return correct OvsdbConnectionInfo object", ovsdbConnectionInfo,
265 ovsdbConnectionInstance.getConnectionInfo());
266 verify(client).getConnectionInfo();
268 // test getMDConnectionInfo()
269 assertEquals("Error, incorrect connectionInfo", key, ovsdbConnectionInstance.getMDConnectionInfo());
271 // test setMDConnectionInfo()
272 ovsdbConnectionInstance.setMDConnectionInfo(key);
273 assertEquals("Error, incorrect ConnectionInfo", key,
274 Whitebox.getInternalState(ovsdbConnectionInstance, "connectionInfo"));
276 // test getInstanceIdentifier()
277 assertEquals("Error, incorrect instanceIdentifier", instanceIdentifier,
278 ovsdbConnectionInstance.getInstanceIdentifier());
281 NodeId nodeId = mock(NodeId.class);
282 NodeKey nodeKey = new NodeKey(nodeId);
283 doReturn(nodeKey).when(ovsdbConnectionInstance).getNodeKey();
284 when(ovsdbConnectionInstance.getNodeKey()).thenReturn(nodeKey);
285 assertEquals("Error, incorrect NodeId object", nodeId, ovsdbConnectionInstance.getNodeId());
287 // test setInstanceIdentifier()
288 ovsdbConnectionInstance.setInstanceIdentifier(instanceIdentifier);
289 assertEquals("Error, incorrect instanceIdentifier", instanceIdentifier,
290 Whitebox.getInternalState(ovsdbConnectionInstance, "instanceIdentifier"));
293 assertNull(ovsdbConnectionInstance.monitor(any(DatabaseSchema.class), any(List.class), any(MonitorHandle.class),
294 any(MonitorCallBack.class)));