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