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.common.context.InstanceIdentifierContext;
29 import org.opendaylight.restconf.nb.rfc8040.ContentParameter;
30 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
31 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
32 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
46 @RunWith(MockitoJUnitRunner.StrictStubs.class)
47 public class ReadDataTransactionUtilTest {
48 private static final TestData DATA = new TestData();
49 private static final NodeIdentifier NODE_IDENTIFIER =
50 new NodeIdentifier(QName.create("ns", "2016-02-28", "container"));
52 private RestconfStrategy mdsalStrategy;
53 private RestconfStrategy netconfStrategy;
55 private NetconfDataTreeService netconfService;
57 private InstanceIdentifierContext<ContainerSchemaNode> context;
59 private DOMDataTreeReadTransaction read;
61 private EffectiveModelContext schemaContext;
63 private DOMDataBroker mockDataBroker;
67 // FIXME: these tests need to be parameterized somehow. The trouble is we need mocking before we invoke
68 // the strategy. This needs some more thought.
69 doReturn(read).when(mockDataBroker).newReadOnlyTransaction();
70 mdsalStrategy = new MdsalRestconfStrategy(mockDataBroker);
71 netconfStrategy = new NetconfRestconfStrategy(netconfService);
75 public void readDataConfigTest() {
76 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
77 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
78 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
79 NormalizedNode normalizedNode = readData(ContentParameter.CONFIG, DATA.path, mdsalStrategy);
80 assertEquals(DATA.data3, normalizedNode);
82 normalizedNode = readData(ContentParameter.CONFIG, DATA.path, netconfStrategy);
83 assertEquals(DATA.data3, normalizedNode);
87 public void readAllHavingOnlyConfigTest() {
88 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
89 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
90 doReturn(immediateFluentFuture(Optional.empty())).when(read)
91 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
92 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
93 doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).get(DATA.path);
94 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path, mdsalStrategy);
95 assertEquals(DATA.data3, normalizedNode);
97 normalizedNode = readData(ContentParameter.ALL, DATA.path, netconfStrategy);
98 assertEquals(DATA.data3, normalizedNode);
102 public void readAllHavingOnlyNonConfigTest() {
103 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
104 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
105 doReturn(immediateFluentFuture(Optional.empty())).when(read)
106 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
107 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(netconfService).get(DATA.path2);
108 doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).getConfig(DATA.path2);
109 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path2, mdsalStrategy);
110 assertEquals(DATA.data2, normalizedNode);
112 normalizedNode = readData(ContentParameter.ALL, DATA.path2, netconfStrategy);
113 assertEquals(DATA.data2, normalizedNode);
117 public void readDataNonConfigTest() {
118 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
119 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
120 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(netconfService).get(DATA.path2);
121 NormalizedNode normalizedNode = readData(ContentParameter.NONCONFIG, DATA.path2, mdsalStrategy);
122 assertEquals(DATA.data2, normalizedNode);
124 normalizedNode = readData(ContentParameter.NONCONFIG, DATA.path2, netconfStrategy);
125 assertEquals(DATA.data2, normalizedNode);
129 public void readContainerDataAllTest() {
130 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
131 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
132 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
133 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
134 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
135 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(netconfService).get(DATA.path);
136 final ContainerNode checkingData = Builders
138 .withNodeIdentifier(NODE_IDENTIFIER)
139 .withChild(DATA.contentLeaf)
140 .withChild(DATA.contentLeaf2)
142 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path, mdsalStrategy);
143 assertEquals(checkingData, normalizedNode);
145 normalizedNode = readData(ContentParameter.ALL, DATA.path, netconfStrategy);
146 assertEquals(checkingData, normalizedNode);
150 public void readContainerDataConfigNoValueOfContentTest() {
151 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
152 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
153 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
154 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
155 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
156 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(netconfService).get(DATA.path);
157 final ContainerNode checkingData = Builders
159 .withNodeIdentifier(NODE_IDENTIFIER)
160 .withChild(DATA.contentLeaf)
161 .withChild(DATA.contentLeaf2)
163 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path, mdsalStrategy);
164 assertEquals(checkingData, normalizedNode);
166 normalizedNode = readData(ContentParameter.ALL, DATA.path, netconfStrategy);
167 assertEquals(checkingData, normalizedNode);
171 public void readListDataAllTest() {
172 doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(read)
173 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
174 doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(read)
175 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
176 doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(netconfService).get(DATA.path3);
177 doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(netconfService).getConfig(DATA.path3);
178 final MapNode checkingData = Builders
180 .withNodeIdentifier(new NodeIdentifier(QName.create("ns", "2016-02-28", "list")))
181 .withChild(DATA.checkData)
183 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path3, mdsalStrategy);
184 assertEquals(checkingData, normalizedNode);
186 normalizedNode = readData(ContentParameter.ALL, DATA.path3, netconfStrategy);
187 assertEquals(checkingData, normalizedNode);
191 public void readOrderedListDataAllTest() {
192 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(read)
193 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
194 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(read)
195 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
196 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(netconfService).get(DATA.path3);
197 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(netconfService)
198 .getConfig(DATA.path3);
199 final MapNode expectedData = Builders.orderedMapBuilder()
200 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
201 .withChild(DATA.checkData)
203 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path3,
205 assertEquals(expectedData, normalizedNode);
207 normalizedNode = readData(ContentParameter.ALL, DATA.path3, netconfStrategy);
208 assertEquals(expectedData, normalizedNode);
212 public void readUnkeyedListDataAllTest() {
213 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(read)
214 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
215 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(read)
216 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
217 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(netconfService).get(DATA.path3);
218 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(netconfService)
219 .getConfig(DATA.path3);
220 final UnkeyedListNode expectedData = Builders.unkeyedListBuilder()
221 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
222 .withChild(Builders.unkeyedListEntryBuilder()
223 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
224 .withChild(DATA.unkeyedListEntryNode1.body().iterator().next())
225 .withChild(DATA.unkeyedListEntryNode2.body().iterator().next()).build()).build();
226 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path3, mdsalStrategy);
227 assertEquals(expectedData, normalizedNode);
229 normalizedNode = readData(ContentParameter.ALL, DATA.path3, netconfStrategy);
230 assertEquals(expectedData, normalizedNode);
234 public void readLeafListDataAllTest() {
235 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(read)
236 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
237 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(read)
238 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
239 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(netconfService)
240 .get(DATA.leafSetNodePath);
241 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(netconfService)
242 .getConfig(DATA.leafSetNodePath);
243 final LeafSetNode<String> expectedData = Builders.<String>leafSetBuilder()
244 .withNodeIdentifier(new NodeIdentifier(DATA.leafListQname))
245 .withValue(ImmutableList.<LeafSetEntryNode<String>>builder()
246 .addAll(DATA.leafSetNode1.body())
247 .addAll(DATA.leafSetNode2.body())
250 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.leafSetNodePath,
252 assertEquals(expectedData, normalizedNode);
254 normalizedNode = readData(ContentParameter.ALL, DATA.leafSetNodePath, netconfStrategy);
255 assertEquals(expectedData, normalizedNode);
259 public void readOrderedLeafListDataAllTest() {
260 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(read)
261 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
262 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(read)
263 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
264 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(netconfService)
265 .get(DATA.leafSetNodePath);
266 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(netconfService)
267 .getConfig(DATA.leafSetNodePath);
268 final LeafSetNode<String> expectedData = Builders.<String>orderedLeafSetBuilder()
269 .withNodeIdentifier(new NodeIdentifier(DATA.leafListQname))
270 .withValue(ImmutableList.<LeafSetEntryNode<String>>builder()
271 .addAll(DATA.orderedLeafSetNode1.body())
272 .addAll(DATA.orderedLeafSetNode2.body())
275 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.leafSetNodePath,
277 assertEquals(expectedData, normalizedNode);
279 normalizedNode = readData(ContentParameter.ALL, DATA.leafSetNodePath, netconfStrategy);
280 assertEquals(expectedData, normalizedNode);
284 public void readDataWrongPathOrNoContentTest() {
285 doReturn(immediateFluentFuture(Optional.empty())).when(read)
286 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
287 doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).getConfig(DATA.path2);
288 NormalizedNode normalizedNode = readData(ContentParameter.CONFIG, DATA.path2, mdsalStrategy);
289 assertNull(normalizedNode);
291 normalizedNode = readData(ContentParameter.CONFIG, DATA.path2, netconfStrategy);
292 assertNull(normalizedNode);
296 * Read specific type of data from data store via transaction.
298 * @param content type of data to read (config, state, all)
299 * @param strategy {@link RestconfStrategy} - wrapper for variables
300 * @return {@link NormalizedNode}
302 private @Nullable NormalizedNode readData(final @NonNull ContentParameter content,
303 final YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy) {
304 return ReadDataTransactionUtil.readData(content, path, strategy, null, schemaContext);