]> git.basschouten.com Git - openhab-addons.git/blob
6090484dcd05c97c7ceffd446cea2cea2b416a60
[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.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.openhab.binding.onewire.internal.OwBindingConstants.CHANNEL_PRESENT;
18
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21
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;
46
47 /**
48  * Abtract test class for onewire devices.
49  *
50  * @author Jan N. Klug - Initial contribution
51  */
52 @ExtendWith(MockitoExtension.class)
53 @MockitoSettings(strictness = Strictness.LENIENT)
54 @NonNullByDefault
55 public abstract class DeviceTestParent<T extends AbstractOwDevice> {
56     private @Nullable Class<T> deviceTestClazz;
57
58     protected @Mock @NonNullByDefault({}) OwBaseThingHandler mockThingHandler;
59     protected @Mock @NonNullByDefault({}) OwserverBridgeHandler mockBridgeHandler;
60     protected @Mock @NonNullByDefault({}) Thing mockThing;
61
62     protected SensorId testSensorId = new SensorId("00.000000000000");
63
64     public void setupMocks(ThingTypeUID thingTypeUID, Class<T> deviceTestClazz) {
65         this.deviceTestClazz = deviceTestClazz;
66
67         Mockito.when(mockThingHandler.getThing()).thenReturn(mockThing);
68         Mockito.when(mockThing.getUID()).thenReturn(new ThingUID(thingTypeUID, "testsensor"));
69
70         addChannel(CHANNEL_PRESENT, "Switch");
71     }
72
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);
76     }
77
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);
82     }
83
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);
88     }
89
90     public T instantiateDevice() {
91         final Class<T> deviceTestClazz = this.deviceTestClazz;
92         if (deviceTestClazz == null) {
93             throw new IllegalStateException("deviceTestClazz is null");
94         }
95         try {
96             Constructor<T> constructor = deviceTestClazz.getConstructor(SensorId.class, OwBaseThingHandler.class);
97             T testDevice = constructor.newInstance(testSensorId, mockThingHandler);
98             assertNotNull(testDevice);
99             return testDevice;
100         } catch (NoSuchMethodException | InstantiationException | IllegalAccessException
101                 | InvocationTargetException e) {
102             throw new IllegalStateException(e);
103         }
104     }
105
106     public T instantiateDevice(OwSensorType sensorType) {
107         final Class<T> deviceTestClazz = this.deviceTestClazz;
108         if (deviceTestClazz == null) {
109             throw new IllegalStateException("deviceTestClazz is null");
110         }
111         try {
112             Constructor<T> constructor = deviceTestClazz.getConstructor(SensorId.class, OwSensorType.class,
113                     OwBaseThingHandler.class);
114             T testDevice = constructor.newInstance(testSensorId, sensorType, mockThingHandler);
115             assertNotNull(testDevice);
116             return testDevice;
117         } catch (NoSuchMethodException | InstantiationException | IllegalAccessException
118                 | InvocationTargetException e) {
119             throw new IllegalStateException(e);
120         }
121     }
122
123     public void presenceTest(OnOffType state) throws OwException {
124         final T testDevice = instantiateDevice();
125         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
126
127         Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(state);
128         testDevice.checkPresence(mockBridgeHandler);
129
130         inOrder.verify(mockThingHandler).updatePresenceStatus(eq(state));
131     }
132 }