]> git.basschouten.com Git - openhab-addons.git/blob
e682c5c2411f1552a1db26b02b7e33c270e3add4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.mockito.ArgumentMatchers.eq;
16 import static org.mockito.MockitoAnnotations.initMocks;
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.Assert;
25 import org.mockito.InOrder;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.openhab.binding.onewire.internal.OwException;
29 import org.openhab.binding.onewire.internal.SensorId;
30 import org.openhab.binding.onewire.internal.device.AbstractOwDevice;
31 import org.openhab.binding.onewire.internal.device.OwSensorType;
32 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
33 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
34 import org.openhab.core.config.core.Configuration;
35 import org.openhab.core.library.types.OnOffType;
36 import org.openhab.core.thing.*;
37 import org.openhab.core.thing.binding.builder.ChannelBuilder;
38 import org.openhab.core.thing.type.ChannelTypeUID;
39
40 /**
41  * Abtract test class for onewire devices.
42  *
43  * @author Jan N. Klug - Initial contribution
44  */
45 @NonNullByDefault
46 public abstract class DeviceTestParent<T extends AbstractOwDevice> {
47     private @Nullable Class<T> deviceTestClazz;
48
49     @Mock
50     @NonNullByDefault({})
51     protected OwBaseThingHandler mockThingHandler;
52
53     @Mock
54     @NonNullByDefault({})
55     protected OwserverBridgeHandler mockBridgeHandler;
56
57     @Mock
58     @NonNullByDefault({})
59     protected Thing mockThing;
60
61     protected SensorId testSensorId = new SensorId("00.000000000000");
62
63     public void setupMocks(ThingTypeUID thingTypeUID, Class<T> deviceTestClazz) {
64         this.deviceTestClazz = deviceTestClazz;
65         initMocks(this);
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             Assert.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             Assert.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) {
124         final T testDevice = instantiateDevice();
125         final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
126         try {
127             Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(state);
128             testDevice.checkPresence(mockBridgeHandler);
129
130             inOrder.verify(mockThingHandler).updatePresenceStatus(eq(state));
131         } catch (OwException e) {
132             Assert.fail("caught unexpected OwException");
133         }
134     }
135 }