]> git.basschouten.com Git - openhab-addons.git/blob
8da6c67198bd5d06933744fd26091f5fcc1e2c40
[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.onewire.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.times;
18 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.InOrder;
25 import org.mockito.Mockito;
26 import org.openhab.binding.onewire.internal.handler.EDSSensorThingHandler;
27 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
28 import org.openhab.binding.onewire.test.AbstractThingHandlerTest;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.builder.ChannelBuilder;
37 import org.openhab.core.thing.binding.builder.ThingBuilder;
38
39 /**
40  * Tests cases for {@link EDSSensorThingHandler}.
41  *
42  * @author Jan N. Klug - Initial contribution
43  */
44 @NonNullByDefault
45 public class EDSSensorThingHandlerTest extends AbstractThingHandlerTest {
46     private static final String TEST_ID = "00.000000000000";
47     private static final ThingUID THING_UID = new ThingUID(THING_TYPE_EDS_ENV, "testthing");
48     private static final ChannelUID CHANNEL_UID_TEMPERATURE = new ChannelUID(THING_UID, CHANNEL_TEMPERATURE);
49     private static final ChannelUID CHANNEL_UID_HUMIDITY = new ChannelUID(THING_UID, CHANNEL_HUMIDITY);
50     private static final ChannelUID CHANNEL_UID_ABSOLUTE_HUMIDITY = new ChannelUID(THING_UID,
51             CHANNEL_ABSOLUTE_HUMIDITY);
52     private static final ChannelUID CHANNEL_UID_DEWPOINT = new ChannelUID(THING_UID, CHANNEL_DEWPOINT);
53
54     @BeforeEach
55     public void setup() throws OwException {
56         initializeBridge();
57
58         final Bridge bridge = this.bridge;
59         if (bridge == null) {
60             fail("bridge is null");
61             return;
62         }
63
64         thingConfiguration.put(CONFIG_ID, TEST_ID);
65
66         channels.add(ChannelBuilder.create(CHANNEL_UID_TEMPERATURE, "Number:Temperature").build());
67         channels.add(ChannelBuilder.create(CHANNEL_UID_HUMIDITY, "Number:Dimensionless").build());
68         channels.add(ChannelBuilder.create(CHANNEL_UID_ABSOLUTE_HUMIDITY, "Number:Density").build());
69         channels.add(ChannelBuilder.create(CHANNEL_UID_DEWPOINT, "Number:Temperature").build());
70
71         thing = ThingBuilder.create(THING_TYPE_EDS_ENV, "testthing").withLabel("Test thing").withChannels(channels)
72                 .withConfiguration(new Configuration(thingConfiguration)).withProperties(thingProperties)
73                 .withBridge(bridge.getUID()).build();
74
75         final Thing thing = this.thing;
76         if (thing == null) {
77             fail("thing is null");
78             return;
79         }
80
81         thingHandler = new EDSSensorThingHandler(thing, stateProvider) {
82             @Override
83             protected @Nullable Bridge getBridge() {
84                 return bridge;
85             }
86         };
87
88         initializeHandlerMocks();
89
90         Mockito.doAnswer(answer -> {
91             return new OwPageBuffer("EDS0065 ".getBytes());
92         }).when(secondBridgeHandler).readPages(any());
93     }
94
95     @Test
96     public void testInitializationEndsWithUnknown() {
97         final ThingHandler thingHandler = this.thingHandler;
98         if (thingHandler == null) {
99             fail("thingHandler is null");
100             return;
101         }
102
103         thingHandler.initialize();
104
105         waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
106     }
107
108     @Test
109     public void testRefresh() throws OwException {
110         final OwBaseThingHandler thingHandler = this.thingHandler;
111         final InOrder inOrder = this.inOrder;
112         if (thingHandler == null || inOrder == null) {
113             fail("prerequisite is null");
114             return;
115         }
116
117         thingHandler.initialize();
118
119         // needed to determine initialization is finished
120         waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
121
122         thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
123
124         inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
125         inOrder.verify(bridgeHandler, times(2)).readDecimalType(eq(new SensorId(TEST_ID)), any());
126
127         inOrder.verifyNoMoreInteractions();
128     }
129 }