2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tplinksmarthome.internal.model;
15 import java.io.IOException;
16 import java.nio.charset.StandardCharsets;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import com.google.gson.Gson;
23 * Util class for reading test resources.
25 * @author Hilbrand Bouwkamp - Initial contribution
28 public final class ModelTestUtil {
30 public static final Gson GSON = GsonUtil.createGson();
32 private ModelTestUtil() {
37 * Util method to read a json file into it's data class.
39 * @param <T> Type of the class the json data represents.
40 * @param gson gson class
41 * @param filename filename of the json file to read. The file is read relative to the directory of this class
42 * @param clazz Data class expected to be read from the json file
43 * @return instance of clazz with read data from json file
44 * @throws IOException when file could not be read.
46 public static <T> T jsonFromFile(String filename, Class<T> clazz) throws IOException {
47 return GSON.fromJson(readJson(filename), clazz);
51 * Util method to read a json file. It normalizes the string by removing returns, tabs and spaces. It's not very
52 * smart as it removes spaces inside json values as well. But this method is mainly intended be able to compare 2
55 * @param filename filename of the json file to read. The file is read relative to the directory of this class
56 * @return read json string
57 * @throws IOException when file could not be read.
59 public static String readJson(String filename) throws IOException {
60 return new String(ModelTestUtil.class.getResourceAsStream(filename + ".json").readAllBytes(),
61 StandardCharsets.UTF_8).replaceAll("[\n\r\t ]", "");