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