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.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.openhab.binding.onewire.internal.OwBindingConstants.CHANNEL_PRESENT;
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.InOrder;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.mockito.junit.jupiter.MockitoSettings;
30 import org.mockito.quality.Strictness;
31 import org.openhab.binding.onewire.internal.OwException;
32 import org.openhab.binding.onewire.internal.SensorId;
33 import org.openhab.binding.onewire.internal.device.AbstractOwDevice;
34 import org.openhab.binding.onewire.internal.device.OwSensorType;
35 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
36 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
37 import org.openhab.core.config.core.Configuration;
38 import org.openhab.core.library.types.OnOffType;
39 import org.openhab.core.thing.Channel;
40 import org.openhab.core.thing.ChannelUID;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingTypeUID;
43 import org.openhab.core.thing.ThingUID;
44 import org.openhab.core.thing.binding.builder.ChannelBuilder;
45 import org.openhab.core.thing.type.ChannelTypeUID;
48 * Abtract test class for onewire devices.
50 * @author Jan N. Klug - Initial contribution
52 @ExtendWith(MockitoExtension.class)
53 @MockitoSettings(strictness = Strictness.LENIENT)
55 public abstract class DeviceTestParent<T extends AbstractOwDevice> {
56 private @Nullable Class<T> deviceTestClazz;
58 protected @Mock @NonNullByDefault({}) OwBaseThingHandler mockThingHandler;
59 protected @Mock @NonNullByDefault({}) OwserverBridgeHandler mockBridgeHandler;
60 protected @Mock @NonNullByDefault({}) Thing mockThing;
62 protected SensorId testSensorId = new SensorId("00.000000000000");
64 public void setupMocks(ThingTypeUID thingTypeUID, Class<T> deviceTestClazz) {
65 this.deviceTestClazz = deviceTestClazz;
67 Mockito.when(mockThingHandler.getThing()).thenReturn(mockThing);
68 Mockito.when(mockThing.getUID()).thenReturn(new ThingUID(thingTypeUID, "testsensor"));
70 addChannel(CHANNEL_PRESENT, "Switch");
73 public void addChannel(String channelId, String itemType) {
74 Channel channel = ChannelBuilder.create(new ChannelUID(mockThing.getUID(), channelId), itemType).build();
75 Mockito.when(mockThing.getChannel(channelId)).thenReturn(channel);
78 public void addChannel(String channelId, String itemType, Configuration channelConfiguration) {
79 Channel channel = ChannelBuilder.create(new ChannelUID(mockThing.getUID(), channelId), itemType)
80 .withConfiguration(channelConfiguration).build();
81 Mockito.when(mockThing.getChannel(channelId)).thenReturn(channel);
84 public void addChannel(String channelId, String itemType, ChannelTypeUID channelTypeUID) {
85 Channel channel = ChannelBuilder.create(new ChannelUID(mockThing.getUID(), channelId), itemType)
86 .withType(channelTypeUID).build();
87 Mockito.when(mockThing.getChannel(channelId)).thenReturn(channel);
90 public T instantiateDevice() {
91 final Class<T> deviceTestClazz = this.deviceTestClazz;
92 if (deviceTestClazz == null) {
93 throw new IllegalStateException("deviceTestClazz is null");
96 Constructor<T> constructor = deviceTestClazz.getConstructor(SensorId.class, OwBaseThingHandler.class);
97 T testDevice = constructor.newInstance(testSensorId, mockThingHandler);
98 assertNotNull(testDevice);
100 } catch (NoSuchMethodException | InstantiationException | IllegalAccessException
101 | InvocationTargetException e) {
102 throw new IllegalStateException(e);
106 public T instantiateDevice(OwSensorType sensorType) {
107 final Class<T> deviceTestClazz = this.deviceTestClazz;
108 if (deviceTestClazz == null) {
109 throw new IllegalStateException("deviceTestClazz is null");
112 Constructor<T> constructor = deviceTestClazz.getConstructor(SensorId.class, OwSensorType.class,
113 OwBaseThingHandler.class);
114 T testDevice = constructor.newInstance(testSensorId, sensorType, mockThingHandler);
115 assertNotNull(testDevice);
117 } catch (NoSuchMethodException | InstantiationException | IllegalAccessException
118 | InvocationTargetException e) {
119 throw new IllegalStateException(e);
123 public void presenceTest(OnOffType state) throws OwException {
124 final T testDevice = instantiateDevice();
125 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
127 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(state);
128 testDevice.checkPresence(mockBridgeHandler);
130 inOrder.verify(mockThingHandler).updatePresenceStatus(eq(state));