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.internal.model;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.time.ZoneId;
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;
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}.
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>
35 public class SunTest {
38 private AstroChannelConfig config;
40 private static final ZoneId ZONE = ZoneId.systemDefault();
45 config = new AstroChannelConfig();
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));
56 public void testGetStateWhenNullPhaseName() throws Exception {
57 sun.getPhase().setName(null);
59 assertEquals(UnDefType.UNDEF,
60 PropertyUtils.getState(new ChannelUID("astro:sun:home:phase#name"), config, sun, ZONE));
64 public void testGetStateWhenNotNullPhaseName() throws Exception {
65 sun.getPhase().setName(SunPhaseName.DAYLIGHT);
67 assertEquals(new StringType("DAYLIGHT"),
68 PropertyUtils.getState(new ChannelUID("astro:sun:home:phase#name"), config, sun, ZONE));
72 public void testGetStateWhenNullPhase() throws Exception {
75 assertNull(sun.getPhase());
77 assertThrows(NullPointerException.class, () -> assertEquals(UnDefType.UNDEF,
78 PropertyUtils.getState(new ChannelUID("astro:sun:home:phase#name"), config, sun, ZONE)));
82 public void testGetAllRangesForNight() {
83 sun.setNight(new Range());
85 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NIGHT));
89 public void testGetAllRangesForMorningNight() {
90 sun.setMorningNight(new Range());
92 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.MORNING_NIGHT));
96 public void testGetAllRangesForAstroDawn() {
97 sun.setAstroDawn(new Range());
99 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.ASTRO_DAWN));
103 public void testGetAllRangesForNauticDawn() {
104 sun.setNauticDawn(new Range());
106 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NAUTIC_DAWN));
110 public void testGetAllRangesForCivilDawn() {
111 sun.setCivilDawn(new Range());
113 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.CIVIL_DAWN));
117 public void testGetAllRangesForRise() {
118 sun.setRise(new Range());
120 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.SUN_RISE));
124 public void testGetAllRangesForDaylight() {
125 sun.setDaylight(new Range());
127 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.DAYLIGHT));
131 public void testGetAllRangesForNoon() {
132 sun.setNoon(new Range());
134 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NOON));
138 public void testGetAllRangesForSet() {
139 sun.setSet(new Range());
141 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.SUN_SET));
145 public void testGetAllRangesForCivilDusk() {
146 sun.setCivilDusk(new Range());
148 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.CIVIL_DUSK));
152 public void testGetAllRangesForNauticDusk() {
153 sun.setNauticDusk(new Range());
155 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NAUTIC_DUSK));
159 public void testGetAllRangesForAstroDusk() {
160 sun.setAstroDusk(new Range());
162 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.ASTRO_DUSK));
166 public void testGetAllRangesForEveningNight() {
167 sun.setEveningNight(new Range());
169 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.EVENING_NIGHT));