From: Robert Varga Date: Sat, 2 Oct 2021 20:51:21 +0000 (+0200) Subject: Add AbstractYangTest X-Git-Tag: v8.0.0~201 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=639bea5c1e9a6edcf6cbf92b472d5728352fbae6;p=yangtools.git Add AbstractYangTest We have a number of common assertion patterns, let's start creating a library. Change-Id: Ieeccf260c638905fb7bdb77369ced8f845b01ca7 Signed-off-by: Robert Varga --- diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6867BasicTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6867BasicTest.java index a154d46e19..9ee65ef787 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6867BasicTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6867BasicTest.java @@ -8,80 +8,57 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc7950; +import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.Test; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; +import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException; +import org.opendaylight.yangtools.yang.stmt.AbstractYangTest; import org.opendaylight.yangtools.yang.stmt.StmtTestUtils; -public class Bug6867BasicTest { +public class Bug6867BasicTest extends AbstractYangTest { @Test public void valid10Test() throws Exception { - final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/basic-test/valid-10.yang"); - assertNotNull(schemaContext); + assertEffectiveModel("/rfc7950/basic-test/valid-10.yang"); } @Test public void valid11Test() throws Exception { - final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/basic-test/valid-11.yang"); - assertNotNull(schemaContext); + assertEffectiveModel("/rfc7950/basic-test/valid-11.yang"); } @Test - public void invalid10Test() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/basic-test/invalid-10.yang"); - fail("Test should fail due to invalid Yang 1.0"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith("NOTIFICATION is not valid for CONTAINER")); - } + public void invalid10Test() { + assertException(InvalidSubstatementException.class, startsWith("NOTIFICATION is not valid for CONTAINER"), + "/rfc7950/basic-test/invalid-10.yang"); } @Test - public void invalid11Test() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/basic-test/invalid-11.yang"); - fail("Test should fail due to invalid Yang 1.1"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith("RPC is not valid for CONTAINER")); - } + public void invalid11Test() { + assertException(InvalidSubstatementException.class, startsWith("RPC is not valid for CONTAINER"), + "/rfc7950/basic-test/invalid-11.yang"); } @Test public void anyData11Test() throws Exception { - final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/basic-test/anydata-11.yang"); - assertNotNull(schemaContext); + assertEffectiveModel("/rfc7950/basic-test/anydata-11.yang"); } @Test - public void anyData10Test() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/basic-test/anydata-10.yang"); - fail("Test should fail due to invalid Yang 1.0"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith("anydata is not a YANG statement or use of extension")); - } + public void anyData10Test() { + assertSourceException(startsWith("anydata is not a YANG statement or use of extension"), + "/rfc7950/basic-test/anydata-10.yang"); } @Test public void yangModelTest() throws Exception { - final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/rfc7950/model"); - assertNotNull(schemaContext); + assertNotNull(StmtTestUtils.parseYangSources("/rfc7950/model")); } @Test - public void unsupportedVersionTest() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/basic-test/unsupported-version.yang"); - fail("Test should fail due to unsupported Yang version"); - } catch (final SomeModifiersUnresolvedException e) { - final Throwable cause = e.getCause(); - assertNotNull(cause); - assertTrue(cause.getMessage().startsWith("Unsupported YANG version 2.3")); - } + public void unsupportedVersionTest() { + assertSourceException(startsWith("Unsupported YANG version 2.3"), + "/rfc7950/basic-test/unsupported-version.yang"); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6869Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6869Test.java index cfbcc1d281..668f7db76a 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6869Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6869Test.java @@ -13,7 +13,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableSet; @@ -27,11 +26,10 @@ import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; -import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; -import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; +import org.opendaylight.yangtools.yang.stmt.AbstractYangTest; import org.opendaylight.yangtools.yang.stmt.StmtTestUtils; -public class Bug6869Test { +public class Bug6869Test extends AbstractYangTest { private static final QName ROOT = QName.create("foo", "root"); private static final QName GRP_LEAF = QName.create("foo", "grp-leaf"); @@ -83,10 +81,7 @@ public class Bug6869Test { @Test public void invalidYang10Test() { - final ReactorException ex = assertThrows(ReactorException.class, - () -> StmtTestUtils.parseYangSource("/rfc7950/bug6869/invalid10.yang")); - final Throwable cause = ex.getCause(); - assertThat(cause, instanceOf(SourceException.class)); - assertThat(cause.getMessage(), startsWith("IF_FEATURE is not valid for IDENTITY")); + assertInvalidSubstatementException(startsWith("IF_FEATURE is not valid for IDENTITY"), + "/rfc7950/bug6869/invalid10.yang"); } } \ No newline at end of file diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6871Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6871Test.java index 3bc9772196..5270c9def9 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6871Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6871Test.java @@ -7,40 +7,32 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc7950; +import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import java.util.Collection; import org.junit.Test; import org.opendaylight.yangtools.yang.common.Revision; import org.opendaylight.yangtools.yang.model.api.InputSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; -import org.opendaylight.yangtools.yang.model.api.MustDefinition; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; import org.opendaylight.yangtools.yang.model.api.OutputSchemaNode; import org.opendaylight.yangtools.yang.model.api.RpcDefinition; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; -import org.opendaylight.yangtools.yang.stmt.StmtTestUtils; - -public class Bug6871Test { +import org.opendaylight.yangtools.yang.stmt.AbstractYangTest; +public class Bug6871Test extends AbstractYangTest { @Test public void testValidYang11Model() throws Exception { - final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6871/foo.yang"); - assertNotNull(schemaContext); - - final Module foo = schemaContext.findModule("foo", Revision.of("2016-12-14")).get(); + final Module foo = assertEffectiveModel("/rfc7950/bug6871/foo.yang") + .findModule("foo", Revision.of("2016-12-14")).orElseThrow(); - final Collection notifications = foo.getNotifications(); + final var notifications = foo.getNotifications(); assertEquals(1, notifications.size()); final NotificationDefinition myNotification = notifications.iterator().next(); - Collection mustConstraints = myNotification.getMustConstraints(); + var mustConstraints = myNotification.getMustConstraints(); assertEquals(2, mustConstraints.size()); - final Collection rpcs = foo.getRpcs(); + final var rpcs = foo.getRpcs(); assertEquals(1, rpcs.size()); final RpcDefinition myRpc = rpcs.iterator().next(); @@ -56,18 +48,10 @@ public class Bug6871Test { } @Test - public void testInvalidYang10Model() throws Exception { - assertException("/rfc7950/bug6871/foo10.yang", "MUST is not valid for NOTIFICATION"); - assertException("/rfc7950/bug6871/bar10.yang", "MUST is not valid for INPUT"); - assertException("/rfc7950/bug6871/baz10.yang", "MUST is not valid for OUTPUT"); - } - - private static void assertException(final String sourcePath, final String exceptionMessage) throws Exception { - try { - StmtTestUtils.parseYangSource(sourcePath); - fail("Test should fail due to invalid Yang 1.0"); - } catch (final ReactorException ex) { - assertTrue(ex.getCause().getMessage().startsWith(exceptionMessage)); - } + public void testInvalidYang10Model() { + assertInvalidSubstatementException(startsWith("MUST is not valid for NOTIFICATION"), + "/rfc7950/bug6871/foo10.yang"); + assertInvalidSubstatementException(startsWith("MUST is not valid for INPUT"), "/rfc7950/bug6871/bar10.yang"); + assertInvalidSubstatementException(startsWith("MUST is not valid for OUTPUT"), "/rfc7950/bug6871/baz10.yang"); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6886Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6886Test.java index f20b1aef7b..3663432823 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6886Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6886Test.java @@ -7,66 +7,44 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc7950; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.hamcrest.CoreMatchers.startsWith; import org.junit.Test; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; -import org.opendaylight.yangtools.yang.stmt.StmtTestUtils; - -public class Bug6886Test { +import org.opendaylight.yangtools.yang.stmt.AbstractYangTest; +public class Bug6886Test extends AbstractYangTest { @Test - public void yang11UnquotedStrTest() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6886/yang11/foo.yang"); - fail("Test should fail due to invalid yang"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage() - .startsWith("YANG 1.1: unquoted string (illegalchars\"test1) contains illegal characters")); - } + public void yang11UnquotedStrTest() { + assertSourceException(startsWith("YANG 1.1: unquoted string (illegalchars\"test1) contains illegal characters"), + "/rfc7950/bug6886/yang11/foo.yang"); } @Test public void yang11UnquotedStrTest2() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6886/yang11/foo2.yang"); - fail("Test should fail due to invalid yang"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage() - .startsWith("YANG 1.1: unquoted string (illegalchars'test2) contains illegal characters")); - } + assertSourceException(startsWith("YANG 1.1: unquoted string (illegalchars'test2) contains illegal characters"), + "/rfc7950/bug6886/yang11/foo2.yang"); } @Test public void yang11DoubleQuotedStrTest() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6886/yang11/foo3.yang"); - fail("Test should fail due to invalid yang"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith("YANG 1.1: illegal double quoted string " - + "(i\\\\\\\\l\\nl\\te\\\"\\galcharstest1). In double quoted string the backslash must be followed " - + "by one of the following character [n,t,\",\\], but was 'g'.")); - } + assertSourceException(startsWith("YANG 1.1: illegal double quoted string " + + "(i\\\\\\\\l\\nl\\te\\\"\\galcharstest1). In double quoted string the backslash must be followed " + + "by one of the following character [n,t,\",\\], but was 'g'."), + "/rfc7950/bug6886/yang11/foo3.yang"); } @Test public void yang10UnquotedStrTest() throws Exception { - final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6886/yang10/foo.yang"); - assertNotNull(schemaContext); + assertEffectiveModel("/rfc7950/bug6886/yang10/foo.yang"); } @Test public void yang10UnquotedStrTest2() throws Exception { - final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6886/yang10/foo2.yang"); - assertNotNull(schemaContext); + assertEffectiveModel("/rfc7950/bug6886/yang10/foo2.yang"); } @Test public void yang10DoubleQuotedStrTest() throws Exception { - final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6886/yang10/foo3.yang"); - assertNotNull(schemaContext); + assertEffectiveModel("/rfc7950/bug6886/yang10/foo3.yang"); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6901Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6901Test.java index 33ad7dbd0e..645016ea7c 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6901Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc7950/Bug6901Test.java @@ -5,109 +5,70 @@ * 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.yangtools.yang.parser.stmt.rfc7950; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.startsWith; import org.junit.Test; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; -import org.opendaylight.yangtools.yang.stmt.StmtTestUtils; - -public class Bug6901Test { +import org.opendaylight.yangtools.yang.stmt.AbstractYangTest; +public class Bug6901Test extends AbstractYangTest { @Test public void ifFeature11EnumBitTest() throws Exception { - final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6901/foo.yang"); - assertNotNull(schemaContext); + assertEffectiveModel("/rfc7950/bug6901/foo.yang"); } @Test - public void ifFeatureOnDefaultValueEnumTest() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-enum.yang"); - fail("Test should fail due to invalid Yang 1.1"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith( - "Leaf '(foo)enum-leaf' has default value 'two' marked with an if-feature statement.")); - } + public void ifFeatureOnDefaultValueEnumTest() { + assertSourceException( + startsWith("Leaf '(foo)enum-leaf' has default value 'two' marked with an if-feature statement."), + "/rfc7950/bug6901/invalid-foo-enum.yang"); } @Test - public void ifFeatureOnDefaultValueEnumTest2() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-enum-2.yang"); - fail("Test should fail due to invalid Yang 1.1"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith( - "Leaf '(foo)enum-leaf' has default value 'two' marked with an if-feature statement.")); - } + public void ifFeatureOnDefaultValueEnumTest2() { + assertSourceException( + startsWith("Leaf '(foo)enum-leaf' has default value 'two' marked with an if-feature statement."), + "/rfc7950/bug6901/invalid-foo-enum-2.yang"); } @Test - public void ifFeatureOnDefaultValueEnumTest3() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-enum-3.yang"); - fail("Test should fail due to invalid Yang 1.1"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith( - "Leaf '(foo)enum-leaf' has default value 'two' marked with an if-feature statement.")); - } + public void ifFeatureOnDefaultValueEnumTest3() { + assertSourceException(startsWith( + "Leaf '(foo)enum-leaf' has default value 'two' marked with an if-feature statement."), + "/rfc7950/bug6901/invalid-foo-enum-3.yang"); } @Test - public void ifFeatureOnDefaultValueBitTest() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-bit.yang"); - fail("Test should fail due to invalid Yang 1.1"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith( - "Typedef '(foo)bits-typedef-2' has default value 'two' marked with an if-feature statement.")); - } + public void ifFeatureOnDefaultValueBitTest() { + assertSourceException( + startsWith("Typedef '(foo)bits-typedef-2' has default value 'two' marked with an if-feature statement."), + "/rfc7950/bug6901/invalid-foo-bit.yang"); } @Test - public void ifFeatureOnDefaultValueUnionTest() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-union.yang"); - fail("Test should fail due to invalid Yang 1.1"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith( - "Leaf '(foo)union-leaf' has default value 'two' marked with an if-feature statement.")); - } + public void ifFeatureOnDefaultValueUnionTest() { + assertSourceException( + startsWith("Leaf '(foo)union-leaf' has default value 'two' marked with an if-feature statement."), + "/rfc7950/bug6901/invalid-foo-union.yang"); } @Test - public void unsupportedFeatureTest() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-enum.yang"); - fail("Test should fail due to invalid Yang 1.1"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue( - e.getCause().getMessage().contains("has default value 'two' marked with an if-feature statement")); - } + public void unsupportedFeatureTest() { + assertSourceException(containsString("has default value 'two' marked with an if-feature statement"), + "/rfc7950/bug6901/invalid-foo-enum.yang"); } @Test - public void ifFeature10EnumTest() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-10-enum.yang"); - fail("Test should fail due to invalid Yang 1.0"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith("IF_FEATURE is not valid for ENUM")); - } + public void ifFeature10EnumTest() { + assertInvalidSubstatementException(startsWith("IF_FEATURE is not valid for ENUM"), + "/rfc7950/bug6901/invalid-foo-10-enum.yang"); } @Test - public void ifFeature10BitTest() throws Exception { - try { - StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-10-bit.yang"); - fail("Test should fail due to invalid Yang 1.0"); - } catch (final SomeModifiersUnresolvedException e) { - assertTrue(e.getCause().getMessage().startsWith("IF_FEATURE is not valid for BIT")); - } + public void ifFeature10BitTest() { + assertInvalidSubstatementException(startsWith("IF_FEATURE is not valid for BIT"), + "/rfc7950/bug6901/invalid-foo-10-bit.yang"); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AbstractModelTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AbstractModelTest.java index 8d6527c692..72872fd815 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AbstractModelTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AbstractModelTest.java @@ -21,7 +21,7 @@ import org.opendaylight.yangtools.yang.model.api.Module; /** * Abstract base class for tests of the model context available in {@code src/test/resources/model}. */ -public abstract class AbstractModelTest { +public abstract class AbstractModelTest extends AbstractYangTest { private static final QNameModule FOO_NS = QNameModule.create(XMLNamespace.of("urn:opendaylight.foo"), Revision.of("2013-02-27")); private static final QNameModule BAR_NS = @@ -36,7 +36,7 @@ public abstract class AbstractModelTest { @BeforeClass public static void beforeClass() throws Exception { - CTX = TestUtils.loadModules("/model"); + CTX = assertEffectiveModelDir("/model"); assertEquals(3, CTX.getModules().size()); FOO = CTX.findModules("foo").iterator().next(); diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AbstractYangTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AbstractYangTest.java new file mode 100644 index 0000000000..64e7a986dc --- /dev/null +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AbstractYangTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * 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.yangtools.yang.stmt; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; + +import org.eclipse.jdt.annotation.NonNull; +import org.hamcrest.Matcher; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; +import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException; +import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; + +/** + * Abstract base class containing useful utilities and assertions. + */ +public abstract class AbstractYangTest { + public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName) + throws Exception { + final var ret = TestUtils.parseYangSource(yangResourceName); + assertNotNull(ret); + return ret; + } + + public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName) + throws Exception { + final var ret = TestUtils.loadModules(resourceDirName); + assertNotNull(ret); + return ret; + } + + public static @NonNull E assertException(final Class cause, + final String... yangResourceName) { + final var ex = assertThrows(SomeModifiersUnresolvedException.class, + () -> TestUtils.parseYangSource(yangResourceName)); + final var actual = ex.getCause(); + assertThat(actual, instanceOf(cause)); + return cause.cast(actual); + } + + public static @NonNull E assertException(final Class cause, + final Matcher matcher, final String... yangResourceName) { + final var ret = assertException(cause, yangResourceName); + assertThat(ret.getMessage(), matcher); + return ret; + } + + public static @NonNull E assertExceptionDir(final String yangResourceName, + final Class cause) { + final var ex = assertThrows(SomeModifiersUnresolvedException.class, + () -> TestUtils.loadModules(yangResourceName)); + final var actual = ex.getCause(); + assertThat(actual, instanceOf(cause)); + return cause.cast(actual); + } + + public static @NonNull E assertExceptionDir(final String yangResourceName, + final Class cause, final Matcher matcher) { + final var ret = assertExceptionDir(yangResourceName, cause); + assertThat(ret.getMessage(), matcher); + return ret; + } + + public static @NonNull InferenceException assertInferenceException(final Matcher matcher, + final String... yangResourceName) { + return assertException(InferenceException.class, matcher, yangResourceName); + } + + public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName, + final Matcher matcher) { + return assertExceptionDir(yangResourceName, InferenceException.class, matcher); + } + + public static @NonNull InvalidSubstatementException assertInvalidSubstatementException( + final Matcher matcher, final String... yangResourceName) { + return assertException(InvalidSubstatementException.class, matcher, yangResourceName); + } + + public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir( + final String yangResourceName, final Matcher matcher) { + return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher); + } + + public static @NonNull SourceException assertSourceException(final Matcher matcher, + final String... yangResourceName) { + final var ret = assertException(SourceException.class, matcher, yangResourceName); + // SourceException is the base of the hierarchy, we should normally assert subclasses + assertEquals(SourceException.class, ret.getClass()); + return ret; + } + + public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName, + final Matcher matcher) { + final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher); + // SourceException is the base of the hierarchy, we should normally assert subclasses + assertEquals(SourceException.class, ret.getClass()); + return ret; + } +} 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 16f4e438b1..b2a54ba487 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 @@ -34,7 +34,7 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute; import org.opendaylight.yangtools.yang.model.api.type.TypeDefinitions; -public class AugmentTest { +public class AugmentTest extends AbstractYangTest { private static final QNameModule FOO = QNameModule.create( XMLNamespace.of("urn:opendaylight.foo"), Revision.of("2013-10-13")); private static final QNameModule BAR = QNameModule.create( @@ -50,7 +50,7 @@ public class AugmentTest { @BeforeClass public static void beforeClass() throws Exception { - AUGMENT_IN_AUGMENT = TestUtils.loadModules("/augment-test/augment-in-augment"); + AUGMENT_IN_AUGMENT = assertEffectiveModelDir("/augment-test/augment-in-augment"); } @Test @@ -255,7 +255,7 @@ public class AugmentTest { @Test public void testAugmentRpc() throws Exception { - final EffectiveModelContext context = TestUtils.loadModules("/augment-test/rpc"); + final EffectiveModelContext context = assertEffectiveModelDir("/augment-test/rpc"); final XMLNamespace NS_BAR = XMLNamespace.of("urn:opendaylight:bar"); final XMLNamespace NS_FOO = XMLNamespace.of("urn:opendaylight:foo"); final Revision revision = Revision.of("2013-10-11"); @@ -323,7 +323,7 @@ public class AugmentTest { @Test public void testAugmentInUsesResolving() throws Exception { - final EffectiveModelContext context = TestUtils.loadModules("/augment-test/augment-in-uses"); + final EffectiveModelContext context = assertEffectiveModelDir("/augment-test/augment-in-uses"); assertEquals(1, context.getModules().size()); final Module test = context.getModules().iterator().next(); diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentToExtensionTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentToExtensionTest.java index 2f45aae367..bf8edd5e59 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentToExtensionTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentToExtensionTest.java @@ -10,7 +10,6 @@ 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.assertThrows; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -20,18 +19,13 @@ import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.UsesNode; import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; -import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; -public class AugmentToExtensionTest { +public class AugmentToExtensionTest extends AbstractYangTest { @Test public void testIncorrectPath() throws Exception { - final var ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModules("/augment-to-extension-test/incorrect-path")); - final var cause = ex.getCause(); - - // FIXME: this should not be here - assertThat(cause, instanceOf(InferenceException.class)); - assertThat(cause.getMessage(), startsWith("Yang model processing phase EFFECTIVE_MODEL failed [at ")); + // FIXME: this should not be here, or why do we need encapsulation? Add asserts for that + final var cause = assertInferenceExceptionDir("/augment-to-extension-test/incorrect-path", + startsWith("Yang model processing phase EFFECTIVE_MODEL failed [at ")); final var firstCause = cause.getCause(); assertThat(firstCause, instanceOf(InferenceException.class)); @@ -46,7 +40,7 @@ public class AugmentToExtensionTest { @Test public void testCorrectPathIntoUnsupportedTarget() throws Exception { final Module devicesModule = - TestUtils.loadModules("/augment-to-extension-test/correct-path-into-unsupported-target") + assertEffectiveModelDir("/augment-to-extension-test/correct-path-into-unsupported-target") .findModules("augment-module").iterator().next(); final ContainerSchemaNode devicesContainer = (ContainerSchemaNode) devicesModule.getDataChildByName( QName.create(devicesModule.getQNameModule(), "my-container")); @@ -57,7 +51,7 @@ public class AugmentToExtensionTest { @Test public void testCorrectAugment() throws Exception { - final Module devicesModule = TestUtils.loadModules("/augment-to-extension-test/correct-augment") + final Module devicesModule = assertEffectiveModelDir("/augment-to-extension-test/correct-augment") .findModules("augment-module").iterator().next(); final ContainerSchemaNode devicesContainer = (ContainerSchemaNode) devicesModule.getDataChildByName(QName diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug1412Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug1412Test.java index 6a872ae0c2..5911ea0ba9 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug1412Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug1412Test.java @@ -27,11 +27,10 @@ import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement; *

* Note: Everything under unknown node is unknown node. */ -public class Bug1412Test { - +public class Bug1412Test extends AbstractYangTest { @Test public void test() throws Exception { - final Module bug1412 = TestUtils.loadModules("/bugs/bug1412").findModules("bug1412").iterator().next(); + final Module bug1412 = assertEffectiveModelDir("/bugs/bug1412").findModules("bug1412").iterator().next(); final ContainerSchemaNode node = (ContainerSchemaNode) bug1412.getDataChildByName(QName.create( bug1412.getQNameModule(), "node")); @@ -41,8 +40,8 @@ public class Bug1412Test { final UnrecognizedStatement action = unknownNodes.iterator().next(); final QNameModule qm = QNameModule.create(XMLNamespace.of("urn:test:bug1412"), Revision.of("2014-07-25")); - QName expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "action"); - assertEquals(expectedNodeType, action.statementDefinition().getStatementName()); + assertEquals(QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "action"), + action.statementDefinition().getStatementName()); assertEquals("hello", action.argument()); unknownNodes = action.declaredSubstatements(UnrecognizedStatement.class); @@ -65,24 +64,23 @@ public class Bug1412Test { } assertNotNull(info); - assertNotNull(description); - assertNotNull(actionPoint); - assertNotNull(output); - - expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "info"); - assertEquals(expectedNodeType, info.statementDefinition().getStatementName()); + assertEquals(QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "info"), + info.statementDefinition().getStatementName()); assertEquals("greeting", info.argument()); - expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "description"); - assertEquals(expectedNodeType, description.statementDefinition().getStatementName()); + assertNotNull(description); + assertEquals(QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "description"), + description.statementDefinition().getStatementName()); assertEquals("say greeting", description.argument()); - expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "actionpoint"); - assertEquals(expectedNodeType, actionPoint.statementDefinition().getStatementName()); + assertNotNull(actionPoint); + assertEquals(QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "actionpoint"), + actionPoint.statementDefinition().getStatementName()); assertEquals("entry", actionPoint.argument()); - expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "output"); - assertEquals(expectedNodeType, output.statementDefinition().getStatementName()); + assertNotNull(output); + assertEquals(QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "output"), + output.statementDefinition().getStatementName()); assertEquals(Empty.getInstance(), output.argument()); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug1413Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug1413Test.java index 2318eb4c5e..8bc384e071 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug1413Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug1413Test.java @@ -20,10 +20,10 @@ import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition; *

