Yang parser refactoring.
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / TestUtils.java
index 5d690280a04543d43387730a974cb5a1ecda3e23..760d0407ed77aa55916b7c81d607e18f03c360c9 100644 (file)
@@ -9,35 +9,17 @@ package org.opendaylight.yangtools.yang.parser.impl;
 
 import static org.junit.Assert.assertEquals;
 
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.URI;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.text.*;
+import java.util.*;
 
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.Module;
-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.api.*;
 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
 
 final class TestUtils {
 
-    static final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
-
     private TestUtils() {
     }
 
@@ -45,12 +27,11 @@ final class TestUtils {
         YangModelParser parser = new YangParserImpl();
         final File testDir = new File(resourceDirectory);
         final String[] fileList = testDir.list();
-        final List<File> testFiles = new ArrayList<File>();
-        if(fileList == null) {
+        final List<File> testFiles = new ArrayList<>();
+        if (fileList == null) {
             throw new FileNotFoundException(resourceDirectory);
         }
-        for (int i = 0; i < fileList.length; i++) {
-            String fileName = fileList[i];
+        for (String fileName : fileList) {
             testFiles.add(new File(testDir, fileName));
         }
         return parser.parseYangModels(testFiles);
@@ -58,37 +39,43 @@ final class TestUtils {
 
     public static Set<Module> loadModules(List<InputStream> input) throws IOException {
         final YangModelParser parser = new YangParserImpl();
-        final Set<Module> modules = new HashSet<Module>(
-                parser.parseYangModelsFromStreams(input));
-        for(InputStream stream : input) {
+        final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
+        for (InputStream stream : input) {
             stream.close();
         }
         return modules;
     }
 
-    public static Module loadModule(final InputStream stream) throws
-            IOException {
+    public static Module loadModule(final InputStream stream) throws IOException {
         final YangModelParser parser = new YangParserImpl();
         final List<InputStream> input = Collections.singletonList(stream);
-        final Set<Module> modules = new HashSet<Module>(
-                parser.parseYangModelsFromStreams(input));
+        final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
         stream.close();
         return modules.iterator().next();
     }
 
-    public static Module loadModuleWithContext(final InputStream stream, final SchemaContext context) throws IOException {
+    public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
+            throws IOException {
         final YangModelParser parser = new YangParserImpl();
         final List<InputStream> input = Collections.singletonList(stream);
-        final Set<Module> modules = new HashSet<Module>(parser.parseYangModelsFromStreams(input, context));
+        final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
         stream.close();
-        return modules.iterator().next();
+        Module result = null;
+        for (Module module : modules) {
+            if (module.getName().equals(name)) {
+                result = module;
+                break;
+            }
+        }
+        return result;
     }
 
-    public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context) throws IOException {
+    public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context)
+            throws IOException {
         final YangModelParser parser = new YangParserImpl();
-        final Set<Module> modules = new HashSet<Module>(parser.parseYangModelsFromStreams(input, context));
-        for(InputStream is : input) {
-            if(is != null) {
+        final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
+        for (InputStream is : input) {
+            if (is != null) {
                 is.close();
             }
         }
@@ -106,8 +93,7 @@ final class TestUtils {
         return result;
     }
 
-    public static ModuleImport findImport(Set<ModuleImport> imports,
-            String prefix) {
+    public static ModuleImport findImport(Set<ModuleImport> imports, String prefix) {
         ModuleImport result = null;
         for (ModuleImport moduleImport : imports) {
             if (moduleImport.getPrefix().equals(prefix)) {
@@ -118,8 +104,7 @@ final class TestUtils {
         return result;
     }
 
-    public static TypeDefinition<?> findTypedef(
-            Set<TypeDefinition<?>> typedefs, String name) {
+    public static TypeDefinition<?> findTypedef(Set<TypeDefinition<?>> typedefs, String name) {
         TypeDefinition<?> result = null;
         for (TypeDefinition<?> td : typedefs) {
             if (td.getQName().getLocalName().equals(name)) {
@@ -130,9 +115,8 @@ final class TestUtils {
         return result;
     }
 
-    public static SchemaPath createPath(boolean absolute, URI namespace,
-            Date revision, String prefix, String... names) {
-        List<QName> path = new ArrayList<QName>();
+    public static SchemaPath createPath(boolean absolute, URI namespace, Date revision, String prefix, String... names) {
+        List<QName> path = new ArrayList<>();
         for (String name : names) {
             path.add(new QName(namespace, revision, prefix, name));
         }
@@ -151,11 +135,13 @@ final class TestUtils {
     }
 
     /**
-     * Test if node has augmenting flag set to true. In case this is
-     * DataNodeContainer, check his child nodes too.
+     * Test if node has augmenting flag set to expected value. In case this is
+     * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
      *
      * @param node
      *            node to check
+     * @param expected
+     *            expected value
      */
     public static void checkIsAugmenting(DataSchemaNode node, boolean expected) {
         assertEquals(expected, node.isAugmenting());
@@ -163,6 +149,45 @@ final class TestUtils {
             for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
                 checkIsAugmenting(child, expected);
             }
+        } else if (node instanceof ChoiceNode) {
+            for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
+                checkIsAugmenting(caseNode, expected);
+            }
+        }
+    }
+
+    /**
+     * Check if node has addedByUses flag set to expected value. In case this is
+     * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
+     *
+     * @param node
+     *            node to check
+     * @param expected
+     *            expected value
+     */
+    public static void checkIsAddedByUses(DataSchemaNode node, boolean expected) {
+        assertEquals(expected, node.isAddedByUses());
+        if (node instanceof DataNodeContainer) {
+            for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
+                checkIsAddedByUses(child, expected);
+            }
+        } else if (node instanceof ChoiceNode) {
+            for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
+                checkIsAddedByUses(caseNode, expected);
+            }
+        }
+    }
+
+    public static void checkIsAddedByUses(GroupingDefinition node, boolean expected) {
+        assertEquals(expected, node.isAddedByUses());
+        if (node instanceof DataNodeContainer) {
+            for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
+                checkIsAddedByUses(child, expected);
+            }
+        } else if (node instanceof ChoiceNode) {
+            for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
+                checkIsAddedByUses(caseNode, expected);
+            }
         }
     }