]> git.basschouten.com Git - openhab-addons.git/blob
e63119126dcd8d304978c3e08cd4ed9ff20f4bd1
[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.test;
14
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.*;
19
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;
26
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;
38
39 /**
40  * Tests for the Astro Channels state
41  *
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
47  */
48 public class AstroStateTest {
49
50     // These test result timestamps are adapted for the +03:00 time zone
51     private static final ZoneId ZONE_ID = ZoneId.of("+03:00");
52
53     public static List<Object[]> data() {
54         AstroParametrizedTestCases cases = new AstroParametrizedTestCases();
55         return cases.getCases();
56     }
57
58     @ParameterizedTest
59     @MethodSource("data")
60     public void testParametrized(String thingID, String channelId, State expectedState) {
61         try {
62             assertStateUpdate(thingID, channelId, expectedState);
63         } catch (Exception e) {
64             // do nothing
65         }
66     }
67
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);
72     }
73
74     private ThingUID getThingUID(String thingID) {
75         switch (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);
80             default:
81                 return null;
82         }
83     }
84
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);
89         switch (thingID) {
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);
96             default:
97                 return null;
98         }
99     }
100 }