Yang parser refactoring.
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / TestUtils.java
index 96053b6b7bb5b5f128971cbef41c0c0c0e8bf936..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() {
     }
 
@@ -72,13 +54,20 @@ final class TestUtils {
         return modules.iterator().next();
     }
 
-    public static Module loadModuleWithContext(final InputStream stream, final SchemaContext context)
+    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<>(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)
@@ -146,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());
@@ -158,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);
+            }
         }
     }