Scripted update of if statements
[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 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.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
35
36 public class TestUtils {
37
38     private TestUtils() {
39     }
40
41     static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException,
42             ReactorException {
43         URI path = StreamToNormalizedNodeTest.class.getResource(resourceDirectory).toURI();
44         final File testDir = new File(path);
45         final String[] fileList = testDir.list();
46         final List<File> testFiles = new ArrayList<>();
47         if (fileList == null) {
48             throw new FileNotFoundException(resourceDirectory);
49         }
50         for (String fileName : fileList) {
51             if (!new File(testDir, fileName).isDirectory()) {
52                 testFiles.add(new File(testDir, fileName));
53             }
54         }
55         return parseYangSources(testFiles);
56     }
57
58     public static SchemaContext parseYangSources(StatementStreamSource... sources)
59             throws SourceException, ReactorException {
60
61         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
62                 .newBuild();
63         reactor.addSources(sources);
64
65         return reactor.buildEffective();
66     }
67
68     public static SchemaContext parseYangSources(File... files) throws SourceException, ReactorException, FileNotFoundException {
69
70         StatementStreamSource[] sources = new StatementStreamSource[files.length];
71
72         for (int i = 0; i<files.length; i++) {
73             sources[i] = new YangStatementSourceImpl(new FileInputStream(files[i]));
74         }
75
76         return parseYangSources(sources);
77     }
78
79     public static SchemaContext parseYangSources(Collection<File> files) throws SourceException, ReactorException, FileNotFoundException {
80         return parseYangSources(files.toArray(new File[files.size()]));
81     }
82
83
84     public static SchemaContext parseYangStreams(List<InputStream> streams)
85             throws SourceException, ReactorException {
86
87         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
88                 .newBuild();
89         return reactor.buildEffective(streams);
90     }
91
92     static String loadTextFile(final File file) throws IOException {
93         FileReader fileReader = new FileReader(file);
94         BufferedReader bufReader = new BufferedReader(fileReader);
95
96         String line = null;
97         StringBuilder result = new StringBuilder();
98         while ((line = bufReader.readLine()) != null) {
99             result.append(line);
100         }
101         bufReader.close();
102         return result.toString();
103     }
104
105     static String loadTextFile(final String relativePath) throws IOException, URISyntaxException {
106         return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
107     }
108
109     static JsonObject childObject(final JsonObject jsonObject, final String... names) {
110         for (String name : names) {
111             JsonObject childJsonObject = jsonObject.getAsJsonObject(name);
112             if (childJsonObject != null) {
113                 return childJsonObject;
114             }
115         }
116         return null;
117     }
118
119     static JsonPrimitive childPrimitive(final JsonObject jsonObject, final String... names) {
120         for (String name : names) {
121             JsonPrimitive childJsonPrimitive = jsonObject.getAsJsonPrimitive(name);
122             if (childJsonPrimitive != null) {
123                 return childJsonPrimitive;
124             }
125         }
126         return null;
127     }
128
129     static JsonArray childArray(final JsonObject jsonObject, final String... names) {
130         for (String name : names) {
131             JsonArray childJsonArray = jsonObject.getAsJsonArray(name);
132             if (childJsonArray != null) {
133                 return childJsonArray;
134             }
135         }
136         return null;
137     }
138
139     static JsonObject resolveCont1(String jsonOutput) {
140         JsonParser parser = new JsonParser();
141         JsonElement rootElement = parser.parse(jsonOutput);
142         assertTrue(rootElement.isJsonObject());
143         JsonObject rootObject = rootElement.getAsJsonObject();
144         JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");
145         return cont1;
146     }
147
148 }