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.handler.test;
15 import static org.mockito.Mockito.*;
16 import static org.openhab.binding.astro.internal.AstroBindingConstants.THING_TYPE_SUN;
17 import static org.openhab.binding.astro.test.cases.AstroBindingTestsData.*;
19 import java.time.ZoneId;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.astro.internal.handler.AstroThingHandler;
23 import org.openhab.binding.astro.internal.handler.SunHandler;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.i18n.TimeZoneProvider;
26 import org.openhab.core.scheduler.CronScheduler;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.ThingStatusInfo;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerCallback;
36 * Tests for the {@link AstroThingHandler}
38 * This class tests the required configuration for the astro thing.
40 * @author Petar Valchev - Initial implementation
41 * @author Svilen Valkanov - Reworked to plain unit tests, removed irrelevant tests
42 * @author Christoph Weitkamp - Migrated tests to pure Java
44 public class AstroValidConfigurationTest {
46 private final String NULL_LONGITUDE = "51.2,null";
47 private final String NULL_LATITUDE = "null,25.4";
50 public void testIfGeolocationIsProvidedForASunThing_theThingStatusBecomesONLINE() {
51 Configuration thingConfiguration = new Configuration();
52 thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
53 thingConfiguration.put(INTERVAL_PROPERTY, INTERVAL_DEFAULT_VALUE);
54 assertThingStatus(thingConfiguration, ThingStatus.ONLINE, ThingStatusDetail.NONE);
58 public void testIfGeolocationIsProvidedForAMoonThing_theThingStatusBecomesONLINE() {
59 Configuration thingConfiguration = new Configuration();
60 thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
61 thingConfiguration.put(INTERVAL_PROPERTY, INTERVAL_DEFAULT_VALUE);
62 assertThingStatus(thingConfiguration, ThingStatus.ONLINE, ThingStatusDetail.NONE);
66 public void testIfGeolocationForASunThingIsNull_theThingStatusBecomesOFFLINE() {
67 Configuration thingConfiguration = new Configuration();
68 thingConfiguration.put(GEOLOCATION_PROPERTY, null);
69 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
73 public void testIfGeolocationForAMoonThingIsNull_theThingStatusBecomesOFFLINE() {
74 Configuration thingConfiguration = new Configuration();
75 thingConfiguration.put(GEOLOCATION_PROPERTY, null);
76 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
80 public void testIfTheLatitudeForASunThingIsNull_theThingStatusBecomesOFFLINE() {
81 Configuration thingConfiguration = new Configuration();
82 thingConfiguration.put(GEOLOCATION_PROPERTY, NULL_LATITUDE);
83 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
87 public void testIfTheLatitudeForAMoonThingIsNull_theThingStatusBecomesOFFLINE() {
88 Configuration thingConfiguration = new Configuration();
89 thingConfiguration.put(GEOLOCATION_PROPERTY, NULL_LATITUDE);
90 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
94 public void testIfTheLongitudeForASunThingIsNull_theThingStatusBecomesOFFLINE() {
95 Configuration thingConfiguration = new Configuration();
96 thingConfiguration.put(GEOLOCATION_PROPERTY, NULL_LONGITUDE);
97 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
101 public void testIfTheLongitudeForAMoonThingIsNull_theThingStatusBecomesOFFLINE() {
102 Configuration thingConfiguration = new Configuration();
103 thingConfiguration.put(GEOLOCATION_PROPERTY, NULL_LONGITUDE);
104 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
108 public void testIfTheIntervalForASunThingIsLessThan1_theThingStatusBecomesOFFLINE() {
109 Configuration thingConfiguration = new Configuration();
110 thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
111 thingConfiguration.put(INTERVAL_PROPERTY, Integer.valueOf(0));
112 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
116 public void testIfTheIntervalForAMoonThingIsLessThan1_theThingStatusBecomesOFFLINE() {
117 Configuration thingConfiguration = new Configuration();
118 thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
119 thingConfiguration.put(INTERVAL_PROPERTY, Integer.valueOf(0));
120 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
124 public void testIfTheIntervalForASunThingIsGreaterThan86400_theThingStatusBecomesOFFLINE() {
125 Configuration thingConfiguration = new Configuration();
126 thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
127 thingConfiguration.put(INTERVAL_PROPERTY, Integer.valueOf(86401));
128 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
132 public void testIfTheIntervalForAMoonThingIsGreaterThan86400_theThingStatusBecomesOFFLINE() {
133 Configuration thingConfiguration = new Configuration();
134 thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
135 thingConfiguration.put(INTERVAL_PROPERTY, Integer.valueOf(86401));
136 assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
139 private void assertThingStatus(Configuration configuration, ThingStatus expectedStatus,
140 ThingStatusDetail expectedStatusDetail) {
141 ThingUID thingUID = new ThingUID(THING_TYPE_SUN, TEST_SUN_THING_ID);
143 Thing thing = mock(Thing.class);
144 when(thing.getConfiguration()).thenReturn(configuration);
145 when(thing.getUID()).thenReturn(thingUID);
147 ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
148 CronScheduler cronScheduler = mock(CronScheduler.class);
149 TimeZoneProvider timeZoneProvider = mock(TimeZoneProvider.class);
150 when(timeZoneProvider.getTimeZone()).thenReturn(ZoneId.systemDefault());
151 ThingHandler sunHandler = new SunHandler(thing, cronScheduler, timeZoneProvider);
152 sunHandler.setCallback(callback);
154 sunHandler.initialize();
156 ThingStatusInfo expectedThingStatus = new ThingStatusInfo(expectedStatus, expectedStatusDetail, null);
157 verify(callback, times(1)).statusUpdated(thing, expectedThingStatus);