2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.onewire.device;
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.times;
17 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
19 import java.util.BitSet;
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;
31 * Tests cases for {@link DS2408}.
33 * @author Jan N. Klug - Initial contribution
36 public class DS2408Test extends DeviceTestParent<DS2408> {
39 public void setupMocks() {
40 setupMocks(THING_TYPE_BASIC, DS2408.class);
42 for (int i = 0; i < 8; i++) {
43 addChannel(channelName(i), "Switch");
48 public void digitalChannel() throws OwException {
49 for (int i = 0; i < 8; i++) {
50 digitalChannelTest(OnOffType.ON, i);
51 digitalChannelTest(OnOffType.OFF, i);
55 private void digitalChannelTest(OnOffType state, int channelNo) throws OwException {
56 final DS2408 testDevice = instantiateDevice();
57 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
59 BitSet returnValue = new BitSet(8);
60 if (state == OnOffType.ON) {
61 returnValue.flip(0, 8);
64 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
65 Mockito.when(mockBridgeHandler.readBitSet(eq(testSensorId), any())).thenReturn(returnValue);
67 testDevice.configureChannels();
68 testDevice.refresh(mockBridgeHandler, true);
70 inOrder.verify(mockBridgeHandler, times(2)).readBitSet(eq(testSensorId), any());
71 inOrder.verify(mockThingHandler).postUpdate(eq(channelName(channelNo)), eq(state));
74 private String channelName(int channelNo) {
75 return CHANNEL_DIGITAL + channelNo;