]> git.basschouten.com Git - openhab-addons.git/blob
09b194859c6b7f1539ff7db659237c3782e55620
[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.device;
14
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.times;
17 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
18
19 import java.util.BitSet;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.InOrder;
26 import org.mockito.Mockito;
27 import org.openhab.binding.onewire.internal.OwException;
28 import org.openhab.binding.onewire.internal.device.DS2405;
29 import org.openhab.core.library.types.OnOffType;
30
31 /**
32  * Tests cases for {@link DS2405}.
33  *
34  * @author Jan N. Klug - Initial contribution
35  */
36 @NonNullByDefault
37 public class DS2405Test extends DeviceTestParent<DS2405> {
38
39     @Before
40     public void setupMocks() {
41         setupMocks(THING_TYPE_BASIC, DS2405.class);
42
43         addChannel(channelName(0), "Switch");
44     }
45
46     @Test
47     public void digitalChannel() {
48         digitalChannelTest(OnOffType.ON, 0);
49         digitalChannelTest(OnOffType.OFF, 0);
50     }
51
52     private void digitalChannelTest(OnOffType state, int channelNo) {
53         final DS2405 testDevice = instantiateDevice();
54         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
55
56         BitSet returnValue = new BitSet(8);
57         if (state == OnOffType.ON) {
58             returnValue.flip(0, 7);
59         }
60
61         try {
62             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
63             Mockito.when(mockBridgeHandler.readBitSet(eq(testSensorId), any())).thenReturn(returnValue);
64
65             testDevice.configureChannels();
66             testDevice.refresh(mockBridgeHandler, true);
67
68             inOrder.verify(mockBridgeHandler, times(2)).readBitSet(eq(testSensorId), any());
69             inOrder.verify(mockThingHandler).postUpdate(eq(channelName(channelNo)), eq(state));
70         } catch (OwException e) {
71             Assert.fail("caught unexpected OwException");
72         }
73     }
74
75     private String channelName(int channelNo) {
76         return CHANNEL_DIGITAL + channelNo;
77     }
78 }