]> git.basschouten.com Git - openhab-addons.git/blob
88f6ca913d119967e80ad2995a610c9bbcff1add
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.ahawastecollection.internal;
14
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
17
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.Arrays;
21 import java.util.Date;
22 import java.util.Map;
23 import java.util.concurrent.ScheduledExecutorService;
24
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;
43
44 /**
45  * @author Sönke Küper - Initial contribution
46  */
47 @NonNullByDefault
48 public class AhaWasteCollectionHandlerTest {
49
50     private static final Configuration CONFIG = createConfig();
51
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");
59         return config;
60     }
61
62     /**
63      * Exception indicating that the execution of a script within the stub-Scheduler failed.
64      */
65     private static class SchedulerRuntimeException extends RuntimeException {
66
67         private static final long serialVersionUID = -1262671065082256315L;
68
69         public SchedulerRuntimeException(@Nullable final Throwable cause) {
70             super(cause);
71         }
72     }
73
74     /**
75      * Creates a {@link CronScheduler} that executes all commands synchronous.
76      */
77     @SuppressWarnings("unchecked")
78     private static CronScheduler createStubScheduler() {
79         return new CronScheduler() {
80
81             @Override
82             public ScheduledCompletableFuture<Void> schedule(final CronJob cronJob, final Map<String, Object> config,
83                     final String cronExpression) {
84                 try {
85                     cronJob.run(config);
86                 } catch (final Exception e) {
87                     throw new SchedulerRuntimeException(e);
88                 }
89                 return Mockito.mock(ScheduledCompletableFuture.class);
90             }
91
92             @Override
93             public ScheduledCompletableFuture<Void> schedule(final SchedulerRunnable runnable,
94                     final String cronExpression) {
95                 try {
96                     runnable.run();
97                 } catch (final Exception e) {
98                     throw new SchedulerRuntimeException(e);
99                 }
100                 return Mockito.mock(ScheduledCompletableFuture.class);
101             }
102         };
103     }
104
105     private static Thing mockThing() {
106         final Thing thing = mock(Thing.class);
107         when(thing.getUID())
108                 .thenReturn(new ThingUID(AhaWasteCollectionBindingConstants.THING_TYPE_SCHEDULE, "collectionCalendar"));
109         when(thing.getConfiguration()).thenReturn(CONFIG);
110
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);
117
118         when(thing.getChannels()).thenReturn(
119                 Arrays.asList(channelBioWaste, channelGeneralWaste, channelLightweightPackaging, channelPaper));
120         return thing;
121     }
122
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));
126         return channel;
127     }
128
129     private static AhaWasteCollectionHandler createAndInitHandler(final ThingHandlerCallback callback,
130             final Thing thing) {
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();
135             return null;
136         }).when(executorStub).execute(any(Runnable.class));
137
138         final AhaWasteCollectionHandler handler = new AhaWasteCollectionHandler(thing, createStubScheduler(),
139                 ZoneId::systemDefault, new AhaCollectionScheduleStubFactory(), executorStub);
140         handler.setCallback(callback);
141         handler.initialize();
142         return handler;
143     }
144
145     private static State getDateTime(final Date day) {
146         final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(day.toInstant(), ZoneId.systemDefault());
147         return new DateTimeType(zonedDateTime);
148     }
149
150     @Test
151     public void testUpdateChannels() {
152         final Thing thing = mockThing();
153         final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
154         final AhaWasteCollectionHandler handler = createAndInitHandler(callback, thing);
155
156         try {
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));
169         } finally {
170             handler.dispose();
171         }
172     }
173 }