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.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;
23 import java.util.concurrent.ScheduledExecutorService;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.junit.jupiter.api.Test;
28 import org.mockito.Mockito;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.openhab.core.config.core.Configuration;
31 import org.openhab.core.library.types.DateTimeType;
32 import org.openhab.core.scheduler.CronJob;
33 import org.openhab.core.scheduler.CronScheduler;
34 import org.openhab.core.scheduler.ScheduledCompletableFuture;
35 import org.openhab.core.scheduler.SchedulerRunnable;
36 import org.openhab.core.thing.Channel;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingStatus;
40 import org.openhab.core.thing.ThingUID;
41 import org.openhab.core.thing.binding.ThingHandlerCallback;
42 import org.openhab.core.types.State;
45 * @author Sönke Küper - Initial contribution
48 public class AhaWasteCollectionHandlerTest {
50 private static final Configuration CONFIG = createConfig();
52 private static Configuration createConfig() {
53 final Configuration config = new Configuration();
54 config.put("commune", "Hannover");
55 config.put("collectionPlace", "02095-0010+");
56 config.put("houseNumber", "10");
57 config.put("houseNumberAddon", "");
58 config.put("street", "02095@Oesterleystr.+/+Südstadt@Südstadt");
63 * Exception indicating that the execution of a script within the stub-Scheduler failed.
65 private static class SchedulerRuntimeException extends RuntimeException {
67 private static final long serialVersionUID = -1262671065082256315L;
69 public SchedulerRuntimeException(@Nullable final Throwable cause) {
75 * Creates a {@link CronScheduler} that executes all commands synchronous.
77 @SuppressWarnings("unchecked")
78 private static CronScheduler createStubScheduler() {
79 return new CronScheduler() {
82 public ScheduledCompletableFuture<Void> schedule(final CronJob cronJob, final Map<String, Object> config,
83 final String cronExpression) {
86 } catch (final Exception e) {
87 throw new SchedulerRuntimeException(e);
89 return Mockito.mock(ScheduledCompletableFuture.class);
93 public ScheduledCompletableFuture<Void> schedule(final SchedulerRunnable runnable,
94 final String cronExpression) {
97 } catch (final Exception e) {
98 throw new SchedulerRuntimeException(e);
100 return Mockito.mock(ScheduledCompletableFuture.class);
105 private static Thing mockThing() {
106 final Thing thing = mock(Thing.class);
108 .thenReturn(new ThingUID(AhaWasteCollectionBindingConstants.THING_TYPE_SCHEDULE, "collectionCalendar"));
109 when(thing.getConfiguration()).thenReturn(CONFIG);
111 final Channel channelBioWaste = mockChannel(thing.getUID(), AhaWasteCollectionBindingConstants.BIOWASTE);
112 final Channel channelGeneralWaste = mockChannel(thing.getUID(),
113 AhaWasteCollectionBindingConstants.GENERAL_WASTE);
114 final Channel channelPaper = mockChannel(thing.getUID(), AhaWasteCollectionBindingConstants.PAPER);
115 final Channel channelLightweightPackaging = mockChannel(thing.getUID(),
116 AhaWasteCollectionBindingConstants.LEIGHTWEIGHT_PACKAGING);
118 when(thing.getChannels()).thenReturn(
119 Arrays.asList(channelBioWaste, channelGeneralWaste, channelLightweightPackaging, channelPaper));
123 private static Channel mockChannel(final ThingUID thingId, final String channelId) {
124 final Channel channel = Mockito.mock(Channel.class);
125 when(channel.getUID()).thenReturn(new ChannelUID(thingId, channelId));
129 private static AhaWasteCollectionHandler createAndInitHandler(final ThingHandlerCallback callback,
131 // Executor that executes all commands synchronous.
132 final ScheduledExecutorService executorStub = Mockito.mock(ScheduledExecutorService.class);
133 doAnswer((InvocationOnMock invocation) -> {
134 ((Runnable) invocation.getArguments()[0]).run();
136 }).when(executorStub).execute(any(Runnable.class));
138 final AhaWasteCollectionHandler handler = new AhaWasteCollectionHandler(thing, createStubScheduler(),
139 ZoneId::systemDefault, new AhaCollectionScheduleStubFactory(), executorStub);
140 handler.setCallback(callback);
141 handler.initialize();
145 private static State getDateTime(final Date day) {
146 final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(day.toInstant(), ZoneId.systemDefault());
147 return new DateTimeType(zonedDateTime);
151 public void testUpdateChannels() {
152 final Thing thing = mockThing();
153 final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
154 final AhaWasteCollectionHandler handler = createAndInitHandler(callback, thing);
157 verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
158 verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
159 verify(callback).stateUpdated(new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.BIOWASTE),
160 getDateTime(AhaCollectionScheduleStub.BIO_WASTE_DATE));
161 verify(callback).stateUpdated(
162 new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.GENERAL_WASTE),
163 getDateTime(AhaCollectionScheduleStub.GENERAL_WASTE_DATE));
164 verify(callback).stateUpdated(
165 new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.LEIGHTWEIGHT_PACKAGING),
166 getDateTime(AhaCollectionScheduleStub.LEIGHTWEIGHT_PACKAGING_DATE));
167 verify(callback).stateUpdated(new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.PAPER),
168 getDateTime(AhaCollectionScheduleStub.PAPER_DATE));