2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.codec.gson;
10 import static org.junit.Assert.assertTrue;
11 import com.google.gson.JsonArray;
12 import com.google.gson.JsonElement;
13 import com.google.gson.JsonObject;
14 import com.google.gson.JsonParser;
15 import com.google.gson.JsonPrimitive;
16 import java.io.BufferedReader;
18 import java.io.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import java.net.URISyntaxException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
36 public class TestUtils {
41 static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException,
43 URI path = StreamToNormalizedNodeTest.class.getResource(resourceDirectory).toURI();
44 final File testDir = new File(path);
45 final String[] fileList = testDir.list();
46 final List<File> testFiles = new ArrayList<>();
47 if (fileList == null) {
48 throw new FileNotFoundException(resourceDirectory);
50 for (String fileName : fileList) {
51 if (!new File(testDir, fileName).isDirectory()) {
52 testFiles.add(new File(testDir, fileName));
55 return parseYangSources(testFiles);
58 public static SchemaContext parseYangSources(StatementStreamSource... sources)
59 throws SourceException, ReactorException {
61 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
63 reactor.addSources(sources);
65 return reactor.buildEffective();
68 public static SchemaContext parseYangSources(File... files) throws SourceException, ReactorException, FileNotFoundException {
70 StatementStreamSource[] sources = new StatementStreamSource[files.length];
72 for(int i = 0; i<files.length; i++) {
73 sources[i] = new YangStatementSourceImpl(new FileInputStream(files[i]));
76 return parseYangSources(sources);
79 public static SchemaContext parseYangSources(Collection<File> files) throws SourceException, ReactorException, FileNotFoundException {
80 return parseYangSources(files.toArray(new File[files.size()]));
84 public static SchemaContext parseYangStreams(List<InputStream> streams)
85 throws SourceException, ReactorException {
87 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
89 return reactor.buildEffective(streams);
92 static String loadTextFile(final File file) throws IOException {
93 FileReader fileReader = new FileReader(file);
94 BufferedReader bufReader = new BufferedReader(fileReader);
97 StringBuilder result = new StringBuilder();
98 while ((line = bufReader.readLine()) != null) {
102 return result.toString();
105 static String loadTextFile(final String relativePath) throws IOException, URISyntaxException {
106 return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
109 static JsonObject childObject(final JsonObject jsonObject, final String... names) {
110 for (String name : names) {
111 JsonObject childJsonObject = jsonObject.getAsJsonObject(name);
112 if (childJsonObject != null) {
113 return childJsonObject;
119 static JsonPrimitive childPrimitive(final JsonObject jsonObject, final String... names) {
120 for (String name : names) {
121 JsonPrimitive childJsonPrimitive = jsonObject.getAsJsonPrimitive(name);
122 if (childJsonPrimitive != null) {
123 return childJsonPrimitive;
129 static JsonArray childArray(final JsonObject jsonObject, final String... names) {
130 for (String name : names) {
131 JsonArray childJsonArray = jsonObject.getAsJsonArray(name);
132 if (childJsonArray != null) {
133 return childJsonArray;
139 static JsonObject resolveCont1(String jsonOutput) {
140 JsonParser parser = new JsonParser();
141 JsonElement rootElement = parser.parse(jsonOutput);
142 assertTrue(rootElement.isJsonObject());
143 JsonObject rootObject = rootElement.getAsJsonObject();
144 JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");