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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
20 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
22 import com.google.common.collect.ImmutableList;
23 import java.util.List;
24 import java.util.Optional;
26 import javax.ws.rs.core.MultivaluedHashMap;
27 import javax.ws.rs.core.UriInfo;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
38 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
39 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
40 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
41 import org.opendaylight.restconf.common.errors.RestconfError;
42 import org.opendaylight.restconf.nb.rfc8040.ContentParameter;
43 import org.opendaylight.restconf.nb.rfc8040.DepthParameter;
44 import org.opendaylight.restconf.nb.rfc8040.WithDefaultsParameter;
45 import org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters;
46 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
47 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
48 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
49 import org.opendaylight.yangtools.yang.common.ErrorTag;
50 import org.opendaylight.yangtools.yang.common.ErrorType;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
60 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
61 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
63 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
65 @RunWith(MockitoJUnitRunner.StrictStubs.class)
66 public class ReadDataTransactionUtilTest {
68 private static final TestData DATA = new TestData();
69 private static final NodeIdentifier NODE_IDENTIFIER =
70 new NodeIdentifier(QName.create("ns", "2016-02-28", "container"));
72 private RestconfStrategy mdsalStrategy;
73 private RestconfStrategy netconfStrategy;
75 private NetconfDataTreeService netconfService;
77 private InstanceIdentifierContext<ContainerSchemaNode> context;
79 private DOMDataTreeReadTransaction read;
81 private EffectiveModelContext schemaContext;
83 private ContainerSchemaNode containerSchemaNode;
85 private LeafSchemaNode containerChildNode;
86 private QName containerChildQName;
90 containerChildQName = QName.create("ns", "2016-02-28", "container-child");
92 when(context.getSchemaContext()).thenReturn(schemaContext);
93 when(context.getSchemaNode()).thenReturn(containerSchemaNode);
94 when(containerSchemaNode.getQName()).thenReturn(NODE_IDENTIFIER.getNodeType());
95 when(containerChildNode.getQName()).thenReturn(containerChildQName);
96 when(containerSchemaNode.dataChildByName(containerChildQName)).thenReturn(containerChildNode);
98 DOMDataBroker mockDataBroker = mock(DOMDataBroker.class);
99 doReturn(read).when(mockDataBroker).newReadOnlyTransaction();
100 mdsalStrategy = new MdsalRestconfStrategy(mockDataBroker);
101 netconfStrategy = new NetconfRestconfStrategy(netconfService);
105 public void readDataConfigTest() {
106 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
107 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
108 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
109 NormalizedNode normalizedNode = readData(ContentParameter.CONFIG, DATA.path, mdsalStrategy);
110 assertEquals(DATA.data3, normalizedNode);
112 normalizedNode = readData(ContentParameter.CONFIG, DATA.path, netconfStrategy);
113 assertEquals(DATA.data3, normalizedNode);
117 public void readAllHavingOnlyConfigTest() {
118 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
119 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
120 doReturn(immediateFluentFuture(Optional.empty())).when(read)
121 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
122 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
123 doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).get(DATA.path);
124 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path, mdsalStrategy);
125 assertEquals(DATA.data3, normalizedNode);
127 normalizedNode = readData(ContentParameter.ALL, DATA.path, netconfStrategy);
128 assertEquals(DATA.data3, normalizedNode);
132 public void readAllHavingOnlyNonConfigTest() {
133 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
134 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
135 doReturn(immediateFluentFuture(Optional.empty())).when(read)
136 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
137 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(netconfService).get(DATA.path2);
138 doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).getConfig(DATA.path2);
139 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path2, mdsalStrategy);
140 assertEquals(DATA.data2, normalizedNode);
142 normalizedNode = readData(ContentParameter.ALL, DATA.path2, netconfStrategy);
143 assertEquals(DATA.data2, normalizedNode);
147 public void readDataNonConfigTest() {
148 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
149 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
150 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(netconfService).get(DATA.path2);
151 NormalizedNode normalizedNode = readData(ContentParameter.NONCONFIG, DATA.path2, mdsalStrategy);
152 assertEquals(DATA.data2, normalizedNode);
154 normalizedNode = readData(ContentParameter.NONCONFIG, DATA.path2, netconfStrategy);
155 assertEquals(DATA.data2, normalizedNode);
159 public void readContainerDataAllTest() {
160 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
161 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
162 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
163 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
164 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
165 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(netconfService).get(DATA.path);
166 final ContainerNode checkingData = Builders
168 .withNodeIdentifier(NODE_IDENTIFIER)
169 .withChild(DATA.contentLeaf)
170 .withChild(DATA.contentLeaf2)
172 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path, mdsalStrategy);
173 assertEquals(checkingData, normalizedNode);
175 normalizedNode = readData(ContentParameter.ALL, DATA.path, netconfStrategy);
176 assertEquals(checkingData, normalizedNode);
180 public void readContainerDataConfigNoValueOfContentTest() {
181 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
182 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
183 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
184 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
185 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(netconfService).getConfig(DATA.path);
186 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(netconfService).get(DATA.path);
187 final ContainerNode checkingData = Builders
189 .withNodeIdentifier(NODE_IDENTIFIER)
190 .withChild(DATA.contentLeaf)
191 .withChild(DATA.contentLeaf2)
193 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path, mdsalStrategy);
194 assertEquals(checkingData, normalizedNode);
196 normalizedNode = readData(ContentParameter.ALL, DATA.path, netconfStrategy);
197 assertEquals(checkingData, normalizedNode);
201 public void readListDataAllTest() {
202 doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(read)
203 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
204 doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(read)
205 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
206 doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(netconfService).get(DATA.path3);
207 doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(netconfService).getConfig(DATA.path3);
208 final MapNode checkingData = Builders
210 .withNodeIdentifier(new NodeIdentifier(QName.create("ns", "2016-02-28", "list")))
211 .withChild(DATA.checkData)
213 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path3, mdsalStrategy);
214 assertEquals(checkingData, normalizedNode);
216 normalizedNode = readData(ContentParameter.ALL, DATA.path3, netconfStrategy);
217 assertEquals(checkingData, normalizedNode);
221 public void readOrderedListDataAllTest() {
222 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(read)
223 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
224 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(read)
225 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
226 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(netconfService).get(DATA.path3);
227 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(netconfService)
228 .getConfig(DATA.path3);
229 final MapNode expectedData = Builders.orderedMapBuilder()
230 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
231 .withChild(DATA.checkData)
233 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path3,
235 assertEquals(expectedData, normalizedNode);
237 normalizedNode = readData(ContentParameter.ALL, DATA.path3, netconfStrategy);
238 assertEquals(expectedData, normalizedNode);
242 public void readUnkeyedListDataAllTest() {
243 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(read)
244 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
245 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(read)
246 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
247 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(netconfService).get(DATA.path3);
248 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(netconfService)
249 .getConfig(DATA.path3);
250 final UnkeyedListNode expectedData = Builders.unkeyedListBuilder()
251 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
252 .withChild(Builders.unkeyedListEntryBuilder()
253 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
254 .withChild(DATA.unkeyedListEntryNode1.body().iterator().next())
255 .withChild(DATA.unkeyedListEntryNode2.body().iterator().next()).build()).build();
256 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.path3, mdsalStrategy);
257 assertEquals(expectedData, normalizedNode);
259 normalizedNode = readData(ContentParameter.ALL, DATA.path3, netconfStrategy);
260 assertEquals(expectedData, normalizedNode);
264 public void readLeafListDataAllTest() {
265 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(read)
266 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
267 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(read)
268 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
269 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(netconfService)
270 .get(DATA.leafSetNodePath);
271 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(netconfService)
272 .getConfig(DATA.leafSetNodePath);
273 final LeafSetNode<String> expectedData = Builders.<String>leafSetBuilder()
274 .withNodeIdentifier(new NodeIdentifier(DATA.leafListQname))
275 .withValue(ImmutableList.<LeafSetEntryNode<String>>builder()
276 .addAll(DATA.leafSetNode1.body())
277 .addAll(DATA.leafSetNode2.body())
280 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.leafSetNodePath,
282 assertEquals(expectedData, normalizedNode);
284 normalizedNode = readData(ContentParameter.ALL, DATA.leafSetNodePath, netconfStrategy);
285 assertEquals(expectedData, normalizedNode);
289 public void readOrderedLeafListDataAllTest() {
290 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(read)
291 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
292 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(read)
293 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
294 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(netconfService)
295 .get(DATA.leafSetNodePath);
296 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(netconfService)
297 .getConfig(DATA.leafSetNodePath);
298 final LeafSetNode<String> expectedData = Builders.<String>orderedLeafSetBuilder()
299 .withNodeIdentifier(new NodeIdentifier(DATA.leafListQname))
300 .withValue(ImmutableList.<LeafSetEntryNode<String>>builder()
301 .addAll(DATA.orderedLeafSetNode1.body())
302 .addAll(DATA.orderedLeafSetNode2.body())
305 NormalizedNode normalizedNode = readData(ContentParameter.ALL, DATA.leafSetNodePath,
307 assertEquals(expectedData, normalizedNode);
309 normalizedNode = readData(ContentParameter.ALL, DATA.leafSetNodePath, netconfStrategy);
310 assertEquals(expectedData, normalizedNode);
314 public void readDataWrongPathOrNoContentTest() {
315 doReturn(immediateFluentFuture(Optional.empty())).when(read)
316 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
317 doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).getConfig(DATA.path2);
318 NormalizedNode normalizedNode = readData(ContentParameter.CONFIG, DATA.path2, mdsalStrategy);
319 assertNull(normalizedNode);
321 normalizedNode = readData(ContentParameter.CONFIG, DATA.path2, netconfStrategy);
322 assertNull(normalizedNode);
326 * Test of parsing default parameters from URI request.
329 public void parseUriParametersDefaultTest() {
330 final UriInfo uriInfo = mock(UriInfo.class);
331 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
333 // no parameters, default values should be used
334 when(uriInfo.getQueryParameters()).thenReturn(parameters);
336 final QueryParameters parsedParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
338 assertEquals(ContentParameter.ALL, parsedParameters.getContent());
339 assertNull(parsedParameters.getDepth());
340 assertNull(parsedParameters.getFields());
344 * Test of parsing user defined parameters from URI request.
347 public void parseUriParametersUserDefinedTest() {
348 final UriInfo uriInfo = mock(UriInfo.class);
349 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
350 parameters.putSingle("content", "config");
351 parameters.putSingle("depth", "10");
352 parameters.putSingle("fields", containerChildQName.getLocalName());
354 when(uriInfo.getQueryParameters()).thenReturn(parameters);
356 final QueryParameters parsedParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
359 assertEquals(ContentParameter.CONFIG, parsedParameters.getContent());
362 final DepthParameter depth = parsedParameters.getDepth();
363 assertNotNull(depth);
364 assertEquals(10, depth.value());
367 assertNotNull(parsedParameters.getFields());
368 assertEquals(1, parsedParameters.getFields().size());
369 assertEquals(1, parsedParameters.getFields().get(0).size());
370 assertEquals(containerChildQName, parsedParameters.getFields().get(0).iterator().next());
374 * Negative test of parsing request URI parameters when content parameter has not allowed value.
377 public void parseUriParametersContentParameterNegativeTest() {
378 final UriInfo uriInfo = mock(UriInfo.class);
379 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
380 parameters.putSingle("content", "not-allowed-parameter-value");
381 when(uriInfo.getQueryParameters()).thenReturn(parameters);
383 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
384 () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
386 assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
387 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
391 * Negative test of parsing request URI parameters when depth parameter has not allowed value.
394 public void parseUriParametersDepthParameterNegativeTest() {
395 final UriInfo uriInfo = mock(UriInfo.class);
396 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
398 // inserted value is not allowed
399 parameters.putSingle("depth", "bounded");
400 when(uriInfo.getQueryParameters()).thenReturn(parameters);
402 RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
403 () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
405 assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
406 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
410 * Negative test of parsing request URI parameters when depth parameter has not allowed value (less than minimum).
413 public void parseUriParametersDepthMinimalParameterNegativeTest() {
414 final UriInfo uriInfo = mock(UriInfo.class);
415 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
417 // inserted value is too low
418 parameters.putSingle("depth", "0");
419 when(uriInfo.getQueryParameters()).thenReturn(parameters);
421 RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
422 () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
424 assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
425 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
429 * Negative test of parsing request URI parameters when depth parameter has not allowed value (more than maximum).
432 public void parseUriParametersDepthMaximalParameterNegativeTest() {
433 final UriInfo uriInfo = mock(UriInfo.class);
434 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
436 // inserted value is too high
437 parameters.putSingle("depth", "65536");
438 when(uriInfo.getQueryParameters()).thenReturn(parameters);
440 RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
441 () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
443 assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
444 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
448 * Testing parsing of with-defaults parameter which value doesn't match report-all or report-all-tagged patterns
449 * - non-reporting setting.
452 public void parseUriParametersWithDefaultAndNonTaggedTest() {
453 // preparation of input data
454 final UriInfo uriInfo = mock(UriInfo.class);
455 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
456 parameters.putSingle("with-defaults", "explicit");
457 when(uriInfo.getQueryParameters()).thenReturn(parameters);
459 final QueryParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
460 assertSame(WithDefaultsParameter.EXPLICIT, writerParameters.getWithDefault());
461 assertFalse(writerParameters.isTagged());
465 * Testing parsing of with-defaults parameter which value which is not supported.
468 public void parseUriParametersWithDefaultInvalidTest() {
469 // preparation of input data
470 final UriInfo uriInfo = mock(UriInfo.class);
471 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
472 parameters.putSingle("with-defaults", "invalid");
473 when(uriInfo.getQueryParameters()).thenReturn(parameters);
475 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
476 () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
477 final List<RestconfError> errors = ex.getErrors();
478 assertEquals(1, errors.size());
479 assertEquals(ErrorTag.INVALID_VALUE, errors.get(0).getErrorTag());
483 * Testing parsing of with-defaults parameter which value matches 'report-all-tagged' setting - default value should
484 * be set to {@code null} and tagged flag should be set to {@code true}.
487 public void parseUriParametersWithDefaultAndTaggedTest() {
488 // preparation of input data
489 final UriInfo uriInfo = mock(UriInfo.class);
490 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
491 parameters.putSingle("with-defaults", "report-all-tagged");
492 when(uriInfo.getQueryParameters()).thenReturn(parameters);
494 final QueryParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
495 assertNull(writerParameters.getWithDefault());
496 assertTrue(writerParameters.isTagged());
500 * Testing parsing of with-defaults parameter which value matches 'report-all' setting - default value should
501 * be set to {@code null} and tagged flag should be set to {@code false}.
504 public void parseUriParametersWithDefaultAndReportAllTest() {
505 // preparation of input data
506 final UriInfo uriInfo = mock(UriInfo.class);
507 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
508 parameters.putSingle("with-defaults", "report-all");
509 when(uriInfo.getQueryParameters()).thenReturn(parameters);
511 final QueryParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
512 assertNull(writerParameters.getWithDefault());
513 assertFalse(writerParameters.isTagged());
517 * Test when parameter is present at most once.
520 public void getSingleParameterTest() {
521 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
522 parameters.putSingle(ContentParameter.uriName(), "all");
523 assertEquals("all", ReadDataTransactionUtil.getSingleParameter(parameters, ContentParameter.uriName()));
527 * Test when parameter is present more than once.
530 public void getSingleParameterNegativeTest() {
531 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
532 parameters.put(ContentParameter.uriName(), List.of("config", "nonconfig", "all"));
534 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
535 () -> ReadDataTransactionUtil.getSingleParameter(parameters, ContentParameter.uriName()));
536 final List<RestconfError> errors = ex.getErrors();
537 assertEquals(1, errors.size());
539 final RestconfError error = errors.get(0);
540 assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
541 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
545 * Test when all parameters are allowed.
548 public void checkParametersTypesTest() {
549 ReadDataTransactionUtil.checkParametersTypes(Set.of("content"),
550 Set.of(ContentParameter.uriName(), DepthParameter.uriName()));
554 * Test when not allowed parameter type is used.
557 public void checkParametersTypesNegativeTest() {
558 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
559 () -> ReadDataTransactionUtil.checkParametersTypes(Set.of("not-allowed-parameter"),
560 Set.of(ContentParameter.uriName(), DepthParameter.uriName())));
561 final List<RestconfError> errors = ex.getErrors();
562 assertEquals(1, errors.size());
564 final RestconfError error = errors.get(0);
565 assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
566 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
570 * Read specific type of data from data store via transaction.
572 * @param content type of data to read (config, state, all)
573 * @param strategy {@link RestconfStrategy} - wrapper for variables
574 * @return {@link NormalizedNode}
576 private @Nullable NormalizedNode readData(final @NonNull ContentParameter content,
577 final YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy) {
578 return ReadDataTransactionUtil.readData(content, path, strategy, null, schemaContext);