Merge "Add JUnit testing for ArpResponderService class."
[ovsdb.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / AbstractServiceInstanceTest.java
1 /*
2  * Copyright (c) 2015 Inocybe and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Matchers.same;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
29
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
37 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
38 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
39 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
40 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
41 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
42 import org.opendaylight.ovsdb.lib.notation.Column;
43 import org.opendaylight.ovsdb.lib.notation.Row;
44 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
45 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
46 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
47 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
48 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
49 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
58 import org.opendaylight.yangtools.yang.binding.DataObject;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61
62 import com.google.common.base.Optional;
63 import com.google.common.util.concurrent.CheckedFuture;
64
65 /**
66  * Unit test for {@link AbstractServiceInstance}
67  */
68 @RunWith(MockitoJUnitRunner.class)
69 public class AbstractServiceInstanceTest {
70
71     @InjectMocks AbstractServiceInstance abstractServiceInstance = mock(AbstractServiceInstance.class, Mockito.CALLS_REAL_METHODS);
72
73     @Mock private OvsdbConfigurationService ovsdbConfigService;
74     @Mock private OvsdbConnectionService connectionService;
75     @Mock private PipelineOrchestrator orchestrator;
76     @Mock private MdsalConsumer mdsalConsumer;
77
78
79     private Service service = Service.L3_FORWARDING;
80
81     private final String ID = "5710881121";
82     private final String NODE_ID = Constants.INTEGRATION_BRIDGE + ":" +  ID;
83     private final String DPID = "154652161";
84
85     /**
86      * Test method {@link AbstractServiceInstance#isBridgeInPipeline(String)}
87      */
88     @Test
89     public void testIsBridgeInPipeline() {
90         Node node = mock(Node.class);
91         when(node.getId()).thenReturn(mock(NodeId.class));
92
93         List<Node> nodes = new ArrayList();
94         nodes.add(node);
95         when(connectionService.getNodes()).thenReturn(nodes);
96
97         ConcurrentMap<String, Row> bridges = new ConcurrentHashMap();
98         bridges.put("key", mock(Row.class));
99         when(ovsdbConfigService.getRows(any(Node.class), anyString())).thenReturn(bridges);
100
101         Bridge bridge = mock(Bridge.class);
102         Column<GenericTableSchema, Set<String>> datapathIdColumn = mock(Column.class);
103         when(bridge.getDatapathIdColumn()).thenReturn(datapathIdColumn);
104         when(bridge.getName()).thenReturn(Constants.INTEGRATION_BRIDGE);
105         Set<String> dpids = new HashSet();
106         dpids.add(DPID);
107         when(datapathIdColumn.getData()).thenReturn(dpids);
108         when(ovsdbConfigService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
109
110         assertTrue("Error, isBridgeInPipeline() did not return the correct value", abstractServiceInstance.isBridgeInPipeline(NODE_ID));
111     }
112
113     /**
114      * Test method {@link AbstractServiceInstance#getTable()}
115      */
116     @Test
117     public void testGetTable() {
118         abstractServiceInstance.setService(service);
119         assertEquals("Error, getTable() did not return the correct value", 70, abstractServiceInstance.getTable());
120     }
121
122     /**
123      * Test method {@link AbstractServiceInstance#createNodeBuilder(String)}
124      */
125     @Test
126     public void testCreateNodeBuilder() {
127         NodeId id = new NodeId(NODE_ID);
128
129         NodeBuilder nodeBuilder = abstractServiceInstance.createNodeBuilder(NODE_ID);
130         assertNotNull("Error, createNodeBuilder() did not return the correct value", nodeBuilder);
131         assertEquals("Error, createNodeBuilder() did not return the correct ID", id, nodeBuilder.getId());
132         assertEquals("Error, createNodeBuilder() did not return the correct Key", new NodeKey(id), nodeBuilder.getKey());
133     }
134
135     /**
136      * Test method {@link AbstractServiceInstance#getMutablePipelineInstructionBuilder()}
137      */
138     @Test
139     public void testGetMutablePipelineInstructionBuilder() {
140         // service == null
141         assertNotNull("Error, getMutablePipelineInstructionBuilder() did not return the correct value", abstractServiceInstance.getMutablePipelineInstructionBuilder());
142         assertTrue("Error, getMutablePipelineInstructionBuilder() did not return a InstructionBuilder object", abstractServiceInstance.getMutablePipelineInstructionBuilder() instanceof InstructionBuilder);
143
144         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
145
146         // service defined
147         assertNotNull("Error, getMutablePipelineInstructionBuilder() did not return the correct value", abstractServiceInstance.getMutablePipelineInstructionBuilder());
148         assertTrue("Error, getMutablePipelineInstructionBuilder() did not return a InstructionBuilder object", abstractServiceInstance.getMutablePipelineInstructionBuilder() instanceof InstructionBuilder);
149     }
150
151     /**
152      * Test method {@link AbstractServiceInstance#writeFlow(FlowBuilder, NodeBuilder)}
153      */
154     @Test
155     public void testWriteFlow() throws Exception {
156         DataBroker dataBrocker = mock(DataBroker.class);
157         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
158         when(dataBrocker.newReadWriteTransaction()).thenReturn(transaction);
159         when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
160         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = mock(CheckedFuture.class);
161         when(transaction.submit()).thenReturn(commitFuture);
162
163         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
164         when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
165
166         FlowBuilder flowBuilder = mock(FlowBuilder.class);
167         when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
168
169         abstractServiceInstance.writeFlow(flowBuilder, nodeBuilder);
170
171         verify(transaction, times(2)).put(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class), any(DataObject.class), eq(true));
172         verify(commitFuture, times(1)).get();
173     }
174
175     /**
176      * Test method {@link AbstractServiceInstance#removeFlow(FlowBuilder, NodeBuilder)}
177      */
178     @Test
179     public void testRemoveFlow() throws Exception {
180         DataBroker dataBrocker = mock(DataBroker.class);
181         WriteTransaction transaction = mock(WriteTransaction.class);
182         when(dataBrocker.newWriteOnlyTransaction()).thenReturn(transaction);
183         when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
184         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = mock(CheckedFuture.class);
185         when(transaction.submit()).thenReturn(commitFuture);
186
187         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
188         when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
189
190         FlowBuilder flowBuilder = mock(FlowBuilder.class);
191         when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
192
193         abstractServiceInstance.removeFlow(flowBuilder, nodeBuilder);
194         verify(transaction, times(1)).delete(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class));
195         verify(commitFuture, times(1)).get();
196     }
197
198     /**
199      * Test method {@link AbstractServiceInstance#getFlow(FlowBuilder, NodeBuilder)}
200      */
201     @Test
202     public void testGetFlow() throws Exception {
203         DataBroker dataBrocker = mock(DataBroker.class);
204         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
205         when(dataBrocker.newReadOnlyTransaction()).thenReturn(transaction);
206         when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
207
208         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
209         when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
210
211         FlowBuilder flowBuilder = mock(FlowBuilder.class);
212         when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
213
214         CheckedFuture dataRead = mock(CheckedFuture.class);
215         when(transaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(dataRead);
216         Optional<Flow> data = mock(Optional.class);
217         when(dataRead.get()).thenReturn(data);
218
219         abstractServiceInstance.getFlow(flowBuilder, nodeBuilder);
220         verify(transaction, times(1)).read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class));
221     }
222
223     /**
224      * Test method {@link AbstractServiceInstance#programDefaultPipelineRule(String)}
225      */
226     @Test
227     public void testProgramDefaultPipelineRule() {
228         Node node = mock(Node.class);
229         when(node.getId()).thenReturn(mock(NodeId.class));
230
231         List<Node> nodes = new ArrayList();
232         nodes.add(node);
233         when(connectionService.getNodes()).thenReturn(nodes);
234
235         ConcurrentMap<String, Row> bridges = new ConcurrentHashMap();
236         bridges.put("key", mock(Row.class));
237         when(ovsdbConfigService.getRows(any(Node.class), anyString())).thenReturn(bridges);
238
239         Bridge bridge = mock(Bridge.class);
240         Column<GenericTableSchema, Set<String>> datapathIdColumn = mock(Column.class);
241         when(bridge.getDatapathIdColumn()).thenReturn(datapathIdColumn);
242         when(bridge.getName()).thenReturn(Constants.INTEGRATION_BRIDGE);
243         Set<String> dpids = new HashSet();
244         dpids.add(DPID);
245         when(datapathIdColumn.getData()).thenReturn(dpids);
246         when(ovsdbConfigService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
247
248         abstractServiceInstance.setService(service);
249
250         abstractServiceInstance.programDefaultPipelineRule(NODE_ID);
251
252         verify(abstractServiceInstance, times(1)).isBridgeInPipeline(NODE_ID);
253         verify(abstractServiceInstance, times(1)).writeFlow(any(FlowBuilder.class), any(NodeBuilder.class));
254     }
255 }