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.jupiter.api.Assertions.*;
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;
26 import java.util.Objects;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.hamcrest.Description;
32 import org.hamcrest.Matcher;
33 import org.hamcrest.TypeSafeMatcher;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.openhab.binding.fmiweather.internal.client.Client;
36 import org.openhab.binding.fmiweather.internal.client.Data;
37 import org.openhab.binding.fmiweather.internal.client.FMIResponse;
38 import org.openhab.binding.fmiweather.internal.client.Location;
41 * Base class for response parsing tests
43 * @author Sami Salonen - Initial contribution
46 public class AbstractFMIResponseParsingTest {
49 protected Client client;
52 public void setUpClient() {
53 client = new Client();
56 protected Path getTestResource(String filename) {
58 return Paths.get(getClass().getResource(filename).toURI());
59 } catch (URISyntaxException e) {
61 // Make the compiler happy by throwing here, fails already above
62 throw new IllegalStateException();
66 protected String readTestResourceUtf8(String filename) {
67 return readTestResourceUtf8(getTestResource(filename));
70 protected String readTestResourceUtf8(Path path) {
72 BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
73 StringBuilder content = new StringBuilder();
74 char[] buffer = new char[1024];
76 while ((read = reader.read(buffer)) != -1) {
77 content.append(buffer, 0, read);
79 return content.toString();
80 } catch (IOException e) {
82 // Make the compiler happy by throwing here, fails already above
83 throw new IllegalStateException();
87 protected static TypeSafeMatcher<Location> deeplyEqualTo(Location location) {
88 return new ResponseLocationMatcher(location);
91 protected static Matcher<Data> deeplyEqualTo(long start, int intervalMinutes, String... values) {
92 return new TypeSafeMatcher<Data>() {
94 private TimestampMatcher timestampMatcher = new TimestampMatcher(start, intervalMinutes, values.length);
95 private ValuesMatcher valuesMatcher = new ValuesMatcher(values);
98 public void describeTo(@Nullable Description description) {
99 if (description == null) {
102 description.appendDescriptionOf(timestampMatcher);
103 description.appendText(" and ");
104 description.appendDescriptionOf(valuesMatcher);
108 protected boolean matchesSafely(Data dataValues) {
109 return timestampMatcher.matches(dataValues.timestampsEpochSecs)
110 && valuesMatcher.matches(dataValues.values);
119 * @throws Throwable exception raised by parseMultiPointCoverageXml
120 * @throws AssertionError exception raised when parseMultiPointCoverageXml method signature does not match excepted
121 * (test & implementation is out-of-sync)
123 protected FMIResponse parseMultiPointCoverageXml(String content) throws Throwable {
125 Method parseMethod = Client.class.getDeclaredMethod("parseMultiPointCoverageXml", String.class);
126 parseMethod.setAccessible(true);
127 return Objects.requireNonNull((FMIResponse) parseMethod.invoke(client, content));
128 } catch (InvocationTargetException e) {
129 throw e.getTargetException();
130 } catch (Exception e) {
131 fail(String.format("Unexpected reflection error (code changed?) %s: %s", e.getClass().getName(),
133 // Make the compiler happy by throwing here, fails already above
134 throw new IllegalStateException();
138 @SuppressWarnings("unchecked")
139 protected Set<Location> parseStations(String content) {
141 Method parseMethod = Objects.requireNonNull(Client.class.getDeclaredMethod("parseStations", String.class));
142 parseMethod.setAccessible(true);
143 return Objects.requireNonNull((Set<Location>) parseMethod.invoke(client, content));
144 } catch (InvocationTargetException e) {
145 throw new RuntimeException(e.getTargetException());
146 } catch (Exception e) {
147 fail(String.format("Unexpected reflection error (code changed?) %s: %s", e.getClass().getName(),
149 // Make the compiler happy by throwing here, fails already above
150 throw new IllegalStateException();