]> git.basschouten.com Git - openhab-addons.git/blob
0229f07fcbde34effd60418cad20dae5472e2e17
[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.handler.test;
14
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.*;
18
19 import java.time.ZoneId;
20
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;
34
35 /**
36  * Tests for the {@link AstroThingHandler}
37  * <p>
38  * This class tests the required configuration for the astro thing.
39  *
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
43  */
44 public class AstroValidConfigurationTest {
45
46     private final String NULL_LONGITUDE = "51.2,null";
47     private final String NULL_LATITUDE = "null,25.4";
48
49     @Test
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);
55     }
56
57     @Test
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);
63     }
64
65     @Test
66     public void testIfGeolocationForASunThingIsNull_theThingStatusBecomesOFFLINE() {
67         Configuration thingConfiguration = new Configuration();
68         thingConfiguration.put(GEOLOCATION_PROPERTY, null);
69         assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
70     }
71
72     @Test
73     public void testIfGeolocationForAMoonThingIsNull_theThingStatusBecomesOFFLINE() {
74         Configuration thingConfiguration = new Configuration();
75         thingConfiguration.put(GEOLOCATION_PROPERTY, null);
76         assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
77     }
78
79     @Test
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);
84     }
85
86     @Test
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);
91     }
92
93     @Test
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);
98     }
99
100     @Test
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);
105     }
106
107     @Test
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);
113     }
114
115     @Test
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);
121     }
122
123     @Test
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);
129     }
130
131     @Test
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);
137     }
138
139     private void assertThingStatus(Configuration configuration, ThingStatus expectedStatus,
140             ThingStatusDetail expectedStatusDetail) {
141         ThingUID thingUID = new ThingUID(THING_TYPE_SUN, TEST_SUN_THING_ID);
142
143         Thing thing = mock(Thing.class);
144         when(thing.getConfiguration()).thenReturn(configuration);
145         when(thing.getUID()).thenReturn(thingUID);
146
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);
153
154         sunHandler.initialize();
155
156         ThingStatusInfo expectedThingStatus = new ThingStatusInfo(expectedStatus, expectedStatusDetail, null);
157         verify(callback, times(1)).statusUpdated(thing, expectedThingStatus);
158     }
159 }