Remove RestconfError.ErrorTag
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / utils / parser / ParserFieldsParameterTest.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.utils.parser;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.when;
16
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.Set;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
26 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
27 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
28 import org.opendaylight.yangtools.yang.common.ErrorTag;
29 import org.opendaylight.yangtools.yang.common.ErrorType;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.common.XMLNamespace;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
38 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
41 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45
46 /**
47  * Unit test for {@link ParserFieldsParameter}.
48  */
49 @RunWith(MockitoJUnitRunner.class)
50 public class ParserFieldsParameterTest {
51
52     @Mock
53     private InstanceIdentifierContext<ContainerSchemaNode> identifierJukebox;
54
55     @Mock
56     private InstanceIdentifierContext<ContainerSchemaNode> identifierTestServices;
57
58     private static final QNameModule Q_NAME_MODULE_JUKEBOX = QNameModule.create(
59         XMLNamespace.of("http://example.com/ns/example-jukebox"), Revision.of("2015-04-04"));
60     private static final QNameModule Q_NAME_MODULE_TEST_SERVICES = QNameModule.create(
61         XMLNamespace.of("tests:test-services"), Revision.of("2019-03-25"));
62     private static final QNameModule Q_NAME_MODULE_AUGMENTED_JUKEBOX = QNameModule.create(
63         XMLNamespace.of("http://example.com/ns/augmented-jukebox"), Revision.of("2016-05-05"));
64
65     // container jukebox
66     @Mock
67     private ContainerSchemaNode containerJukebox;
68     private static final QName JUKEBOX_Q_NAME = QName.create(Q_NAME_MODULE_JUKEBOX, "jukebox");
69
70     // container player
71     @Mock
72     private ContainerSchemaNode containerPlayer;
73     private static final QName PLAYER_Q_NAME = QName.create(Q_NAME_MODULE_JUKEBOX, "player");
74
75     // container library
76     @Mock
77     private ContainerSchemaNode containerLibrary;
78     private static final QName LIBRARY_Q_NAME = QName.create(Q_NAME_MODULE_JUKEBOX, "library");
79
80     // container augmented library
81     @Mock
82     private ContainerSchemaNode augmentedContainerLibrary;
83     private static final QName AUGMENTED_LIBRARY_Q_NAME = QName.create(Q_NAME_MODULE_AUGMENTED_JUKEBOX,
84             "augmented-library");
85
86     // augmentation that contains speed leaf
87     @Mock
88     private AugmentationSchemaNode speedAugmentation;
89
90     // leaf speed
91     @Mock
92     private LeafSchemaNode leafSpeed;
93     private static final QName SPEED_Q_NAME = QName.create(Q_NAME_MODULE_AUGMENTED_JUKEBOX, "speed");
94
95     // list album
96     @Mock
97     private ListSchemaNode listAlbum;
98     private static final QName ALBUM_Q_NAME = QName.create(Q_NAME_MODULE_JUKEBOX, "album");
99
100     // leaf name
101     @Mock
102     private LeafSchemaNode leafName;
103     private static final QName NAME_Q_NAME = QName.create(Q_NAME_MODULE_JUKEBOX, "name");
104
105     // container test data
106     @Mock
107     private ContainerSchemaNode containerTestData;
108     private static final QName TEST_DATA_Q_NAME = QName.create(Q_NAME_MODULE_TEST_SERVICES, "test-data");
109
110     // list services
111     @Mock
112     private ListSchemaNode listServices;
113     private static final QName SERVICES_Q_NAME = QName.create(Q_NAME_MODULE_TEST_SERVICES, "services");
114
115     // leaf type-of-service
116     @Mock
117     private LeafSchemaNode leafTypeOfService;
118     private static final QName TYPE_OF_SERVICE_Q_NAME = QName.create(Q_NAME_MODULE_TEST_SERVICES, "type-of-service");
119
120     // list instance
121     @Mock
122     private ListSchemaNode listInstance;
123     private static final QName INSTANCE_Q_NAME = QName.create(Q_NAME_MODULE_TEST_SERVICES, "instance");
124
125     // leaf instance-name
126     @Mock
127     private LeafSchemaNode leafInstanceName;
128     private static final QName INSTANCE_NAME_Q_NAME = QName.create(Q_NAME_MODULE_TEST_SERVICES, "instance-name");
129
130     // leaf provider
131     @Mock
132     private LeafSchemaNode leafProvider;
133     private static final QName PROVIDER_Q_NAME = QName.create(Q_NAME_MODULE_TEST_SERVICES, "provider");
134
135     // container next-data
136     @Mock
137     private ContainerSchemaNode containerNextData;
138     private static final QName NEXT_DATA_Q_NAME = QName.create(Q_NAME_MODULE_TEST_SERVICES, "next-data");
139
140     // leaf next-service
141     @Mock
142     private LeafSchemaNode leafNextService;
143     private static final QName NEXT_SERVICE_Q_NAME = QName.create(Q_NAME_MODULE_TEST_SERVICES, "next-service");
144
145     // leaf-list protocols
146     @Mock
147     private LeafListSchemaNode leafListProtocols;
148     private static final QName PROTOCOLS_Q_NAME = QName.create(Q_NAME_MODULE_TEST_SERVICES, "protocols");
149
150     @Before
151     public void setUp() throws Exception {
152         final EffectiveModelContext schemaContextJukebox =
153                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/jukebox"));
154         initJukeboxSchemaNodes(schemaContextJukebox);
155
156         final EffectiveModelContext schemaContextTestServices =
157                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/test-services"));
158         initTestServicesSchemaNodes(schemaContextTestServices);
159     }
160
161     private void initJukeboxSchemaNodes(final EffectiveModelContext schemaContext) {
162         when(identifierJukebox.getSchemaContext()).thenReturn(schemaContext);
163         when(containerJukebox.getQName()).thenReturn(JUKEBOX_Q_NAME);
164         when(identifierJukebox.getSchemaNode()).thenReturn(containerJukebox);
165
166         when(containerLibrary.getQName()).thenReturn(LIBRARY_Q_NAME);
167         when(containerJukebox.dataChildByName(LIBRARY_Q_NAME)).thenReturn(containerLibrary);
168
169         when(augmentedContainerLibrary.getQName()).thenReturn(AUGMENTED_LIBRARY_Q_NAME);
170         when(containerJukebox.dataChildByName(AUGMENTED_LIBRARY_Q_NAME))
171                 .thenReturn(augmentedContainerLibrary);
172
173         when(containerPlayer.getQName()).thenReturn(PLAYER_Q_NAME);
174         when(containerJukebox.dataChildByName(PLAYER_Q_NAME)).thenReturn(containerPlayer);
175
176         when(listAlbum.getQName()).thenReturn(ALBUM_Q_NAME);
177         when(containerLibrary.dataChildByName(ALBUM_Q_NAME)).thenReturn(listAlbum);
178
179         when(leafName.getQName()).thenReturn(NAME_Q_NAME);
180         when(listAlbum.dataChildByName(NAME_Q_NAME)).thenReturn(leafName);
181
182         when(leafSpeed.getQName()).thenReturn(SPEED_Q_NAME);
183         when(leafSpeed.isAugmenting()).thenReturn(true);
184         when(containerPlayer.dataChildByName(SPEED_Q_NAME)).thenReturn(leafSpeed);
185         when(containerPlayer.getDataChildByName(SPEED_Q_NAME)).thenReturn(leafSpeed);
186         doReturn(List.of(leafSpeed)).when(speedAugmentation).getChildNodes();
187         doReturn(List.of(speedAugmentation)).when(containerPlayer).getAvailableAugmentations();
188         when(speedAugmentation.findDataChildByName(SPEED_Q_NAME)).thenReturn(Optional.of(leafSpeed));
189     }
190
191     private void initTestServicesSchemaNodes(final EffectiveModelContext schemaContext) {
192         when(identifierTestServices.getSchemaContext()).thenReturn(schemaContext);
193         when(containerTestData.getQName()).thenReturn(TEST_DATA_Q_NAME);
194         when(identifierTestServices.getSchemaNode()).thenReturn(containerTestData);
195
196         when(listServices.getQName()).thenReturn(SERVICES_Q_NAME);
197         when(containerTestData.dataChildByName(SERVICES_Q_NAME)).thenReturn(listServices);
198
199         when(leafListProtocols.getQName()).thenReturn(PROTOCOLS_Q_NAME);
200         when(containerTestData.dataChildByName(PROTOCOLS_Q_NAME)).thenReturn(leafListProtocols);
201
202         when(leafTypeOfService.getQName()).thenReturn(TYPE_OF_SERVICE_Q_NAME);
203         when(listServices.dataChildByName(TYPE_OF_SERVICE_Q_NAME)).thenReturn(leafTypeOfService);
204
205         when(listInstance.getQName()).thenReturn(INSTANCE_Q_NAME);
206         when(listServices.dataChildByName(INSTANCE_Q_NAME)).thenReturn(listInstance);
207
208         when(leafInstanceName.getQName()).thenReturn(INSTANCE_NAME_Q_NAME);
209         when(listInstance.dataChildByName(INSTANCE_NAME_Q_NAME)).thenReturn(leafInstanceName);
210
211         when(leafProvider.getQName()).thenReturn(PROVIDER_Q_NAME);
212         when(listInstance.dataChildByName(PROVIDER_Q_NAME)).thenReturn(leafProvider);
213
214         when(containerNextData.getQName()).thenReturn(NEXT_DATA_Q_NAME);
215         when(listServices.dataChildByName(NEXT_DATA_Q_NAME)).thenReturn(containerNextData);
216
217         when(leafNextService.getQName()).thenReturn(NEXT_SERVICE_Q_NAME);
218         when(containerNextData.dataChildByName(NEXT_SERVICE_Q_NAME)).thenReturn(leafNextService);
219     }
220
221     /**
222      * Test parse fields parameter containing only one child selected.
223      */
224     @Test
225     public void parseFieldsParameterSimplePathTest() {
226         final String input = "library";
227         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierJukebox, input);
228
229         assertNotNull(parsedFields);
230         assertEquals(1, parsedFields.size());
231         assertEquals(1, parsedFields.get(0).size());
232         assertTrue(parsedFields.get(0).contains(LIBRARY_Q_NAME));
233     }
234
235     /**
236      * Test parse fields parameter containing two child nodes selected.
237      */
238     @Test
239     public void parseFieldsParameterDoublePathTest() {
240         final String input = "library;player";
241         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierJukebox, input);
242
243         assertNotNull(parsedFields);
244         assertEquals(1, parsedFields.size());
245         assertEquals(2, parsedFields.get(0).size());
246         assertTrue(parsedFields.get(0).contains(LIBRARY_Q_NAME));
247         assertTrue(parsedFields.get(0).contains(PLAYER_Q_NAME));
248     }
249
250     /**
251      * Test parse fields parameter containing sub-children selected delimited by slash.
252      */
253     @Test
254     public void parseFieldsParameterSubPathTest() {
255         final String input = "library/album/name";
256         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierJukebox, input);
257
258         assertNotNull(parsedFields);
259         assertEquals(3, parsedFields.size());
260
261         assertEquals(1, parsedFields.get(0).size());
262         assertTrue(parsedFields.get(0).contains(LIBRARY_Q_NAME));
263
264         assertEquals(1, parsedFields.get(1).size());
265         assertTrue(parsedFields.get(1).contains(ALBUM_Q_NAME));
266
267         assertEquals(1, parsedFields.get(2).size());
268         assertTrue(parsedFields.get(2).contains(NAME_Q_NAME));
269     }
270
271     /**
272      * Test parse fields parameter containing sub-children selected delimited by parenthesis.
273      */
274     @Test
275     public void parseFieldsParameterChildrenPathTest() {
276         final String input = "library(album(name))";
277         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierJukebox, input);
278
279         assertNotNull(parsedFields);
280         assertEquals(3, parsedFields.size());
281
282         assertEquals(1, parsedFields.get(0).size());
283         assertTrue(parsedFields.get(0).contains(LIBRARY_Q_NAME));
284
285         assertEquals(1, parsedFields.get(1).size());
286         assertTrue(parsedFields.get(1).contains(ALBUM_Q_NAME));
287
288         assertEquals(1, parsedFields.get(2).size());
289         assertTrue(parsedFields.get(2).contains(NAME_Q_NAME));
290     }
291
292     /**
293      * Test parse fields parameter when augmentation with different namespace is used.
294      */
295     @Test
296     public void parseFieldsParameterNamespaceTest() {
297         final String input = "augmented-jukebox:augmented-library";
298         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierJukebox, input);
299
300         assertNotNull(parsedFields);
301         assertEquals(1, parsedFields.size());
302
303         assertEquals(1, parsedFields.get(0).size());
304         assertTrue(parsedFields.get(0).contains(AUGMENTED_LIBRARY_Q_NAME));
305     }
306
307     /**
308      * Testing of fields parameter parsing when multiple nodes are wrapped in brackets and these nodes are not
309      * direct children of parent node - multiple children which are constructed using '/'.
310      */
311     @Test
312     public void parseFieldsParameterWithMultipleChildrenTest1() {
313         final String input = "services(type-of-service;instance/instance-name;instance/provider)";
314         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierTestServices, input);
315
316         assertNotNull(parsedFields);
317         assertEquals(parsedFields.size(), 3);
318
319         assertEquals(parsedFields.get(0).size(), 1);
320         assertTrue(parsedFields.get(0).contains(SERVICES_Q_NAME));
321
322         assertEquals(parsedFields.get(1).size(), 2);
323         assertTrue(parsedFields.get(1).containsAll(List.of(TYPE_OF_SERVICE_Q_NAME, INSTANCE_Q_NAME)));
324
325         assertEquals(parsedFields.get(2).size(), 2);
326         assertTrue(parsedFields.get(2).containsAll(List.of(INSTANCE_NAME_Q_NAME, PROVIDER_Q_NAME)));
327     }
328
329     /**
330      * Testing of fields parameter parsing when multiple nodes are wrapped in brackets and these nodes are not
331      * direct children of parent node - one of children nodes is typed using brackets, other is constructed using '/'.
332      */
333     @Test
334     public void parseFieldsParameterWithMultipleChildrenTest2() {
335         final String input = "services(type-of-service;instance(instance-name;provider))";
336         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierTestServices, input);
337
338         assertNotNull(parsedFields);
339         assertEquals(parsedFields.size(), 3);
340
341         assertEquals(parsedFields.get(0).size(), 1);
342         assertTrue(parsedFields.get(0).contains(SERVICES_Q_NAME));
343
344         assertEquals(parsedFields.get(1).size(), 2);
345         assertTrue(parsedFields.get(1).containsAll(List.of(TYPE_OF_SERVICE_Q_NAME, INSTANCE_Q_NAME)));
346
347         assertEquals(parsedFields.get(2).size(), 2);
348         assertTrue(parsedFields.get(2).containsAll(List.of(INSTANCE_NAME_Q_NAME, PROVIDER_Q_NAME)));
349     }
350
351     /**
352      * Testing of fields parameter parsing when multiple nodes are wrapped in brackets and these nodes are not
353      * direct children of parent node - multiple children with different parent nodes.
354      */
355     @Test
356     public void parseFieldsParameterWithMultipleChildrenTest3() {
357         final String input = "services(instance/instance-name;type-of-service;next-data/next-service)";
358         final List<Set<QName>> parsedFields = ParserFieldsParameter.parseFieldsParameter(identifierTestServices, input);
359
360         assertNotNull(parsedFields);
361         assertEquals(parsedFields.size(), 3);
362
363         assertEquals(parsedFields.get(0).size(), 1);
364         assertTrue(parsedFields.get(0).contains(SERVICES_Q_NAME));
365
366         assertEquals(parsedFields.get(1).size(), 3);
367         assertTrue(parsedFields.get(1).containsAll(
368                 List.of(TYPE_OF_SERVICE_Q_NAME, INSTANCE_Q_NAME, NEXT_DATA_Q_NAME)));
369
370         assertEquals(parsedFields.get(2).size(), 2);
371         assertTrue(parsedFields.get(2).containsAll(
372                 List.of(INSTANCE_NAME_Q_NAME, NEXT_SERVICE_Q_NAME)));
373     }
374
375     /**
376      * Test parse fields parameter containing not expected character.
377      */
378     @Test
379     public void parseFieldsParameterNotExpectedCharacterNegativeTest() {
380         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
381             () -> ParserFieldsParameter.parseFieldsParameter(identifierJukebox, "*"));
382         // Bad request
383         assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
384         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
385     }
386
387     /**
388      * Test parse fields parameter with missing closing parenthesis.
389      */
390     @Test
391     public void parseFieldsParameterMissingParenthesisNegativeTest() {
392         final String input = "library(";
393
394         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
395             () -> ParserFieldsParameter.parseFieldsParameter(identifierJukebox, input));
396         // Bad request
397         assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
398         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
399     }
400
401     /**
402      * Test parse fields parameter when not existing child node selected.
403      */
404     @Test
405     public void parseFieldsParameterMissingChildNodeNegativeTest() {
406         final String input = "library(not-existing)";
407
408         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
409             () -> ParserFieldsParameter.parseFieldsParameter(identifierJukebox, input));
410         // Bad request
411         assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
412         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
413     }
414
415     /**
416      * Test parse fields parameter with unexpected character after parenthesis.
417      */
418     @Test
419     public void parseFieldsParameterAfterParenthesisNegativeTest() {
420         final String input = "library(album);";
421
422         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
423             () -> ParserFieldsParameter.parseFieldsParameter(identifierJukebox, input));
424         // Bad request
425         assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
426         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
427     }
428
429     /**
430      * Test parse fields parameter with missing semicolon after parenthesis.
431      */
432     @Test
433     public void parseFieldsParameterMissingSemicolonNegativeTest() {
434         final String input = "library(album)player";
435
436         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
437             () -> ParserFieldsParameter.parseFieldsParameter(this.identifierJukebox, input));
438         // Bad request
439         assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
440         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
441     }
442
443     @Test
444     public void parseTopLevelContainerToPathTest() {
445         final String input = "library";
446         final List<YangInstanceIdentifier> parsedFields = ParserFieldsParameter.parseFieldsPaths(
447                 identifierJukebox, input);
448
449         assertNotNull(parsedFields);
450         assertEquals(1, parsedFields.size());
451         final List<PathArgument> pathArguments = parsedFields.get(0).getPathArguments();
452         assertEquals(1, pathArguments.size());
453         assertEquals(LIBRARY_Q_NAME, pathArguments.get(0).getNodeType());
454     }
455
456     @Test
457     public void parseTwoTopLevelContainersToPathsTest() {
458         final String input = "library;player";
459         final List<YangInstanceIdentifier> parsedFields = ParserFieldsParameter.parseFieldsPaths(
460                 identifierJukebox, input);
461
462         assertNotNull(parsedFields);
463         assertEquals(2, parsedFields.size());
464
465         final Optional<YangInstanceIdentifier> libraryPath = findPath(parsedFields, LIBRARY_Q_NAME);
466         assertTrue(libraryPath.isPresent());
467         assertEquals(1, libraryPath.get().getPathArguments().size());
468
469         final Optional<YangInstanceIdentifier> playerPath = findPath(parsedFields, PLAYER_Q_NAME);
470         assertTrue(playerPath.isPresent());
471         assertEquals(1, libraryPath.get().getPathArguments().size());
472     }
473
474     @Test
475     public void parseNestedLeafToPathTest() {
476         final String input = "library/album/name";
477         final List<YangInstanceIdentifier> parsedFields = ParserFieldsParameter.parseFieldsPaths(
478                 identifierJukebox, input);
479
480         assertEquals(1, parsedFields.size());
481         final List<PathArgument> pathArguments = parsedFields.get(0).getPathArguments();
482         assertEquals(3, pathArguments.size());
483
484         assertEquals(LIBRARY_Q_NAME, pathArguments.get(0).getNodeType());
485         assertEquals(ALBUM_Q_NAME, pathArguments.get(1).getNodeType());
486         assertEquals(NAME_Q_NAME, pathArguments.get(2).getNodeType());
487     }
488
489     @Test
490     public void parseAugmentedLeafToPathTest() {
491         final String input = "player/augmented-jukebox:speed";
492         final List<YangInstanceIdentifier> parsedFields = ParserFieldsParameter.parseFieldsPaths(
493                 identifierJukebox, input);
494
495         assertEquals(1, parsedFields.size());
496         final List<PathArgument> pathArguments = parsedFields.get(0).getPathArguments();
497
498         assertEquals(3, pathArguments.size());
499         assertEquals(PLAYER_Q_NAME, pathArguments.get(0).getNodeType());
500         assertTrue(pathArguments.get(1) instanceof AugmentationIdentifier);
501         assertEquals(SPEED_Q_NAME, pathArguments.get(2).getNodeType());
502     }
503
504     @Test
505     public void parseMultipleFieldsOnDifferentLevelsToPathsTest() {
506         final String input = "services(type-of-service;instance/instance-name;instance/provider)";
507         final List<YangInstanceIdentifier> parsedFields = ParserFieldsParameter.parseFieldsPaths(
508                 identifierTestServices, input);
509
510         assertEquals(3, parsedFields.size());
511
512         final Optional<YangInstanceIdentifier> tosPath = findPath(parsedFields, TYPE_OF_SERVICE_Q_NAME);
513         assertTrue(tosPath.isPresent());
514         assertEquals(2, tosPath.get().getPathArguments().size());
515
516         final Optional<YangInstanceIdentifier> instanceNamePath = findPath(parsedFields, INSTANCE_NAME_Q_NAME);
517         assertTrue(instanceNamePath.isPresent());
518         assertEquals(3, instanceNamePath.get().getPathArguments().size());
519
520         final Optional<YangInstanceIdentifier> providerPath = findPath(parsedFields, PROVIDER_Q_NAME);
521         assertTrue(providerPath.isPresent());
522         assertEquals(3, providerPath.get().getPathArguments().size());
523     }
524
525     @Test
526     public void parseListFieldUnderListToPathTest() {
527         final String input = "services/instance";
528         final List<YangInstanceIdentifier> parsedFields = ParserFieldsParameter.parseFieldsPaths(
529                 identifierTestServices, input);
530
531         assertEquals(1, parsedFields.size());
532         final List<PathArgument> pathArguments = parsedFields.get(0).getPathArguments();
533         assertEquals(2, pathArguments.size());
534
535         assertEquals(SERVICES_Q_NAME, pathArguments.get(0).getNodeType());
536         assertTrue(pathArguments.get(0) instanceof NodeIdentifier);
537         assertEquals(INSTANCE_Q_NAME, pathArguments.get(1).getNodeType());
538         assertTrue(pathArguments.get(1) instanceof NodeIdentifier);
539     }
540
541     @Test
542     public void parseLeafListFieldToPathTest() {
543         final String input = "protocols";
544         final List<YangInstanceIdentifier> parsedFields = ParserFieldsParameter.parseFieldsPaths(
545                 identifierTestServices, input);
546
547         assertEquals(1, parsedFields.size());
548         final List<PathArgument> pathArguments = parsedFields.get(0).getPathArguments();
549         assertEquals(1, pathArguments.size());
550         assertTrue(pathArguments.get(0) instanceof NodeIdentifier);
551         assertEquals(PROTOCOLS_Q_NAME, pathArguments.get(0).getNodeType());
552     }
553
554     private static Optional<YangInstanceIdentifier> findPath(final List<YangInstanceIdentifier> paths,
555                                                              final QName lastPathArg) {
556         return paths.stream()
557                 .filter(path -> lastPathArg.equals(path.getLastPathArgument().getNodeType()))
558                 .findAny();
559     }
560 }