]> git.basschouten.com Git - openhab-addons.git/blob
79cbb4d591a330610e6078b0d05bd6a4524da709
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.miele.internal;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.equalTo;
17 import static org.hamcrest.Matchers.is;
18
19 import java.time.Clock;
20 import java.time.Instant;
21 import java.time.ZoneId;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26
27 /**
28  * Unit tests for {@link TimeStabilizer}.
29  *
30  * @author Jacob Laursen - Initial contribution
31  */
32 @NonNullByDefault
33 public class TimeStabilizerTest {
34
35     private @NonNullByDefault({}) TimeStabilizer stabilizer;
36
37     @BeforeEach
38     public void initialize() {
39         stabilizer = new TimeStabilizer();
40     }
41
42     @Test
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"))));
50     }
51
52     @Test
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"))));
60     }
61
62     @Test
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"))));
70     }
71
72     @Test
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"))));
82     }
83
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);
87     }
88 }