]> git.basschouten.com Git - openhab-addons.git/blob
9e709f3911e2f2248103083cba182bc294bf85d0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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
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;
41
42 /**
43  * @author Sönke Küper - Initial contribution
44  */
45 @NonNullByDefault
46 public class AhaWasteCollectionHandlerTest {
47
48     private static final Configuration CONFIG = createConfig();
49
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");
57         return config;
58     }
59
60     /**
61      * Exception indicating that the execution of an script within the stub-Scheduler failed.
62      */
63     private static class SchedulerRuntimeException extends RuntimeException {
64
65         private static final long serialVersionUID = -1262671065082256315L;
66
67         public SchedulerRuntimeException(@Nullable final Throwable cause) {
68             super(cause);
69         }
70     }
71
72     /**
73      * Creates an {@link CronScheduler} that executes all commands synchronous.
74      */
75     @SuppressWarnings("unchecked")
76     private static CronScheduler createStubScheduler() {
77         return new CronScheduler() {
78
79             @Override
80             public final ScheduledCompletableFuture<Void> schedule(final CronJob cronJob,
81                     final Map<String, Object> config, final String cronExpression) {
82                 try {
83                     cronJob.run(config);
84                 } catch (final Exception e) {
85                     throw new SchedulerRuntimeException(e);
86                 }
87                 return Mockito.mock(ScheduledCompletableFuture.class);
88             }
89
90             @Override
91             public final ScheduledCompletableFuture<Void> schedule(final SchedulerRunnable runnable,
92                     final String cronExpression) {
93                 try {
94                     runnable.run();
95                 } catch (final Exception e) {
96                     throw new SchedulerRuntimeException(e);
97                 }
98                 return Mockito.mock(ScheduledCompletableFuture.class);
99             }
100         };
101     }
102
103     private static Thing mockThing(final Configuration config) {
104         final Thing thing = mock(Thing.class);
105         when(thing.getUID())
106                 .thenReturn(new ThingUID(AhaWasteCollectionBindingConstants.THING_TYPE_SCHEDULE, "collectionCalendar"));
107         when(thing.getConfiguration()).thenReturn(config);
108
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);
115
116         when(thing.getChannels()).thenReturn(
117                 Arrays.asList(channelBioWaste, channelGeneralWaste, channelLeightweightPackaging, channelPaper));
118         return thing;
119     }
120
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));
124         return channel;
125     }
126
127     private static AhaWasteCollectionHandler createAndInitHandler(final ThingHandlerCallback callback,
128             final Thing thing) {
129         final AhaWasteCollectionHandler handler = new AhaWasteCollectionHandler(thing, createStubScheduler(),
130                 ZoneId::systemDefault, new AhaCollectionScheduleStubFactory());
131         handler.setCallback(callback);
132         handler.initialize();
133         return handler;
134     }
135
136     private static State getDateTime(final Date day) {
137         final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(day.toInstant(), ZoneId.systemDefault());
138         return new DateTimeType(zonedDateTime);
139     }
140
141     @Test
142     public void testUpdateChannels() {
143         final Thing thing = mockThing(CONFIG);
144         final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
145         final AhaWasteCollectionHandler handler = createAndInitHandler(callback, thing);
146
147         try {
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));
163         } finally {
164             handler.dispose();
165         }
166     }
167 }