]> git.basschouten.com Git - openhab-addons.git/blob
387a6ab0ca857a5a38e92dcc2ef050af59493b22
[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.internal.model;
14
15 import static org.junit.Assert.*;
16
17 import java.time.ZoneId;
18
19 import org.junit.Before;
20 import org.junit.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     @Before
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(expected = NullPointerException.class)
72     public void testGetStateWhenNullPhase() throws Exception {
73         sun.setPhase(null);
74
75         assertNull(sun.getPhase());
76         assertEquals(UnDefType.UNDEF,
77                 PropertyUtils.getState(new ChannelUID("astro:sun:home:phase#name"), config, sun, ZONE));
78     }
79
80     @Test
81     public void testGetAllRangesForNight() {
82         sun.setNight(new Range());
83
84         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NIGHT));
85     }
86
87     @Test
88     public void testGetAllRangesForMorningNight() {
89         sun.setMorningNight(new Range());
90
91         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.MORNING_NIGHT));
92     }
93
94     @Test
95     public void testGetAllRangesForAstroDawn() {
96         sun.setAstroDawn(new Range());
97
98         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.ASTRO_DAWN));
99     }
100
101     @Test
102     public void testGetAllRangesForNauticDawn() {
103         sun.setNauticDawn(new Range());
104
105         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NAUTIC_DAWN));
106     }
107
108     @Test
109     public void testGetAllRangesForCivilDawn() {
110         sun.setCivilDawn(new Range());
111
112         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.CIVIL_DAWN));
113     }
114
115     @Test
116     public void testGetAllRangesForRise() {
117         sun.setRise(new Range());
118
119         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.SUN_RISE));
120     }
121
122     @Test
123     public void testGetAllRangesForDaylight() {
124         sun.setDaylight(new Range());
125
126         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.DAYLIGHT));
127     }
128
129     @Test
130     public void testGetAllRangesForNoon() {
131         sun.setNoon(new Range());
132
133         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NOON));
134     }
135
136     @Test
137     public void testGetAllRangesForSet() {
138         sun.setSet(new Range());
139
140         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.SUN_SET));
141     }
142
143     @Test
144     public void testGetAllRangesForCivilDusk() {
145         sun.setCivilDusk(new Range());
146
147         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.CIVIL_DUSK));
148     }
149
150     @Test
151     public void testGetAllRangesForNauticDusk() {
152         sun.setNauticDusk(new Range());
153
154         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NAUTIC_DUSK));
155     }
156
157     @Test
158     public void testGetAllRangesForAstroDusk() {
159         sun.setAstroDusk(new Range());
160
161         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.ASTRO_DUSK));
162     }
163
164     @Test
165     public void testGetAllRangesForEveningNight() {
166         sun.setEveningNight(new Range());
167
168         assertTrue(sun.getAllRanges().containsKey(SunPhaseName.EVENING_NIGHT));
169     }
170 }