Refactoring of cisco-xr-driver and impl modules.
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / utils / MdsalUtilsTest.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
9 package org.opendaylight.unimgr.utils;
10
11 import static org.junit.Assert.*;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.argThat;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import org.junit.Before;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.ExpectedException;
23 import org.junit.runner.RunWith;
24 import org.mockito.ArgumentMatcher;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 import org.opendaylight.unimgr.impl.UnimgrConstants;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.g_forwardingconstruct.FcPort;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.*;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import com.google.common.base.Optional;
51 import com.google.common.util.concurrent.CheckedFuture;
52
53 import ch.qos.logback.classic.spi.LoggingEvent;
54 import ch.qos.logback.core.Appender;
55
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest({LogicalDatastoreType.class, EvcUtils.class})
58 public class MdsalUtilsTest {
59
60     @Rule
61     public final ExpectedException exception = ExpectedException.none();
62     @Mock private DataBroker dataBroker;
63     @Mock private Node bridgeNode;
64     @Mock private String bridgeName;
65     @Mock private String portName;
66     @Mock private String type;
67     @Mock private WriteTransaction transaction;
68     @Mock private IpAddress mockIp;
69     @SuppressWarnings("rawtypes")
70     @Mock private Appender mockAppender;
71     @SuppressWarnings({ "rawtypes" })
72     @Mock private CheckedFuture checkedFuture;
73     private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8");
74     private ch.qos.logback.classic.Logger root;
75
76     @SuppressWarnings("unchecked")
77     @Before
78     public void setUp() throws Exception {
79         PowerMockito.mockStatic(MdsalUtils.class, Mockito.CALLS_REAL_METHODS);
80         PowerMockito.mockStatic(LogicalDatastoreType.class);
81         root = (ch.qos.logback.classic.Logger)
82                 LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
83         // Check logger messages
84         when(mockAppender.getName()).thenReturn("MOCK");
85         root.addAppender(mockAppender);
86     }
87
88     @SuppressWarnings({ "unchecked", "rawtypes" })
89     @Test
90     public void testDeleteNode() throws Exception {
91         InstanceIdentifier<Node> genericNode = InstanceIdentifier
92                                                    .create(NetworkTopology.class)
93                                                    .child(Topology.class,
94                                                            new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
95                                                    .child(Node.class);
96         when(dataBroker.newWriteOnlyTransaction()).thenReturn(transaction);
97         doNothing().when(transaction).delete(any(LogicalDatastoreType.class),
98                                              any(InstanceIdentifier.class));
99         when(transaction.submit()).thenReturn(checkedFuture);
100         assertEquals(true, MdsalUtils.deleteNode(dataBroker, genericNode, LogicalDatastoreType.CONFIGURATION));
101         verify(transaction).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
102         verify(transaction).submit();
103         verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {
104             @Override
105             public boolean matches(final Object argument) {
106               return ((LoggingEvent)argument).getFormattedMessage().contains("Received a request to delete node");
107             }
108           }));
109     }
110
111     @SuppressWarnings("unchecked")
112     @Test
113     public void testRead() throws ReadFailedException {
114         DataBroker dataBroker = mock(DataBroker.class);
115         InstanceIdentifier<Node> nodeIid = PowerMockito.mock(InstanceIdentifier.class);
116         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
117         when(dataBroker.newReadOnlyTransaction()).thenReturn(transaction);
118         Optional<Node> optionalDataObject = mock(Optional.class);
119         CheckedFuture<Optional<Node>, ReadFailedException> future = mock(CheckedFuture.class);
120         Node nd = mock(Node.class);
121         when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(future);
122         when(future.checkedGet()).thenReturn(optionalDataObject);
123         when(optionalDataObject.isPresent()).thenReturn(true);
124         when(optionalDataObject.get()).thenReturn(nd);
125         Node expectedNode = MdsalUtils.read(dataBroker, LogicalDatastoreType.CONFIGURATION, nodeIid);
126         verify(transaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
127         verify(transaction).close();
128         assertNotNull(expectedNode);
129         assertEquals(expectedNode, nd);
130     }
131
132     @SuppressWarnings("unchecked")
133     @Test
134     public void testReadLink() throws ReadFailedException {
135         LinkId linkId = new LinkId("evc://7011db35-f44b-4aab-90f6-d89088caf9d8");
136         InstanceIdentifier<?> nodeIid = InstanceIdentifier
137                 .create(NetworkTopology.class)
138                 .child(Topology.class,
139                         new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
140                 .child(Link.class,
141                         new LinkKey(linkId));
142         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
143         when(dataBroker.newReadOnlyTransaction()).thenReturn(transaction);
144         CheckedFuture<Optional<Link>, ReadFailedException> linkFuture = mock(CheckedFuture.class);
145         Optional<Link> optLink = mock(Optional.class);
146         when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(linkFuture);
147         when(linkFuture.checkedGet()).thenReturn(optLink);
148         Optional<Link> expectedOpt = MdsalUtils.readLink(dataBroker, LogicalDatastoreType.CONFIGURATION, nodeIid);
149         verify(transaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
150         assertNotNull(expectedOpt);
151         assertEquals(expectedOpt, optLink);
152     }
153
154     @SuppressWarnings("unchecked")
155     @Test
156     public void testReadNode() throws ReadFailedException {
157         InstanceIdentifier<?> nodeIid = InstanceIdentifier
158                                             .create(NetworkTopology.class)
159                                             .child(Topology.class,
160                                                     new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
161                                             .child(Node.class,
162                                                     new NodeKey(OVSDB_NODE_ID));
163         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
164         when(dataBroker.newReadOnlyTransaction()).thenReturn(transaction);
165         CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = mock(CheckedFuture.class);
166         Optional<Node> optNode = mock(Optional.class);
167         when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(nodeFuture);
168         when(nodeFuture.checkedGet()).thenReturn(optNode);
169         Optional<Node> expectedOpt = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.CONFIGURATION, nodeIid);
170         verify(transaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
171         assertNotNull(expectedOpt);
172         assertEquals(expectedOpt, optNode);
173     }
174
175     @Test
176     public void testReadOptionalPositive() throws ReadFailedException {
177         //given
178         DataBroker dataBroker = mock(DataBroker.class);
179         InstanceIdentifier<Node> nodeIid = PowerMockito.mock(InstanceIdentifier.class);
180         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
181         Optional<Node> optionalDataObject = mock(Optional.class);
182         CheckedFuture<Optional<Node>, ReadFailedException> future = mock(CheckedFuture.class);
183         Node exceptedNode = mock(Node.class);
184
185         when(dataBroker.newReadOnlyTransaction()).thenReturn(transaction);
186         when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(future);
187         when(future.checkedGet()).thenReturn(optionalDataObject);
188         when(optionalDataObject.isPresent()).thenReturn(true);
189         when(optionalDataObject.get()).thenReturn(exceptedNode);
190
191         //when
192         Optional<Node> actualNodeOptional = MdsalUtils.readOptional(dataBroker, LogicalDatastoreType.CONFIGURATION, nodeIid);
193
194         //then
195         assertNotNull(actualNodeOptional);
196         assertTrue(actualNodeOptional.isPresent());
197
198         verify(transaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
199         verify(transaction).close();
200
201         assertEquals(actualNodeOptional.get(), exceptedNode);
202     }
203
204     @Test
205     public void testReadOptionalNegative() throws ReadFailedException {
206         //given
207         DataBroker dataBroker = mock(DataBroker.class);
208         InstanceIdentifier<Node> nodeIid = PowerMockito.mock(InstanceIdentifier.class);
209         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
210         Optional<Node> optionalDataObject = mock(Optional.class);
211         CheckedFuture<Optional<Node>, ReadFailedException> future = mock(CheckedFuture.class);
212
213         when(dataBroker.newReadOnlyTransaction()).thenReturn(transaction);
214         when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(future);
215         when(future.checkedGet()).thenReturn(optionalDataObject);
216         when(optionalDataObject.isPresent()).thenReturn(false);
217
218         //when
219         Optional<Node> actualNodeOptional = MdsalUtils.readOptional(dataBroker, LogicalDatastoreType.CONFIGURATION, nodeIid);
220
221         //then
222         assertNotNull(actualNodeOptional);
223         assertFalse(actualNodeOptional.isPresent());
224
225         verify(transaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
226         verify(transaction).close();
227     }
228
229     @Test
230     public void testReadTerminationPoint() throws ReadFailedException {
231         //given
232         TerminationPoint expectedTp = mock(TerminationPoint.class);
233         FcPort fcPort = mock(FcPort.class);
234         when(fcPort.getNode()).thenReturn(new NodeId("r1"));
235         when(fcPort.getTopology()).thenReturn(new TopologyId("topology-netconf"));
236         when(fcPort.getTp()).thenReturn(new TpId("tp1"));
237
238         DataBroker dataBroker = mock(DataBroker.class);
239         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
240         Optional<TerminationPoint> optionalDataObject = mock(Optional.class);
241         CheckedFuture<Optional<TerminationPoint>, ReadFailedException> future = mock(CheckedFuture.class);
242
243         when(dataBroker.newReadOnlyTransaction()).thenReturn(transaction);
244         when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(future);
245         when(future.checkedGet()).thenReturn(optionalDataObject);
246         when(optionalDataObject.isPresent()).thenReturn(true);
247         when(optionalDataObject.get()).thenReturn(expectedTp);
248
249         //when
250         Optional<TerminationPoint> actualTpOptional = MdsalUtils.readTerminationPoint(dataBroker, LogicalDatastoreType.CONFIGURATION, fcPort);
251
252         //then
253         assertNotNull(actualTpOptional);
254         assertTrue(actualTpOptional.isPresent());
255         assertEquals(expectedTp, actualTpOptional.get());
256
257         verify(transaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
258         verify(transaction).close();
259     }
260 }