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.astro.test;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.openhab.binding.astro.internal.AstroBindingConstants.*;
17 import static org.openhab.binding.astro.test.cases.AstroBindingTestsData.*;
18 import static org.openhab.binding.astro.test.cases.AstroParametrizedTestCases.*;
20 import java.time.LocalDateTime;
21 import java.time.ZoneId;
22 import java.time.ZonedDateTime;
23 import java.util.Calendar;
24 import java.util.GregorianCalendar;
25 import java.util.List;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.MethodSource;
29 import org.openhab.binding.astro.internal.calc.MoonCalc;
30 import org.openhab.binding.astro.internal.calc.SunCalc;
31 import org.openhab.binding.astro.internal.config.AstroChannelConfig;
32 import org.openhab.binding.astro.internal.model.Planet;
33 import org.openhab.binding.astro.internal.util.PropertyUtils;
34 import org.openhab.binding.astro.test.cases.AstroParametrizedTestCases;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.ThingUID;
37 import org.openhab.core.types.State;
40 * Tests for the Astro Channels state
42 * @see {@link AstroParametrizedTestCases}
43 * @author Petar Valchev - Initial contribution
44 * @author Svilen Valkanov - Reworked to plain unit tests
45 * @author Erdoan Hadzhiyusein - Adapted the class to work with the new DateTimeType
46 * @author Christoph Weitkamp - Migrated tests to pure Java
48 public class AstroStateTest {
50 // These test result timestamps are adapted for the +03:00 time zone
51 private static final ZoneId ZONE_ID = ZoneId.of("+03:00");
53 public static List<Object[]> data() {
54 AstroParametrizedTestCases cases = new AstroParametrizedTestCases();
55 return cases.getCases();
60 public void testParametrized(String thingID, String channelId, State expectedState) {
62 assertStateUpdate(thingID, channelId, expectedState);
63 } catch (Exception e) {
68 private void assertStateUpdate(String thingID, String channelId, State expectedState) throws Exception {
69 ChannelUID testItemChannelUID = new ChannelUID(getThingUID(thingID), channelId);
70 State state = PropertyUtils.getState(testItemChannelUID, new AstroChannelConfig(), getPlanet(thingID), ZONE_ID);
71 assertEquals(expectedState, state);
74 private ThingUID getThingUID(String thingID) {
76 case (TEST_SUN_THING_ID):
77 return new ThingUID(THING_TYPE_SUN, thingID);
78 case (TEST_MOON_THING_ID):
79 return new ThingUID(THING_TYPE_MOON, thingID);
85 private Planet getPlanet(String thingID) {
86 LocalDateTime time = LocalDateTime.of(TEST_YEAR, TEST_MONTH, TEST_DAY, 0, 0);
87 ZonedDateTime zonedTime = ZonedDateTime.ofLocal(time, ZONE_ID, null);
88 Calendar calendar = GregorianCalendar.from(zonedTime);
90 case (TEST_SUN_THING_ID):
91 SunCalc sunCalc = new SunCalc();
92 return sunCalc.getSunInfo(calendar, TEST_LATITUDE, TEST_LONGITUDE, null, false);
93 case (TEST_MOON_THING_ID):
94 MoonCalc moonCalc = new MoonCalc();
95 return moonCalc.getMoonInfo(calendar, TEST_LATITUDE, TEST_LONGITUDE);