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;
26 * Unit test for {@link NetconfFieldsTranslator}.
28 @RunWith(MockitoJUnitRunner.class)
29 public class NetconfFieldsTranslatorTest extends AbstractFieldsTranslatorTest<YangInstanceIdentifier> {
31 protected List<YangInstanceIdentifier> translateFields(final InstanceIdentifierContext<?> context,
32 final FieldsParam fields) {
33 return NetconfFieldsTranslator.translate(context, fields);
37 protected void assertSimplePath(final List<YangInstanceIdentifier> result) {
38 assertEquals(1, result.size());
39 final var pathArguments = result.get(0).getPathArguments();
40 assertEquals(1, pathArguments.size());
41 assertEquals(LIBRARY_Q_NAME, pathArguments.get(0).getNodeType());
45 protected void assertDoublePath(final List<YangInstanceIdentifier> result) {
46 assertEquals(2, result.size());
48 final var libraryPath = assertPath(result, LIBRARY_Q_NAME);
49 assertEquals(1, libraryPath.getPathArguments().size());
51 final var playerPath = assertPath(result, PLAYER_Q_NAME);
52 assertEquals(1, playerPath.getPathArguments().size());
56 protected void assertSubPath(final List<YangInstanceIdentifier> result) {
57 // FIXME: NETCONF-820: add assertions
61 protected void assertChildrenPath(final List<YangInstanceIdentifier> result) {
62 assertEquals(1, result.size());
63 final var pathArguments = result.get(0).getPathArguments();
64 assertEquals(3, pathArguments.size());
65 assertEquals(LIBRARY_Q_NAME, pathArguments.get(0).getNodeType());
66 assertEquals(ALBUM_Q_NAME, pathArguments.get(1).getNodeType());
67 assertEquals(NAME_Q_NAME, pathArguments.get(2).getNodeType());
71 protected void assertNamespace(final List<YangInstanceIdentifier> result) {
72 // FIXME: add assertions
76 protected void assertMultipleChildren1(final List<YangInstanceIdentifier> result) {
77 assertEquals(3, result.size());
79 final var tosPath = assertPath(result, TYPE_OF_SERVICE_Q_NAME);
80 assertEquals(2, tosPath.getPathArguments().size());
82 final var instanceNamePath = assertPath(result, INSTANCE_NAME_Q_NAME);
83 assertEquals(3, instanceNamePath.getPathArguments().size());
85 final var providerPath = assertPath(result, PROVIDER_Q_NAME);
86 assertEquals(3, providerPath.getPathArguments().size());
90 protected void assertMultipleChildren2(final List<YangInstanceIdentifier> result) {
91 // FIXME: add assertions
95 protected void assertMultipleChildren3(final List<YangInstanceIdentifier> result) {
96 // FIXME: add assertions
100 protected void assertAugmentedChild(final List<YangInstanceIdentifier> result) {
101 assertEquals(1, result.size());
102 final var pathArguments = result.get(0).getPathArguments();
104 assertEquals(3, pathArguments.size());
105 assertEquals(PLAYER_Q_NAME, pathArguments.get(0).getNodeType());
106 assertThat(pathArguments.get(1), instanceOf(AugmentationIdentifier.class));
107 assertEquals(SPEED_Q_NAME, pathArguments.get(2).getNodeType());
111 protected void assertListFieldUnderList(final List<YangInstanceIdentifier> result) {
112 assertEquals(1, result.size());
113 assertEquals(List.of(new NodeIdentifier(SERVICES_Q_NAME), new NodeIdentifier(INSTANCE_Q_NAME)),
114 result.get(0).getPathArguments());
118 protected void assertLeafList(final List<YangInstanceIdentifier> parsedFields) {
119 assertEquals(1, parsedFields.size());
120 assertEquals(List.of(new NodeIdentifier(PROTOCOLS_Q_NAME)), parsedFields.get(0).getPathArguments());
123 private static YangInstanceIdentifier assertPath(final List<YangInstanceIdentifier> paths, final QName lastArg) {
124 return paths.stream()
125 .filter(path -> lastArg.equals(path.getLastPathArgument().getNodeType()))
127 .orElseThrow(() -> new AssertionError("Path ending with " + lastArg + " not found"));