]> git.basschouten.com Git - openhab-addons.git/blob
f8db754dba792c8097e767b332d068c623a9482a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.Assert.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.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.types.State;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.junit.runners.Parameterized.Parameters;
34 import org.openhab.binding.astro.internal.calc.MoonCalc;
35 import org.openhab.binding.astro.internal.calc.SunCalc;
36 import org.openhab.binding.astro.internal.config.AstroChannelConfig;
37 import org.openhab.binding.astro.internal.model.Planet;
38 import org.openhab.binding.astro.internal.util.PropertyUtils;
39 import org.openhab.binding.astro.test.cases.AstroParametrizedTestCases;
40
41 /**
42  * Tests for the Astro Channels state
43  *
44  * @See {@link AstroParametrizedTestCases}
45  * @author Petar Valchev - Initial implementation
46  * @author Svilen Valkanov - Reworked to plain unit tests
47  * @author Erdoan Hadzhiyusein - Adapted the class to work with the new DateTimeType
48  * @author Christoph Weitkamp - Migrated tests to pure Java
49  */
50 @RunWith(Parameterized.class)
51 public class AstroStateTest {
52
53     private String thingID;
54     private String channelId;
55     private State expectedState;
56
57     // These test result timestamps are adapted for the +03:00 time zone
58     private static final ZoneId ZONE_ID = ZoneId.of("+03:00");
59
60     public AstroStateTest(String thingID, String channelId, State expectedState) {
61         this.thingID = thingID;
62         this.channelId = channelId;
63         this.expectedState = expectedState;
64     }
65
66     @Parameters
67     public static List<Object[]> data() {
68         AstroParametrizedTestCases cases = new AstroParametrizedTestCases();
69         return cases.getCases();
70     }
71
72     @Test
73     public void testParametrized() {
74         try {
75             assertStateUpdate(thingID, channelId, expectedState);
76         } catch (Exception e) {
77             // do nothing
78         }
79     }
80
81     private void assertStateUpdate(String thingID, String channelId, State expectedState) throws Exception {
82         ChannelUID testItemChannelUID = new ChannelUID(getThingUID(thingID), channelId);
83         State state = PropertyUtils.getState(testItemChannelUID, new AstroChannelConfig(), getPlanet(thingID), ZONE_ID);
84         assertEquals(expectedState, state);
85     }
86
87     private ThingUID getThingUID(String thingID) {
88         switch (thingID) {
89             case (TEST_SUN_THING_ID):
90                 return new ThingUID(THING_TYPE_SUN, thingID);
91             case (TEST_MOON_THING_ID):
92                 return new ThingUID(THING_TYPE_MOON, thingID);
93             default:
94                 return null;
95         }
96     }
97
98     private Planet getPlanet(String thingID) {
99         LocalDateTime time = LocalDateTime.of(TEST_YEAR, TEST_MONTH, TEST_DAY, 0, 0);
100         ZonedDateTime zonedTime = ZonedDateTime.ofLocal(time, ZONE_ID, null);
101         Calendar calendar = GregorianCalendar.from(zonedTime);
102         switch (thingID) {
103             case (TEST_SUN_THING_ID):
104                 SunCalc sunCalc = new SunCalc();
105                 return sunCalc.getSunInfo(calendar, TEST_LATITUDE, TEST_LONGITUDE, null, false);
106             case (TEST_MOON_THING_ID):
107                 MoonCalc moonCalc = new MoonCalc();
108                 return moonCalc.getMoonInfo(calendar, TEST_LATITUDE, TEST_LONGITUDE);
109             default:
110                 return null;
111         }
112     }
113 }