2 * Copyright (c) 2015 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.retest;
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.FileNotFoundException;
19 import java.io.FileReader;
20 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 public class TestUtils {
33 static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException,
35 URI path = StreamToNormalizedNodeTest.class.getResource(resourceDirectory).toURI();
36 final File testDir = new File(path);
37 final String[] fileList = testDir.list();
38 final List<File> testFiles = new ArrayList<>();
39 if (fileList == null) {
40 throw new FileNotFoundException(resourceDirectory);
42 for (String fileName : fileList) {
43 if (!new File(testDir, fileName).isDirectory()) {
44 testFiles.add(new File(testDir, fileName));
47 return RetestUtils.parseYangSources(testFiles);
50 static String loadTextFile(final File file) throws IOException {
51 FileReader fileReader = new FileReader(file);
52 BufferedReader bufReader = new BufferedReader(fileReader);
55 StringBuilder result = new StringBuilder();
56 while ((line = bufReader.readLine()) != null) {
60 return result.toString();
63 static String loadTextFile(final String relativePath) throws IOException, URISyntaxException {
64 return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
67 static JsonObject childObject(final JsonObject jsonObject, final String... names) {
68 for (String name : names) {
69 JsonObject childJsonObject = jsonObject.getAsJsonObject(name);
70 if (childJsonObject != null) {
71 return childJsonObject;
77 static JsonPrimitive childPrimitive(final JsonObject jsonObject, final String... names) {
78 for (String name : names) {
79 JsonPrimitive childJsonPrimitive = jsonObject.getAsJsonPrimitive(name);
80 if (childJsonPrimitive != null) {
81 return childJsonPrimitive;
87 static JsonArray childArray(final JsonObject jsonObject, final String... names) {
88 for (String name : names) {
89 JsonArray childJsonArray = jsonObject.getAsJsonArray(name);
90 if (childJsonArray != null) {
91 return childJsonArray;
97 static JsonObject resolveCont1(String jsonOutput) {
98 JsonParser parser = new JsonParser();
99 JsonElement rootElement = parser.parse(jsonOutput);
100 assertTrue(rootElement.isJsonObject());
101 JsonObject rootObject = rootElement.getAsJsonObject();
102 JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");