Drop unneeded generic type specifiers
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / retest / TestUtils.java
1 /*
2  * Copyright (c) 2015 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.retest;
9
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;
17 import java.io.File;
18 import java.io.FileNotFoundException;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.net.URI;
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;
27
28 public class TestUtils {
29
30     private TestUtils() {
31     }
32
33     static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException,
34             ReactorException {
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);
41         }
42         for (String fileName : fileList) {
43             if (!new File(testDir, fileName).isDirectory()) {
44                 testFiles.add(new File(testDir, fileName));
45             }
46         }
47         return RetestUtils.parseYangSources(testFiles);
48     }
49
50     static String loadTextFile(final File file) throws IOException {
51         FileReader fileReader = new FileReader(file);
52         BufferedReader bufReader = new BufferedReader(fileReader);
53
54         String line = null;
55         StringBuilder result = new StringBuilder();
56         while ((line = bufReader.readLine()) != null) {
57             result.append(line);
58         }
59         bufReader.close();
60         return result.toString();
61     }
62
63     static String loadTextFile(final String relativePath) throws IOException, URISyntaxException {
64         return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
65     }
66
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;
72             }
73         }
74         return null;
75     }
76
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;
82             }
83         }
84         return null;
85     }
86
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;
92             }
93         }
94         return null;
95     }
96
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");
103         return cont1;
104     }
105
106 }