]> git.basschouten.com Git - openhab-addons.git/blob
adc33a911b65e6493617ee7dfea5f67623c8406f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.internal.model;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.time.ZoneId;
18
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.astro.internal.config.AstroChannelConfig;
22 import org.openhab.binding.astro.internal.util.PropertyUtils;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.types.UnDefType;
26
27 /***
28  * A set of standard unit test of {@link Sun} class. In particular it checks if
29  * {@link Sun#getAllRanges()} contains a correct {@link SunPhaseName}.
30  *
31  * @author Witold Markowski - Initial contribution
32  * @see <a href="https://github.com/openhab/openhab-addons/issues/5006">[astro]
33  *      Sun Phase returns UNDEF</a>
34  */
35 public class SunTest {
36
37     private Sun sun;
38     private AstroChannelConfig config;
39
40     private static ZoneId ZONE = ZoneId.systemDefault();
41
42     @BeforeEach
43     public void init() {
44         sun = new Sun();
45         config = new AstroChannelConfig();
46     }
47
48     @Test
49     public void testConstructor() throws Exception {
50         assertNotNull(sun.getPhase());
51         assertEquals(UnDefType.UNDEF,
52                 PropertyUtils.getState(new ChannelUID("astro:sun:home:phase#name"), config, sun, ZONE));
53     }
54
55     @Test
56     public void testGetStateWhenNullPhaseName() throws Exception {
57         sun.getPhase().setName(null);
58
59         assertEquals(UnDefType.UNDEF,
60                 PropertyUtils.getState(new ChannelUID("astro:sun:home:phase#name"), config, sun, ZONE));
61     }
62
63     @Test
64     public void testGetStateWhenNotNullPhaseName() throws Exception {
65         sun.getPhase().setName(SunPhaseName.DAYLIGHT);
66
67         assertEquals(new StringType("DAYLIGHT"),
68                 PropertyUtils.getState(new ChannelUID("astro:sun:home:phase#name"), config, sun, ZONE));
69     }
70
71     @Test
72     public void testGetStateWhenNullPhase() throws Exception {
73         sun.setPhase(null);
74
75         assertNull(sun.getPhase());
76
77         assertThrows(NullPointerException.class, () -> assertEquals(UnDefType.UNDEF,
78                 PropertyUtils.getState(new ChannelUID("astro:sun:home:phase#name"), config, sun, ZONE)));
79     }
80
81     @Test
82     public void testGetAllRangesForNight() {
83         sun.setNight(new Range());
84
85         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NIGHT));
86     }
87
88     @Test
89     public void testGetAllRangesForMorningNight() {
90         sun.setMorningNight(new Range());
91
92         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.MORNING_NIGHT));
93     }
94
95     @Test
96     public void testGetAllRangesForAstroDawn() {
97         sun.setAstroDawn(new Range());
98
99         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.ASTRO_DAWN));
100     }
101
102     @Test
103     public void testGetAllRangesForNauticDawn() {
104         sun.setNauticDawn(new Range());
105
106         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NAUTIC_DAWN));
107     }
108
109     @Test
110     public void testGetAllRangesForCivilDawn() {
111         sun.setCivilDawn(new Range());
112
113         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.CIVIL_DAWN));
114     }
115
116     @Test
117     public void testGetAllRangesForRise() {
118         sun.setRise(new Range());
119
120         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.SUN_RISE));
121     }
122
123     @Test
124     public void testGetAllRangesForDaylight() {
125         sun.setDaylight(new Range());
126
127         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.DAYLIGHT));
128     }
129
130     @Test
131     public void testGetAllRangesForNoon() {
132         sun.setNoon(new Range());
133
134         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NOON));
135     }
136
137     @Test
138     public void testGetAllRangesForSet() {
139         sun.setSet(new Range());
140
141         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.SUN_SET));
142     }
143
144     @Test
145     public void testGetAllRangesForCivilDusk() {
146         sun.setCivilDusk(new Range());
147
148         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.CIVIL_DUSK));
149     }
150
151     @Test
152     public void testGetAllRangesForNauticDusk() {
153         sun.setNauticDusk(new Range());
154
155         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NAUTIC_DUSK));
156     }
157
158     @Test
159     public void testGetAllRangesForAstroDusk() {
160         sun.setAstroDusk(new Range());
161
162         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.ASTRO_DUSK));
163     }
164
165     @Test
166     public void testGetAllRangesForEveningNight() {
167         sun.setEveningNight(new Range());
168
169         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.EVENING_NIGHT));
170     }
171 }