]> git.basschouten.com Git - openhab-addons.git/blob
bd7778c2014df625326e284fb3b4b6f45399a22a
[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 -> new OwPageBuffer("EDS0065 ".getBytes())).when(secondBridgeHandler).readPages(any());
91     }
92
93     @Test
94     public void testInitializationEndsWithUnknown() {
95         final ThingHandler thingHandler = this.thingHandler;
96         if (thingHandler == null) {
97             fail("thingHandler is null");
98             return;
99         }
100
101         thingHandler.initialize();
102
103         waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
104     }
105
106     @Test
107     public void testRefresh() throws OwException {
108         final OwBaseThingHandler thingHandler = this.thingHandler;
109         final InOrder inOrder = this.inOrder;
110         if (thingHandler == null || inOrder == null) {
111             fail("prerequisite is null");
112             return;
113         }
114
115         thingHandler.initialize();
116
117         // needed to determine initialization is finished
118         waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
119
120         thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
121
122         inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
123         inOrder.verify(bridgeHandler, times(2)).readDecimalType(eq(new SensorId(TEST_ID)), any());
124
125         inOrder.verifyNoMoreInteractions();
126     }
127 }