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.astro.internal.util;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import java.util.Calendar;
18 import java.util.GregorianCalendar;
19 import java.util.TimeZone;
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;
27 * Test class for {@link DateTimeUtils}.
29 * @author Hilbrand Bouwkamp - Initial contribution
31 public class DateTimeUtilsTest {
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 double AMSTERDAM_LATITUDE = 52.367607;
39 private static final double SYDNEY_LATITUDE = -33.87;
41 private SeasonCalc seasonCalc;
45 seasonCalc = new SeasonCalc();
49 public void testGetSeasonAmsterdam() {
50 Season season = seasonCalc.getSeason(DEC_10_2020, AMSTERDAM_LATITUDE, true);
51 assertNextSeason(season.getSpring(), JAN_20_2020, season);
52 assertNextSeason(season.getSummer(), MAY_20_2020, season);
53 assertNextSeason(season.getWinter(), SEPT_20_2020, season);
54 assertNextSeason(season.getSpring(), DEC_10_2020, season);
58 public void testGetSeasonSydney() {
59 Season season = seasonCalc.getSeason(DEC_10_2020, SYDNEY_LATITUDE, true);
60 assertNextSeason(season.getAutumn(), JAN_20_2020, season);
61 assertNextSeason(season.getWinter(), MAY_20_2020, season);
62 assertNextSeason(season.getSummer(), SEPT_20_2020, season);
63 assertNextSeason(season.getAutumn(), DEC_10_2020, season);
68 Calendar cal = newCalendar(2021, 9, 30, 11, 54, TIME_ZONE);
69 Calendar target = newCalendar(2021, 9, 30, 0, 0, TIME_ZONE);
70 Calendar truncated = DateTimeUtils.truncateToMidnight(cal);
71 assertEquals(truncated, target);
72 Calendar endOfDay = DateTimeUtils.endOfDayDate(cal);
73 Calendar target2 = new GregorianCalendar(2021, 9, 30, 23, 59, 59);
74 target2.setTimeZone(TIME_ZONE);
75 target2.set(Calendar.MILLISECOND, 999);
76 assertEquals(endOfDay, target2);
79 private void assertNextSeason(Calendar expectedSeason, Calendar date, Season season) {
80 assertEquals(expectedSeason, DateTimeUtils.getNext(date, season.getSpring(), season.getSummer(),
81 season.getAutumn(), season.getWinter()));
84 private static Calendar newCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, TimeZone zone) {
85 Calendar result = new GregorianCalendar(year, month, dayOfMonth, hourOfDay, minute);
86 result.setTimeZone(zone);