]> git.basschouten.com Git - openhab-addons.git/blob
7d1a4fa0b811a9e2cad1b142834deb4d851200f0
[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.device.OwSensorType;
27 import org.openhab.binding.onewire.internal.handler.BasicThingHandler;
28 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
29 import org.openhab.binding.onewire.test.AbstractThingHandlerTest;
30 import org.openhab.core.config.core.Configuration;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.builder.ThingBuilder;
36
37 /**
38  * Tests cases for {@link BasicThingHandler}.
39  *
40  * @author Jan N. Klug - Initial contribution
41  */
42 @NonNullByDefault
43 public class BasicThingHandlerTest extends AbstractThingHandlerTest {
44     private static final String TEST_ID = "00.000000000000";
45
46     @BeforeEach
47     public void setup() throws OwException {
48         initializeBridge();
49
50         final Bridge bridge = this.bridge;
51         if (bridge == null) {
52             fail("bridge is null");
53             return;
54         }
55
56         thingConfiguration.put(CONFIG_ID, TEST_ID);
57
58         thing = ThingBuilder.create(THING_TYPE_BASIC, "testthing").withLabel("Test thing")
59                 .withConfiguration(new Configuration(thingConfiguration)).withProperties(thingProperties)
60                 .withBridge(bridge.getUID()).build();
61
62         final Thing thing = this.thing;
63         if (thing == null) {
64             fail("thing is null");
65             return;
66         }
67
68         thingHandler = new BasicThingHandler(thing, stateProvider) {
69             @Override
70             protected @Nullable Bridge getBridge() {
71                 return bridge;
72             }
73         };
74
75         initializeHandlerMocks();
76     }
77
78     @Test
79     public void testInitializationEndsWithUnknown() throws OwException {
80         final ThingHandler thingHandler = this.thingHandler;
81         if (thingHandler == null) {
82             fail("thingHandler is null");
83             return;
84         }
85
86         Mockito.doAnswer(answer -> {
87             return OwSensorType.DS2401;
88         }).when(secondBridgeHandler).getType(any());
89
90         thingHandler.initialize();
91
92         waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
93     }
94
95     @Test
96     public void testRefreshAnalog() throws OwException {
97         final OwBaseThingHandler thingHandler = this.thingHandler;
98         final InOrder inOrder = this.inOrder;
99         if (thingHandler == null || inOrder == null) {
100             fail("prerequisite is null");
101             return;
102         }
103
104         Mockito.doAnswer(answer -> {
105             return OwSensorType.DS18B20;
106         }).when(secondBridgeHandler).getType(any());
107
108         thingHandler.initialize();
109         waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
110
111         thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
112
113         inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
114         inOrder.verify(bridgeHandler, times(1)).readDecimalType(eq(new SensorId(TEST_ID)), any());
115
116         inOrder.verifyNoMoreInteractions();
117     }
118
119     @Test
120     public void testRefreshDigital() throws OwException {
121         final OwBaseThingHandler thingHandler = this.thingHandler;
122         final InOrder inOrder = this.inOrder;
123         if (thingHandler == null || inOrder == null) {
124             fail("prerequisite is null");
125             return;
126         }
127
128         Mockito.doAnswer(answer -> {
129             return OwSensorType.DS2408;
130         }).when(secondBridgeHandler).getType(any());
131
132         thingHandler.initialize();
133         waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
134
135         thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
136
137         inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
138         inOrder.verify(bridgeHandler, times(2)).readBitSet(eq(new SensorId(TEST_ID)), any());
139
140         inOrder.verifyNoMoreInteractions();
141     }
142 }