2 * Copyright (c) 2010-2023 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.miele.internal;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.equalTo;
17 import static org.hamcrest.Matchers.is;
19 import java.time.Clock;
20 import java.time.Instant;
21 import java.time.ZoneId;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
28 * Unit tests for {@link TimeStabilizer}.
30 * @author Jacob Laursen - Initial contribution
33 public class TimeStabilizerTest {
35 private @NonNullByDefault({}) TimeStabilizer stabilizer;
38 public void initialize() {
39 stabilizer = new TimeStabilizer();
43 public void whenLongestPeriodIsFloorThenWeightedAverageIsLess() {
44 assertThat(stabilizer.apply(getInstantOf("02:00:00"), getInstantOf("22:00:00")),
45 is(equalTo(getInstantOf("02:00:00"))));
46 assertThat(stabilizer.apply(getInstantOf("02:01:00"), getInstantOf("22:00:31")),
47 is(equalTo(getInstantOf("02:00:00"))));
48 assertThat(stabilizer.apply(getInstantOf("02:00:00"), getInstantOf("22:01:00")),
49 is(equalTo(getInstantOf("02:00:29"))));
53 public void whenLongestPeriodIsCeilThenWeightedAverageIsMore() {
54 assertThat(stabilizer.apply(getInstantOf("02:00:00"), getInstantOf("22:00:00")),
55 is(equalTo(getInstantOf("02:00:00"))));
56 assertThat(stabilizer.apply(getInstantOf("02:01:00"), getInstantOf("22:00:29")),
57 is(equalTo(getInstantOf("02:00:00"))));
58 assertThat(stabilizer.apply(getInstantOf("02:00:00"), getInstantOf("22:01:00")),
59 is(equalTo(getInstantOf("02:00:31"))));
63 public void whenTooMuchFluctuationThenAverageIsDisregarded() {
64 assertThat(stabilizer.apply(getInstantOf("02:00:00"), getInstantOf("22:00:00")),
65 is(equalTo(getInstantOf("02:00:00"))));
66 assertThat(stabilizer.apply(getInstantOf("02:03:00"), getInstantOf("22:03:00")),
67 is(equalTo(getInstantOf("02:00:00"))));
68 assertThat(stabilizer.apply(getInstantOf("02:04:00"), getInstantOf("22:03:00")),
69 is(equalTo(getInstantOf("02:04:00"))));
73 public void whenOutsideSlidingWindowThenValueIsDisregarded() {
74 assertThat(stabilizer.apply(getInstantOf("02:00:00"), getInstantOf("22:00:00")),
75 is(equalTo(getInstantOf("02:00:00"))));
76 assertThat(stabilizer.apply(getInstantOf("02:01:00"), getInstantOf("22:10:00")),
77 is(equalTo(getInstantOf("02:00:00"))));
78 assertThat(stabilizer.apply(getInstantOf("02:02:00"), getInstantOf("22:15:00")),
79 is(equalTo(getInstantOf("02:00:30"))));
80 assertThat(stabilizer.apply(getInstantOf("02:02:00"), getInstantOf("22:15:01")),
81 is(equalTo(getInstantOf("02:01:00"))));
84 private Instant getInstantOf(String time) {
85 Clock clock = Clock.fixed(Instant.parse("2022-09-13T" + time + "Z"), ZoneId.of("UTC"));
86 return Instant.now(clock);