From 8d1f52e9562af06fc0be6441da5840195d360eaa Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 2 Oct 2021 21:56:53 +0200 Subject: [PATCH] Remove more TestUtils classes We have some duplication still and some assert code which should be with its users in AugmentTest. Change-Id: Ic0df63afaf8f2a5003339b8315a85aaea08de1b5 Signed-off-by: Robert Varga --- .../yangtools/yang/stmt/AugmentTest.java | 26 ++++++++- .../yangtools/yang/stmt/TestUtils.java | 57 +++---------------- .../yang/stmt/YangParserNegativeTest.java | 16 +++--- 3 files changed, 38 insertions(+), 61 deletions(-) diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentTest.java index 553c8a1c45..16f4e438b1 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentTest.java @@ -67,7 +67,7 @@ public class AugmentTest { final Collection augmentChildren = augment.getChildNodes(); assertEquals(4, augmentChildren.size()); for (final DataSchemaNode dsn : augmentChildren) { - TestUtils.checkIsAugmenting(dsn, false); + checkIsAugmenting(dsn, false); } final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create( @@ -150,7 +150,7 @@ public class AugmentTest { // augment "/br:interfaces/br:ifEntry" { final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ, "augment-holder")); - TestUtils.checkIsAugmenting(augmentHolder, true); + checkIsAugmenting(augmentHolder, true); assertEquals(Q2, augmentHolder.getQName()); // foo.yang @@ -190,7 +190,7 @@ public class AugmentTest { module2.getQNameModule(), "ifEntry")); final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create( BAZ, "augment-holder")); - TestUtils.checkIsAugmenting(augmentedHolder, true); + checkIsAugmenting(augmentedHolder, true); // foo.yang // augment "/br:interfaces/br:ifEntry/bz:augment-holder" @@ -341,4 +341,24 @@ public class AugmentTest { final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id")); assertTrue(id.isAugmenting()); } + + /** + * Test if node has augmenting flag set to expected value. In case this is DataNodeContainer/ChoiceNode, check its + * child nodes/case nodes too. + * + * @param node node to check + * @param expected expected value + */ + private static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) { + assertEquals(expected, node.isAugmenting()); + if (node instanceof DataNodeContainer) { + for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) { + checkIsAugmenting(child, expected); + } + } else if (node instanceof ChoiceSchemaNode) { + for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) { + checkIsAugmenting(caseNode, expected); + } + } + } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TestUtils.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TestUtils.java index 27114fa4fb..0d07b374dd 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TestUtils.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TestUtils.java @@ -7,8 +7,6 @@ */ package org.opendaylight.yangtools.yang.stmt; -import static org.junit.Assert.assertEquals; - import java.io.File; import java.io.IOException; import java.net.URI; @@ -17,17 +15,12 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.eclipse.jdt.annotation.NonNull; -import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; -import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.ModuleImport; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource; -import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource; @@ -69,18 +62,17 @@ public final class TestUtils { .buildEffective(); } - public static EffectiveModelContext loadModuleResources(final Class refClass, final String... resourceNames) - throws IOException, ReactorException, YangSyntaxErrorException { - final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild(); - - for (String resourceName : resourceNames) { - reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass, - resourceName))); + public static EffectiveModelContext parseYangSource(final String... yangSourceFilePath) throws Exception { + final var reactor = RFC7950Reactors.defaultReactor().newBuild(); + for (var resourcePath : yangSourceFilePath) { + reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forPath(Path.of( + TestUtils.class.getResource(resourcePath).toURI())))); } - return reactor.buildEffective(); } + // FIXME: these remain unaudited + public static EffectiveModelContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException, IOException { final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild(); @@ -119,39 +111,4 @@ public final class TestUtils { } return null; } - - /** - * Test if node has augmenting flag set to expected value. In case this is - * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too. - * - * @param node - * node to check - * @param expected - * expected value - */ - public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) { - assertEquals(expected, node.isAugmenting()); - if (node instanceof DataNodeContainer) { - for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) { - checkIsAugmenting(child, expected); - } - } else if (node instanceof ChoiceSchemaNode) { - for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) { - checkIsAugmenting(caseNode, expected); - } - } - } - - public static EffectiveModelContext parseYangSource(final String... yangSourceFilePath) throws Exception { - final var reactor = RFC7950Reactors.defaultReactor().newBuild(); - for (var resourcePath : yangSourceFilePath) { - reactor.addSource(sourceForResource(resourcePath)); - } - return reactor.buildEffective(); - } - - public static YangStatementStreamSource sourceForResource(final String resourceName) throws Exception { - return YangStatementStreamSource.create(YangTextSchemaSource.forPath(Path.of( - TestUtils.class.getResource(resourceName).toURI()))); - } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserNegativeTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserNegativeTest.java index ad7ce2b243..c2c4ab3f38 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserNegativeTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserNegativeTest.java @@ -24,7 +24,7 @@ public class YangParserNegativeTest { @Test public void testInvalidImport() { final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile1.yang")); + () -> TestUtils.parseYangSource("/negative-scenario/testfile1.yang")); final Throwable rootCause = Throwables.getRootCause(ex); assertThat(rootCause, isA(InferenceException.class)); @@ -35,7 +35,7 @@ public class YangParserNegativeTest { @Test public void testTypeNotFound() { final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile2.yang")); + () -> TestUtils.parseYangSource("/negative-scenario/testfile2.yang")); final Throwable rootCause = Throwables.getRootCause(ex); assertThat(rootCause, isA(InferenceException.class)); assertThat(rootCause.getMessage(), @@ -45,7 +45,7 @@ public class YangParserNegativeTest { @Test public void testInvalidAugmentTarget() { final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModuleResources(getClass(), + () -> TestUtils.parseYangSource( "/negative-scenario/testfile0.yang", "/negative-scenario/testfile3.yang")); final Throwable rootCause = Throwables.getRootCause(ex); assertThat(rootCause, isA(InferenceException.class)); @@ -56,7 +56,7 @@ public class YangParserNegativeTest { @Test public void testInvalidRefine() { final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile4.yang")); + () -> TestUtils.parseYangSource("/negative-scenario/testfile4.yang")); final Throwable cause = ex.getCause(); assertThat(cause, isA(SourceException.class)); assertThat(cause.getMessage(), containsString("Error in module 'test4' in the refine of uses " @@ -67,7 +67,7 @@ public class YangParserNegativeTest { @Test public void testInvalidLength() { final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile5.yang")); + () -> TestUtils.parseYangSource("/negative-scenario/testfile5.yang")); final Throwable cause = ex.getCause(); assertThat(cause, isA(SourceException.class)); assertThat(cause.getMessage(), containsString("Invalid length constraint [4..10]")); @@ -128,7 +128,7 @@ public class YangParserNegativeTest { @Test public void testDuplicityInAugmentTarget1() { final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModuleResources(getClass(), + () -> TestUtils.parseYangSource( "/negative-scenario/duplicity/augment0.yang", "/negative-scenario/duplicity/augment1.yang")); final Throwable cause = ex.getCause(); assertThat(cause, isA(InferenceException.class)); @@ -139,7 +139,7 @@ public class YangParserNegativeTest { @Test public void testDuplicityInAugmentTarget2() { final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModuleResources(getClass(), + () -> TestUtils.parseYangSource( "/negative-scenario/duplicity/augment0.yang", "/negative-scenario/duplicity/augment2.yang")); final Throwable rootCause = Throwables.getRootCause(ex); assertThat(rootCause, isA(SubstatementIndexingException.class)); @@ -150,7 +150,7 @@ public class YangParserNegativeTest { @Test public void testMandatoryInAugment() { final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModuleResources(getClass(), + () -> TestUtils.parseYangSource( "/negative-scenario/testfile8.yang", "/negative-scenario/testfile7.yang")); final Throwable cause = ex.getCause(); assertThat(cause, isA(InferenceException.class)); -- 2.36.6