]> git.basschouten.com Git - openhab-addons.git/blob
397e46832ffc17211bfe694a263785dcb0ee2b08
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.fmiweather;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
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;
27 import java.util.Set;
28
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;
39
40 /**
41  * Base class for response parsing tests
42  *
43  * @author Sami Salonen - Initial contribution
44  */
45 @NonNullByDefault
46 public class AbstractFMIResponseParsingTest {
47
48     @NonNullByDefault({})
49     protected Client client;
50
51     @BeforeEach
52     public void setUpClient() {
53         client = new Client();
54     }
55
56     protected Path getTestResource(String filename) {
57         try {
58             return Paths.get(getClass().getResource(filename).toURI());
59         } catch (URISyntaxException e) {
60             fail(e.getMessage());
61             // Make the compiler happy by throwing here, fails already above
62             throw new IllegalStateException();
63         }
64     }
65
66     protected String readTestResourceUtf8(String filename) {
67         return readTestResourceUtf8(getTestResource(filename));
68     }
69
70     protected String readTestResourceUtf8(Path path) {
71         try {
72             BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
73             StringBuilder content = new StringBuilder();
74             char[] buffer = new char[1024];
75             int read = -1;
76             while ((read = reader.read(buffer)) != -1) {
77                 content.append(buffer, 0, read);
78             }
79             return content.toString();
80         } catch (IOException e) {
81             fail(e.getMessage());
82             // Make the compiler happy by throwing here, fails already above
83             throw new IllegalStateException();
84         }
85     }
86
87     protected static TypeSafeMatcher<Location> deeplyEqualTo(Location location) {
88         return new ResponseLocationMatcher(location);
89     }
90
91     protected static Matcher<Data> deeplyEqualTo(long start, int intervalMinutes, String... values) {
92         return new TypeSafeMatcher<Data>() {
93
94             private TimestampMatcher timestampMatcher = new TimestampMatcher(start, intervalMinutes, values.length);
95             private ValuesMatcher valuesMatcher = new ValuesMatcher(values);
96
97             @Override
98             public void describeTo(@Nullable Description description) {
99                 if (description == null) {
100                     return;
101                 }
102                 description.appendDescriptionOf(timestampMatcher);
103                 description.appendText(" and ");
104                 description.appendDescriptionOf(valuesMatcher);
105             }
106
107             @Override
108             protected boolean matchesSafely(Data dataValues) {
109                 return timestampMatcher.matches(dataValues.timestampsEpochSecs)
110                         && valuesMatcher.matches(dataValues.values);
111             }
112         };
113     }
114
115     /**
116      *
117      * @param content
118      * @return
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)
122      */
123     protected FMIResponse parseMultiPointCoverageXml(String content) throws Throwable {
124         try {
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(),
132                     e.getMessage()));
133             // Make the compiler happy by throwing here, fails already above
134             throw new IllegalStateException();
135         }
136     }
137
138     @SuppressWarnings("unchecked")
139     protected Set<Location> parseStations(String content) {
140         try {
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(),
148                     e.getMessage()));
149             // Make the compiler happy by throwing here, fails already above
150             throw new IllegalStateException();
151         }
152     }
153 }