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.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.when;
18 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
20 import com.google.common.collect.ImmutableList;
21 import java.util.Collections;
22 import java.util.Optional;
23 import javax.ws.rs.core.MultivaluedHashMap;
24 import javax.ws.rs.core.UriInfo;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
33 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
34 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
35 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
36 import org.opendaylight.restconf.common.context.WriterParameters;
37 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
38 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
39 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
40 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
41 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
42 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
43 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
44 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
53 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
54 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
55 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
56 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
58 public class ReadDataTransactionUtilTest {
60 private static final TestData DATA = new TestData();
61 private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER = new YangInstanceIdentifier
62 .NodeIdentifier(QName.create("ns", "2016-02-28", "container"));
64 private RestconfStrategy mdsalStrategy;
65 private RestconfStrategy netconfStrategy;
67 private NetconfDataTreeService netconfService;
69 private DOMTransactionChain transactionChain;
71 private InstanceIdentifierContext<ContainerSchemaNode> context;
73 private DOMDataTreeReadTransaction read;
75 private EffectiveModelContext schemaContext;
77 private ContainerSchemaNode containerSchemaNode;
79 private LeafSchemaNode containerChildNode;
80 private QName containerChildQName;
84 MockitoAnnotations.initMocks(this);
86 containerChildQName = QName.create("ns", "2016-02-28", "container-child");
88 when(transactionChain.newReadOnlyTransaction()).thenReturn(read);
89 when(context.getSchemaContext()).thenReturn(schemaContext);
90 when(context.getSchemaNode()).thenReturn(containerSchemaNode);
91 when(containerSchemaNode.getQName()).thenReturn(NODE_IDENTIFIER.getNodeType());
92 when(containerChildNode.getQName()).thenReturn(containerChildQName);
93 when(containerSchemaNode.getDataChildByName(containerChildQName)).thenReturn(containerChildNode);
95 DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class);
96 Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
97 mdsalStrategy = new MdsalRestconfStrategy(this.context, new TransactionChainHandler(mockDataBroker));
98 netconfStrategy = new NetconfRestconfStrategy(this.netconfService, this.context);
102 public void readDataConfigTest() {
103 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
104 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
105 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path);
106 doReturn(DATA.path).when(context).getInstanceIdentifier();
107 final String valueOfContent = RestconfDataServiceConstant.ReadData.CONFIG;
108 NormalizedNode<?, ?> normalizedNode =
109 ReadDataTransactionUtil.readData(valueOfContent, mdsalStrategy, schemaContext);
110 assertEquals(DATA.data3, normalizedNode);
112 normalizedNode = ReadDataTransactionUtil.readData(valueOfContent, netconfStrategy, schemaContext);
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(this.netconfService).getConfig(DATA.path);
123 doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).get(DATA.path);
124 doReturn(DATA.path).when(context).getInstanceIdentifier();
125 final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
126 NormalizedNode<?, ?> normalizedNode =
127 ReadDataTransactionUtil.readData(valueOfContent, mdsalStrategy, schemaContext);
128 assertEquals(DATA.data3, normalizedNode);
131 ReadDataTransactionUtil.readData(valueOfContent, netconfStrategy, schemaContext);
132 assertEquals(DATA.data3, normalizedNode);
136 public void readAllHavingOnlyNonConfigTest() {
137 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
138 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
139 doReturn(immediateFluentFuture(Optional.empty())).when(read)
140 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
141 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(this.netconfService).get(DATA.path2);
142 doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(DATA.path2);
143 doReturn(DATA.path2).when(context).getInstanceIdentifier();
144 final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
145 NormalizedNode<?, ?> normalizedNode =
146 ReadDataTransactionUtil.readData(valueOfContent, mdsalStrategy, schemaContext);
147 assertEquals(DATA.data2, normalizedNode);
150 ReadDataTransactionUtil.readData(valueOfContent, netconfStrategy, schemaContext);
151 assertEquals(DATA.data2, normalizedNode);
155 public void readDataNonConfigTest() {
156 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
157 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
158 doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(this.netconfService).get(DATA.path2);
159 doReturn(DATA.path2).when(context).getInstanceIdentifier();
160 final String valueOfContent = RestconfDataServiceConstant.ReadData.NONCONFIG;
161 NormalizedNode<?, ?> normalizedNode =
162 ReadDataTransactionUtil.readData(valueOfContent, mdsalStrategy, schemaContext);
163 assertEquals(DATA.data2, normalizedNode);
166 ReadDataTransactionUtil.readData(valueOfContent, netconfStrategy, schemaContext);
167 assertEquals(DATA.data2, normalizedNode);
171 public void readContainerDataAllTest() {
172 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
173 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
174 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
175 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
176 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path);
177 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(this.netconfService).get(DATA.path);
178 doReturn(DATA.path).when(context).getInstanceIdentifier();
179 final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
180 final ContainerNode checkingData = Builders
182 .withNodeIdentifier(NODE_IDENTIFIER)
183 .withChild(DATA.contentLeaf)
184 .withChild(DATA.contentLeaf2)
186 NormalizedNode<?, ?> normalizedNode =
187 ReadDataTransactionUtil.readData(valueOfContent, mdsalStrategy, schemaContext);
188 assertEquals(checkingData, normalizedNode);
191 ReadDataTransactionUtil.readData(valueOfContent, netconfStrategy, schemaContext);
192 assertEquals(checkingData, normalizedNode);
196 public void readContainerDataConfigNoValueOfContentTest() {
197 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
198 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
199 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
200 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
201 doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path);
202 doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(this.netconfService).get(DATA.path);
203 doReturn(DATA.path).when(context).getInstanceIdentifier();
204 final ContainerNode checkingData = Builders
206 .withNodeIdentifier(NODE_IDENTIFIER)
207 .withChild(DATA.contentLeaf)
208 .withChild(DATA.contentLeaf2)
210 NormalizedNode<?, ?> normalizedNode = ReadDataTransactionUtil.readData(
211 RestconfDataServiceConstant.ReadData.ALL, mdsalStrategy, schemaContext);
212 assertEquals(checkingData, normalizedNode);
214 normalizedNode = ReadDataTransactionUtil.readData(
215 RestconfDataServiceConstant.ReadData.ALL, netconfStrategy, schemaContext);
216 assertEquals(checkingData, normalizedNode);
220 public void readListDataAllTest() {
221 doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(read)
222 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
223 doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(read)
224 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
225 doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(this.netconfService).get(DATA.path3);
226 doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(this.netconfService).getConfig(DATA.path3);
227 doReturn(DATA.path3).when(context).getInstanceIdentifier();
228 final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
229 final MapNode checkingData = Builders
231 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("ns", "2016-02-28", "list")))
232 .withChild(DATA.checkData)
234 NormalizedNode<?, ?> normalizedNode =
235 ReadDataTransactionUtil.readData(valueOfContent, mdsalStrategy, schemaContext);
236 assertEquals(checkingData, normalizedNode);
239 ReadDataTransactionUtil.readData(valueOfContent, netconfStrategy, schemaContext);
240 assertEquals(checkingData, normalizedNode);
244 public void readOrderedListDataAllTest() {
245 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(read)
246 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
247 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(read)
248 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
249 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(this.netconfService).get(DATA.path3);
250 doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(this.netconfService)
251 .getConfig(DATA.path3);
252 doReturn(DATA.path3).when(context).getInstanceIdentifier();
253 final MapNode expectedData = Builders.orderedMapBuilder()
254 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DATA.listQname)).withChild(DATA.checkData)
256 NormalizedNode<?, ?> normalizedNode = ReadDataTransactionUtil
257 .readData(RestconfDataServiceConstant.ReadData.ALL, mdsalStrategy, schemaContext);
258 assertEquals(expectedData, normalizedNode);
260 normalizedNode = ReadDataTransactionUtil
261 .readData(RestconfDataServiceConstant.ReadData.ALL, netconfStrategy, schemaContext);
262 assertEquals(expectedData, normalizedNode);
266 public void readUnkeyedListDataAllTest() {
267 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(read)
268 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
269 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(read)
270 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
271 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(this.netconfService).get(DATA.path3);
272 doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(this.netconfService)
273 .getConfig(DATA.path3);
274 doReturn(DATA.path3).when(context).getInstanceIdentifier();
275 final UnkeyedListNode expectedData = Builders.unkeyedListBuilder()
276 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DATA.listQname))
277 .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(
278 new YangInstanceIdentifier.NodeIdentifier(DATA.listQname))
279 .withChild(DATA.unkeyedListEntryNode1.getValue().iterator().next())
280 .withChild(DATA.unkeyedListEntryNode2.getValue().iterator().next()).build()).build();
281 NormalizedNode<?, ?> normalizedNode = ReadDataTransactionUtil
282 .readData(RestconfDataServiceConstant.ReadData.ALL, mdsalStrategy, schemaContext);
283 assertEquals(expectedData, normalizedNode);
285 normalizedNode = ReadDataTransactionUtil
286 .readData(RestconfDataServiceConstant.ReadData.ALL, netconfStrategy, schemaContext);
287 assertEquals(expectedData, normalizedNode);
291 public void readLeafListDataAllTest() {
292 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(read)
293 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
294 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(read)
295 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
296 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(this.netconfService)
297 .get(DATA.leafSetNodePath);
298 doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(this.netconfService)
299 .getConfig(DATA.leafSetNodePath);
300 doReturn(DATA.leafSetNodePath).when(context).getInstanceIdentifier();
301 final LeafSetNode<String> expectedData = Builders.<String>leafSetBuilder().withNodeIdentifier(
302 new YangInstanceIdentifier.NodeIdentifier(DATA.leafListQname)).withValue(
303 ImmutableList.<LeafSetEntryNode<String>>builder().addAll(DATA.leafSetNode1.getValue())
304 .addAll(DATA.leafSetNode2.getValue()).build()).build();
305 NormalizedNode<?, ?> normalizedNode = ReadDataTransactionUtil
306 .readData(RestconfDataServiceConstant.ReadData.ALL, mdsalStrategy, schemaContext);
307 assertEquals(expectedData, normalizedNode);
309 normalizedNode = ReadDataTransactionUtil
310 .readData(RestconfDataServiceConstant.ReadData.ALL, netconfStrategy, schemaContext);
311 assertEquals(expectedData, normalizedNode);
315 public void readOrderedLeafListDataAllTest() {
316 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(read)
317 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
318 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(read)
319 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
320 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(this.netconfService)
321 .get(DATA.leafSetNodePath);
322 doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(this.netconfService)
323 .getConfig(DATA.leafSetNodePath);
324 doReturn(DATA.leafSetNodePath).when(context).getInstanceIdentifier();
325 final LeafSetNode<String> expectedData = Builders.<String>orderedLeafSetBuilder().withNodeIdentifier(
326 new YangInstanceIdentifier.NodeIdentifier(DATA.leafListQname)).withValue(
327 ImmutableList.<LeafSetEntryNode<String>>builder().addAll(DATA.orderedLeafSetNode1.getValue())
328 .addAll(DATA.orderedLeafSetNode2.getValue()).build()).build();
329 NormalizedNode<?, ?> normalizedNode = ReadDataTransactionUtil
330 .readData(RestconfDataServiceConstant.ReadData.ALL, mdsalStrategy, schemaContext);
331 assertEquals(expectedData, normalizedNode);
333 normalizedNode = ReadDataTransactionUtil
334 .readData(RestconfDataServiceConstant.ReadData.ALL, netconfStrategy, schemaContext);
335 assertEquals(expectedData, normalizedNode);
339 public void readDataWrongPathOrNoContentTest() {
340 doReturn(immediateFluentFuture(Optional.empty())).when(read)
341 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
342 doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(DATA.path2);
343 doReturn(DATA.path2).when(context).getInstanceIdentifier();
344 final String valueOfContent = RestconfDataServiceConstant.ReadData.CONFIG;
345 NormalizedNode<?, ?> normalizedNode =
346 ReadDataTransactionUtil.readData(valueOfContent, mdsalStrategy, schemaContext);
347 assertNull(normalizedNode);
350 ReadDataTransactionUtil.readData(valueOfContent, netconfStrategy, schemaContext);
351 assertNull(normalizedNode);
354 @Test(expected = RestconfDocumentedException.class)
355 public void readDataFailTest() {
356 final String valueOfContent = RestconfDataServiceConstant.ReadData.READ_TYPE_TX;
357 NormalizedNode<?, ?> normalizedNode = ReadDataTransactionUtil.readData(
358 valueOfContent, mdsalStrategy, schemaContext);
359 assertNull(normalizedNode);
361 normalizedNode = ReadDataTransactionUtil.readData(
362 valueOfContent, netconfStrategy, schemaContext);
363 assertNull(normalizedNode);
367 * Test of parsing default parameters from URI request.
370 public void parseUriParametersDefaultTest() {
371 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
372 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
374 // no parameters, default values should be used
375 when(uriInfo.getQueryParameters()).thenReturn(parameters);
377 final WriterParameters parsedParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
379 assertEquals("Not correctly parsed URI parameter",
380 RestconfDataServiceConstant.ReadData.ALL, parsedParameters.getContent());
381 assertNull("Not correctly parsed URI parameter",
382 parsedParameters.getDepth());
383 assertNull("Not correctly parsed URI parameter",
384 parsedParameters.getFields());
388 * Test of parsing user defined parameters from URI request.
391 public void parseUriParametersUserDefinedTest() {
392 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
393 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
395 final String content = "config";
396 final String depth = "10";
397 final String fields = containerChildQName.getLocalName();
399 parameters.put("content", Collections.singletonList(content));
400 parameters.put("depth", Collections.singletonList(depth));
401 parameters.put("fields", Collections.singletonList(fields));
403 when(uriInfo.getQueryParameters()).thenReturn(parameters);
405 final WriterParameters parsedParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
408 assertEquals("Not correctly parsed URI parameter",
409 content, parsedParameters.getContent());
412 assertNotNull("Not correctly parsed URI parameter",
413 parsedParameters.getDepth());
414 assertEquals("Not correctly parsed URI parameter",
415 depth, parsedParameters.getDepth().toString());
418 assertNotNull("Not correctly parsed URI parameter",
419 parsedParameters.getFields());
420 assertEquals("Not correctly parsed URI parameter",
421 1, parsedParameters.getFields().size());
422 assertEquals("Not correctly parsed URI parameter",
423 1, parsedParameters.getFields().get(0).size());
424 assertEquals("Not correctly parsed URI parameter",
425 containerChildQName, parsedParameters.getFields().get(0).iterator().next());
429 * Negative test of parsing request URI parameters when content parameter has not allowed value.
432 public void parseUriParametersContentParameterNegativeTest() {
433 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
434 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
436 parameters.put("content", Collections.singletonList("not-allowed-parameter-value"));
437 when(uriInfo.getQueryParameters()).thenReturn(parameters);
440 ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
441 fail("Test expected to fail due to not allowed parameter value");
442 } catch (final RestconfDocumentedException e) {
444 assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
445 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
446 assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
451 * Negative test of parsing request URI parameters when depth parameter has not allowed value.
454 public void parseUriParametersDepthParameterNegativeTest() {
455 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
456 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
458 // inserted value is not allowed
459 parameters.put("depth", Collections.singletonList("bounded"));
460 when(uriInfo.getQueryParameters()).thenReturn(parameters);
463 ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
464 fail("Test expected to fail due to not allowed parameter value");
465 } catch (final RestconfDocumentedException e) {
467 assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
468 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
469 assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
474 * Negative test of parsing request URI parameters when depth parameter has not allowed value (less than minimum).
477 public void parseUriParametersDepthMinimalParameterNegativeTest() {
478 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
479 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
481 // inserted value is too low
483 "depth", Collections.singletonList(String.valueOf(RestconfDataServiceConstant.ReadData.MIN_DEPTH - 1)));
484 when(uriInfo.getQueryParameters()).thenReturn(parameters);
487 ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
488 fail("Test expected to fail due to not allowed parameter value");
489 } catch (final RestconfDocumentedException e) {
491 assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
492 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
493 assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
498 * Negative test of parsing request URI parameters when depth parameter has not allowed value (more than maximum).
501 public void parseUriParametersDepthMaximalParameterNegativeTest() {
502 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
503 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
505 // inserted value is too high
507 "depth", Collections.singletonList(String.valueOf(RestconfDataServiceConstant.ReadData.MAX_DEPTH + 1)));
508 when(uriInfo.getQueryParameters()).thenReturn(parameters);
511 ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
512 fail("Test expected to fail due to not allowed parameter value");
513 } catch (final RestconfDocumentedException e) {
515 assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
516 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
517 assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
522 * Testing parsing of with-defaults parameter which value doesn't match report-all or report-all-tagged patterns
523 * - non-reporting setting.
526 public void parseUriParametersWithDefaultAndNonTaggedTest() {
527 // preparation of input data
528 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
529 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
530 final String preparedDefaultValue = "sample-default";
531 parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
532 Collections.singletonList(preparedDefaultValue));
533 when(uriInfo.getQueryParameters()).thenReturn(parameters);
535 final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
536 assertEquals(preparedDefaultValue, writerParameters.getWithDefault());
537 assertFalse(writerParameters.isTagged());
541 * Testing parsing of with-defaults parameter which value matches 'report-all-tagged' setting - default value should
542 * be set to {@code null} and tagged flag should be set to {@code true}.
545 public void parseUriParametersWithDefaultAndTaggedTest() {
546 // preparation of input data
547 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
548 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
549 parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
550 Collections.singletonList(ReadData.REPORT_ALL_TAGGED_DEFAULT_VALUE));
551 when(uriInfo.getQueryParameters()).thenReturn(parameters);
553 final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
554 assertNull(writerParameters.getWithDefault());
555 assertTrue(writerParameters.isTagged());
559 * Testing parsing of with-defaults parameter which value matches 'report-all' setting - default value should
560 * be set to {@code null} and tagged flag should be set to {@code false}.
563 public void parseUriParametersWithDefaultAndReportAllTest() {
564 // preparation of input data
565 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
566 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
567 parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
568 Collections.singletonList(ReadData.REPORT_ALL_DEFAULT_VALUE));
569 when(uriInfo.getQueryParameters()).thenReturn(parameters);
571 final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
572 assertNull(writerParameters.getWithDefault());
573 assertFalse(writerParameters.isTagged());