import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
-
+import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.URI;
+import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
-import java.util.LinkedHashSet;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.model.util.Decimal64;
import org.opendaylight.yangtools.yang.model.util.ExtendedType;
import org.opendaylight.yangtools.yang.model.util.Int16;
import org.opendaylight.yangtools.yang.model.util.Uint32;
import org.opendaylight.yangtools.yang.model.util.UnionType;
import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
+import org.opendaylight.yangtools.yang.parser.util.YangParseException;
public class YangParserTest {
public static final String FS = File.separator;
ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
// test SchemaNode args
- QName expectedQName = new QName(expectedNamespace, barRev, expectedPrefix, "ifEntry");
+ QName expectedQName = QName.create(expectedNamespace, barRev, "ifEntry");
assertEquals(expectedQName, ifEntry.getQName());
SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, barRev, expectedPrefix, "interfaces",
"ifEntry");
ConstraintDefinition constraints = ifEntry.getConstraints();
assertNull(constraints.getWhenCondition());
assertEquals(0, constraints.getMustConstraints().size());
- assertFalse(constraints.isMandatory());
+ assertTrue(constraints.isMandatory());
assertEquals(1, (int) constraints.getMinElements());
assertEquals(11, (int) constraints.getMaxElements());
// test AugmentationTarget args
assertEquals(2, availableAugmentations.size());
// test ListSchemaNode args
List<QName> expectedKey = new ArrayList<>();
- expectedKey.add(new QName(expectedNamespace, barRev, expectedPrefix, "ifIndex"));
+ expectedKey.add(QName.create(expectedNamespace, barRev, "ifIndex"));
assertEquals(expectedKey, ifEntry.getKeyDefinition());
assertFalse(ifEntry.isUserOrdered());
// test DataNodeContainer args
List<PatternConstraint> patterns = type.getPatternConstraints();
assertEquals(1, patterns.size());
PatternConstraint pattern = patterns.iterator().next();
- assertEquals("[e-z]*", pattern.getRegularExpression());
+ assertEquals("^[e-z]*$", pattern.getRegularExpression());
assertTrue(type.getLengthConstraints().isEmpty());
assertTrue(type.getRangeConstraints().isEmpty());
patterns = baseType1.getPatternConstraints();
assertEquals(1, patterns.size());
pattern = patterns.iterator().next();
- assertEquals("[b-u]*", pattern.getRegularExpression());
+ assertEquals("^[b-u]*$", pattern.getRegularExpression());
assertTrue(baseType1.getLengthConstraints().isEmpty());
assertTrue(baseType1.getRangeConstraints().isEmpty());
patterns = baseType3.getPatternConstraints();
assertEquals(1, patterns.size());
pattern = patterns.iterator().next();
- assertEquals("[a-k]*", pattern.getRegularExpression());
+ assertEquals("^[a-k]*$", pattern.getRegularExpression());
List<LengthConstraint> baseType3Lengths = baseType3.getLengthConstraints();
assertEquals(1, baseType3Lengths.size());
length = baseType3Lengths.get(0);
assertTrue(baseType3.getBaseType() instanceof StringType);
}
+ @Test
+ public void testTypedefInvalidPatternsResolving() {
+ Module foo = TestUtils.findModule(modules, "foo");
+ final LeafSchemaNode invalidPatternStringLeaf = (LeafSchemaNode) foo.getDataChildByName("invalid-pattern-string-leaf");
+ ExtendedType type = (ExtendedType) invalidPatternStringLeaf.getType();
+ QName typeQName = type.getQName();
+ assertEquals("invalid-string-pattern", typeQName.getLocalName());
+ assertEquals(barNS, typeQName.getNamespace());
+ assertEquals(barRev, typeQName.getRevision());
+ assertNull(type.getUnits());
+ assertNull(type.getDefaultValue());
+ List<PatternConstraint> patterns = type.getPatternConstraints();
+ assertTrue(patterns.isEmpty());
+
+ final LeafSchemaNode invalidDirectStringPatternDefLeaf = (LeafSchemaNode) foo.getDataChildByName("invalid-direct-string-pattern-def-leaf");
+ type = (ExtendedType) invalidDirectStringPatternDefLeaf.getType();
+ typeQName = type.getQName();
+ assertEquals("string", typeQName.getLocalName());
+ assertEquals(fooNS, typeQName.getNamespace());
+ assertEquals(fooRev, typeQName.getRevision());
+ assertNull(type.getUnits());
+ assertNull(type.getDefaultValue());
+ patterns = type.getPatternConstraints();
+ assertTrue(patterns.isEmpty());
+
+ final LeafSchemaNode multiplePatternStringLeaf = (LeafSchemaNode) foo.getDataChildByName("multiple-pattern-string-leaf");
+ type = (ExtendedType) multiplePatternStringLeaf.getType();
+ typeQName = type.getQName();
+ assertEquals("multiple-pattern-string", typeQName.getLocalName());
+ assertEquals(barNS, typeQName.getNamespace());
+ assertEquals(barRev, typeQName.getRevision());
+ assertNull(type.getUnits());
+ assertNull(type.getDefaultValue());
+ patterns = type.getPatternConstraints();
+ assertTrue(!patterns.isEmpty());
+ assertEquals(1, patterns.size());
+ PatternConstraint pattern = patterns.iterator().next();
+ assertEquals("^[e-z]*$", pattern.getRegularExpression());
+ assertTrue(type.getLengthConstraints().isEmpty());
+ assertTrue(type.getRangeConstraints().isEmpty());
+
+ final LeafSchemaNode multiplePatternDirectStringDefLeaf = (LeafSchemaNode) foo.getDataChildByName("multiple-pattern-direct-string-def-leaf");
+ type = (ExtendedType) multiplePatternDirectStringDefLeaf.getType();
+ typeQName = type.getQName();
+ assertEquals("string", typeQName.getLocalName());
+ assertEquals(fooNS, typeQName.getNamespace());
+ assertEquals(fooRev, typeQName.getRevision());
+ assertNull(type.getUnits());
+ assertNull(type.getDefaultValue());
+ patterns = type.getPatternConstraints();
+ assertTrue(!patterns.isEmpty());
+ assertEquals(2, patterns.size());
+
+ boolean isEZPattern = false;
+ boolean isADPattern = false;
+ for (final PatternConstraint patternConstraint : patterns) {
+ if (patternConstraint.getRegularExpression().equals("^[e-z]*$")) {
+ isEZPattern = true;
+ } else if (patternConstraint.getRegularExpression().equals("^[a-d]*$")) {
+ isADPattern = true;
+ }
+ }
+ assertTrue(isEZPattern);
+ assertTrue( isADPattern);
+ }
+
@Test
public void testTypedefLengthsResolving() {
Module foo = TestUtils.findModule(modules, "foo");
List<PatternConstraint> patterns = baseType2.getPatternConstraints();
assertEquals(1, patterns.size());
PatternConstraint pattern = patterns.iterator().next();
- assertEquals("[a-k]*", pattern.getRegularExpression());
+ assertEquals("^[a-k]*$", pattern.getRegularExpression());
List<LengthConstraint> baseType3Lengths = baseType2.getLengthConstraints();
assertEquals(1, baseType3Lengths.size());
length = baseType3Lengths.get(0);
public void testChoice() {
Module foo = TestUtils.findModule(modules, "foo");
ContainerSchemaNode transfer = (ContainerSchemaNode) foo.getDataChildByName("transfer");
- ChoiceNode how = (ChoiceNode) transfer.getDataChildByName("how");
+ ChoiceSchemaNode how = (ChoiceSchemaNode) transfer.getDataChildByName("how");
Set<ChoiceCaseNode> cases = how.getCases();
assertEquals(5, cases.size());
ChoiceCaseNode input = null;
assertEquals("system/user ref", dev.getReference());
List<QName> path = new ArrayList<>();
- path.add(new QName(barNS, barRev, "br", "interfaces"));
- path.add(new QName(barNS, barRev, "br", "ifEntry"));
+ path.add(QName.create(barNS, barRev, "interfaces"));
+ path.add(QName.create(barNS, barRev, "ifEntry"));
SchemaPath expectedPath = SchemaPath.create(path, true);
assertEquals(expectedPath, dev.getTargetPath());
NotificationDefinition notification = notifications.iterator().next();
// test SchemaNode args
- QName expectedQName = new QName(bazNS, bazRev, expectedPrefix, "event");
+ QName expectedQName = QName.create(bazNS, bazRev, "event");
assertEquals(expectedQName, notification.getQName());
SchemaPath expectedPath = TestUtils.createPath(true, bazNS, bazRev, expectedPrefix, "event");
assertEquals(expectedPath, notification.getPath());
File dependenciesDir = new File(getClass().getResource("/sorting-test").toURI());
YangContextParser parser = new YangParserImpl();
modules = parser.parseFile(yangFile, dependenciesDir).getModules();
- SchemaContext ctx = new SchemaContextImpl(modules, Collections.<ModuleIdentifier, String> emptyMap());
+ SchemaContext ctx = new SchemaContextImpl(modules, Collections.emptyMap());
checkOrder(modules);
assertSetEquals(modules, ctx.getModules());
}
Set<Module> newModules = parser.parseFiles(testFiles).getModules();
assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
+ ctx = new SchemaContextImpl(newModules, Collections.emptyMap());
assertSetEquals(newModules, ctx.getModules());
// ##########
newModules = parser.parseFiles(testFiles, null).getModules();
assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
+ ctx = new SchemaContextImpl(newModules, Collections.emptyMap());
assertSetEquals(newModules, ctx.getModules());
// ##########
List<InputStream> streams = new ArrayList<>();
}
newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles)).getModules();
assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
+ ctx = new SchemaContextImpl(newModules, Collections.emptyMap());
assertSetEquals(newModules, ctx.getModules());
// ##########
streams.clear();
}
newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles), null).getModules();
assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
- assertSetEquals(newModules, ctx.getModules());
- // ##########
- Map<File, Module> mapped = parser.parseYangModelsMapped(testFiles);
- newModules = new LinkedHashSet<>(mapped.values());
- assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
- assertSetEquals(newModules, ctx.getModules());
- // ##########
- streams.clear();
- for (File f : testFiles) {
- streams.add(new FileInputStream(f));
- }
- Map<InputStream, Module> mappedStreams = parser.parseYangModelsFromStreamsMapped(streams);
- newModules = new LinkedHashSet<>(mappedStreams.values());
- assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
+ ctx = new SchemaContextImpl(newModules, Collections.emptyMap());
assertSetEquals(newModules, ctx.getModules());
}
- private void checkOrder(final Collection<Module> modules) {
+ private static void checkOrder(final Collection<Module> modules) {
Iterator<Module> it = modules.iterator();
Module m = it.next();
assertEquals("m2", m.getName());
assertEquals("m1", m.getName());
}
- private void assertSetEquals(final Set<Module> s1, final Set<Module> s2) {
+ private static void assertSetEquals(final Set<Module> s1, final Set<Module> s2) {
assertEquals(s1, s2);
Iterator<Module> it = s1.iterator();
for (Module m : s2) {
assertEquals(2, foo.getAugmentations().size());
}
+ @Test
+ public void unknownStatementInSubmoduleHeaderTest() throws IOException, URISyntaxException {
+
+ File yang = new File(getClass().getResource("/yang-grammar-test/submodule-header-extension.yang").toURI());
+
+ try {
+ YangParserImpl.getInstance().parseFile(yang, yang.getParentFile());
+ } catch (YangSyntaxErrorException | YangParseException e) {
+ e.printStackTrace();
+ fail("YangSyntaxErrorException or YangParseException should not be thrown");
+ }
+
+ }
+
+ @Test
+ public void unknownStatementBetweenRevisionsTest() throws IOException, URISyntaxException {
+
+ File yangModul = new File(getClass().getResource("/yang-grammar-test/revisions-extension.yang").toURI());
+ File yangSubmodul = new File(getClass().getResource("/yang-grammar-test/submodule-header-extension.yang")
+ .toURI());
+
+ List<File> yangs = new ArrayList<>();
+ yangs.add(yangModul);
+ yangs.add(yangSubmodul);
+
+ try {
+ YangParserImpl.getInstance().parseFiles(yangs);
+ } catch (YangParseException e) {
+ e.printStackTrace();
+ fail("YangParseException should not be thrown");
+ }
+ }
+
+ @Test
+ public void unknownStatementsInStatementsTest() throws IOException, URISyntaxException {
+ File yangModule1 = new File(getClass().getResource("/yang-grammar-test/stmtsep-in-statements.yang").toURI());
+ File yangModule2 = new File(getClass().getResource("/yang-grammar-test/stmtsep-in-statements2.yang").toURI());
+ File yangSubModule = new File(getClass().getResource("/yang-grammar-test/stmtsep-in-statements-sub.yang").toURI());
+
+ List<File> yangs = new ArrayList<>();
+ yangs.add(yangModule1);
+ yangs.add(yangModule2);
+ yangs.add(yangSubModule);
+
+ try {
+ YangParserImpl.getInstance().parseFiles(yangs);
+ } catch (YangParseException e) {
+ e.printStackTrace();
+ fail("YangParseException should not be thrown");
+ }
+ }
}