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