Use QName.parseRevision() instead of SimpleDateFormatUtil
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / TestUtils.java
index a05080207fead888a15f52213f50ef70207080fc..322e59e493a780400a0f94f2268b1b0b824e2d3c 100644 (file)
@@ -10,20 +10,18 @@ package org.opendaylight.yangtools.yang.stmt;
 import static org.junit.Assert.assertEquals;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
+import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Date;
 import java.util.List;
+import java.util.Optional;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.YangConstants;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
@@ -34,17 +32,21 @@ import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
+import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
+import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinStatementStreamSource;
+import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinTextToDomTransformer;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
+import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YinStatementSourceImpl;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
-import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.xml.sax.SAXException;
 
 public final class TestUtils {
     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
@@ -52,87 +54,58 @@ public final class TestUtils {
     private TestUtils() {
     }
 
-    public static Set<Module> loadModules(final URI resourceDirectory)
-            throws SourceException, ReactorException {
+    public static SchemaContext loadModules(final URI resourceDirectory)
+            throws ReactorException, IOException, YangSyntaxErrorException {
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
                 .newBuild();
         File[] files = new File(resourceDirectory).listFiles();
 
         for (File file : files) {
-            if (file.getName().endsWith(".yang")) {
-                addSources(reactor, new YangStatementSourceImpl(file.getPath(), true));
+            if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
+                reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
             } else {
                 LOG.info("Ignoring non-yang file {}", file);
             }
         }
 
-        EffectiveSchemaContext ctx = reactor.buildEffective();
-        return ctx.getModules();
+        return reactor.buildEffective();
     }
 
-    public static Set<Module> loadModules(final List<InputStream> streams)
-        throws SourceException, ReactorException {
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
-            .newBuild();
-        for (InputStream inputStream : streams) {
-            addSources(reactor, new YangStatementSourceImpl(inputStream));
+    public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
+            throws IOException, ReactorException, YangSyntaxErrorException {
+        final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
+
+        for (String resourceName : resourceNames) {
+            reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass, resourceName)));
         }
 
-        EffectiveSchemaContext ctx = reactor.buildEffective();
-        return ctx.getModules();
+        return reactor.buildEffective();
     }
 
-    public static Set<Module> loadYinModules(final URI resourceDirectory) throws ReactorException {
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
+    public static SchemaContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException,
+            IOException {
+        final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
         for (File file : new File(resourceDirectory).listFiles()) {
-            addYinSources(reactor, new YinStatementSourceImpl(file.getPath(), true));
-        }
-
-        EffectiveSchemaContext ctx = reactor.buildEffective();
-        return ctx.getModules();
-    }
-
-    public static Set<Module> loadYinModules(final List<InputStream> streams) throws SourceException, ReactorException {
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
-        for (InputStream inputStream : streams) {
-            addYinSources(reactor, new YinStatementSourceImpl(inputStream));
+            reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
+                YinTextSchemaSource.forFile(file))));
         }
 
-        EffectiveSchemaContext ctx = reactor.buildEffective();
-        return ctx.getModules();
-    }
-
-    public static Module loadModule(final InputStream stream)
-        throws SourceException, ReactorException {
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
-            .newBuild();
-        addSources(reactor, new YangStatementSourceImpl(stream));
-        EffectiveSchemaContext ctx = reactor.buildEffective();
-        return ctx.getModules().iterator().next();
+        return reactor.buildEffective();
     }
 
-    public static Module loadYinModule(final InputStream stream) throws SourceException, ReactorException {
+    public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException, IOException {
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
-        addYinSources(reactor, new YinStatementSourceImpl(stream));
-        EffectiveSchemaContext ctx = reactor.buildEffective();
+        reactor.addSources(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)));
+        SchemaContext ctx = reactor.buildEffective();
         return ctx.getModules().iterator().next();
     }
 
