*/
package org.opendaylight.yangtools.yang.parser.impl;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.*;
+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.ConstraintDefinition;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Deviation;
import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
+import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
+import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
+import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
+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.ModuleIdentifier;
+import org.opendaylight.yangtools.yang.model.api.ModuleImport;
+import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
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.RangeConstraint;
-import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
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.StringType;
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;
public class YangParserTest {
public static final String FS = File.separator;
private Set<Module> modules;
@Before
- public void init() throws FileNotFoundException, ParseException {
+ public void init() throws Exception {
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
fooRev = simpleDateFormat.parse("2013-02-27");
barRev = simpleDateFormat.parse("2013-07-03");
bazRev = simpleDateFormat.parse("2013-02-27");
- modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
+ modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
assertEquals(3, modules.size());
}
List<QName> path = new ArrayList<>();
path.add(new QName(barNS, barRev, "br", "interfaces"));
path.add(new QName(barNS, barRev, "br", "ifEntry"));
- SchemaPath expectedPath = new SchemaPath(path, true);
+ SchemaPath expectedPath = SchemaPath.create(path, true);
assertEquals(expectedPath, dev.getTargetPath());
assertEquals(Deviate.ADD, dev.getDeviate());
assertEquals("int32-ext1", int32TypedefQName.getLocalName());
SchemaPath typeSchemaPath = int32ext1.getPath();
- List<QName> typePath = typeSchemaPath.getPath();
- assertEquals(1, typePath.size());
- assertEquals(int32TypedefQName, typePath.get(0));
+ Iterable<QName> typePath = typeSchemaPath.getPathFromRoot();
+ Iterator<QName> typePathIt = typePath.iterator();
+ assertEquals(int32TypedefQName, typePathIt.next());
+ assertFalse(typePathIt.hasNext());
+
// int32-ext1/int32
Int32 int32 = (Int32) int32ext1.getBaseType();
assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
SchemaPath typeSchemaPath = myDecType.getPath();
- List<QName> typePath = typeSchemaPath.getPath();
- assertEquals(1, typePath.size());
- assertEquals(myDecTypeQName, typePath.get(0));
+ Iterable<QName> typePath = typeSchemaPath.getPathFromRoot();
+ Iterator<QName> typePathIt = typePath.iterator();
+ assertEquals(myDecTypeQName, typePathIt.next());
+ assertFalse(typePathIt.hasNext());
// my-base-int32-type/int32
Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
assertEquals("decimal64", dec64QName.getLocalName());
SchemaPath dec64SchemaPath = dec64.getPath();
- List<QName> dec64Path = dec64SchemaPath.getPath();
- assertEquals(2, dec64Path.size());
- assertEquals(myDecTypeQName, dec64Path.get(0));
- assertEquals(dec64QName, dec64Path.get(1));
+ Iterable<QName> dec64Path = dec64SchemaPath.getPathFromRoot();
+ Iterator<QName> dec64PathIt = dec64Path.iterator();
+ assertEquals(myDecTypeQName, dec64PathIt.next());
+ assertEquals(dec64QName, dec64PathIt.next());
+ assertFalse(dec64PathIt.hasNext());
}
@Test
- public void testParseMethod1() throws ParseException {
- File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
- File dependenciesDir = new File(getClass().getResource("/parse-methods").getPath());
- YangModelParser parser = new YangParserImpl();
- modules = parser.parseYangModels(yangFile, dependenciesDir);
+ public void testParseMethod1() throws Exception {
+ File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI());
+ File dependenciesDir = new File(getClass().getResource("/parse-methods").toURI());
+ YangContextParser parser = new YangParserImpl();
+ modules = parser.parseFile(yangFile, dependenciesDir).getModules();
assertEquals(6, modules.size());
}
@Test
- public void testParseMethod2() throws ParseException {
- File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
- File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").getPath());
- YangModelParser parser = new YangParserImpl();
- modules = parser.parseYangModels(yangFile, dependenciesDir);
+ public void testParseMethod2() throws Exception {
+ File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI());
+ File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").toURI());
+ YangContextParser parser = new YangParserImpl();
+ modules = parser.parseFile(yangFile, dependenciesDir).getModules();
assertEquals(6, modules.size());
}
@Test
- public void testSorting() throws FileNotFoundException {
+ public void testSorting() throws Exception {
// Correct order: m2, m4, m6, m8, m7, m6, m3, m1
- File yangFile = new File(getClass().getResource("/sorting-test/m1.yang").getPath());
- File dependenciesDir = new File(getClass().getResource("/sorting-test").getPath());
- YangModelParser parser = new YangParserImpl();
- modules = parser.parseYangModels(yangFile, dependenciesDir);
- SchemaContext ctx = new SchemaContextImpl(modules);
+ File yangFile = new File(getClass().getResource("/sorting-test/m1.yang").toURI());
+ 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());
checkOrder(modules);
assertSetEquals(modules, ctx.getModules());
for (String fileName : fileList) {
testFiles.add(new File(testDir, fileName));
}
- Set<Module> newModules = parser.parseYangModels(testFiles);
+ Set<Module> newModules = parser.parseFiles(testFiles).getModules();
assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
assertSetEquals(newModules, ctx.getModules());
// ##########
- newModules = parser.parseYangModels(testFiles, null);
+ newModules = parser.parseFiles(testFiles, null).getModules();
assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
assertSetEquals(newModules, ctx.getModules());
// ##########
List<InputStream> streams = new ArrayList<>();
for (File f : testFiles) {
streams.add(new FileInputStream(f));
}
- newModules = parser.parseYangModelsFromStreams(streams);
+ newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles)).getModules();
assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
assertSetEquals(newModules, ctx.getModules());
// ##########
streams.clear();
for (File f : testFiles) {
streams.add(new FileInputStream(f));
}
- newModules = parser.parseYangModelsFromStreams(streams, null);
+ newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles), null).getModules();
assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules);
+ 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);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
assertSetEquals(newModules, ctx.getModules());
// ##########
streams.clear();
Map<InputStream, Module> mappedStreams = parser.parseYangModelsFromStreamsMapped(streams);
newModules = new LinkedHashSet<>(mappedStreams.values());
assertSetEquals(newModules, modules);
- ctx = new SchemaContextImpl(newModules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
assertSetEquals(newModules, ctx.getModules());
}
- private void checkOrder(Collection<Module> modules) {
+ private 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(Set<Module> s1, Set<Module> s2) {
+ private void assertSetEquals(final Set<Module> s1, final Set<Module> s2) {
assertEquals(s1, s2);
Iterator<Module> it = s1.iterator();
for (Module m : s2) {
}
@Test
- public void testSubmodules() {
- String yangFilePath = getClass().getResource(FS + "submodule-test" + FS + "subfoo.yang").getPath();
- String directoryPath = getClass().getResource(FS + "model").getPath();
+ public void testSubmodules() throws Exception {
+ URI yangFilePath = getClass().getResource("/submodule-test/subfoo.yang").toURI();
+ URI directoryPath = getClass().getResource("/model").toURI();
File directory = new File(directoryPath);
File yangFile = new File(yangFilePath);
- Set<Module> modules = new YangParserImpl().parseYangModels(yangFile, directory);
+ Set<Module> modules = new YangParserImpl().parseFile(yangFile, directory).getModules();
assertEquals(3, modules.size());
Module foo = TestUtils.findModule(modules, "foo");