]> git.basschouten.com Git - openhab-addons.git/blob
654fcf2ab85933165755011b3a80dd1da75b410d
[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.astro.internal.util;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16
17 import java.util.Calendar;
18 import java.util.GregorianCalendar;
19 import java.util.TimeZone;
20
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.astro.internal.calc.SeasonCalc;
24 import org.openhab.binding.astro.internal.model.Season;
25
26 /**
27  * Test class for {@link DateTimeUtils}.
28  *
29  * @author Hilbrand Bouwkamp - Initial contribution
30  */
31 public class DateTimeUtilsTest {
32
33     private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("Europe/Amsterdam");
34     private static final Calendar JAN_20_2020 = newCalendar(2020, Calendar.JANUARY, 20, 1, 0, TIME_ZONE);
35     private static final Calendar MAY_20_2020 = newCalendar(2020, Calendar.MAY, 20, 1, 0, TIME_ZONE);
36     private static final Calendar SEPT_20_2020 = newCalendar(2020, Calendar.SEPTEMBER, 20, 1, 0, TIME_ZONE);
37     private static final Calendar DEC_10_2020 = newCalendar(2020, Calendar.DECEMBER, 1, 1, 0, TIME_ZONE);
38     private static final Calendar DEC_10_2021 = newCalendar(2021, Calendar.DECEMBER, 1, 1, 0, TIME_ZONE);
39     private static final double AMSTERDAM_LATITUDE = 52.367607;
40     private static final double SYDNEY_LATITUDE = -33.87;
41
42     private SeasonCalc seasonCalc;
43
44     @BeforeEach
45     public void init() {
46         seasonCalc = new SeasonCalc();
47     }
48
49     @Test
50     public void testGetSeasonAmsterdam() {
51         final Season season = seasonCalc.getSeason(DEC_10_2020, AMSTERDAM_LATITUDE, true);
52
53         assertNextSeason(season.getSpring(), 2020, JAN_20_2020, season);
54         assertNextSeason(season.getSummer(), 2020, MAY_20_2020, season);
55         assertNextSeason(season.getWinter(), 2020, SEPT_20_2020, season);
56         assertNextSeason(seasonCalc.getSeason(DEC_10_2021, AMSTERDAM_LATITUDE, true).getSpring(), 2021, DEC_10_2020,
57                 season);
58     }
59
60     @Test
61     public void testGetSeasonSydney() {
62         final Season season = seasonCalc.getSeason(DEC_10_2020, SYDNEY_LATITUDE, true);
63
64         assertNextSeason(season.getAutumn(), 2020, JAN_20_2020, season);
65         assertNextSeason(season.getWinter(), 2020, MAY_20_2020, season);
66         assertNextSeason(season.getSummer(), 2020, SEPT_20_2020, season);
67         assertNextSeason(seasonCalc.getSeason(DEC_10_2021, SYDNEY_LATITUDE, true).getAutumn(), 2021, DEC_10_2020,
68                 season);
69     }
70
71     @Test
72     void testTruncate() {
73         Calendar cal = newCalendar(2021, 9, 30, 11, 54, TIME_ZONE);
74         Calendar target = newCalendar(2021, 9, 30, 0, 0, TIME_ZONE);
75         Calendar truncated = DateTimeUtils.truncateToMidnight(cal);
76         assertEquals(truncated, target);
77         Calendar endOfDay = DateTimeUtils.endOfDayDate(cal);
78         Calendar target2 = new GregorianCalendar(2021, 9, 30, 23, 59, 59);
79         target2.setTimeZone(TIME_ZONE);
80         target2.set(Calendar.MILLISECOND, 999);
81         assertEquals(endOfDay, target2);
82     }
83
84     private static void assertNextSeason(Calendar expectedSeason, int expectedYear, Calendar date, Season season) {
85         final Calendar nextSeason = DateTimeUtils.getNext(date, season.getSpring(), season.getSummer(),
86                 season.getAutumn(), season.getWinter());
87         assertEquals(expectedSeason, nextSeason, "Should return the expected season name.");
88         assertEquals(expectedYear, nextSeason.get(Calendar.YEAR), "Should return the year matching the next season.");
89     }
90
91     private static Calendar newCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, TimeZone zone) {
92         Calendar result = new GregorianCalendar(year, month, dayOfMonth, hourOfDay, minute);
93         result.setTimeZone(zone);
94
95         return result;
96     }
97 }