2 * Copyright (c) 2014 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.FileNotFoundException;
20 import java.io.FileReader;
21 import java.io.IOException;
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;
30 public class TestUtils {
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<>();
41 if (fileList == null) {
42 throw new FileNotFoundException(resourceDirectory);
44 for (String fileName : fileList) {
45 if (!new File(testDir, fileName).isDirectory()) {
46 testFiles.add(new File(testDir, fileName));
49 return parser.parseFiles(testFiles);
52 static String loadTextFile(final File file) throws IOException {
53 FileReader fileReader = new FileReader(file);
54 BufferedReader bufReader = new BufferedReader(fileReader);
57 StringBuilder result = new StringBuilder();
58 while ((line = bufReader.readLine()) != null) {
62 return result.toString();
65 static String loadTextFile(final String relativePath) throws IOException, URISyntaxException {
66 return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
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;
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;
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;
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");