/* * Copyright © 2020 FRINX s.r.o. and others. All rights reserved. * Copyright © 2021 PANTHEON.tech, s.r.o. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.restconf.nb.rfc8040.utils.parser; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.restconf.nb.rfc8040.FieldsParam; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; /** * Unit test for {@link NetconfFieldsTranslator}. */ @RunWith(MockitoJUnitRunner.class) public class NetconfFieldsTranslatorTest extends AbstractFieldsTranslatorTest { @Override protected List translateFields(final InstanceIdentifierContext context, final FieldsParam fields) { return NetconfFieldsTranslator.translate(context, fields); } @Override protected void assertSimplePath(final List result) { assertEquals(1, result.size()); final var pathArguments = result.get(0).getPathArguments(); assertEquals(1, pathArguments.size()); assertEquals(LIBRARY_Q_NAME, pathArguments.get(0).getNodeType()); } @Override protected void assertDoublePath(final List result) { assertEquals(2, result.size()); final var libraryPath = assertPath(result, LIBRARY_Q_NAME); assertEquals(1, libraryPath.getPathArguments().size()); final var playerPath = assertPath(result, PLAYER_Q_NAME); assertEquals(1, playerPath.getPathArguments().size()); } @Override protected void assertSubPath(final List result) { // FIXME: NETCONF-820: add assertions } @Override protected void assertChildrenPath(final List result) { assertEquals(1, result.size()); final var pathArguments = result.get(0).getPathArguments(); assertEquals(4, pathArguments.size()); assertEquals(LIBRARY_Q_NAME, pathArguments.get(0).getNodeType()); assertEquals(ARTIST_Q_NAME, pathArguments.get(1).getNodeType()); assertEquals(ALBUM_Q_NAME, pathArguments.get(2).getNodeType()); assertEquals(NAME_Q_NAME, pathArguments.get(3).getNodeType()); } @Override protected void assertNamespace(final List result) { // FIXME: add assertions } @Override protected void assertMultipleChildren1(final List result) { assertEquals(3, result.size()); final var tosPath = assertPath(result, TYPE_OF_SERVICE_Q_NAME); assertEquals(2, tosPath.getPathArguments().size()); final var instanceNamePath = assertPath(result, INSTANCE_NAME_Q_NAME); assertEquals(3, instanceNamePath.getPathArguments().size()); final var providerPath = assertPath(result, PROVIDER_Q_NAME); assertEquals(3, providerPath.getPathArguments().size()); } @Override protected void assertMultipleChildren2(final List result) { // FIXME: add assertions } @Override protected void assertMultipleChildren3(final List result) { // FIXME: add assertions } @Override protected void assertAugmentedChild(final List result) { assertEquals(1, result.size()); final var pathArguments = result.get(0).getPathArguments(); assertEquals(3, pathArguments.size()); assertEquals(PLAYER_Q_NAME, pathArguments.get(0).getNodeType()); assertThat(pathArguments.get(1), instanceOf(AugmentationIdentifier.class)); assertEquals(SPEED_Q_NAME, pathArguments.get(2).getNodeType()); } @Override protected void assertListFieldUnderList(final List result) { assertEquals(1, result.size()); assertEquals(List.of( // FIXME: this does not look right: where are the corresponding NodeIdentifiers? NodeIdentifierWithPredicates.of(SERVICES_Q_NAME), NodeIdentifierWithPredicates.of(INSTANCE_Q_NAME)), result.get(0).getPathArguments()); } @Override protected void assertLeafList(final List parsedFields) { assertEquals(1, parsedFields.size()); assertEquals(List.of(new NodeIdentifier(PROTOCOLS_Q_NAME)), parsedFields.get(0).getPathArguments()); } private static YangInstanceIdentifier assertPath(final List paths, final QName lastArg) { return paths.stream() .filter(path -> lastArg.equals(path.getLastPathArgument().getNodeType())) .findAny() .orElseThrow(() -> new AssertionError("Path ending with " + lastArg + " not found")); } }