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