BUG 1440 - additional tests for data-codec-gson
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / TestUtils.java
1 /*
2  * Copyright (c) 2014 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.FileNotFoundException;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29
30 public class TestUtils {
31
32     private TestUtils() {
33     }
34
35     static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException {
36         YangContextParser parser = new YangParserImpl();
37         URI path = StreamToNormalizedNodeTest.class.getResource(resourceDirectory).toURI();
38         final File testDir = new File(path);
39         final String[] fileList = testDir.list();
40         final List<File> testFiles = new ArrayList<File>();
41         if (fileList == null) {
42             throw new FileNotFoundException(resourceDirectory);
43         }
44         for (String fileName : fileList) {
45             if (new File(testDir, fileName).isDirectory() == false) {
46                 testFiles.add(new File(testDir, fileName));
47             }
48         }
49         return parser.parseFiles(testFiles);
50     }
51
52     static String loadTextFile(final File file) throws IOException {
53         FileReader fileReader = new FileReader(file);
54         BufferedReader bufReader = new BufferedReader(fileReader);
55
56         String line = null;
57         StringBuilder result = new StringBuilder();
58         while ((line = bufReader.readLine()) != null) {
59             result.append(line);
60         }
61         bufReader.close();
62         return result.toString();
63     }
64
65     static String loadTextFile(final String relativePath) throws IOException, URISyntaxException {
66         return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
67     }
68
69     static JsonObject childObject(final JsonObject jsonObject,final String ... names) {
70         for (String name : names) {
71             JsonObject childJsonObject = jsonObject.getAsJsonObject(name);
72             if (childJsonObject != null) {
73                 return childJsonObject;
74             }
75         }
76         return null;
77     }
78
79     static JsonPrimitive childPrimitive(final JsonObject jsonObject,final String ... names) {
80         for (String name : names) {
81             JsonPrimitive childJsonPrimitive = jsonObject.getAsJsonPrimitive(name);
82             if (childJsonPrimitive != null) {
83                 return childJsonPrimitive;
84             }
85         }
86         return null;
87     }
88
89     static JsonArray childArray(final JsonObject jsonObject,final String ... names) {
90         for (String name : names) {
91             JsonArray childJsonArray = jsonObject.getAsJsonArray(name);
92             if (childJsonArray != null) {
93                 return childJsonArray;
94             }
95         }
96         return null;
97     }
98
99     static JsonObject resolveCont1(String jsonOutput) {
100         JsonParser parser = new JsonParser();
101         JsonElement rootElement = parser.parse(jsonOutput);
102         assertTrue(rootElement.isJsonObject());
103         JsonObject rootObject = rootElement.getAsJsonObject();
104         JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");
105         return cont1;
106     }
107
108
109 }