]> git.basschouten.com Git - openhab-addons.git/blob
38f50d646a480c03838985f340b757923d59c59e
[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.Assert.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.*;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.math.BigDecimal;
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.jupiter.params.ParameterizedTest;
27 import org.junit.jupiter.params.provider.MethodSource;
28 import org.openhab.binding.fmiweather.internal.AbstractWeatherHandler;
29 import org.openhab.binding.fmiweather.internal.client.Data;
30
31 /**
32  * Base class for response parsing tests
33  *
34  * @author Sami Salonen - Initial contribution
35  */
36 @NonNullByDefault
37
38 public class AbstractWeatherHandlerTest {
39
40     private static BigDecimal bd(Number x) {
41         return new BigDecimal(x.doubleValue());
42     }
43
44     protected static long floorToEvenMinutes(long epochSeconds, int roundMinutes) {
45         final Method method;
46         try {
47             method = AbstractWeatherHandler.class.getDeclaredMethod("floorToEvenMinutes", long.class, int.class);
48             method.setAccessible(true);
49             Object res = method.invoke(null, epochSeconds, roundMinutes);
50             assertNotNull(res);
51             return (long) res;
52         } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
53                 | InvocationTargetException e) {
54             fail(e);
55             throw new IllegalStateException(); // to make compiler happy
56         }
57     }
58
59     protected static long ceilToEvenMinutes(long epochSeconds, int roundMinutes) {
60         final Method method;
61         try {
62             method = AbstractWeatherHandler.class.getDeclaredMethod("ceilToEvenMinutes", long.class, int.class);
63             method.setAccessible(true);
64             Object res = method.invoke(null, epochSeconds, roundMinutes);
65             assertNotNull(res);
66             return (long) res;
67         } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
68                 | InvocationTargetException e) {
69             fail(e);
70             throw new IllegalStateException(); // to make compiler happy
71         }
72     }
73
74     public static final List<Object[]> parametersForFloorToEvenMinutes() {
75         return Arrays.asList(new Object[][] { //
76                 { 1626605128L /* 2021-07-18 10:45:28 */, 1, 1626605100 /* 10:45 */ }, //
77                 { 1626605128L /* 2021-07-18 10:45:28 */, 5, 1626605100 /* 10:45 */ }, //
78                 { 1626605128L /* 2021-07-18 10:45:28 */, 10, 1626604800 /* 10:40 */ }, //
79                 { 1626605128L /* 2021-07-18 10:45:28 */, 30, 1626604200 /* 10:30 */ }, //
80                 { 1626605128L /* 2021-07-18 10:45:28 */, 60, 1626602400 /* 10:00 */ }, //
81         });
82     }
83
84     protected static int lastValidIndex(Data data) {
85         final Method method;
86         try {
87             method = AbstractWeatherHandler.class.getDeclaredMethod("lastValidIndex", Data.class);
88             method.setAccessible(true);
89             Object res = method.invoke(null, data);
90             assertNotNull(res);
91             return (int) res;
92         } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
93                 | InvocationTargetException e) {
94             fail(e);
95             throw new IllegalStateException(); // to make compiler happy
96         }
97     }
98
99     @ParameterizedTest
100     @MethodSource("parametersForFloorToEvenMinutes")
101     public void testFloorToEvenMinutes(long epochSeconds, int roundMinutes, long expected) {
102         assertEquals(expected, floorToEvenMinutes(epochSeconds, roundMinutes));
103     }
104
105     public static final List<Object[]> parametersForCeilToEvenMinutes() {
106         return Arrays.asList(new Object[][] { //
107                 { 1626605128L /* 2021-07-18 10:45:28 */, 1, 1626605160 /* 10:46 */ }, //
108                 { 1626605128L /* 2021-07-18 10:45:28 */, 5, 1626605400 /* 10:50 */ }, //
109                 { 1626605128L /* 2021-07-18 10:45:28 */, 10, 1626605400 /* 10:50 */ }, //
110                 { 1626605128L /* 2021-07-18 10:45:28 */, 30, 1626606000 /* 11:00 */ }, //
111                 { 1626605128L /* 2021-07-18 10:45:28 */, 60, 1626606000 /* 11:00 */ }, //
112         });
113     }
114
115     @ParameterizedTest
116     @MethodSource("parametersForCeilToEvenMinutes")
117     public void testCeilToEvenMinutes(long epochSeconds, int roundMinutes, long expected) {
118         assertEquals(expected, ceilToEvenMinutes(epochSeconds, roundMinutes));
119     }
120
121     public static final List<Object[]> parametersForLastValidIndex() {
122         return Arrays.asList(new Object[][] { //
123                 { "no nulls", 1, new BigDecimal[] { bd(1), bd(2) } }, //
124                 { "one null in beginning", 1, new BigDecimal[] { null, bd(2) } }, //
125                 { "two nulls", 2, new BigDecimal[] { null, null, bd(2) } }, //
126                 { "three nulls", 3, new BigDecimal[] { null, null, null, bd(2) } }, //
127                 { "null at end", 1, new BigDecimal[] { bd(1), bd(2), null } }, //
128                 { "null at end #2", 2, new BigDecimal[] { bd(1), bd(2), bd(2), null } }, //
129                 { "null in middle", 3, new BigDecimal[] { bd(1), bd(2), null, bd(3) } }, //
130                 { "null in beginning and middle", 3, new BigDecimal[] { null, bd(2), null, bd(3) } }, //
131                 { "all null #1", -1, new BigDecimal[] { null, null, } }, //
132                 { "all null #2", -1, new BigDecimal[] { null, null, null, } }, //
133                 { "all null #3", -1, new BigDecimal[] { null, null, null, null } }, //
134         });
135     }
136
137     @ParameterizedTest
138     @MethodSource("parametersForLastValidIndex")
139     public void testLastValidIndex(String msg, int expected, @Nullable BigDecimal... values) {
140         long[] dummyTimestamps = new long[values.length];
141         assertEquals(expected, lastValidIndex(new Data(dummyTimestamps, values)), msg);
142     }
143 }