Remove RestconfError.ErrorType
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / ReadDataTransactionUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
9
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.junit.Assert.fail;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
22
23 import com.google.common.collect.ImmutableList;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.Set;
27 import javax.ws.rs.core.MultivaluedHashMap;
28 import javax.ws.rs.core.UriInfo;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
39 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
40 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
41 import org.opendaylight.restconf.common.context.WriterParameters;
42 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
43 import org.opendaylight.restconf.common.errors.RestconfError;
44 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
45 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
46 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
47 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
48 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData;
49 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData.WithDefaults;
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;
64
65 @RunWith(MockitoJUnitRunner.StrictStubs.class)
66 public class ReadDataTransactionUtilTest {
67
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"));
71
72     private RestconfStrategy mdsalStrategy;
73     private RestconfStrategy netconfStrategy;
74     @Mock
75     private NetconfDataTreeService netconfService;
76     @Mock
77     private InstanceIdentifierContext<ContainerSchemaNode> context;
78     @Mock
79     private DOMDataTreeReadTransaction read;
80     @Mock
81     private EffectiveModelContext schemaContext;
82     @Mock
83     private ContainerSchemaNode containerSchemaNode;
84     @Mock
85     private LeafSchemaNode containerChildNode;
86     private QName containerChildQName;
87
88     @Before
89     public void setUp() {
90         containerChildQName = QName.create("ns", "2016-02-28", "container-child");
91
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);
97
98         DOMDataBroker mockDataBroker = mock(DOMDataBroker.class);
99         doReturn(read).when(mockDataBroker).newReadOnlyTransaction();
100         mdsalStrategy = new MdsalRestconfStrategy(mockDataBroker);
101         netconfStrategy = new NetconfRestconfStrategy(this.netconfService);
102     }
103
104     @Test
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(this.netconfService).getConfig(DATA.path);
109         final String valueOfContent = RestconfDataServiceConstant.ReadData.CONFIG;
110         NormalizedNode normalizedNode = readData(valueOfContent, DATA.path, mdsalStrategy);
111         assertEquals(DATA.data3, normalizedNode);
112
113         normalizedNode = readData(valueOfContent, DATA.path, netconfStrategy);
114         assertEquals(DATA.data3, normalizedNode);
115     }
116
117     @Test
118     public void readAllHavingOnlyConfigTest() {
119         doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
120                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
121         doReturn(immediateFluentFuture(Optional.empty())).when(read)
122                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
123         doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path);
124         doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).get(DATA.path);
125         final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
126         NormalizedNode normalizedNode = readData(valueOfContent, DATA.path, mdsalStrategy);
127         assertEquals(DATA.data3, normalizedNode);
128
129         normalizedNode = readData(valueOfContent, DATA.path, netconfStrategy);
130         assertEquals(DATA.data3, normalizedNode);
131     }
132
133     @Test
134     public void readAllHavingOnlyNonConfigTest() {
135         doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
136                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
137         doReturn(immediateFluentFuture(Optional.empty())).when(read)
138                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
139         doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(this.netconfService).get(DATA.path2);
140         doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(DATA.path2);
141         final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
142         NormalizedNode normalizedNode = readData(valueOfContent, DATA.path2, mdsalStrategy);
143         assertEquals(DATA.data2, normalizedNode);
144
145         normalizedNode = readData(valueOfContent, DATA.path2, netconfStrategy);
146         assertEquals(DATA.data2, normalizedNode);
147     }
148
149     @Test
150     public void readDataNonConfigTest() {
151         doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
152                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
153         doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(this.netconfService).get(DATA.path2);
154         final String valueOfContent = RestconfDataServiceConstant.ReadData.NONCONFIG;
155         NormalizedNode normalizedNode = readData(valueOfContent, DATA.path2, mdsalStrategy);
156         assertEquals(DATA.data2, normalizedNode);
157
158         normalizedNode = readData(valueOfContent, DATA.path2, netconfStrategy);
159         assertEquals(DATA.data2, normalizedNode);
160     }
161
162     @Test
163     public void readContainerDataAllTest() {
164         doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
165                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
166         doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
167                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
168         doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path);
169         doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(this.netconfService).get(DATA.path);
170         final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
171         final ContainerNode checkingData = Builders
172                 .containerBuilder()
173                 .withNodeIdentifier(NODE_IDENTIFIER)
174                 .withChild(DATA.contentLeaf)
175                 .withChild(DATA.contentLeaf2)
176                 .build();
177         NormalizedNode normalizedNode = readData(valueOfContent, DATA.path, mdsalStrategy);
178         assertEquals(checkingData, normalizedNode);
179
180         normalizedNode = readData(valueOfContent, DATA.path, netconfStrategy);
181         assertEquals(checkingData, normalizedNode);
182     }
183
184     @Test
185     public void readContainerDataConfigNoValueOfContentTest() {
186         doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
187                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
188         doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
189                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
190         doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(this.netconfService).getConfig(DATA.path);
191         doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(this.netconfService).get(DATA.path);
192         final ContainerNode checkingData = Builders
193                 .containerBuilder()
194                 .withNodeIdentifier(NODE_IDENTIFIER)
195                 .withChild(DATA.contentLeaf)
196                 .withChild(DATA.contentLeaf2)
197                 .build();
198         NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path, mdsalStrategy);
199         assertEquals(checkingData, normalizedNode);
200
201         normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path, netconfStrategy);
202         assertEquals(checkingData, normalizedNode);
203     }
204
205     @Test
206     public void readListDataAllTest() {
207         doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(read)
208                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
209         doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(read)
210                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
211         doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(this.netconfService).get(DATA.path3);
212         doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(this.netconfService).getConfig(DATA.path3);
213         final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
214         final MapNode checkingData = Builders
215                 .mapBuilder()
216                 .withNodeIdentifier(new NodeIdentifier(QName.create("ns", "2016-02-28", "list")))
217                 .withChild(DATA.checkData)
218                 .build();
219         NormalizedNode normalizedNode = readData(valueOfContent, DATA.path3, mdsalStrategy);
220         assertEquals(checkingData, normalizedNode);
221
222         normalizedNode = readData(valueOfContent, DATA.path3, netconfStrategy);
223         assertEquals(checkingData, normalizedNode);
224     }
225
226     @Test
227     public void readOrderedListDataAllTest() {
228         doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(read)
229                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
230         doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(read)
231                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
232         doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(this.netconfService).get(DATA.path3);
233         doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(this.netconfService)
234                 .getConfig(DATA.path3);
235         final MapNode expectedData = Builders.orderedMapBuilder()
236                 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
237                 .withChild(DATA.checkData)
238                 .build();
239         NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path3,
240                 mdsalStrategy);
241         assertEquals(expectedData, normalizedNode);
242
243         normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path3, netconfStrategy);
244         assertEquals(expectedData, normalizedNode);
245     }
246
247     @Test
248     public void readUnkeyedListDataAllTest() {
249         doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(read)
250                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
251         doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(read)
252                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
253         doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(this.netconfService).get(DATA.path3);
254         doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(this.netconfService)
255                 .getConfig(DATA.path3);
256         final UnkeyedListNode expectedData = Builders.unkeyedListBuilder()
257                 .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
258                 .withChild(Builders.unkeyedListEntryBuilder()
259                         .withNodeIdentifier(new NodeIdentifier(DATA.listQname))
260                         .withChild(DATA.unkeyedListEntryNode1.body().iterator().next())
261                         .withChild(DATA.unkeyedListEntryNode2.body().iterator().next()).build()).build();
262         NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path3, mdsalStrategy);
263         assertEquals(expectedData, normalizedNode);
264
265         normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.path3, netconfStrategy);
266         assertEquals(expectedData, normalizedNode);
267     }
268
269     @Test
270     public void readLeafListDataAllTest() {
271         doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(read)
272                 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
273         doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(read)
274                 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
275         doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(this.netconfService)
276                 .get(DATA.leafSetNodePath);
277         doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(this.netconfService)
278                 .getConfig(DATA.leafSetNodePath);
279         final LeafSetNode<String> expectedData = Builders.<String>leafSetBuilder()
280                 .withNodeIdentifier(new NodeIdentifier(DATA.leafListQname))
281                 .withValue(ImmutableList.<LeafSetEntryNode<String>>builder()
282                         .addAll(DATA.leafSetNode1.body())
283                         .addAll(DATA.leafSetNode2.body())
284                         .build())
285                 .build();
286         NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.leafSetNodePath,
287                 mdsalStrategy);
288         assertEquals(expectedData, normalizedNode);
289
290         normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.leafSetNodePath, netconfStrategy);
291         assertEquals(expectedData, normalizedNode);
292     }
293
294     @Test
295     public void readOrderedLeafListDataAllTest() {
296         doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(read)
297                 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
298         doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(read)
299                 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
300         doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(this.netconfService)
301                 .get(DATA.leafSetNodePath);
302         doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(this.netconfService)
303                 .getConfig(DATA.leafSetNodePath);
304         final LeafSetNode<String> expectedData = Builders.<String>orderedLeafSetBuilder()
305                 .withNodeIdentifier(new NodeIdentifier(DATA.leafListQname))
306                 .withValue(ImmutableList.<LeafSetEntryNode<String>>builder()
307                         .addAll(DATA.orderedLeafSetNode1.body())
308                         .addAll(DATA.orderedLeafSetNode2.body())
309                         .build())
310                 .build();
311         NormalizedNode normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.leafSetNodePath,
312                 mdsalStrategy);
313         assertEquals(expectedData, normalizedNode);
314
315         normalizedNode = readData(RestconfDataServiceConstant.ReadData.ALL, DATA.leafSetNodePath, netconfStrategy);
316         assertEquals(expectedData, normalizedNode);
317     }
318
319     @Test
320     public void readDataWrongPathOrNoContentTest() {
321         doReturn(immediateFluentFuture(Optional.empty())).when(read)
322                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
323         doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(DATA.path2);
324         final String valueOfContent = RestconfDataServiceConstant.ReadData.CONFIG;
325         NormalizedNode normalizedNode = readData(valueOfContent, DATA.path2, mdsalStrategy);
326         assertNull(normalizedNode);
327
328         normalizedNode = readData(valueOfContent, DATA.path2, netconfStrategy);
329         assertNull(normalizedNode);
330     }
331
332     @Test(expected = RestconfDocumentedException.class)
333     public void readDataFailTest() {
334         final String valueOfContent = "nonsense";
335         NormalizedNode normalizedNode = readData(valueOfContent, null, mdsalStrategy);
336         assertNull(normalizedNode);
337
338         normalizedNode = readData(valueOfContent, null, netconfStrategy);
339         assertNull(normalizedNode);
340     }
341
342     /**
343      * Test of parsing default parameters from URI request.
344      */
345     @Test
346     public void parseUriParametersDefaultTest() {
347         final UriInfo uriInfo = mock(UriInfo.class);
348         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
349
350         // no parameters, default values should be used
351         when(uriInfo.getQueryParameters()).thenReturn(parameters);
352
353         final WriterParameters parsedParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
354
355         assertEquals("Not correctly parsed URI parameter",
356                 RestconfDataServiceConstant.ReadData.ALL, parsedParameters.getContent());
357         assertNull("Not correctly parsed URI parameter",
358                 parsedParameters.getDepth());
359         assertNull("Not correctly parsed URI parameter",
360                 parsedParameters.getFields());
361     }
362
363     /**
364      * Test of parsing user defined parameters from URI request.
365      */
366     @Test
367     public void parseUriParametersUserDefinedTest() {
368         final UriInfo uriInfo = mock(UriInfo.class);
369         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
370
371         final String content = "config";
372         final String depth = "10";
373         final String fields = containerChildQName.getLocalName();
374
375         parameters.put("content", List.of(content));
376         parameters.put("depth", List.of(depth));
377         parameters.put("fields", List.of(fields));
378
379         when(uriInfo.getQueryParameters()).thenReturn(parameters);
380
381         final WriterParameters parsedParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
382
383         // content
384         assertEquals("Not correctly parsed URI parameter",
385                 content, parsedParameters.getContent());
386
387         // depth
388         assertNotNull("Not correctly parsed URI parameter",
389                 parsedParameters.getDepth());
390         assertEquals("Not correctly parsed URI parameter",
391                 depth, parsedParameters.getDepth().toString());
392
393         // fields
394         assertNotNull("Not correctly parsed URI parameter",
395                 parsedParameters.getFields());
396         assertEquals("Not correctly parsed URI parameter",
397                 1, parsedParameters.getFields().size());
398         assertEquals("Not correctly parsed URI parameter",
399                 1, parsedParameters.getFields().get(0).size());
400         assertEquals("Not correctly parsed URI parameter",
401                 containerChildQName, parsedParameters.getFields().get(0).iterator().next());
402     }
403
404     /**
405      * Negative test of parsing request URI parameters when content parameter has not allowed value.
406      */
407     @Test
408     public void parseUriParametersContentParameterNegativeTest() {
409         final UriInfo uriInfo = mock(UriInfo.class);
410         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
411
412         parameters.put("content", List.of("not-allowed-parameter-value"));
413         when(uriInfo.getQueryParameters()).thenReturn(parameters);
414
415         try {
416             ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
417             fail("Test expected to fail due to not allowed parameter value");
418         } catch (final RestconfDocumentedException e) {
419             // Bad request
420             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
421             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
422             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
423         }
424     }
425
426     /**
427      * Negative test of parsing request URI parameters when depth parameter has not allowed value.
428      */
429     @Test
430     public void parseUriParametersDepthParameterNegativeTest() {
431         final UriInfo uriInfo = mock(UriInfo.class);
432         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
433
434         // inserted value is not allowed
435         parameters.put("depth", List.of("bounded"));
436         when(uriInfo.getQueryParameters()).thenReturn(parameters);
437
438         try {
439             ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
440             fail("Test expected to fail due to not allowed parameter value");
441         } catch (final RestconfDocumentedException e) {
442             // Bad request
443             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
444             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
445             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
446         }
447     }
448
449     /**
450      * Negative test of parsing request URI parameters when depth parameter has not allowed value (less than minimum).
451      */
452     @Test
453     public void parseUriParametersDepthMinimalParameterNegativeTest() {
454         final UriInfo uriInfo = mock(UriInfo.class);
455         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
456
457         // inserted value is too low
458         parameters.put(
459                 "depth", List.of(String.valueOf(RestconfDataServiceConstant.ReadData.MIN_DEPTH - 1)));
460         when(uriInfo.getQueryParameters()).thenReturn(parameters);
461
462         try {
463             ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
464             fail("Test expected to fail due to not allowed parameter value");
465         } catch (final RestconfDocumentedException e) {
466             // Bad request
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());
470         }
471     }
472
473     /**
474      * Negative test of parsing request URI parameters when depth parameter has not allowed value (more than maximum).
475      */
476     @Test
477     public void parseUriParametersDepthMaximalParameterNegativeTest() {
478         final UriInfo uriInfo = mock(UriInfo.class);
479         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
480
481         // inserted value is too high
482         parameters.put(
483                 "depth", List.of(String.valueOf(RestconfDataServiceConstant.ReadData.MAX_DEPTH + 1)));
484         when(uriInfo.getQueryParameters()).thenReturn(parameters);
485
486         try {
487             ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
488             fail("Test expected to fail due to not allowed parameter value");
489         } catch (final RestconfDocumentedException e) {
490             // Bad request
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());
494         }
495     }
496
497     /**
498      * Testing parsing of with-defaults parameter which value doesn't match report-all or report-all-tagged patterns
499      * - non-reporting setting.
500      */
501     @Test
502     public void parseUriParametersWithDefaultAndNonTaggedTest() {
503         // preparation of input data
504         final UriInfo uriInfo = mock(UriInfo.class);
505         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
506         parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS, List.of("explicit"));
507         when(uriInfo.getQueryParameters()).thenReturn(parameters);
508
509         final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
510         assertSame(WithDefaults.EXPLICIT.value(), writerParameters.getWithDefault());
511         assertFalse(writerParameters.isTagged());
512     }
513
514     /**
515      * Testing parsing of with-defaults parameter which value which is not supported.
516      */
517     @Test
518     public void parseUriParametersWithDefaultInvalidTest() {
519         // preparation of input data
520         final UriInfo uriInfo = mock(UriInfo.class);
521         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
522         parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS, List.of("invalid"));
523         when(uriInfo.getQueryParameters()).thenReturn(parameters);
524
525         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
526             () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
527         final List<RestconfError> errors = ex.getErrors();
528         assertEquals(1, errors.size());
529         assertEquals(ErrorTag.INVALID_VALUE, errors.get(0).getErrorTag());
530     }
531
532     /**
533      * Testing parsing of with-defaults parameter which value matches 'report-all-tagged' setting - default value should
534      * be set to {@code null} and tagged flag should be set to {@code true}.
535      */
536     @Test
537     public void parseUriParametersWithDefaultAndTaggedTest() {
538         // preparation of input data
539         final UriInfo uriInfo = mock(UriInfo.class);
540         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
541         parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
542                 List.of(ReadData.WithDefaults.REPORT_ALL_TAGGED.value()));
543         when(uriInfo.getQueryParameters()).thenReturn(parameters);
544
545         final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
546         assertNull(writerParameters.getWithDefault());
547         assertTrue(writerParameters.isTagged());
548     }
549
550     /**
551      * Testing parsing of with-defaults parameter which value matches 'report-all' setting - default value should
552      * be set to {@code null} and tagged flag should be set to {@code false}.
553      */
554     @Test
555     public void parseUriParametersWithDefaultAndReportAllTest() {
556         // preparation of input data
557         final UriInfo uriInfo = mock(UriInfo.class);
558         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
559         parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
560                 List.of(ReadData.WithDefaults.REPORT_ALL.value()));
561         when(uriInfo.getQueryParameters()).thenReturn(parameters);
562
563         final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
564         assertNull(writerParameters.getWithDefault());
565         assertFalse(writerParameters.isTagged());
566     }
567
568     /**
569      * Test when parameter is present at most once.
570      */
571     @Test
572     public void checkParameterCountTest() {
573         ReadDataTransactionUtil.checkParameterCount(List.of("all"), RestconfDataServiceConstant.ReadData.CONTENT);
574     }
575
576     /**
577      * Test when parameter is present more than once.
578      */
579     @Test
580     public void checkParameterCountNegativeTest() {
581         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
582             () -> ReadDataTransactionUtil.checkParameterCount(List.of("config", "nonconfig", "all"),
583                     RestconfDataServiceConstant.ReadData.CONTENT));
584         final List<RestconfError> errors = ex.getErrors();
585         assertEquals(1, errors.size());
586
587         final RestconfError error = errors.get(0);
588         assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
589         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
590         assertEquals("Error status code is not correct", 400, error.getErrorTag().getStatusCode());
591     }
592
593
594     /**
595      * Test when all parameters are allowed.
596      */
597     @Test
598     public void checkParametersTypesTest() {
599         ReadDataTransactionUtil.checkParametersTypes(Set.of("content"),
600             Set.of(RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH));
601     }
602
603     /**
604      * Test when not allowed parameter type is used.
605      */
606     @Test
607     public void checkParametersTypesNegativeTest() {
608         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
609             () -> ReadDataTransactionUtil.checkParametersTypes(Set.of("not-allowed-parameter"),
610                 Set.of(RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH)));
611         final List<RestconfError> errors = ex.getErrors();
612         assertEquals(1, errors.size());
613
614         final RestconfError error = errors.get(0);
615         assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
616         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
617         assertEquals("Error status code is not correct", 400, error.getErrorTag().getStatusCode());
618     }
619
620     /**
621      * Read specific type of data from data store via transaction.
622      *
623      * @param valueOfContent type of data to read (config, state, all)
624      * @param strategy       {@link RestconfStrategy} - wrapper for variables
625      * @return {@link NormalizedNode}
626      */
627     private @Nullable NormalizedNode readData(final @NonNull String valueOfContent,
628             final YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy) {
629         return ReadDataTransactionUtil.readData(valueOfContent, path, strategy, null, schemaContext);
630     }
631 }