* Note: Everything under unknown node is unknown node. */ -public class Bug1413Test { +public class Bug1413Test extends AbstractYangTest { @Test public void test() throws Exception { - var extensions = TestUtils.loadModules("/bugs/bug1413").findModules("bug1413").iterator().next() + var extensions = assertEffectiveModelDir("/bugs/bug1413").findModules("bug1413").iterator().next() .getExtensionSchemaNodes(); assertEquals(1, extensions.size()); diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug3859Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug3859Test.java index d42755970e..f5ca2985aa 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug3859Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug3859Test.java @@ -11,9 +11,9 @@ import static org.junit.Assert.assertEquals; import org.junit.Test; -public class Bug3859Test { +public class Bug3859Test extends AbstractYangTest { @Test public void test() throws Exception { - assertEquals(1, TestUtils.loadModules("/bugs/bug3859").findModules("reference-in-unknown").size()); + assertEquals(1, assertEffectiveModelDir("/bugs/bug3859").findModules("reference-in-unknown").size()); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug394Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug394Test.java index bda951ba73..e18a0ba3b2 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug394Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug394Test.java @@ -26,10 +26,10 @@ import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement; /** * Test antlr grammar capability to parse nested unknown nodes. */ -public class Bug394Test { +public class Bug394Test extends AbstractYangTest { @Test public void testParseList() throws Exception { - final EffectiveModelContext context = TestUtils.loadModules("/bugs/bug394-retest"); + final EffectiveModelContext context = assertEffectiveModelDir("/bugs/bug394-retest"); final Module bug394 = context.findModules("bug394").iterator().next(); final Module bug394_ext = context.findModules("bug394-ext").iterator().next(); diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java index 9c2c8fcab1..ef661b8a4c 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java @@ -27,13 +27,13 @@ import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; -public class Bug4623Test { +public class Bug4623Test extends AbstractYangTest { private static Module TYPES; @BeforeClass public static void beforeClass() throws Exception { // given - TYPES = TestUtils.loadModules("/bugs/bug4623").findModules("types").iterator().next(); + TYPES = assertEffectiveModelDir("/bugs/bug4623").findModules("types").iterator().next(); } @Test @@ -103,8 +103,6 @@ public class Bug4623Test { QName.create(XMLNamespace.of("urn:custom.types.demo"), "leaf-unknown-length-pattern")); // then - assertNotNull(leaf); - final TypeDefinition type = leaf.getType(); assertNotNull(type); assertEquals(0, type.getUnknownSchemaNodes().size()); @@ -112,17 +110,14 @@ public class Bug4623Test { final LengthConstraint lengthConstraints = ((StringTypeDefinition) type).getLengthConstraint().get(); - final List patternConstraints = ((StringTypeDefinition) type).getPatternConstraints(); - - assertNotNull(lengthConstraints); - assertNotNull(patternConstraints); assertEquals(1, lengthConstraints.getAllowedRanges().asRanges().size()); - assertFalse(patternConstraints.size() == 0); final Range lengthConstraint = lengthConstraints.getAllowedRanges().span(); assertEquals(Integer.valueOf(2), lengthConstraint.lowerEndpoint()); assertEquals(Integer.valueOf(10), lengthConstraint.upperEndpoint()); + final List patternConstraints = ((StringTypeDefinition) type).getPatternConstraints(); + assertEquals(1, patternConstraints.size()); final PatternConstraint patternConstraint = patternConstraints.get(0); assertEquals(patternConstraint.getRegularExpressionString(), "[0-9a-fA-F]"); } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6771Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6771Test.java index e915511a1b..e7b693a3db 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6771Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6771Test.java @@ -28,7 +28,7 @@ public class Bug6771Test { @Test public void augmentTest() throws Exception { - final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/bug6771/augment.yang") + final ModuleEffectiveStatement module = TestUtils.parseYangSource("/bugs/bug6771/augment.yang") .getModuleStatement(NS); verifyLeafType(module, ROOT, CONT_B, LEAF_CONT_B); @@ -37,7 +37,7 @@ public class Bug6771Test { @Test public void choiceCaseTest() throws Exception { - final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/bug6771/choice-case.yang") + final ModuleEffectiveStatement module = TestUtils.parseYangSource("/bugs/bug6771/choice-case.yang") .getModuleStatement(NS); final QName myChoice = QName.create(NS, "my-choice"); @@ -55,7 +55,7 @@ public class Bug6771Test { @Test public void groupingTest() throws Exception { - verifyLeafType(StmtTestUtils.parseYangSource("/bugs/bug6771/grouping.yang").getModuleStatement(NS), + verifyLeafType(TestUtils.parseYangSource("/bugs/bug6771/grouping.yang").getModuleStatement(NS), ROOT, CONT_B, LEAF_CONT_B); } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7865Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7865Test.java index 9ed0e4e02e..3fe7fa3859 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7865Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7865Test.java @@ -18,10 +18,10 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement; -public class Bug7865Test { +public class Bug7865Test extends AbstractYangTest { @Test public void test() throws Exception { - final EffectiveModelContext context = TestUtils.loadModules("/bugs/bug7865"); + final EffectiveModelContext context = assertEffectiveModelDir("/bugs/bug7865"); final DataSchemaNode root = context.getDataChildByName(foo("root")); assertTrue(root instanceof ContainerSchemaNode); final Collection unknownSchemaNodes = ((ContainerSchemaNode) root) diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7879Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7879Test.java index 98d187645d..c01a680baf 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7879Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7879Test.java @@ -20,25 +20,25 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement; -public class Bug7879Test { +public class Bug7879Test extends AbstractYangTest { private static final XMLNamespace NS = XMLNamespace.of("my-model-ns"); @Test public void test() throws Exception { - final ModuleEffectiveStatement module = TestUtils.loadModules("/bugs/bug7879") - .getModuleStatement(QName.create(NS, "my-model")); + final ModuleEffectiveStatement module = assertEffectiveModelDir("/bugs/bug7879") + .getModuleStatement(qn("my-model")); final SchemaTreeEffectiveStatement container = module.findSchemaTreeNode( - qN("my-alarm"), qN("my-content"), qN("my-event-container")).orElse(null); + qn("my-alarm"), qn("my-content"), qn("my-event-container")).orElse(null); assertThat(container, instanceOf(ContainerSchemaNode.class)); final SchemaTreeEffectiveStatement leaf = module.findSchemaTreeNode( - qN("my-alarm"), qN("my-content"), qN("my-event-value")).orElse(null); + qn("my-alarm"), qn("my-content"), qn("my-event-value")).orElse(null); assertThat(leaf, instanceOf(LeafSchemaNode.class)); assertEquals(Optional.of("new description"), ((LeafSchemaNode) leaf).getDescription()); } - private static QName qN(final String localName) { + private static QName qn(final String localName) { return QName.create(NS, localName); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug8831Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug8831Test.java index fd513b5b18..3e63fdaf67 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug8831Test.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug8831Test.java @@ -8,36 +8,24 @@ package org.opendaylight.yangtools.yang.stmt; import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThrows; import org.junit.Test; -import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; -import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; -public class Bug8831Test { +public class Bug8831Test extends AbstractYangTest { @Test public void test() throws Exception { - assertNotNull(TestUtils.loadModules("/bugs/bug8831/valid")); + assertEffectiveModelDir("/bugs/bug8831/valid"); } @Test public void invalidModelsTest() { - final var ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.parseYangSource("/bugs/bug8831/invalid/inv-model.yang")); - final var cause = ex.getCause(); - assertThat(cause, instanceOf(SourceException.class)); - assertThat(cause.getMessage(), containsString("has default value 'any' marked with an if-feature statement")); + assertSourceException(containsString("has default value 'any' marked with an if-feature statement"), + "/bugs/bug8831/invalid/inv-model.yang"); } @Test public void invalidModelsTest2() { - final var ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.parseYangSource("/bugs/bug8831/invalid/inv-model2.yang")); - final var cause = ex.getCause(); - assertThat(cause, instanceOf(SourceException.class)); - assertThat(cause.getMessage(), containsString("has default value 'any' marked with an if-feature statement")); + assertSourceException(containsString("has default value 'any' marked with an if-feature statement"), + "/bugs/bug8831/invalid/inv-model2.yang"); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/GroupingTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/GroupingTest.java index ecf521376f..548dd417c2 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/GroupingTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/GroupingTest.java @@ -491,7 +491,7 @@ public class GroupingTest extends AbstractModelTest { @Test public void testAddedByUsesLeafTypeQName() throws Exception { - final EffectiveModelContext loadModules = TestUtils.loadModules("/added-by-uses-leaf-test"); + final EffectiveModelContext loadModules = assertEffectiveModelDir("/added-by-uses-leaf-test"); assertEquals(2, loadModules.getModules().size()); final Module foo = Iterables.getOnlyElement(loadModules.findModules("foo")); diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/ListKeysTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/ListKeysTest.java index 43e96ba619..dbd7816dc4 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/ListKeysTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/ListKeysTest.java @@ -19,13 +19,13 @@ import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; public class ListKeysTest { @Test public void correctListKeysTest() throws Exception { - StmtTestUtils.parseYangSource("/list-keys-test/correct-list-keys-test.yang"); + TestUtils.parseYangSource("/list-keys-test/correct-list-keys-test.yang"); } @Test public void incorrectListKeysTest1() { final ReactorException ex = assertThrows(ReactorException.class, - () -> StmtTestUtils.parseYangSource("/list-keys-test/incorrect-list-keys-test.yang")); + () -> TestUtils.parseYangSource("/list-keys-test/incorrect-list-keys-test.yang")); final Throwable cause = ex.getCause(); assertThat(cause, instanceOf(SourceException.class)); assertThat(cause.getMessage(), startsWith("Key 'test1_key1 test1_key2' misses node 'test1_key2'")); @@ -34,7 +34,7 @@ public class ListKeysTest { @Test public void incorrectListKeysTest2() { final ReactorException ex = assertThrows(ReactorException.class, - () -> StmtTestUtils.parseYangSource("/list-keys-test/incorrect-list-keys-test2.yang")); + () -> TestUtils.parseYangSource("/list-keys-test/incorrect-list-keys-test2.yang")); final Throwable cause = ex.getCause(); assertThat(cause, instanceOf(SourceException.class)); assertThat(cause.getMessage(), startsWith("Key 'test1_key1 test1_key2' misses node 'test1_key2'")); @@ -43,7 +43,7 @@ public class ListKeysTest { @Test public void incorrectListKeysTest3() { final ReactorException ex = assertThrows(ReactorException.class, - () -> StmtTestUtils.parseYangSource("/list-keys-test/incorrect-list-keys-test3.yang")); + () -> TestUtils.parseYangSource("/list-keys-test/incorrect-list-keys-test3.yang")); final Throwable cause = ex.getCause(); assertThat(cause, instanceOf(SourceException.class)); assertThat(cause.getMessage(), startsWith("Key 'grp_list' misses node 'grp_list'")); @@ -52,7 +52,7 @@ public class ListKeysTest { @Test public void incorrectListKeysTest4() { final ReactorException ex = assertThrows(ReactorException.class, - () -> StmtTestUtils.parseYangSource("/list-keys-test/incorrect-list-keys-test4.yang")); + () -> TestUtils.parseYangSource("/list-keys-test/incorrect-list-keys-test4.yang")); final Throwable cause = ex.getCause(); assertThat(cause, instanceOf(SourceException.class)); assertThat(cause.getMessage(), startsWith("Key 'grp_leaf' misses node 'grp_leaf'")); diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/ParsingExtensionValueTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/ParsingExtensionValueTest.java index 6508e62038..a6541396ae 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/ParsingExtensionValueTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/ParsingExtensionValueTest.java @@ -7,24 +7,16 @@ */ 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.assertThrows; import org.junit.Test; -import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; -import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; /** * Test for testing of extensions and their arguments. */ -public class ParsingExtensionValueTest { +public class ParsingExtensionValueTest extends AbstractYangTest { @Test public void extensionTest() { - final var ex = assertThrows(SomeModifiersUnresolvedException.class, () -> TestUtils.loadModules("/extensions")); - final var cause = ex.getCause(); - assertThat(cause, instanceOf(SourceException.class)); - assertThat(cause.getMessage(), startsWith("ext:id is not a YANG statement or use of extension")); + assertSourceExceptionDir("/extensions", startsWith("ext:id is not a YANG statement or use of extension")); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java index 4d4d046d5e..1a6ddb260a 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java @@ -84,6 +84,8 @@ public final class StmtTestUtils { } } + @Deprecated(forRemoval = true) + // TestUtils.parseYangSource() instead, but callers need also further cleanup public static EffectiveModelContext parseYangSource(final String yangSourcePath) throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException { return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, null); diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/SubstatementValidatorTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/SubstatementValidatorTest.java index 9b5f9215fc..7969cc8a16 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/SubstatementValidatorTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/SubstatementValidatorTest.java @@ -7,66 +7,48 @@ */ 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.assertEquals; -import static org.junit.Assert.assertThrows; import org.junit.Test; -import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException; import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException; -import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; -import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; -public class SubstatementValidatorTest { +public class SubstatementValidatorTest extends AbstractYangTest { @Test public void noException() throws Exception { - assertEquals(3, TestUtils.loadModules("/augment-test/augment-in-augment").getModules().size()); + assertEquals(3, assertEffectiveModelDir("/augment-test/augment-in-augment").getModules().size()); } @Test public void undesirableElementException() throws Exception { - final var ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModules("/substatement-validator/undesirable-element")); - final var cause = ex.getCause(); - assertThat(cause, instanceOf(InvalidSubstatementException.class)); - assertThat(cause.getMessage(), startsWith("TYPE is not valid for REVISION. Error in module undesirable " - + "(QNameModule{ns=urn:opendaylight.undesirable, rev=2015-11-11}) [at ")); + assertInvalidSubstatementExceptionDir("/substatement-validator/undesirable-element", + startsWith("TYPE is not valid for REVISION. Error in module undesirable " + + "(QNameModule{ns=urn:opendaylight.undesirable, rev=2015-11-11}) [at ")); } @Test public void maximalElementCountException() throws Exception { - final var ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModules("/substatement-validator/maximal-element")); - final var cause = ex.getCause(); - assertThat(cause, instanceOf(InvalidSubstatementException.class)); - assertThat(cause.getMessage(), startsWith("Maximal count of DESCRIPTION for AUGMENT is 1, detected 2. Error in " - + "module baz (QNameModule{ns=urn:opendaylight.baz, rev=2015-11-11}) [at ")); + assertInvalidSubstatementExceptionDir("/substatement-validator/maximal-element", + startsWith("Maximal count of DESCRIPTION for AUGMENT is 1, detected 2. Error in module baz " + + "(QNameModule{ns=urn:opendaylight.baz, rev=2015-11-11}) [at ")); } @Test public void missingElementException() { - final var ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModules("/substatement-validator/missing-element")); - final var cause = ex.getCause(); // FIXME: should be MissingSubstatementException? - assertThat(cause, instanceOf(SourceException.class)); - assertThat(cause.getMessage(), startsWith("Missing prefix statement [at ")); + assertSourceExceptionDir("/substatement-validator/missing-element", + startsWith("Missing prefix statement [at ")); } @Test public void bug6173Test() throws Exception { - assertEquals(1, TestUtils.loadModules("/substatement-validator/empty-element").getModules().size()); + assertEquals(1, assertEffectiveModelDir("/substatement-validator/empty-element").getModules().size()); } @Test public void bug4310test() throws Exception { - final var ex = assertThrows(SomeModifiersUnresolvedException.class, - () -> TestUtils.loadModules("/substatement-validator/bug-4310")); - final var cause = ex.getCause(); - assertThat(cause, instanceOf(MissingSubstatementException.class)); - assertThat(cause.getMessage(), startsWith("TYPE is missing TYPE. Minimal count is 1. Error in module bug4310 " - + "(QNameModule{ns=urn:opendaylight.bug4310}) [at ")); + assertExceptionDir("/substatement-validator/bug-4310", MissingSubstatementException.class, + startsWith("TYPE is missing TYPE. Minimal count is 1. Error in module bug4310 " + + "(QNameModule{ns=urn:opendaylight.bug4310}) [at ")); } } diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TwoRevisionsTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TwoRevisionsTest.java index 8046fec265..7e5ff6e24d 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TwoRevisionsTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TwoRevisionsTest.java @@ -14,10 +14,10 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.opendaylight.yangtools.yang.common.Revision; -public class TwoRevisionsTest { +public class TwoRevisionsTest extends AbstractYangTest { @Test public void testTwoRevisions() throws Exception { - var it = TestUtils.loadModules("/ietf").findModuleStatements("network-topology").iterator(); + var it = assertEffectiveModelDir("/ietf").findModuleStatements("network-topology").iterator(); assertTrue(it.hasNext()); assertEquals(Revision.ofNullable("2013-10-21"), it.next().localQNameModule().getRevision()); assertTrue(it.hasNext()); diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/UsesAugmentTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/UsesAugmentTest.java index 4169541241..70467aa67e 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/UsesAugmentTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/UsesAugmentTest.java @@ -22,27 +22,27 @@ import org.opendaylight.yangtools.yang.common.XMLNamespace; import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes; -public class UsesAugmentTest { +public class UsesAugmentTest extends AbstractYangTest { private static final QNameModule UG = QNameModule.create( XMLNamespace.of("urn:opendaylight:params:xml:ns:yang:uses-grouping"), Revision.of("2013-07-30")); private static final QNameModule GD = QNameModule.create( XMLNamespace.of("urn:opendaylight:params:xml:ns:yang:grouping-definitions"), Revision.of("2013-09-04")); - private static SchemaContext CONTEXT; + private static EffectiveModelContext CONTEXT; @BeforeClass public static void beforeClass() throws Exception { - CONTEXT = TestUtils.loadModules("/grouping-test"); + CONTEXT = assertEffectiveModelDir("/grouping-test"); } /* diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserSimpleTest.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserSimpleTest.java index 119ab35e0a..d431e541f6 100644 --- a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserSimpleTest.java +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserSimpleTest.java @@ -28,30 +28,26 @@ import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.MustDefinition; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.Status; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.UsesNode; -public class YangParserSimpleTest { +public class YangParserSimpleTest extends AbstractYangTest { private static final XMLNamespace NS = XMLNamespace.of("urn:opendaylight:simple-nodes"); private static final QNameModule SN = QNameModule.create(NS, Revision.of("2013-07-30")); private static final QName SN_NODES = QName.create(SN, "nodes"); - private static SchemaContext CONTEXT; private static Module MODULE; @BeforeClass public static void beforeClass() throws Exception { - CONTEXT = TestUtils.loadModules("/simple-test"); - MODULE = CONTEXT.findModules("simple-nodes").iterator().next(); + MODULE = assertEffectiveModelDir("/simple-test").findModules("simple-nodes").iterator().next(); } @Test public void testParseAnyXml() { final AnyxmlSchemaNode data = (AnyxmlSchemaNode) MODULE.getDataChildByName( QName.create(MODULE.getQNameModule(), "data")); - assertNotNull("'anyxml data not found'", data); assertFalse(data.equals(null)); assertEquals("RegularAnyxmlEffectiveStatement{qname=(urn:opendaylight:simple-nodes?revision=2013-07-30)data}", data.toString());