]> git.basschouten.com Git - openhab-addons.git/blob
03b24c148261adf9b34af00d74c8e688735b60cc
[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.DS2406_DS2413;
29 import org.openhab.core.library.types.OnOffType;
30
31 /**
32  * Tests cases for {@link DS2406_DS2413}.
33  *
34  * @author Jan N. Klug - Initial contribution
35  */
36 @NonNullByDefault
37 public class DS2406_DS2413Test extends DeviceTestParent<DS2406_DS2413> {
38
39     @Before
40     public void setupMocks() {
41         setupMocks(THING_TYPE_BASIC, DS2406_DS2413.class);
42
43         for (int i = 0; i < 2; i++) {
44             addChannel(channelName(i), "Switch");
45         }
46     }
47
48     @Test
49     public void digitalChannel() {
50         for (int i = 0; i < 2; i++) {
51             digitalChannelTest(OnOffType.ON, i);
52             digitalChannelTest(OnOffType.OFF, i);
53         }
54     }
55
56     private void digitalChannelTest(OnOffType state, int channelNo) {
57         final DS2406_DS2413 testDevice = instantiateDevice();
58         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
59
60         BitSet returnValue = new BitSet(8);
61         if (state == OnOffType.ON) {
62             returnValue.flip(0, 7);
63         }
64
65         try {
66             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
67             Mockito.when(mockBridgeHandler.readBitSet(eq(testSensorId), any())).thenReturn(returnValue);
68
69             testDevice.configureChannels();
70             testDevice.refresh(mockBridgeHandler, true);
71
72             inOrder.verify(mockBridgeHandler, times(2)).readBitSet(eq(testSensorId), any());
73             inOrder.verify(mockThingHandler).postUpdate(eq(channelName(channelNo)), eq(state));
74         } catch (OwException e) {
75             Assert.fail("caught unexpected OwException");
76         }
77     }
78
79     private String channelName(int channelNo) {
80         return CHANNEL_DIGITAL + channelNo;
81     }
82 }