From 4cf9500ea52a77d1b71024093aefe5d45d92827e Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 14 Jan 2021 09:39:33 +0100 Subject: [PATCH] Do not catch instantiation exceptions during augment The try/catch block here is overly broad and ill-defined, as it suppresses failures produced by onStatementAdded(). A number of test cases actually worked around this by checking either effects of the omission or that the presence of a message being logged. These are corrected to assert the equivalent failure. This necessitates splitting Bug8126Test into legal and illegal parts. JIRA: YANGTOOLS-1186 Change-Id: Icd7c15ed82df38ccbdbd64e635c5a479e5a180aa Signed-off-by: Robert Varga --- .../stmt/augment/AugmentInferenceAction.java | 12 +-- .../yang/parser/stmt/rfc7950/Bug6876Test.java | 30 ++++---- .../yangtools/yang/stmt/Bug5335Test.java | 74 ++++++------------- .../yangtools/yang/stmt/Bug6669Test.java | 52 +++++++------ .../yangtools/yang/stmt/Bug8126Test.java | 52 ++++++++++--- .../yang/stmt/YangParserNegativeTest.java | 49 ++++-------- .../bugs/bug8126/{ => inv-choice}/foo.yang | 0 .../bugs/bug8126/inv-choice/ill1.yang | 16 ++++ .../resources/bugs/bug8126/inv-cont/foo.yang | 7 ++ .../resources/bugs/bug8126/inv-cont/ill3.yang | 22 ++++++ .../resources/bugs/bug8126/inv-list/foo.yang | 7 ++ .../resources/bugs/bug8126/inv-list/ill2.yang | 14 ++++ .../bugs/bug8126/{ => valid}/bar.yang | 31 -------- .../resources/bugs/bug8126/valid/foo.yang | 7 ++ 14 files changed, 200 insertions(+), 173 deletions(-) rename yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/{ => inv-choice}/foo.yang (100%) create mode 100644 yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-choice/ill1.yang create mode 100644 yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-cont/foo.yang create mode 100644 yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-cont/ill3.yang create mode 100644 yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-list/foo.yang create mode 100644 yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-list/ill2.yang rename yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/{ => valid}/bar.yang (54%) create mode 100644 yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/valid/foo.yang diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/augment/AugmentInferenceAction.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/augment/AugmentInferenceAction.java index f00172ffbc..028a216688 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/augment/AugmentInferenceAction.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/augment/AugmentInferenceAction.java @@ -34,7 +34,6 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prereq import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; -import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace; import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType; import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase; @@ -76,15 +75,8 @@ final class AugmentInferenceAction implements InferenceAction { augmentNode.addToNs(AugmentImplicitHandlingNamespace.class, Empty.getInstance(), augmentTargetCtx); } - // FIXME: this is a workaround for models which augment a node which is added via an extension - // which we do not handle. This needs to be reworked in terms of unknown schema nodes. - try { - copyFromSourceToTarget((StatementContextBase) augmentNode, augmentTargetCtx); - augmentTargetCtx.addEffectiveSubstatement(augmentNode.replicaAsChildOf(augmentTargetCtx)); - } catch (final SourceException e) { - LOG.warn("Failed to add augmentation {} defined at {}", augmentTargetCtx.sourceReference(), - augmentNode.sourceReference(), e); - } + copyFromSourceToTarget((StatementContextBase) augmentNode, augmentTargetCtx); + augmentTargetCtx.addEffectiveSubstatement(augmentNode.replicaAsChildOf(augmentTargetCtx)); } @Override diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6876Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6876Test.java index b74618d875..4b059177ed 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6876Test.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6876Test.java @@ -7,9 +7,11 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc7950; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; import org.junit.Test; @@ -19,6 +21,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; +import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.stmt.StmtTestUtils; public class Bug6876Test { @@ -31,20 +35,20 @@ public class Bug6876Test { final SchemaContext context = StmtTestUtils.parseYangSources("/rfc7950/bug6876/yang11"); assertNotNull(context); - assertTrue(findNode(context, ImmutableList.of(bar("augment-target"), - bar("my-leaf"))) instanceof LeafSchemaNode); - assertTrue(findNode(context, ImmutableList.of(bar("augment-target"), - foo("mandatory-leaf"))) instanceof LeafSchemaNode); + assertThat(findNode(context, ImmutableList.of(bar("augment-target"), bar("my-leaf"))), + instanceOf(LeafSchemaNode.class)); + assertThat(findNode(context, ImmutableList.of(bar("augment-target"), foo("mandatory-leaf"))), + instanceOf(LeafSchemaNode.class)); } @Test - public void yang10Test() throws Exception { - final SchemaContext context = StmtTestUtils.parseYangSources("/rfc7950/bug6876/yang10"); - assertNotNull(context); - - assertTrue(findNode(context, ImmutableList.of(bar("augment-target"), - bar("my-leaf"))) instanceof LeafSchemaNode); - assertNull(findNode(context, ImmutableList.of(bar("augment-target"), foo("mandatory-leaf")))); + public void yang10Test() { + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/rfc7950/bug6876/yang10")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( + "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target")); } private static SchemaNode findNode(final SchemaContext context, final Iterable qnames) { diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5335Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5335Test.java index ac9912c4cc..a580748dcf 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5335Test.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5335Test.java @@ -7,16 +7,13 @@ */ package org.opendaylight.yangtools.yang.stmt; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; @@ -24,6 +21,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; +import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; public class Bug5335Test { private static final String FOO = "foo"; @@ -31,66 +30,39 @@ public class Bug5335Test { private static final String REV = "2016-03-04"; private static final QName ROOT = QName.create(FOO, REV, "root"); - private static final QName PRESENCE_CONTAINER_F = QName.create(FOO, REV, "presence-container"); private static final QName NON_PRESENCE_CONTAINER_F = QName.create(FOO, REV, "non-presence-container"); private static final QName MANDATORY_LEAF_F = QName.create(FOO, REV, "mandatory-leaf"); private static final QName PRESENCE_CONTAINER_B = QName.create(BAR, REV, "presence-container"); private static final QName NON_PRESENCE_CONTAINER_B = QName.create(BAR, REV, "non-presence-container"); private static final QName MANDATORY_LEAF_B = QName.create(BAR, REV, "mandatory-leaf"); - private final ByteArrayOutputStream output = new ByteArrayOutputStream(); - - @Before - public void setUp() throws UnsupportedEncodingException { - System.setOut(new PrintStream(output, true, StandardCharsets.UTF_8)); - } - - @After - @SuppressWarnings("checkstyle:regexpSinglelineJava") - public void cleanUp() { - System.setOut(System.out); - } - @Test - public void incorrectTest1() throws Exception { - final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1"); - assertNotNull(context); - - final SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_B, MANDATORY_LEAF_B); - final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath); - assertNull(mandatoryLeaf); - - final String testLog = output.toString(); - assertTrue(testLog.contains( + public void incorrectTest1() { + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target")); } @Test - public void incorrectTest2() throws Exception { - final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2"); - assertNotNull(context); - - final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, MANDATORY_LEAF_B); - final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath); - assertNull(mandatoryLeaf); - - final String testLog = output.toString(); - assertTrue(testLog.contains( + public void incorrectTest2() { + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target")); } @Test public void incorrectTest3() throws Exception { - final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2"); - assertNotNull(context); - - final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, NON_PRESENCE_CONTAINER_B, - MANDATORY_LEAF_B); - final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath); - assertNull(mandatoryLeaf); - - final String testLog = output.toString(); - assertTrue(testLog.contains( + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target")); } diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6669Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6669Test.java index 2e520c20db..20bdd12f10 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6669Test.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6669Test.java @@ -7,8 +7,11 @@ */ package org.opendaylight.yangtools.yang.stmt; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -19,6 +22,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; +import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; public class Bug6669Test { private static final String REV = "2016-09-08"; @@ -29,36 +34,35 @@ public class Bug6669Test { private static final QName BAR_1 = QName.create(BAR_NS, REV, "bar1"); private static final QName BAR_2 = QName.create(BAR_NS, REV, "bar2"); private static final QName M = QName.create(BAR_NS, REV, "m"); - private static final QName L = QName.create(BAR_NS, REV, "l"); @Test - public void testInvalidAugment() throws Exception { - final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test1"); - assertNotNull(context); - - final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, - SchemaPath.create(true, ROOT, BAR, BAR_1, M)); - assertNull(findDataSchemaNode); + public void testInvalidAugment() { + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test1")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( + "An augment cannot add node 'm' because it is mandatory and in module different than target")); } @Test - public void testInvalidAugment2() throws Exception { - final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test2"); - assertNotNull(context); - - final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, - SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, M)); - assertNull(findDataSchemaNode); + public void testInvalidAugment2() { + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test2")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( + "An augment cannot add node 'm' because it is mandatory and in module different than target")); } @Test - public void testInvalidAugment3() throws Exception { - final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test3"); - assertNotNull(context); - - final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, - SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, L)); - assertNull(findDataSchemaNode); + public void testInvalidAugment3() { + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test3")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( + "An augment cannot add node 'l' because it is mandatory and in module different than target")); } @Test @@ -87,7 +91,7 @@ public class Bug6669Test { assertNotNull(context); final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, - SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, L)); + SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, QName.create(BAR_NS, REV, "l"))); assertTrue(findDataSchemaNode instanceof ListSchemaNode); } } diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug8126Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug8126Test.java index f245320b74..f513ed4ceb 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug8126Test.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug8126Test.java @@ -7,9 +7,11 @@ */ package org.opendaylight.yangtools.yang.stmt; -import static org.junit.Assert.assertNotNull; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThrows; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; @@ -18,25 +20,57 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; +import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; public class Bug8126Test { private static final String FOO_NS = "foo"; private static final String BAR_NS = "bar"; @Test - public void test() throws Exception { - final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug8126"); - assertNotNull(context); - assertTrue(findNode(context, foo("root"), bar("my-container"), bar("my-choice"), bar("one"), bar("one"), - bar("mandatory-leaf")) instanceof LeafSchemaNode); - assertTrue(findNode(context, foo("root"), bar("my-list"), bar("two"), bar("mandatory-leaf-2")) - instanceof LeafSchemaNode); + public void testValidAugments() throws Exception { + final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug8126/valid"); + assertThat(findNode(context, foo("root"), bar("my-container"), bar("my-choice"), bar("one"), bar("one"), + bar("mandatory-leaf")), instanceOf(LeafSchemaNode.class)); + assertThat(findNode(context, foo("root"), bar("my-list"), bar("two"), bar("mandatory-leaf-2")), + instanceOf(LeafSchemaNode.class)); assertNull(findNode(context, foo("root"), bar("mandatory-list"))); assertNull(findNode(context, foo("root"), bar("mandatory-container"), bar("mandatory-choice"))); assertNull(findNode(context, foo("root"), bar("mandatory-container-2"), bar("one"), bar("mandatory-leaf-3"))); } + @Test + public void testAugmentMandatoryChoice() { + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-choice")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( + "An augment cannot add node 'mandatory-choice' because it is mandatory and in module different than ")); + } + + @Test + public void testAugmentMandatoryList() { + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-list")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( + "An augment cannot add node 'mandatory-list' because it is mandatory and in module different than ")); + } + + @Test + public void testAugmentMandatoryContainer() { + final ReactorException ex = assertThrows(ReactorException.class, + () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-cont")); + final Throwable cause = ex.getCause(); + assertThat(cause, instanceOf(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( + "An augment cannot add node 'mandatory-leaf-3' because it is mandatory and in module different than ")); + } + + private static SchemaNode findNode(final SchemaContext context, final QName... qnames) { return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, qnames)); } diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserNegativeTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserNegativeTest.java index 4808fdf6aa..e83bef0bc5 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserNegativeTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserNegativeTest.java @@ -14,37 +14,13 @@ import static org.hamcrest.Matchers.isA; import static org.junit.Assert.assertThrows; import com.google.common.base.Throwables; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.nio.charset.StandardCharsets; -import org.junit.After; -import org.junit.Before; import org.junit.Test; -import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.SubstatementIndexingException; import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; -import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; public class YangParserNegativeTest { - - @SuppressWarnings("checkstyle:regexpSinglelineJava") - private final PrintStream stdout = System.out; - private final ByteArrayOutputStream output = new ByteArrayOutputStream(); - private String testLog; - - @Before - public void setUp() { - System.setOut(new PrintStream(output, true, StandardCharsets.UTF_8)); - } - - @After - public void cleanUp() { - System.setOut(stdout); - } - @Test public void testInvalidImport() { final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, @@ -150,11 +126,13 @@ public class YangParserNegativeTest { } @Test - public void testDuplicityInAugmentTarget1() throws IOException, ReactorException, YangSyntaxErrorException { - TestUtils.loadModuleResources(getClass(), - "/negative-scenario/duplicity/augment0.yang", "/negative-scenario/duplicity/augment1.yang"); - testLog = output.toString(); - assertThat(testLog, containsString( + public void testDuplicityInAugmentTarget1() { + final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, + () -> TestUtils.loadModuleResources(getClass(), + "/negative-scenario/duplicity/augment0.yang", "/negative-scenario/duplicity/augment1.yang")); + final Throwable cause = ex.getCause(); + assertThat(cause, isA(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( "An augment cannot add node named 'id' because this name is already used in target")); } @@ -170,12 +148,13 @@ public class YangParserNegativeTest { } @Test - public void testMandatoryInAugment() throws IOException, ReactorException, YangSyntaxErrorException { - TestUtils.loadModuleResources(getClass(), - "/negative-scenario/testfile8.yang", - "/negative-scenario/testfile7.yang"); - testLog = output.toString(); - assertThat(testLog, containsString( + public void testMandatoryInAugment() { + final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class, + () -> TestUtils.loadModuleResources(getClass(), + "/negative-scenario/testfile8.yang", "/negative-scenario/testfile7.yang")); + final Throwable cause = ex.getCause(); + assertThat(cause, isA(InferenceException.class)); + assertThat(cause.getMessage(), startsWith( "An augment cannot add node 'linkleaf' because it is mandatory and in module different than target")); } diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/foo.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-choice/foo.yang similarity index 100% rename from yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/foo.yang rename to yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-choice/foo.yang diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-choice/ill1.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-choice/ill1.yang new file mode 100644 index 0000000000..df2198fd4d --- /dev/null +++ b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-choice/ill1.yang @@ -0,0 +1,16 @@ +module ill1 { + namespace ill1; + prefix ill1; + + import foo { prefix foo; } + + //invalid augment (mandatory choice) + augment "/foo:root" { + container mandatory-container { + choice mandatory-choice { + mandatory true; + } + } + } +} + diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-cont/foo.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-cont/foo.yang new file mode 100644 index 0000000000..b1b0df7885 --- /dev/null +++ b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-cont/foo.yang @@ -0,0 +1,7 @@ +module foo { + namespace foo; + prefix foo; + + container root { + } +} diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-cont/ill3.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-cont/ill3.yang new file mode 100644 index 0000000000..cd9856c3ab --- /dev/null +++ b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-cont/ill3.yang @@ -0,0 +1,22 @@ +module ill3 { + namespace ill3; + prefix ill3; + + import foo { prefix foo; } + + //invalid augments (mandatory container) + augment "/foo:root" { + container mandatory-container-2 { + } + } + + augment "/foo:root/mandatory-container-2" { + container one { + leaf mandatory-leaf-3 { + mandatory true; + type empty; + } + } + } +} + diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-list/foo.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-list/foo.yang new file mode 100644 index 0000000000..b1b0df7885 --- /dev/null +++ b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-list/foo.yang @@ -0,0 +1,7 @@ +module foo { + namespace foo; + prefix foo; + + container root { + } +} diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-list/ill2.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-list/ill2.yang new file mode 100644 index 0000000000..af790e31a1 --- /dev/null +++ b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/inv-list/ill2.yang @@ -0,0 +1,14 @@ +module ill2 { + namespace ill2; + prefix ill2; + + import foo { prefix foo; } + + //invalid augment (mandatory list) + augment "/foo:root" { + list mandatory-list { + min-elements 1; + } + } +} + diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/bar.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/valid/bar.yang similarity index 54% rename from yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/bar.yang rename to yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/valid/bar.yang index c142cbed06..071f72902e 100644 --- a/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/bar.yang +++ b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/valid/bar.yang @@ -38,35 +38,4 @@ module bar { } } } - - //invalid augment (mandatory choice) - augment "/foo:root" { - container mandatory-container { - choice mandatory-choice { - mandatory true; - } - } - } - - //invalid augment (mandatory list) - augment "/foo:root" { - list mandatory-list { - min-elements 1; - } - } - - //invalid augments (mandatory container) - augment "/foo:root" { - container mandatory-container-2 { - } - } - - augment "/foo:root/mandatory-container-2" { - container one { - leaf mandatory-leaf-3 { - mandatory true; - type empty; - } - } - } } diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/valid/foo.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/valid/foo.yang new file mode 100644 index 0000000000..b1b0df7885 --- /dev/null +++ b/yang/yang-parser-rfc7950/src/test/resources/bugs/bug8126/valid/foo.yang @@ -0,0 +1,7 @@ +module foo { + namespace foo; + prefix foo; + + container root { + } +} -- 2.36.6