137ae44c8ea78136353e7dbac5fc1d75fb0e9a66
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / utils / parser / NetconfFieldsTranslatorTest.java
1 /*
2  * Copyright © 2020 FRINX s.r.o. and others.  All rights reserved.
3  * Copyright © 2021 PANTHEON.tech, s.r.o.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
10
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14
15 import java.util.List;
16 import org.junit.runner.RunWith;
17 import org.mockito.junit.MockitoJUnitRunner;
18 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
19 import org.opendaylight.restconf.nb.rfc8040.FieldsParam;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25
26 /**
27  * Unit test for {@link NetconfFieldsTranslator}.
28  */
29 @RunWith(MockitoJUnitRunner.class)
30 public class NetconfFieldsTranslatorTest extends AbstractFieldsTranslatorTest<YangInstanceIdentifier> {
31     @Override
32     protected List<YangInstanceIdentifier> translateFields(final InstanceIdentifierContext context,
33             final FieldsParam fields) {
34         return NetconfFieldsTranslator.translate(context, fields);
35     }
36
37     @Override
38     protected void assertSimplePath(final List<YangInstanceIdentifier> result) {
39         assertEquals(1, result.size());
40         final var pathArguments = result.get(0).getPathArguments();
41         assertEquals(1, pathArguments.size());
42         assertEquals(LIBRARY_Q_NAME, pathArguments.get(0).getNodeType());
43     }
44
45     @Override
46     protected void assertDoublePath(final List<YangInstanceIdentifier> result) {
47         assertEquals(2, result.size());
48
49         final var libraryPath = assertPath(result, LIBRARY_Q_NAME);
50         assertEquals(1, libraryPath.getPathArguments().size());
51
52         final var playerPath = assertPath(result, PLAYER_Q_NAME);
53         assertEquals(1, playerPath.getPathArguments().size());
54     }
55
56     @Override
57     protected void assertSubPath(final List<YangInstanceIdentifier> result) {
58         // FIXME: NETCONF-820: add assertions
59     }
60
61     @Override
62     protected void assertChildrenPath(final List<YangInstanceIdentifier> result) {
63         assertEquals(1, result.size());
64         final var pathArguments = result.get(0).getPathArguments();
65         assertEquals(4, pathArguments.size());
66         assertEquals(LIBRARY_Q_NAME, pathArguments.get(0).getNodeType());
67         assertEquals(ARTIST_Q_NAME, pathArguments.get(1).getNodeType());
68         assertEquals(ALBUM_Q_NAME, pathArguments.get(2).getNodeType());
69         assertEquals(NAME_Q_NAME, pathArguments.get(3).getNodeType());
70     }
71
72     @Override
73     protected void assertNamespace(final List<YangInstanceIdentifier> result) {
74         // FIXME: add assertions
75     }
76
77     @Override
78     protected void assertMultipleChildren1(final List<YangInstanceIdentifier> result) {
79         assertEquals(3, result.size());
80
81         final var tosPath = assertPath(result, TYPE_OF_SERVICE_Q_NAME);
82         assertEquals(2, tosPath.getPathArguments().size());
83
84         final var instanceNamePath = assertPath(result, INSTANCE_NAME_Q_NAME);
85         assertEquals(3, instanceNamePath.getPathArguments().size());
86
87         final var providerPath = assertPath(result, PROVIDER_Q_NAME);
88         assertEquals(3, providerPath.getPathArguments().size());
89     }
90
91     @Override
92     protected void assertMultipleChildren2(final List<YangInstanceIdentifier> result) {
93         // FIXME: add assertions
94     }
95
96     @Override
97     protected void assertMultipleChildren3(final List<YangInstanceIdentifier> result) {
98         // FIXME: add assertions
99     }
100
101     @Override
102     protected void assertAugmentedChild(final List<YangInstanceIdentifier> result) {
103         assertEquals(1, result.size());
104         final var pathArguments = result.get(0).getPathArguments();
105
106         assertEquals(3, pathArguments.size());
107         assertEquals(PLAYER_Q_NAME, pathArguments.get(0).getNodeType());
108         assertThat(pathArguments.get(1), instanceOf(AugmentationIdentifier.class));
109         assertEquals(SPEED_Q_NAME, pathArguments.get(2).getNodeType());
110     }
111
112     @Override
113     protected void assertListFieldUnderList(final List<YangInstanceIdentifier> result) {
114         assertEquals(1, result.size());
115         assertEquals(List.of(
116             // FIXME: this does not look right: where are the corresponding NodeIdentifiers?
117             NodeIdentifierWithPredicates.of(SERVICES_Q_NAME),
118             NodeIdentifierWithPredicates.of(INSTANCE_Q_NAME)),
119             result.get(0).getPathArguments());
120     }
121
122     @Override
123     protected void assertLeafList(final List<YangInstanceIdentifier> parsedFields) {
124         assertEquals(1, parsedFields.size());
125         assertEquals(List.of(new NodeIdentifier(PROTOCOLS_Q_NAME)), parsedFields.get(0).getPathArguments());
126     }
127
128     private static YangInstanceIdentifier assertPath(final List<YangInstanceIdentifier> paths, final QName lastArg) {
129         return paths.stream()
130             .filter(path -> lastArg.equals(path.getLastPathArgument().getNodeType()))
131             .findAny()
132             .orElseThrow(() -> new AssertionError("Path ending with " + lastArg + " not found"));
133     }
134 }