manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / impl / UnimgrProviderTest.java
1 /*
2  * Copyright (c) 2016 CableLabs 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 package org.opendaylight.unimgr.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Matchers.isNull;
14 import static org.mockito.Mockito.atLeast;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
21 import java.util.Dictionary;
22 import java.util.List;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
33 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
34 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
35 import org.opendaylight.unimgr.utils.MdsalUtils;
36 import org.opendaylight.unimgr.utils.OvsdbUtils;
37 import org.opendaylight.unimgr.utils.UniUtils;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.osgi.framework.Bundle;
54 import org.osgi.framework.BundleContext;
55 import org.osgi.framework.FrameworkUtil;
56 import org.osgi.framework.ServiceRegistration;
57 import org.powermock.api.mockito.PowerMockito;
58 import org.powermock.api.support.membermodification.MemberMatcher;
59 import org.powermock.api.support.membermodification.MemberModifier;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
62 import org.powermock.reflect.Whitebox;
63
64 import com.google.common.base.Optional;
65 import com.google.common.util.concurrent.CheckedFuture;
66
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({UnimgrProvider.class,
69                  InstanceIdentifier.class,
70                  LogicalDatastoreType.class,
71                  FrameworkUtil.class,
72                  UniUtils.class,
73                  MdsalUtils.class,
74                  OvsdbUtils.class,
75                  UnimgrMapper.class})
76 public class UnimgrProviderTest {
77
78     @Mock private IUnimgrConsoleProvider console;
79     @Mock private UniDataTreeChangeListener uniListener;
80     @Mock private EvcDataTreeChangeListener evcListener;
81     @Mock private OvsNodeDataTreeChangeListener ovsListener;
82     @Mock private DataBroker dataBroker;
83     @Mock private ServiceRegistration<IUnimgrConsoleProvider> mockUnimgrConsoleRegistration;
84     @Mock private UnimgrProvider unimgrProvider;
85
86     @Before
87     public void setUp() throws Exception {
88         unimgrProvider = PowerMockito.mock(UnimgrProvider.class, Mockito.CALLS_REAL_METHODS);
89         MemberModifier.field(UnimgrProvider.class, "dataBroker").set(unimgrProvider, dataBroker);
90         MemberModifier.field(UnimgrProvider.class, "uniListener").set(unimgrProvider, uniListener);
91         MemberModifier.field(UnimgrProvider.class, "evcListener").set(unimgrProvider, evcListener);
92         MemberModifier.field(UnimgrProvider.class, "ovsListener").set(unimgrProvider, ovsListener);
93         console = unimgrProvider;
94     }
95
96     @Test
97     public void testAddEvc() throws Exception {
98         assertEquals(false, unimgrProvider.addEvc(any(EvcAugmentation.class)));
99     }
100
101     @Test
102     public void testAddUni() throws Exception {
103         PowerMockito.mockStatic(UniUtils.class);
104         assertEquals(unimgrProvider.addUni(null), false);
105         final UniAugmentation mockUniAug = mock(UniAugmentation.class);
106         // false case
107         when(mockUniAug.getIpAddress()).thenReturn(null);
108         assertEquals(unimgrProvider.addUni(mockUniAug), false);
109         when(mockUniAug.getMacAddress()).thenReturn(null);
110         assertEquals(unimgrProvider.addUni(mockUniAug), false);
111         // true case
112         when(mockUniAug.getIpAddress()).thenReturn(mock(IpAddress.class));
113         when(mockUniAug.getMacAddress()).thenReturn(mock(MacAddress.class));
114         final UniAugmentationBuilder uniAugBuilder = new UniAugmentationBuilder()
115                                                     .setIpAddress(mock(IpAddress.class))
116                                                     .setMacAddress(mock(MacAddress.class));
117         when(UniUtils.createUniNode(any(DataBroker.class),
118                                        any(UniAugmentation.class)))
119                         .thenReturn(true);
120         assertEquals(true, unimgrProvider.addUni(uniAugBuilder.build()));
121     }
122
123     @Test
124     public void testGetEvc() throws Exception {
125         assertEquals(null, unimgrProvider.getEvc(any(String.class)));
126     }
127
128     @Test
129     public void testgetUni() throws Exception {
130         PowerMockito.mockStatic(UniUtils.class);
131         final UniAugmentation mockUniAug = mock(UniAugmentation.class);
132         when(UniUtils.getUni(any(DataBroker.class),
133                                 any(LogicalDatastoreType.class),
134                                 any(IpAddress.class)))
135                         .thenReturn(mockUniAug);
136         final IpAddress mockIpAddress = mock(IpAddress.class);
137         assertEquals(mockUniAug, unimgrProvider.getUni(mockIpAddress));
138     }
139
140     @SuppressWarnings("unchecked")
141     @Test
142     public void testInitDatastore() throws Exception {
143         final ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
144         when(dataBroker.newReadWriteTransaction()).thenReturn(transaction);
145
146         //suppress calls to initializeTopology()
147         MemberModifier.suppress(MemberMatcher.method(UnimgrProvider.class,
148                                                      "initializeTopology",
149                                                      LogicalDatastoreType.class));
150
151         final CheckedFuture<Optional<Topology>, ReadFailedException> unimgrTp = mock(CheckedFuture.class);
152         when(transaction.read(any(LogicalDatastoreType.class),
153                               any(InstanceIdentifier.class))).thenReturn(unimgrTp);
154
155         //true case
156         final Optional<Topology> optTopo = mock(Optional.class);
157         when(unimgrTp.get()).thenReturn(optTopo);
158         when(optTopo.isPresent()).thenReturn(false);
159         final TopologyBuilder tpb = mock(TopologyBuilder.class);
160         PowerMockito.whenNew(TopologyBuilder.class).withNoArguments().thenReturn(tpb);
161         when(tpb.setTopologyId(any(TopologyId.class))).thenReturn(tpb);
162         final Topology data = mock(Topology.class);
163         when(tpb.build()).thenReturn(data);
164         doNothing().when(transaction).put(any(LogicalDatastoreType.class),
165                                           any(InstanceIdentifier.class),
166                                           any(Topology.class));
167         when(transaction.submit()).thenReturn(mock(CheckedFuture.class));
168
169         final LogicalDatastoreType type = PowerMockito.mock(LogicalDatastoreType.class);
170         final TopologyId mockTopoId = mock(TopologyId.class);
171         Whitebox.invokeMethod(unimgrProvider, "initDatastore", type, mockTopoId);
172         PowerMockito.verifyPrivate(unimgrProvider).invoke("initializeTopology", type);
173         verify(tpb).setTopologyId(any(TopologyId.class));
174         verify(tpb).build();
175         verify(transaction).put(any(LogicalDatastoreType.class),
176                                 any(InstanceIdentifier.class),
177                                 any(NetworkTopology.class));
178         verify(transaction).submit();
179
180         //false case
181         when(optTopo.isPresent()).thenReturn(false);
182         when(transaction.cancel()).thenReturn(true);
183         Whitebox.invokeMethod(unimgrProvider, "initDatastore", type, mockTopoId);
184         PowerMockito.verifyPrivate(unimgrProvider, times(2)).invoke("initializeTopology",
185                                                                     any(LogicalDatastoreType.class));
186     }
187
188     @SuppressWarnings("unchecked")
189     @Test
190     public void testInitializeTopology() throws Exception {
191         final InstanceIdentifier<NetworkTopology> path = mock(InstanceIdentifier.class);
192         PowerMockito.mockStatic(InstanceIdentifier.class);
193         when(InstanceIdentifier.create(NetworkTopology.class)).thenReturn(path);
194
195         final CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = mock(CheckedFuture.class);
196         final ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
197         when(dataBroker.newReadWriteTransaction()).thenReturn(transaction);
198         when(transaction.read(any(LogicalDatastoreType.class),
199                               any(InstanceIdentifier.class))).thenReturn(topology);
200
201         final Optional<NetworkTopology> optNetTopo = mock(Optional.class);
202         when(topology.get()).thenReturn(optNetTopo);
203         when(optNetTopo.isPresent()).thenReturn(false);
204         final NetworkTopologyBuilder ntb = mock(NetworkTopologyBuilder.class);
205         PowerMockito.whenNew(NetworkTopologyBuilder.class).withNoArguments().thenReturn(ntb);
206         final NetworkTopology networkTopology = mock(NetworkTopology.class);
207         when(ntb.build()).thenReturn(networkTopology);
208         doNothing().when(transaction).put(any(LogicalDatastoreType.class),
209                                           any(InstanceIdentifier.class),
210                                           any(NetworkTopology.class));
211         when(transaction.submit()).thenReturn(mock(CheckedFuture.class));
212
213         final LogicalDatastoreType type = PowerMockito.mock(LogicalDatastoreType.class);
214         Whitebox.invokeMethod(unimgrProvider, "initializeTopology", type);
215         verify(ntb).build();
216         verify(transaction).put(any(LogicalDatastoreType.class),
217                                 any(InstanceIdentifier.class),
218                                 any(NetworkTopology.class));
219         verify(transaction).submit();
220     }
221
222     @SuppressWarnings("unchecked")
223     @Test
224     public void testlistUnis() throws Exception {
225         PowerMockito.mockStatic(UniUtils.class);
226         final List<UniAugmentation> mockUniList = mock(List.class);
227         when(UniUtils.getUnis(any(DataBroker.class),
228                                  any(LogicalDatastoreType.class)))
229                         .thenReturn(mockUniList);
230         assertEquals(mockUniList,
231                      unimgrProvider.listUnis(any(LogicalDatastoreType.class)));
232     }
233
234     @SuppressWarnings("unchecked")
235     @Test
236     public void testOnSessionInitiated() throws Exception {
237         final ProviderContext session = mock(ProviderContext.class);
238         when(session.getSALService(DataBroker.class)).thenReturn(dataBroker);
239         final BundleContext context = mock(BundleContext.class);
240         PowerMockito.mockStatic(FrameworkUtil.class);
241         final Bundle bundle = mock(Bundle.class);
242         when(bundle.getBundleContext()).thenReturn(context);
243         PowerMockito.when(FrameworkUtil.getBundle(unimgrProvider.getClass())).thenReturn(bundle);
244         mockUnimgrConsoleRegistration = mock(ServiceRegistration.class);
245         when(context.registerService(eq(IUnimgrConsoleProvider.class),
246                                      any(IUnimgrConsoleProvider.class),
247                                      isNull(Dictionary.class))).thenReturn(mockUnimgrConsoleRegistration);
248         PowerMockito.whenNew(UniDataTreeChangeListener.class).withArguments(any(DataBroker.class)).thenReturn(uniListener);
249         PowerMockito.whenNew(EvcDataTreeChangeListener.class).withArguments(any(DataBroker.class)).thenReturn(evcListener);
250         PowerMockito.whenNew(OvsNodeDataTreeChangeListener.class).withArguments(any(DataBroker.class)).thenReturn(ovsListener);
251         MemberModifier.suppress(MemberMatcher.method(UnimgrProvider.class, "initDatastore"));
252         unimgrProvider.onSessionInitiated(session);
253         verify(unimgrProvider, atLeast(4)).initDatastore(any(LogicalDatastoreType.class), any(TopologyId.class));
254     }
255
256     @Test
257     public void testRemoveEvc() throws Exception {
258         assertEquals(false, unimgrProvider.removeEvc(any(String.class)));
259     }
260
261     @SuppressWarnings("unchecked")
262     @Test
263     public void testRemoveUni() throws Exception {
264         PowerMockito.mockStatic(MdsalUtils.class);
265         PowerMockito.mockStatic(UnimgrMapper.class);
266         PowerMockito.mockStatic(InstanceIdentifier.class);
267
268         // false case
269         final IpAddress mockIpAddress = mock(IpAddress.class);
270         PowerMockito.when(UnimgrMapper.getUniIid(any(DataBroker.class),
271                                                  any(IpAddress.class),
272                                                  any(LogicalDatastoreType.class)))
273                          .thenReturn(null);
274         assertEquals(false, unimgrProvider.removeUni(mockIpAddress));
275
276         // true case
277         final InstanceIdentifier<Node> iid = mock(InstanceIdentifier.class);
278         final IpAddress ipAddress = new IpAddress(new Ipv4Address("192.168.1.1"));
279         PowerMockito.when(UnimgrMapper.getUniIid(any(DataBroker.class),
280                                                  any(IpAddress.class),
281                                                  any(LogicalDatastoreType.class)))
282                                       .thenReturn(iid);
283         PowerMockito.when(MdsalUtils.deleteNode(any(DataBroker.class),
284                                                  any(InstanceIdentifier.class),
285                                                  any(LogicalDatastoreType.class)))
286                                      .thenReturn(true);
287         assertEquals(true, unimgrProvider.removeUni(ipAddress));
288     }
289
290     @Test
291     public void testUpdateEvc() throws Exception {
292         final UniSource uniSource = mock(UniSource.class);
293         final UniDest uniDest = mock(UniDest.class);
294         when(uniSource.getUni()).thenReturn(null);
295         when(uniDest.getUni()).thenReturn(null);
296         //TODO
297     }
298
299     @Test
300     public void testUpdateUni() throws Exception {
301         assertEquals(false, unimgrProvider.updateUni(null));
302     }
303
304 }