2 * Copyright (c) 2010-2020 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.Assert.*;
17 import java.time.ZoneId;
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;
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 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));
71 @Test(expected = NullPointerException.class)
72 public void testGetStateWhenNullPhase() throws Exception {
75 assertNull(sun.getPhase());
76 assertEquals(UnDefType.UNDEF,
77 PropertyUtils.getState(new ChannelUID("astro:sun:home:phase#name"), config, sun, ZONE));
81 public void testGetAllRangesForNight() {
82 sun.setNight(new Range());
84 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NIGHT));
88 public void testGetAllRangesForMorningNight() {
89 sun.setMorningNight(new Range());
91 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.MORNING_NIGHT));
95 public void testGetAllRangesForAstroDawn() {
96 sun.setAstroDawn(new Range());
98 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.ASTRO_DAWN));
102 public void testGetAllRangesForNauticDawn() {
103 sun.setNauticDawn(new Range());
105 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NAUTIC_DAWN));
109 public void testGetAllRangesForCivilDawn() {
110 sun.setCivilDawn(new Range());
112 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.CIVIL_DAWN));
116 public void testGetAllRangesForRise() {
117 sun.setRise(new Range());
119 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.SUN_RISE));
123 public void testGetAllRangesForDaylight() {
124 sun.setDaylight(new Range());
126 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.DAYLIGHT));
130 public void testGetAllRangesForNoon() {
131 sun.setNoon(new Range());
133 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NOON));
137 public void testGetAllRangesForSet() {
138 sun.setSet(new Range());
140 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.SUN_SET));
144 public void testGetAllRangesForCivilDusk() {
145 sun.setCivilDusk(new Range());
147 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.CIVIL_DUSK));
151 public void testGetAllRangesForNauticDusk() {
152 sun.setNauticDusk(new Range());
154 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.NAUTIC_DUSK));
158 public void testGetAllRangesForAstroDusk() {
159 sun.setAstroDusk(new Range());
161 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.ASTRO_DUSK));
165 public void testGetAllRangesForEveningNight() {
166 sun.setEveningNight(new Range());
168 assertTrue(sun.getAllRanges().containsKey(SunPhaseName.EVENING_NIGHT));