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;
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
*/
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;
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 {
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<QName> qnames) {
*/
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;
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";
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"));
}
*/
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;
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";
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
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);
}
}
*/
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;
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));
}
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,
}
@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"));
}
}
@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"));
}
--- /dev/null
+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;
+ }
+ }
+ }
+}
+
--- /dev/null
+module foo {
+ namespace foo;
+ prefix foo;
+
+ container root {
+ }
+}
--- /dev/null
+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;
+ }
+ }
+ }
+}
+
--- /dev/null
+module foo {
+ namespace foo;
+ prefix foo;
+
+ container root {
+ }
+}
--- /dev/null
+module ill2 {
+ namespace ill2;
+ prefix ill2;
+
+ import foo { prefix foo; }
+
+ //invalid augment (mandatory list)
+ augment "/foo:root" {
+ list mandatory-list {
+ min-elements 1;
+ }
+ }
+}
+
}
}
}
-
- //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;
- }
- }
- }
}
--- /dev/null
+module foo {
+ namespace foo;
+ prefix foo;
+
+ container root {
+ }
+}