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.handler.test;
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
17 import static org.openhab.binding.astro.internal.AstroBindingConstants.THING_TYPE_SUN;
18 import static org.openhab.binding.astro.test.cases.AstroBindingTestsData.*;
20 import java.time.ZoneId;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.astro.internal.handler.AstroThingHandler;
24 import org.openhab.binding.astro.internal.handler.SunHandler;
25 import org.openhab.binding.astro.internal.model.Sun;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.i18n.TimeZoneProvider;
28 import org.openhab.core.scheduler.CronScheduler;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandlerCallback;
34 import org.openhab.core.thing.binding.builder.ChannelBuilder;
35 import org.openhab.core.types.RefreshType;
36 import org.openhab.core.types.State;
39 * OSGi test for the {@link AstroThingHandler}
41 * This class tests the commands for the astro thing.
43 * @author Petar Valchev - Initial implementation
44 * @author Svilen Valkanov - Reworked to plain unit tests
45 * @author Christoph Weitkamp - Migrated tests to pure Java
47 public class AstroCommandTest {
50 public void testRefreshCommandUpdatesTheStateOfTheChannels() {
51 ThingUID thingUID = new ThingUID(THING_TYPE_SUN, TEST_SUN_THING_ID);
52 ChannelUID channelUID = new ChannelUID(thingUID, DEFAULT_TEST_CHANNEL_ID);
53 Channel channel = ChannelBuilder.create(channelUID, DEFAULT_IMEM_TYPE).build();
55 Configuration thingConfiguration = new Configuration();
56 thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
57 thingConfiguration.put(INTERVAL_PROPERTY, INTERVAL_DEFAULT_VALUE);
59 Thing thing = mock(Thing.class);
60 when(thing.getConfiguration()).thenReturn(thingConfiguration);
61 when(thing.getUID()).thenReturn(thingUID);
62 when(thing.getChannel(DEFAULT_TEST_CHANNEL_ID)).thenReturn(channel);
64 ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
65 CronScheduler cronScheduler = mock(CronScheduler.class);
66 TimeZoneProvider timeZoneProvider = mock(TimeZoneProvider.class);
67 when(timeZoneProvider.getTimeZone()).thenReturn(ZoneId.systemDefault());
68 AstroThingHandler sunHandler = spy(new SunHandler(thing, cronScheduler, timeZoneProvider));
70 // Required from the AstroThingHandler to send the status update
71 doReturn(true).when(callback).isChannelLinked(eq(channelUID));
72 doReturn(new Sun()).when(sunHandler).getPlanet();
73 sunHandler.setCallback(callback);
75 sunHandler.handleCommand(channelUID, RefreshType.REFRESH);
76 verify(callback, times(1)).stateUpdated(eq(channelUID), any(State.class));