]> git.basschouten.com Git - openhab-addons.git/blob
771492b41e21923836ba30b3b7f5ece8c61f7a19
[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_MS_TX;
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.BasicMultisensorThingHandler;
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 BasicMultisensorThingHandler}.
43  *
44  * @author Jan N. Klug - Initial contribution
45  */
46 @NonNullByDefault
47 public class MultisensorThingHandlerTest 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_MS_TX, "testthing").withLabel("Test thing").withChannels(channels)
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 BasicMultisensorThingHandler(thing, stateProvider) {
75             @Override
76             protected @Nullable Bridge getBridge() {
77                 return bridge;
78             }
79         };
80
81         initializeHandlerMocks();
82
83         Mockito.doAnswer(answer -> {
84             return OwSensorType.DS2438;
85         }).when(secondBridgeHandler).getType(any());
86
87         Mockito.doAnswer(answer -> {
88             OwPageBuffer pageBuffer = new OwPageBuffer(8);
89             pageBuffer.setByte(3, 0, (byte) 0x19);
90             return pageBuffer;
91         }).when(secondBridgeHandler).readPages(any());
92     }
93
94     @Test
95     public void testInitializationEndsWithUnknown() {
96         final ThingHandler thingHandler = this.thingHandler;
97         if (thingHandler == null) {
98             Assert.fail("thingHandler is null");
99             return;
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             Assert.fail("prerequisite is null");
112             return;
113         }
114         thingHandler.initialize();
115
116         // needed to determine initialization is finished
117         waitForAssert(() -> assertEquals(ThingStatus.UNKNOWN, thingHandler.getThing().getStatusInfo().getStatus()));
118
119         thingHandler.refresh(bridgeHandler, System.currentTimeMillis());
120
121         inOrder.verify(bridgeHandler, times(1)).checkPresence(new SensorId(TEST_ID));
122         inOrder.verify(bridgeHandler, times(3)).readDecimalType(eq(new SensorId(TEST_ID)), any());
123
124         inOrder.verifyNoMoreInteractions();
125     }
126 }