-    public static Module findModule(final Set<Module> modules,
-        final String moduleName) {
-        Module result = null;
-        for (Module module : modules) {
-            if (module.getName().equals(moduleName)) {
-                result = module;
-                break;
-            }
-        }
-        return result;
+    public static Optional<Module> findModule(final SchemaContext context, final String moduleName) {
+        return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
     }
 
-    public static ModuleImport findImport(final Set<ModuleImport> imports,
-            final String prefix) {
+    public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
         ModuleImport result = null;
         for (ModuleImport moduleImport : imports) {
             if (moduleImport.getPrefix().equals(prefix)) {
@@ -143,8 +116,7 @@ public final class TestUtils {
         return result;
     }
 
-    public static TypeDefinition<?> findTypedef(
-            final Set<TypeDefinition<?>> typedefs, final String name) {
+    public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
         TypeDefinition<?> result = null;
         for (TypeDefinition<?> td : typedefs) {
             if (td.getQName().getLocalName().equals(name)) {
@@ -155,27 +127,14 @@ public final class TestUtils {
         return result;
     }
 
-    public static SchemaPath createPath(final boolean absolute,
-            final URI namespace, final Date revision, final String prefix,
-            final String... names) {
-        List<QName> path = new ArrayList<>();
+    public static SchemaPath createPath(final boolean absolute, final QNameModule module, final String... names) {
+        List<QName> path = new ArrayList<>(names.length);
         for (String name : names) {
-            path.add(QName.create(namespace, revision, name));
+            path.add(QName.create(module, name));
         }
         return SchemaPath.create(path, absolute);
     }
 
-    public static Date createDate(final String date) {
-        Date result;
-        final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
-        try {
-            result = simpleDateFormat.parse(date);
-        } catch (ParseException e) {
-            result = null;
-        }
-        return result;
-    }
-
     /**
      * Test if node has augmenting flag set to expected value. In case this is
      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
@@ -185,8 +144,7 @@ public final class TestUtils {
      * @param expected
      *            expected value
      */
-    public static void checkIsAugmenting(final DataSchemaNode node,
-            final boolean expected) {
+    public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
         assertEquals(expected, node.isAugmenting());
         if (node instanceof DataNodeContainer) {
             for (DataSchemaNode child : ((DataNodeContainer) node)
@@ -209,8 +167,7 @@ public final class TestUtils {
      * @param expected
      *            expected value
      */
-    public static void checkIsAddedByUses(final DataSchemaNode node,
-            final boolean expected) {
+    public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
         assertEquals(expected, node.isAddedByUses());
         if (node instanceof DataNodeContainer) {
             for (DataSchemaNode child : ((DataNodeContainer) node)
@@ -224,16 +181,14 @@ public final class TestUtils {
         }
     }
 
-    public static void checkIsAddedByUses(final GroupingDefinition node,
-            final boolean expected) {
+    public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
         assertEquals(expected, node.isAddedByUses());
-        for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
+        for (DataSchemaNode child : node.getChildNodes()) {
             checkIsAddedByUses(child, expected);
         }
     }
 
-    public static List<Module> findModules(final Set<Module> modules,
-            final String moduleName) {
+    public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
         List<Module> result = new ArrayList<>();
         for (Module module : modules) {
             if (module.getName().equals(moduleName)) {
@@ -243,16 +198,7 @@ public final class TestUtils {
         return result;
     }
 
-    private static void addSources(
-            final CrossSourceStatementReactor.BuildAction reactor,
-            final YangStatementSourceImpl... sources) {
-        for (YangStatementSourceImpl source : sources) {
-            reactor.addSource(source);
-        }
-    }
-
-    public static SchemaContext parseYangSources(
-            final StatementStreamSource... sources) throws SourceException,
+    public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
             ReactorException {
 
         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
@@ -263,49 +209,37 @@ public final class TestUtils {
     }
 
     public static SchemaContext parseYangSources(final File... files)
-            throws SourceException, ReactorException, FileNotFoundException {
+            throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
 
         StatementStreamSource[] sources = new StatementStreamSource[files.length];
 
         for (int i = 0; i < files.length; i++) {
-            sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
+            sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
         }
 
         return parseYangSources(sources);
     }
 
     public static SchemaContext parseYangSources(final Collection<File> files)
-            throws SourceException, ReactorException, FileNotFoundException {
+            throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
         return parseYangSources(files.toArray(new File[files.size()]));
     }
 
     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
-            throws SourceException, ReactorException, FileNotFoundException,
-            URISyntaxException {
+            throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
 
-        URL resourceDir = StmtTestUtils.class
-                .getResource(yangSourcesDirectoryPath);
+        URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
         File testSourcesDir = new File(resourceDir.toURI());
 
         return parseYangSources(testSourcesDir.listFiles());
     }
 
     public static SchemaContext parseYangSource(final String yangSourceFilePath)
-            throws SourceException, ReactorException, FileNotFoundException,
-            URISyntaxException {
+            throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
 
-        URL resourceFile = StmtTestUtils.class
-                .getResource(yangSourceFilePath);
+        URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
         File testSourcesFile = new File(resourceFile.toURI());
 
         return parseYangSources(testSourcesFile);
     }
-
-    private static void addYinSources(final CrossSourceStatementReactor.BuildAction reactor,
-                                      final YinStatementSourceImpl... sources) {
-        for (YinStatementSourceImpl source : sources) {
-            reactor.addSource(source);
-        }
-    }
-
 }