2 * Copyright (c) 2010-2020 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.fmiweather;
15 import static org.junit.Assert.fail;
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.net.URISyntaxException;
22 import java.nio.charset.StandardCharsets;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.hamcrest.Description;
31 import org.hamcrest.Matcher;
32 import org.hamcrest.TypeSafeMatcher;
33 import org.junit.Before;
34 import org.openhab.binding.fmiweather.internal.client.Client;
35 import org.openhab.binding.fmiweather.internal.client.Data;
36 import org.openhab.binding.fmiweather.internal.client.FMIResponse;
37 import org.openhab.binding.fmiweather.internal.client.Location;
40 * Base class for response parsing tests
42 * @author Sami Salonen - Initial contribution
45 public class AbstractFMIResponseParsingTest {
48 protected Client client;
51 public void setUpClient() {
52 client = new Client();
55 protected Path getTestResource(String filename) {
57 return Paths.get(getClass().getResource(filename).toURI());
58 } catch (URISyntaxException e) {
60 // Make the compiler happy by throwing here, fails already above
61 throw new IllegalStateException();
65 protected String readTestResourceUtf8(String filename) {
66 return readTestResourceUtf8(getTestResource(filename));
69 protected String readTestResourceUtf8(Path path) {
71 BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
72 StringBuilder content = new StringBuilder();
73 char[] buffer = new char[1024];
75 while ((read = reader.read(buffer)) != -1) {
76 content.append(buffer, 0, read);
78 return content.toString();
79 } catch (IOException e) {
81 // Make the compiler happy by throwing here, fails already above
82 throw new IllegalStateException();
86 protected static TypeSafeMatcher<Location> deeplyEqualTo(Location location) {
87 return new ResponseLocationMatcher(location);
90 protected static Matcher<Data> deeplyEqualTo(long start, int intervalMinutes, String... values) {
91 return new TypeSafeMatcher<Data>() {
93 private TimestampMatcher timestampMatcher = new TimestampMatcher(start, intervalMinutes, values.length);
94 private ValuesMatcher valuesMatcher = new ValuesMatcher(values);
97 public void describeTo(@Nullable Description description) {
98 if (description == null) {
101 description.appendDescriptionOf(timestampMatcher);
102 description.appendText(" and ");
103 description.appendDescriptionOf(valuesMatcher);
107 protected boolean matchesSafely(Data dataValues) {
108 return timestampMatcher.matches(dataValues.timestampsEpochSecs)
109 && valuesMatcher.matches(dataValues.values);
118 * @throws Throwable exception raised by parseMultiPointCoverageXml
119 * @throws AssertionError exception raised when parseMultiPointCoverageXml method signature does not match excepted
120 * (test & implementation is out-of-sync)
122 protected FMIResponse parseMultiPointCoverageXml(String content) throws Throwable {
124 Method parseMethod = Client.class.getDeclaredMethod("parseMultiPointCoverageXml", String.class);
125 parseMethod.setAccessible(true);
126 return (FMIResponse) parseMethod.invoke(client, content);
127 } catch (InvocationTargetException e) {
128 throw e.getTargetException();
129 } catch (Exception e) {
130 fail(String.format("Unexpected reflection error (code changed?) %s: %s", e.getClass().getName(),
132 // Make the compiler happy by throwing here, fails already above
133 throw new IllegalStateException();
137 @SuppressWarnings("unchecked")
138 protected Set<Location> parseStations(String content) {
140 Method parseMethod = Client.class.getDeclaredMethod("parseStations", String.class);
141 parseMethod.setAccessible(true);
142 return (Set<Location>) parseMethod.invoke(client, content);
143 } catch (InvocationTargetException e) {
144 throw new RuntimeException(e.getTargetException());
145 } catch (Exception e) {
146 fail(String.format("Unexpected reflection error (code changed?) %s: %s", e.getClass().getName(),
148 // Make the compiler happy by throwing here, fails already above
149 throw new IllegalStateException();