2 * Copyright (c) 2010-2022 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.ahawastecollection.internal;
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.Arrays;
21 import java.util.Date;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.jupiter.api.Test;
27 import org.mockito.Mockito;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.library.types.DateTimeType;
30 import org.openhab.core.scheduler.CronJob;
31 import org.openhab.core.scheduler.CronScheduler;
32 import org.openhab.core.scheduler.ScheduledCompletableFuture;
33 import org.openhab.core.scheduler.SchedulerRunnable;
34 import org.openhab.core.thing.Channel;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.ThingHandlerCallback;
40 import org.openhab.core.types.State;
43 * @author Sönke Küper - Initial contribution
46 public class AhaWasteCollectionHandlerTest {
48 private static final Configuration CONFIG = createConfig();
50 private static Configuration createConfig() {
51 final Configuration config = new Configuration();
52 config.put("commune", "Hannover");
53 config.put("collectionPlace", "02095-0010+");
54 config.put("houseNumber", "10");
55 config.put("houseNumberAddon", "");
56 config.put("street", "02095@Oesterleystr.+/+Südstadt@Südstadt");
61 * Exception indicating that the execution of a script within the stub-Scheduler failed.
63 private static class SchedulerRuntimeException extends RuntimeException {
65 private static final long serialVersionUID = -1262671065082256315L;
67 public SchedulerRuntimeException(@Nullable final Throwable cause) {
73 * Creates a {@link CronScheduler} that executes all commands synchronous.
75 @SuppressWarnings("unchecked")
76 private static CronScheduler createStubScheduler() {
77 return new CronScheduler() {
80 public final ScheduledCompletableFuture<Void> schedule(final CronJob cronJob,
81 final Map<String, Object> config, final String cronExpression) {
84 } catch (final Exception e) {
85 throw new SchedulerRuntimeException(e);
87 return Mockito.mock(ScheduledCompletableFuture.class);
91 public final ScheduledCompletableFuture<Void> schedule(final SchedulerRunnable runnable,
92 final String cronExpression) {
95 } catch (final Exception e) {
96 throw new SchedulerRuntimeException(e);
98 return Mockito.mock(ScheduledCompletableFuture.class);
103 private static Thing mockThing(final Configuration config) {
104 final Thing thing = mock(Thing.class);
106 .thenReturn(new ThingUID(AhaWasteCollectionBindingConstants.THING_TYPE_SCHEDULE, "collectionCalendar"));
107 when(thing.getConfiguration()).thenReturn(config);
109 final Channel channelBioWaste = mockChannel(thing.getUID(), AhaWasteCollectionBindingConstants.BIOWASTE);
110 final Channel channelGeneralWaste = mockChannel(thing.getUID(),
111 AhaWasteCollectionBindingConstants.GENERAL_WASTE);
112 final Channel channelPaper = mockChannel(thing.getUID(), AhaWasteCollectionBindingConstants.PAPER);
113 final Channel channelLeightweightPackaging = mockChannel(thing.getUID(),
114 AhaWasteCollectionBindingConstants.LEIGHTWEIGHT_PACKAGING);
116 when(thing.getChannels()).thenReturn(
117 Arrays.asList(channelBioWaste, channelGeneralWaste, channelLeightweightPackaging, channelPaper));
121 private static Channel mockChannel(final ThingUID thingId, final String channelId) {
122 final Channel channel = Mockito.mock(Channel.class);
123 when(channel.getUID()).thenReturn(new ChannelUID(thingId, channelId));
127 private static AhaWasteCollectionHandler createAndInitHandler(final ThingHandlerCallback callback,
129 final AhaWasteCollectionHandler handler = new AhaWasteCollectionHandler(thing, createStubScheduler(),
130 ZoneId::systemDefault, new AhaCollectionScheduleStubFactory());
131 handler.setCallback(callback);
132 handler.initialize();
136 private static State getDateTime(final Date day) {
137 final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(day.toInstant(), ZoneId.systemDefault());
138 return new DateTimeType(zonedDateTime);
142 public void testUpdateChannels() {
143 final Thing thing = mockThing(CONFIG);
144 final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
145 final AhaWasteCollectionHandler handler = createAndInitHandler(callback, thing);
148 verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
149 verify(callback, timeout(1000)).statusUpdated(eq(thing),
150 argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
151 verify(callback, timeout(1000)).stateUpdated(
152 new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.BIOWASTE),
153 getDateTime(AhaCollectionScheduleStub.BIO_WASTE_DATE));
154 verify(callback, timeout(1000)).stateUpdated(
155 new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.GENERAL_WASTE),
156 getDateTime(AhaCollectionScheduleStub.GENERAL_WASTE_DATE));
157 verify(callback, timeout(1000)).stateUpdated(
158 new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.LEIGHTWEIGHT_PACKAGING),
159 getDateTime(AhaCollectionScheduleStub.LEIGHTWEIGHT_PACKAGING_DATE));
160 verify(callback, timeout(1000)).stateUpdated(
161 new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.PAPER),
162 getDateTime(AhaCollectionScheduleStub.PAPER_DATE));