]> git.basschouten.com Git - openhab-addons.git/blob
737bec1290b21ce14165e5bcf12f66f909d0b532
[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.buienradar;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.math.BigDecimal;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.Optional;
21
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.buienradar.internal.buienradarapi.BuienradarParseException;
24 import org.openhab.binding.buienradar.internal.buienradarapi.BuienradarPredictionAPI;
25 import org.openhab.binding.buienradar.internal.buienradarapi.Prediction;
26
27 /**
28  * Tests {@link BuienradarPredictionAPI}.
29  *
30  * @author Edwin de Jong - Initial contribution
31  */
32 public class BuienradarPredictionAPITest {
33     private static final ZonedDateTime NOW = ZonedDateTime.of(2019, 3, 10, 20, 37, 0, 0, ZoneId.of("Europe/Amsterdam"));
34
35     private static final ZonedDateTime ACTUAL = ZonedDateTime.of(2019, 3, 10, 20, 35, 0, 0,
36             ZoneId.of("Europe/Amsterdam"));
37
38     private static final ZonedDateTime NOW_LONDON = ZonedDateTime.of(2019, 3, 10, 20, 37, 0, 0,
39             ZoneId.of("Europe/London"));
40
41     @Test
42     public void testParseIntensity000() throws BuienradarParseException {
43         assertEquals(BigDecimal.valueOf(0, 2), BuienradarPredictionAPI.parseIntensity("000"));
44     }
45
46     @Test
47     public void testParseIntensity050() throws BuienradarParseException {
48         assertEquals(BigDecimal.valueOf(1, 2), BuienradarPredictionAPI.parseIntensity("050"));
49     }
50
51     @Test
52     public void testParseIntensity500() throws BuienradarParseException {
53         assertEquals(BigDecimal.valueOf(165481709994318L, 2), BuienradarPredictionAPI.parseIntensity("500"));
54     }
55
56     @Test
57     public void testParseIntensity101() throws BuienradarParseException {
58         assertEquals(BigDecimal.valueOf(56, 2), BuienradarPredictionAPI.parseIntensity("101"));
59     }
60
61     @Test
62     public void testParseDateTime() throws BuienradarParseException {
63         final ZonedDateTime parsed = BuienradarPredictionAPI.parseDateTime("20:45", NOW);
64         assertEquals(ZonedDateTime.of(2019, 3, 10, 20, 45, 0, 0, ZoneId.of("Europe/Amsterdam")), parsed);
65     }
66
67     @Test
68     public void testParseDateTimeLondon() throws BuienradarParseException {
69         // 20:37 in London is *before* 20:45 in Amsterdam, therefore, it should be parsed as a timestamp happening
70         // tomorrow.
71         final ZonedDateTime parsed = BuienradarPredictionAPI.parseDateTime("20:45", NOW_LONDON);
72         assertEquals(ZonedDateTime.of(2019, 3, 11, 20, 45, 0, 0, ZoneId.of("Europe/Amsterdam")), parsed);
73     }
74
75     @Test
76     public void testParseDateTimeTomorrow() throws BuienradarParseException {
77         final ZonedDateTime parsed = BuienradarPredictionAPI.parseDateTime("19:40", NOW);
78         assertEquals(ZonedDateTime.of(2019, 3, 11, 19, 40, 0, 0, ZoneId.of("Europe/Amsterdam")), parsed);
79     }
80
81     @Test
82     public void testParseLine() throws BuienradarParseException {
83         final Prediction parsed = BuienradarPredictionAPI.parseLine("000|19:35", NOW, Optional.of(ACTUAL));
84         assertEquals(ZonedDateTime.of(2019, 3, 11, 19, 35, 0, 0, ZoneId.of("Europe/Amsterdam")),
85                 parsed.getDateTimeOfPrediction());
86         assertEquals(BigDecimal.valueOf(0, 2), parsed.getIntensity());
87     }
88 }