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;
12 import com.google.gson.JsonArray;
13 import com.google.gson.JsonElement;
14 import com.google.gson.JsonObject;
15 import com.google.gson.JsonParser;
16 import com.google.gson.JsonPrimitive;
17 import java.io.BufferedReader;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.net.URISyntaxException;
23 public class TestUtils {
28 static String loadTextFile(final File file) throws IOException {
29 FileReader fileReader = new FileReader(file);
30 BufferedReader bufReader = new BufferedReader(fileReader);
33 StringBuilder result = new StringBuilder();
34 while ((line = bufReader.readLine()) != null) {
38 return result.toString();
41 static String loadTextFile(final String relativePath) throws IOException, URISyntaxException {
42 return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
45 static JsonObject childObject(final JsonObject jsonObject, final String... names) {
46 for (String name : names) {
47 JsonObject childJsonObject = jsonObject.getAsJsonObject(name);
48 if (childJsonObject != null) {
49 return childJsonObject;
55 static JsonPrimitive childPrimitive(final JsonObject jsonObject, final String... names) {
56 for (String name : names) {
57 JsonPrimitive childJsonPrimitive = jsonObject.getAsJsonPrimitive(name);
58 if (childJsonPrimitive != null) {
59 return childJsonPrimitive;
65 static JsonArray childArray(final JsonObject jsonObject, final String... names) {
66 for (String name : names) {
67 JsonArray childJsonArray = jsonObject.getAsJsonArray(name);
68 if (childJsonArray != null) {
69 return childJsonArray;
75 static JsonObject resolveCont1(String jsonOutput) {
76 JsonParser parser = new JsonParser();
77 JsonElement rootElement = parser.parse(jsonOutput);
78 assertTrue(rootElement.isJsonObject());
79 JsonObject rootObject = rootElement.getAsJsonObject();
80 JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");