2 * Copyright (c) 2010-2020 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.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.*;
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.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;
42 * Tests for the Astro Channels state
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
50 @RunWith(Parameterized.class)
51 public class AstroStateTest {
53 private String thingID;
54 private String channelId;
55 private State expectedState;
57 // These test result timestamps are adapted for the +03:00 time zone
58 private static final ZoneId ZONE_ID = ZoneId.of("+03:00");
60 public AstroStateTest(String thingID, String channelId, State expectedState) {
61 this.thingID = thingID;
62 this.channelId = channelId;
63 this.expectedState = expectedState;
67 public static List<Object[]> data() {
68 AstroParametrizedTestCases cases = new AstroParametrizedTestCases();
69 return cases.getCases();
73 public void testParametrized() {
75 assertStateUpdate(thingID, channelId, expectedState);
76 } catch (Exception e) {
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);
87 private ThingUID getThingUID(String 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);
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);
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);