import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-
import com.google.common.base.Throwables;
+import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.InputStream;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.util.YangParseException;
+import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
public class YangParserNegativeTest {
+ private final PrintStream stdout = System.out;
+ private final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ private String testLog;
+
+ @Before
+ public void setUp() throws UnsupportedEncodingException {
+ System.setOut(new PrintStream(output, true, "UTF-8"));
+ }
+
+ @After
+ public void cleanUp() {
+ System.setOut(stdout);
+ }
+
@Test
public void testInvalidImport() throws Exception {
File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
try {
- try (InputStream stream = new FileInputStream(yang)) {
+ try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
TestUtils.loadModule(stream);
fail("SomeModifiersUnresolvedException should be thrown");
}
Throwable rootCause = Throwables.getRootCause(e);
assertTrue(rootCause instanceof InferenceException);
assertTrue(rootCause.getMessage().startsWith("Imported module"));
- assertTrue(rootCause.getMessage().endsWith("was not found."));
+ assertTrue(rootCause.getMessage().contains("was not found."));
}
}
public void testTypeNotFound() throws Exception {
File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
try {
- try (InputStream stream = new FileInputStream(yang)) {
+ try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
TestUtils.loadModule(stream);
- fail("SomeModifiersUnresolvedException should be thrown.");
+ fail("InferenceException should be thrown");
}
} catch (SomeModifiersUnresolvedException e) {
Throwable rootCause = Throwables.getRootCause(e);
File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
try {
final List<InputStream> streams = new ArrayList<>(2);
- try (InputStream testFile0 = new FileInputStream(yang1)) {
+ try (InputStream testFile0 = new NamedFileInputStream(yang1, yang1.getPath())) {
streams.add(testFile0);
- try (InputStream testFile3 = new FileInputStream(yang2)) {
+ try (InputStream testFile3 = new NamedFileInputStream(yang2, yang2.getPath())) {
streams.add(testFile3);
assertEquals("Expected loaded files count is 2", 2, streams.size());
TestUtils.loadModules(streams);
} catch (SomeModifiersUnresolvedException e) {
final Throwable rootCause = Throwables.getRootCause(e);
assertTrue(rootCause instanceof InferenceException);
- assertEquals(
- "Augment target not found: Absolute{path=[(urn:simple.container.demo?revision=1970-01-01)unknown]}",
- rootCause.getMessage());
+ assertTrue(rootCause.getMessage().startsWith("Augment target not found: Absolute{path=[(urn:simple" +
+ ".container.demo?revision=1970-01-01)unknown]}"));
}
}
public void testInvalidRefine() throws Exception {
File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
try {
- try (InputStream stream = new FileInputStream(yang)) {
+ try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
TestUtils.loadModule(stream);
fail("SourceException should be thrown");
}
} catch (SourceException e) {
- assertTrue(e
- .getMessage()
- .contains(
- "Error in module 'test4' in the refine of uses 'Relative{path=[(urn:simple.container.demo?revision=1970-01-01)node]}': can not perform refine of 'PRESENCE' for the target 'LEAF_LIST'."));
+ assertTrue(e.getMessage().contains("Error in module 'test4' in the refine of uses 'Relative{path=[" +
+ "(urn:simple.container.demo?revision=1970-01-01)node]}': can not perform refine of 'PRESENCE' for" +
+ " the target 'LEAF_LIST'."));
}
}
public void testInvalidLength() throws Exception {
File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
try {
- try (InputStream stream = new FileInputStream(yang)) {
+ try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
TestUtils.loadModule(stream);
fail("YangParseException should be thrown");
}
- } catch (YangParseException e) {
+ } catch (SourceException e) {
assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
}
}
public void testInvalidRange() throws Exception {
File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
try {
- try (InputStream stream = new FileInputStream(yang)) {
+ try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
TestUtils.loadModule(stream);
fail("Exception should be thrown");
}
- } catch (YangParseException e) {
+ } catch (SourceException e) {
assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
}
}
public void testDuplicateContainer() throws Exception {
File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
try {
- try (InputStream stream = new FileInputStream(yang)) {
+ try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
TestUtils.loadModule(stream);
fail("SourceException should be thrown");
}
} catch (SourceException e) {
- String expected = "Error in module 'container': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared.";
- assertEquals(expected, e.getMessage());
+ String expected = "Error in module 'container': can not add '(urn:simple.container" +
+ ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" +
+ ".demo?revision=1970-01-01)foo' already declared.";
+ assertTrue(e.getMessage().contains(expected));
}
}
public void testDuplicateContainerList() throws Exception {
File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
try {
- try (InputStream stream = new FileInputStream(yang)) {
+ try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
TestUtils.loadModule(stream);
fail("SourceException should be thrown");
}
} catch (SourceException e) {
- String expected = "Error in module 'container-list': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared.";
- assertEquals(expected, e.getMessage());
+ String expected = "Error in module 'container-list': can not add '(urn:simple.container" +
+ ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" +
+ ".demo?revision=1970-01-01)foo' already declared.";
+ assertTrue(e.getMessage().contains(expected));
}
}
public void testDuplicateContainerLeaf() throws Exception {
File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
try {
- try (InputStream stream = new FileInputStream(yang)) {
+ try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
TestUtils.loadModule(stream);
fail("SourceException should be thrown");
}
} catch (SourceException e) {
- String expected = "Error in module 'container-leaf': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared.";
- assertEquals(expected, e.getMessage());
+ String expected = "Error in module 'container-leaf': can not add '(urn:simple.container" +
+ ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" +
+ ".demo?revision=1970-01-01)foo' already declared.";
+ assertTrue(e.getMessage().contains(expected));
}
}
public void testDuplicateTypedef() throws Exception {
File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
try {
- try (InputStream stream = new FileInputStream(yang)) {
+ try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
TestUtils.loadModule(stream);
- fail("IllegalArgumentException should be thrown");
+ fail("SourceException should be thrown");
}
- } catch (IllegalArgumentException e) {
- String expected = "Duplicate name for typedef (urn:simple.container.demo?revision=1970-01-01)int-ext";
- assertEquals(expected, e.getMessage());
+ } catch (SourceException e) {
+ String expected = "Error in module 'typedef': can not add '(urn:simple.container" +
+ ".demo?revision=1970-01-01)int-ext'. Node name collision: '(urn:simple.container" +
+ ".demo?revision=1970-01-01)int-ext' already declared.";
+ assertTrue(e.getMessage().startsWith(expected));
}
}
public void testDuplicityInAugmentTarget1() throws Exception {
File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
- try {
- try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
- TestUtils.loadModules(Arrays.asList(stream1, stream2));
- fail("IllegalStateException should be thrown");
- }
- } catch (IllegalStateException e) {
- assertEquals(e.getMessage(),
- "An augment cannot add node named 'id' because this name is already used in target");
+ try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath());
+ InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) {
+ TestUtils.loadModules(Arrays.asList(stream1, stream2));
+ testLog = output.toString();
+ assertTrue(testLog.contains("An augment cannot add node named 'id' because this name is already used in target"));
}
}
public void testDuplicityInAugmentTarget2() throws Exception {
File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
- try {
- try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
- TestUtils.loadModules(Arrays.asList(stream1, stream2));
- fail("IllegalStateException should be thrown");
- }
- } catch (IllegalStateException e) {
- assertEquals(e.getMessage(),
- "An augment cannot add node named 'delta' because this name is already used in target");
+ try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath());
+ InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) {
+ TestUtils.loadModules(Arrays.asList(stream1, stream2));
+ testLog = output.toString();
+ assertTrue(testLog.contains("An augment cannot add node named 'delta' because this name is already used in target"));
}
}
public void testMandatoryInAugment() throws Exception {
File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
- try {
- try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
- TestUtils.loadModules(Arrays.asList(stream1, stream2));
- fail("IllegalArgumentException should be thrown");
- }
- } catch (IllegalArgumentException e) {
- String expected = "An augment cannot add node 'linkleaf' because it is mandatory and in module different from target";
- assertEquals(expected, e.getMessage());
+ try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath());
+ InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) {
+ TestUtils.loadModules(Arrays.asList(stream1, stream2));
+ testLog = output.toString();
+ assertTrue(testLog.contains(
+ "An augment cannot add node 'linkleaf' because it is mandatory and in module different from target"));
}
}
public void testInvalidListKeyDefinition() throws Exception {
File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
try {
- try (InputStream stream1 = new FileInputStream(yang1)) {
+ try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath())) {
TestUtils.loadModule(stream1);
- fail("IllegalArgumentException should be thrown");
+ fail("InferenceException should be thrown");
}
- } catch (IllegalArgumentException e) {
- String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)application-map'";
+ } catch (InferenceException e) {
+ String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)" +
+ "application-map'";
assertTrue(e.getMessage().startsWith(expected));
}
}