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