]> git.basschouten.com Git - openhab-addons.git/blob
49467aded761beb8a428c5a4e711063400e5d426
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.InOrder;
25 import org.mockito.Mockito;
26 import org.openhab.binding.onewire.internal.OwException;
27 import org.openhab.binding.onewire.internal.device.DS2405;
28 import org.openhab.core.library.types.OnOffType;
29
30 /**
31  * Tests cases for {@link DS2405}.
32  *
33  * @author Jan N. Klug - Initial contribution
34  */
35 @NonNullByDefault
36 public class DS2405Test extends DeviceTestParent<DS2405> {
37
38     @BeforeEach
39     public void setupMocks() {
40         setupMocks(THING_TYPE_BASIC, DS2405.class);
41
42         addChannel(channelName(0), "Switch");
43     }
44
45     @Test
46     public void digitalChannel() throws OwException {
47         digitalChannelTest(OnOffType.ON, 0);
48         digitalChannelTest(OnOffType.OFF, 0);
49     }
50
51     private void digitalChannelTest(OnOffType state, int channelNo) throws OwException {
52         final DS2405 testDevice = instantiateDevice();
53         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
54
55         BitSet returnValue = new BitSet(8);
56         if (state == OnOffType.ON) {
57             returnValue.flip(0, 7);
58         }
59
60         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
61         Mockito.when(mockBridgeHandler.readBitSet(eq(testSensorId), any())).thenReturn(returnValue);
62
63         testDevice.configureChannels();
64         testDevice.refresh(mockBridgeHandler, true);
65
66         inOrder.verify(mockBridgeHandler, times(2)).readBitSet(eq(testSensorId), any());
67         inOrder.verify(mockThingHandler).postUpdate(eq(channelName(channelNo)), eq(state));
68     }
69
70     private String channelName(int channelNo) {
71         return CHANNEL_DIGITAL + channelNo;
72     }
73 }