2 * Copyright (c) 2016 Cisco Systems, Inc. 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
8 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.doReturn;
13 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
15 import com.google.common.collect.ImmutableList;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
27 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
28 import org.opendaylight.restconf.nb.rfc8040.ContentParam;
29 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
30 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
31 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
42 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class ReadDataTransactionUtilTest {
46 private static final TestData DATA = new TestData();
47 private static final NodeIdentifier NODE_IDENTIFIER =
48 new NodeIdentifier(QName.create("ns", "2016-02-28", "container"));
50 private RestconfStrategy mdsalStrategy;
51 private RestconfStrategy netconfStrategy;
53 private NetconfDataTreeService netconfService;
55 private DOMDataTreeReadTransaction read;
57 private EffectiveModelContext schemaContext;
59 private DOMDataBroker mockDataBroker;
63 // FIXME: these tests need to be parameterized somehow. The trouble is we need mocking before we invoke
64 // the strategy. This needs some more thought.
65 doReturn(read).when(mockDataBroker).newReadOnlyTransaction();
66 mdsalStrategy = new MdsalRestconfStrategy(mockDataBroker);
67 netconfStrategy = new NetconfRestconfStrategy(netconfService);
71 public void readDataConfigTest() {
72 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
73 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
74 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
75 NormalizedNode normalizedNode = readData(ContentParam.CONFIG, DATA.path, mdsalStrategy);
76 assertEquals(DATA.data3, normalizedNode);
78 normalizedNode = readData(ContentParam.CONFIG, DATA.path, netconfStrategy);
79 assertEquals(DATA.data3, normalizedNode);
83 public void readAllHavingOnlyConfigTest() {
84 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
85 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
86 doReturn(immediateFluentFuture(Optional.empty())).when(read)
87 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
88 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
89 doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).get(DATA.path);
90 NormalizedNode normalizedNode = readData(ContentParam.ALL, DATA.path, mdsalStrategy);
91 assertEquals(DATA.data3, normalizedNode);
93 normalizedNode = readData(ContentParam.ALL, DATA.path, netconfStrategy);
94 assertEquals(DATA.data3, normalizedNode);
98 public void readAllHavingOnlyNonConfigTest() {
99 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
100 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
101 doReturn(immediateFluentFuture(Optional.empty())).when(read)
102 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
103 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(netconfService).get(DATA.path2);
104 doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).getConfig(DATA.path2);
105 NormalizedNode normalizedNode = readData(ContentParam.ALL, DATA.path2, mdsalStrategy);
106 assertEquals(DATA.data2, normalizedNode);
108 normalizedNode = readData(ContentParam.ALL, DATA.path2, netconfStrategy);
109 assertEquals(DATA.data2, normalizedNode);
113 public void readDataNonConfigTest() {
114 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
115 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
116 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(netconfService).get(DATA.path2);
117 NormalizedNode normalizedNode = readData(ContentParam.NONCONFIG, DATA.path2, mdsalStrategy);
118 assertEquals(DATA.data2, normalizedNode);
120 normalizedNode = readData(ContentParam.NONCONFIG, DATA.path2, netconfStrategy);
121 assertEquals(DATA.data2, normalizedNode);
125 public void readContainerDataAllTest() {
126 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
127 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
128 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
129 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
130 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
131 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(netconfService).get(DATA.path);
132 final ContainerNode checkingData = Builders
134 .withNodeIdentifier(NODE_IDENTIFIER)
135 .withChild(DATA.contentLeaf)
136 .withChild(DATA.contentLeaf2)
138 NormalizedNode normalizedNode = readData(ContentParam.ALL, DATA.path, mdsalStrategy);
139 assertEquals(checkingData, normalizedNode);
141 normalizedNode = readData(ContentParam.ALL, DATA.path, netconfStrategy);
142 assertEquals(checkingData, normalizedNode);
146 public void readContainerDataConfigNoValueOfContentTest() {
147 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
148 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
149 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
150 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
151 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
152 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(netconfService).get(DATA.path);
153 final ContainerNode checkingData = Builders
155 .withNodeIdentifier(NODE_IDENTIFIER)
156 .withChild(DATA.contentLeaf)
157 .withChild(DATA.contentLeaf2)
159 NormalizedNode normalizedNode = readData(ContentParam.ALL, DATA.path, mdsalStrategy);
160 assertEquals(checkingData, normalizedNode);
162 normalizedNode = readData(ContentParam.ALL, DATA.path, netconfStrategy);
163 assertEquals(checkingData, normalizedNode);
167 public void readListDataAllTest() {
168 doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(read)
169 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
170 doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(read)
171 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
172 doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(netconfService).get(DATA.path3);
173 doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(netconfService).getConfig(DATA.path3);
174 final MapNode checkingData = Builders
176 .withNodeIdentifier(new NodeIdentifier(QName.create("ns", "2016-02-28", "list")))
177 .withChild(DATA.checkData)
179 NormalizedNode normalizedNode = readData(ContentParam.ALL, DATA.path3, mdsalStrategy);
180 assertEquals(checkingData, normalizedNode);
182 normalizedNode = readData(ContentParam.ALL, DATA.path3, netconfStrategy);
183 assertEquals(checkingData, normalizedNode);
187 public void readOrderedListDataAllTest() {
188 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(read)
189 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
190 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(read)
191 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
192 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(netconfService).get(DATA.path3);
193 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(netconfService)
194 .getConfig(DATA.path3);
195 final MapNode expectedData = Builders.orderedMapBuilder()
196 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
197 .withChild(DATA.checkData)
199 NormalizedNode normalizedNode = readData(ContentParam.ALL, DATA.path3,
201 assertEquals(expectedData, normalizedNode);
203 normalizedNode = readData(ContentParam.ALL, DATA.path3, netconfStrategy);
204 assertEquals(expectedData, normalizedNode);
208 public void readUnkeyedListDataAllTest() {
209 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(read)
210 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
211 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(read)
212 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
213 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(netconfService).get(DATA.path3);
214 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(netconfService)
215 .getConfig(DATA.path3);
216 final UnkeyedListNode expectedData = Builders.unkeyedListBuilder()
217 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
218 .withChild(Builders.unkeyedListEntryBuilder()
219 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
220 .withChild(DATA.unkeyedListEntryNode1.body().iterator().next())
221 .withChild(DATA.unkeyedListEntryNode2.body().iterator().next()).build()).build();
222 NormalizedNode normalizedNode = readData(ContentParam.ALL, DATA.path3, mdsalStrategy);
223 assertEquals(expectedData, normalizedNode);
225 normalizedNode = readData(ContentParam.ALL, DATA.path3, netconfStrategy);
226 assertEquals(expectedData, normalizedNode);
230 public void readLeafListDataAllTest() {
231 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(read)
232 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
233 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(read)
234 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
235 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(netconfService)
236 .get(DATA.leafSetNodePath);
237 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(netconfService)
238 .getConfig(DATA.leafSetNodePath);
239 final LeafSetNode<String> expectedData = Builders.<String>leafSetBuilder()
240 .withNodeIdentifier(new NodeIdentifier(DATA.leafListQname))
241 .withValue(ImmutableList.<LeafSetEntryNode<String>>builder()
242 .addAll(DATA.leafSetNode1.body())
243 .addAll(DATA.leafSetNode2.body())
246 NormalizedNode normalizedNode = readData(ContentParam.ALL, DATA.leafSetNodePath,
248 assertEquals(expectedData, normalizedNode);
250 normalizedNode = readData(ContentParam.ALL, DATA.leafSetNodePath, netconfStrategy);
251 assertEquals(expectedData, normalizedNode);
255 public void readOrderedLeafListDataAllTest() {
256 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(read)
257 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
258 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(read)
259 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
260 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(netconfService)
261 .get(DATA.leafSetNodePath);
262 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(netconfService)
263 .getConfig(DATA.leafSetNodePath);
264 final LeafSetNode<String> expectedData = Builders.<String>orderedLeafSetBuilder()
265 .withNodeIdentifier(new NodeIdentifier(DATA.leafListQname))
266 .withValue(ImmutableList.<LeafSetEntryNode<String>>builder()
267 .addAll(DATA.orderedLeafSetNode1.body())
268 .addAll(DATA.orderedLeafSetNode2.body())
271 NormalizedNode normalizedNode = readData(ContentParam.ALL, DATA.leafSetNodePath,
273 assertEquals(expectedData, normalizedNode);
275 normalizedNode = readData(ContentParam.ALL, DATA.leafSetNodePath, netconfStrategy);
276 assertEquals(expectedData, normalizedNode);
280 public void readDataWrongPathOrNoContentTest() {
281 doReturn(immediateFluentFuture(Optional.empty())).when(read)
282 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
283 doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).getConfig(DATA.path2);
284 NormalizedNode normalizedNode = readData(ContentParam.CONFIG, DATA.path2, mdsalStrategy);
285 assertNull(normalizedNode);
287 normalizedNode = readData(ContentParam.CONFIG, DATA.path2, netconfStrategy);
288 assertNull(normalizedNode);
292 * Read specific type of data from data store via transaction.
294 * @param content type of data to read (config, state, all)
295 * @param strategy {@link RestconfStrategy} - wrapper for variables
296 * @return {@link NormalizedNode}
298 private @Nullable NormalizedNode readData(final @NonNull ContentParam content,
299 final YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy) {
300 return ReadDataTransactionUtil.readData(content, path, strategy, null, schemaContext);