2 * Copyright (c) 2010-2020 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.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.DS2408;
29 import org.openhab.core.library.types.OnOffType;
32 * Tests cases for {@link DS2408}.
34 * @author Jan N. Klug - Initial contribution
37 public class DS2408Test extends DeviceTestParent<DS2408> {
40 public void setupMocks() {
41 setupMocks(THING_TYPE_BASIC, DS2408.class);
43 for (int i = 0; i < 8; i++) {
44 addChannel(channelName(i), "Switch");
49 public void digitalChannel() {
50 for (int i = 0; i < 8; i++) {
51 digitalChannelTest(OnOffType.ON, i);
52 digitalChannelTest(OnOffType.OFF, i);
56 private void digitalChannelTest(OnOffType state, int channelNo) {
57 final DS2408 testDevice = instantiateDevice();
58 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
60 BitSet returnValue = new BitSet(8);
61 if (state == OnOffType.ON) {
62 returnValue.flip(0, 8);
66 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
67 Mockito.when(mockBridgeHandler.readBitSet(eq(testSensorId), any())).thenReturn(returnValue);
69 testDevice.configureChannels();
70 testDevice.refresh(mockBridgeHandler, true);
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");
79 private String channelName(int channelNo) {
80 return CHANNEL_DIGITAL + channelNo;