2 * Copyright (c) 2010-2021 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.Assert.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.*;
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;
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;
32 * Base class for response parsing tests
34 * @author Sami Salonen - Initial contribution
38 public class AbstractWeatherHandlerTest {
40 private static BigDecimal bd(Number x) {
41 return new BigDecimal(x.doubleValue());
44 protected static long floorToEvenMinutes(long epochSeconds, int roundMinutes) {
47 method = AbstractWeatherHandler.class.getDeclaredMethod("floorToEvenMinutes", long.class, int.class);
48 method.setAccessible(true);
49 Object res = method.invoke(null, epochSeconds, roundMinutes);
52 } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
53 | InvocationTargetException e) {
55 throw new IllegalStateException(); // to make compiler happy
59 protected static long ceilToEvenMinutes(long epochSeconds, int roundMinutes) {
62 method = AbstractWeatherHandler.class.getDeclaredMethod("ceilToEvenMinutes", long.class, int.class);
63 method.setAccessible(true);
64 Object res = method.invoke(null, epochSeconds, roundMinutes);
67 } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
68 | InvocationTargetException e) {
70 throw new IllegalStateException(); // to make compiler happy
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 */ }, //
84 protected static int lastValidIndex(Data data) {
87 method = AbstractWeatherHandler.class.getDeclaredMethod("lastValidIndex", Data.class);
88 method.setAccessible(true);
89 Object res = method.invoke(null, data);
92 } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
93 | InvocationTargetException e) {
95 throw new IllegalStateException(); // to make compiler happy
100 @MethodSource("parametersForFloorToEvenMinutes")
101 public void testFloorToEvenMinutes(long epochSeconds, int roundMinutes, long expected) {
102 assertEquals(expected, floorToEvenMinutes(epochSeconds, roundMinutes));
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 */ }, //
116 @MethodSource("parametersForCeilToEvenMinutes")
117 public void testCeilToEvenMinutes(long epochSeconds, int roundMinutes, long expected) {
118 assertEquals(expected, ceilToEvenMinutes(epochSeconds, roundMinutes));
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 } }, //
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);