Bug 7159: Add yang-test-util artifact
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / TestUtils.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson;
9
10 import static org.junit.Assert.assertTrue;
11
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;
18 import java.io.File;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.net.URISyntaxException;
22
23 public class TestUtils {
24
25     private TestUtils() {
26     }
27
28     static String loadTextFile(final File file) throws IOException {
29         FileReader fileReader = new FileReader(file);
30         BufferedReader bufReader = new BufferedReader(fileReader);
31
32         String line = null;
33         StringBuilder result = new StringBuilder();
34         while ((line = bufReader.readLine()) != null) {
35             result.append(line);
36         }
37         bufReader.close();
38         return result.toString();
39     }
40
41     static String loadTextFile(final String relativePath) throws IOException, URISyntaxException {
42         return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
43     }
44
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;
50             }
51         }
52         return null;
53     }
54
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;
60             }
61         }
62         return null;
63     }
64
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;
70             }
71         }
72         return null;
73     }
74
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");
81         return cont1;
82     }
83
84 }