2 * Copyright © 2020 FRINX s.r.o. and others. All rights reserved.
3 * Copyright © 2021 PANTHEON.tech, s.r.o.
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
9 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
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;
27 * Unit test for {@link NetconfFieldsTranslator}.
29 @RunWith(MockitoJUnitRunner.class)
30 public class NetconfFieldsTranslatorTest extends AbstractFieldsTranslatorTest<YangInstanceIdentifier> {
32 protected List<YangInstanceIdentifier> translateFields(final InstanceIdentifierContext context,
33 final FieldsParam fields) {
34 return NetconfFieldsTranslator.translate(context, fields);
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());
46 protected void assertDoublePath(final List<YangInstanceIdentifier> result) {
47 assertEquals(2, result.size());
49 final var libraryPath = assertPath(result, LIBRARY_Q_NAME);
50 assertEquals(1, libraryPath.getPathArguments().size());
52 final var playerPath = assertPath(result, PLAYER_Q_NAME);
53 assertEquals(1, playerPath.getPathArguments().size());
57 protected void assertSubPath(final List<YangInstanceIdentifier> result) {
58 // FIXME: NETCONF-820: add assertions
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());
73 protected void assertNamespace(final List<YangInstanceIdentifier> result) {
74 // FIXME: add assertions
78 protected void assertMultipleChildren1(final List<YangInstanceIdentifier> result) {
79 assertEquals(3, result.size());
81 final var tosPath = assertPath(result, TYPE_OF_SERVICE_Q_NAME);
82 assertEquals(2, tosPath.getPathArguments().size());
84 final var instanceNamePath = assertPath(result, INSTANCE_NAME_Q_NAME);
85 assertEquals(3, instanceNamePath.getPathArguments().size());
87 final var providerPath = assertPath(result, PROVIDER_Q_NAME);
88 assertEquals(3, providerPath.getPathArguments().size());
92 protected void assertMultipleChildren2(final List<YangInstanceIdentifier> result) {
93 // FIXME: add assertions
97 protected void assertMultipleChildren3(final List<YangInstanceIdentifier> result) {
98 // FIXME: add assertions
102 protected void assertAugmentedChild(final List<YangInstanceIdentifier> result) {
103 assertEquals(1, result.size());
104 final var pathArguments = result.get(0).getPathArguments();
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());
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());
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());
128 private static YangInstanceIdentifier assertPath(final List<YangInstanceIdentifier> paths, final QName lastArg) {
129 return paths.stream()
130 .filter(path -> lastArg.equals(path.getLastPathArgument().getNodeType()))
132 .orElseThrow(() -> new AssertionError("Path ending with " + lastArg + " not found"));