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.DS2405;
29 import org.openhab.core.library.types.OnOffType;
32 * Tests cases for {@link DS2405}.
34 * @author Jan N. Klug - Initial contribution
37 public class DS2405Test extends DeviceTestParent<DS2405> {
40 public void setupMocks() {
41 setupMocks(THING_TYPE_BASIC, DS2405.class);
43 addChannel(channelName(0), "Switch");
47 public void digitalChannel() {
48 digitalChannelTest(OnOffType.ON, 0);
49 digitalChannelTest(OnOffType.OFF, 0);
52 private void digitalChannelTest(OnOffType state, int channelNo) {
53 final DS2405 testDevice = instantiateDevice();
54 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
56 BitSet returnValue = new BitSet(8);
57 if (state == OnOffType.ON) {
58 returnValue.flip(0, 7);
62 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
63 Mockito.when(mockBridgeHandler.readBitSet(eq(testSensorId), any())).thenReturn(returnValue);
65 testDevice.configureChannels();
66 testDevice.refresh(mockBridgeHandler, true);
68 inOrder.verify(mockBridgeHandler, times(2)).readBitSet(eq(testSensorId), any());
69 inOrder.verify(mockThingHandler).postUpdate(eq(channelName(channelNo)), eq(state));
70 } catch (OwException e) {
71 Assert.fail("caught unexpected OwException");
75 private String channelName(int channelNo) {
76 return CHANNEL_DIGITAL + channelNo;