*/
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.assertNotNull;
import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
-import java.io.UnsupportedEncodingException;
-import java.nio.charset.StandardCharsets;
-import java.util.Collection;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+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 {
- @SuppressWarnings("checkstyle:regexpSinglelineJava")
- private final PrintStream stdout = System.out;
- private final ByteArrayOutputStream output = new ByteArrayOutputStream();
-
- @Before
- public void setUp() throws UnsupportedEncodingException {
- System.setOut(new PrintStream(output, true, StandardCharsets.UTF_8));
- }
-
- @After
- public void cleanUp() {
- System.setOut(stdout);
- }
-
@Test
public void noException() throws Exception {
- assertNotNull(TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI()));
+ assertEquals(3, TestUtils.loadModules("/augment-test/augment-in-augment").getModules().size());
}
@Test
public void undesirableElementException() throws Exception {
- try {
- TestUtils.loadModules(getClass().getResource("/substatement-validator/undesirable-element").toURI());
- fail("Unexpected success");
- } catch (ReactorException ex) {
- assertNotNull(ex.getCause());
- assertTrue(ex.getCause().getMessage().contains("TYPE is not valid for REVISION"));
- }
+ 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 "));
}
@Test
public void maximalElementCountException() throws Exception {
- try {
- TestUtils.loadModules(getClass().getResource("/substatement-validator/maximal-element").toURI());
- fail("Unexpected success");
- } catch (ReactorException ex) {
- assertNotNull(ex.getCause());
- assertTrue(ex.getCause().getMessage().contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
- }
+ 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 "));
}
@Test
public void missingElementException() {
- assertThrows(SomeModifiersUnresolvedException.class, () -> TestUtils.loadModules(
- SubstatementValidatorTest.class.getResource("/substatement-validator/missing-element").toURI()));
+ 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 "));
}
@Test
public void bug6173Test() throws Exception {
- final Collection<? extends Module> loadModules = TestUtils.loadModules(getClass()
- .getResource("/substatement-validator/empty-element").toURI()).getModules();
- assertEquals(1, loadModules.size());
+ assertEquals(1, TestUtils.loadModules("/substatement-validator/empty-element").getModules().size());
}
@Test
public void bug4310test() throws Exception {
- assertThrows(SomeModifiersUnresolvedException.class, () -> TestUtils.loadModules(
- SubstatementValidatorTest.class.getResource("/substatement-validator/bug-4310").toURI()));
+ 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 "));
}
-}
\ No newline at end of file
+}
// Hidden on purpose
}
- public static @NonNull List<StatementStreamSource> loadSources(final String yangFilesDirectoryPath)
+ public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
throws Exception {
- final var files = new File(TestUtils.class.getResource(yangFilesDirectoryPath).toURI())
+ return loadSources(TestUtils.class, resourceDirectory);
+ }
+
+ public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
+ throws Exception {
+ final var files = new File(cls.getResource(resourceDirectory).toURI())
.listFiles(StmtTestUtils.YANG_FILE_FILTER);
final var sources = new ArrayList<StatementStreamSource>(files.length);
for (var file : files) {
return sources;
}
+ public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
+ return loadModules(TestUtils.class, resourceDirectory);
+ }
+
+ public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
+ throws Exception {
+ return loadModules(cls.getResource(resourceDirectory).toURI());
+ }
+
public static EffectiveModelContext loadModules(final URI resourceDirectory)
throws ReactorException, IOException, YangSyntaxErrorException {
final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();