Bug 5085: Clean-up test and retest JUnit tests
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / TestUtils.java
index c5b6d80c2e775f635e6ab39f9245c5c8cbb339ed..0e13ecbbfe8e18e1f7be52a6de1419958b4e34ff 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,7 +8,6 @@
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
 import static org.junit.Assert.assertTrue;
-
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
@@ -16,24 +15,31 @@ import com.google.gson.JsonParser;
 import com.google.gson.JsonPrimitive;
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+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.rfc6020.YangInferencePipeline;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
 
 public class TestUtils {
 
     private TestUtils() {
     }
 
-    static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException {
-        YangContextParser parser = new YangParserImpl();
+    static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException,
+            ReactorException {
         URI path = StreamToNormalizedNodeTest.class.getResource(resourceDirectory).toURI();
         final File testDir = new File(path);
         final String[] fileList = testDir.list();
@@ -46,7 +52,41 @@ public class TestUtils {
                 testFiles.add(new File(testDir, fileName));
             }
         }
-        return parser.parseFiles(testFiles);
+        return parseYangSources(testFiles);
+    }
+
+    public static SchemaContext parseYangSources(StatementStreamSource... sources)
+            throws SourceException, ReactorException {
+
+        CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
+                .newBuild();
+        reactor.addSources(sources);
+
+        return reactor.buildEffective();
+    }
+
+    public static SchemaContext parseYangSources(File... files) throws SourceException, ReactorException, FileNotFoundException {
+
+        StatementStreamSource[] sources = new StatementStreamSource[files.length];
+
+        for(int i = 0; i<files.length; i++) {
+            sources[i] = new YangStatementSourceImpl(new FileInputStream(files[i]));
+        }
+
+        return parseYangSources(sources);
+    }
+
+    public static SchemaContext parseYangSources(Collection<File> files) throws SourceException, ReactorException, FileNotFoundException {
+        return parseYangSources(files.toArray(new File[files.size()]));
+    }
+
+
+    public static SchemaContext parseYangStreams(List<InputStream> streams)
+            throws SourceException, ReactorException {
+
+        CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
+                .newBuild();
+        return reactor.buildEffective(streams);
     }
 
     static String loadTextFile(final File file) throws IOException {
@@ -66,7 +106,7 @@ public class TestUtils {
         return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
     }
 
-    static JsonObject childObject(final JsonObject jsonObject,final String ... names) {
+    static JsonObject childObject(final JsonObject jsonObject, final String... names) {
         for (String name : names) {
             JsonObject childJsonObject = jsonObject.getAsJsonObject(name);
             if (childJsonObject != null) {
@@ -76,7 +116,7 @@ public class TestUtils {
         return null;
     }
 
-    static JsonPrimitive childPrimitive(final JsonObject jsonObject,final String ... names) {
+    static JsonPrimitive childPrimitive(final JsonObject jsonObject, final String... names) {
         for (String name : names) {
             JsonPrimitive childJsonPrimitive = jsonObject.getAsJsonPrimitive(name);
             if (childJsonPrimitive != null) {
@@ -86,7 +126,7 @@ public class TestUtils {
         return null;
     }
 
-    static JsonArray childArray(final JsonObject jsonObject,final String ... names) {
+    static JsonArray childArray(final JsonObject jsonObject, final String... names) {
         for (String name : names) {
             JsonArray childJsonArray = jsonObject.getAsJsonArray(name);
             if (childJsonArray != null) {
@@ -105,5 +145,4 @@ public class TestUtils {
         return cont1;
     }
 
-
 }