]> git.basschouten.com Git - openhab-addons.git/blob
03e180a5b80f2ed8c6ab16347eb434538357d26d
[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.tplinksmarthome.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.when;
17
18 import java.lang.reflect.Field;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.junit.jupiter.api.extension.ExtendWith;
23 import org.junit.jupiter.params.ParameterizedTest;
24 import org.junit.jupiter.params.provider.MethodSource;
25 import org.mockito.Mock;
26 import org.mockito.junit.jupiter.MockitoExtension;
27 import org.openhab.binding.tplinksmarthome.internal.device.BulbDevice;
28 import org.openhab.binding.tplinksmarthome.internal.device.DimmerDevice;
29 import org.openhab.binding.tplinksmarthome.internal.device.EnergySwitchDevice;
30 import org.openhab.binding.tplinksmarthome.internal.device.PowerStripDevice;
31 import org.openhab.binding.tplinksmarthome.internal.device.RangeExtenderDevice;
32 import org.openhab.binding.tplinksmarthome.internal.device.SwitchDevice;
33 import org.openhab.binding.tplinksmarthome.internal.handler.SmartHomeHandler;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingTypeUID;
36
37 /**
38  * Test class for {@link TPLinkSmartHomeHandlerFactory}.
39  *
40  * @author Hilbrand Bouwkamp - Initial contribution
41  */
42 @ExtendWith(MockitoExtension.class)
43 public class TPLinkSmartHomeHandlerFactoryTest {
44
45     private static final String SMART_HOME_DEVICE_FIELD = "smartHomeDevice";
46
47     private final TPLinkSmartHomeHandlerFactory factory = new TPLinkSmartHomeHandlerFactory();
48
49     // @formatter:off
50     private static final List<Object[]> TESTS = Arrays.asList(new Object[][] {
51             { "hs100", SwitchDevice.class },
52             { "hs110", EnergySwitchDevice.class },
53             { "hs200", SwitchDevice.class },
54             { "hs220", DimmerDevice.class },
55             { "hs300", PowerStripDevice.class },
56             { "lb100", BulbDevice.class },
57             { "lb120", BulbDevice.class },
58             { "lb130", BulbDevice.class },
59             { "lb230", BulbDevice.class },
60             { "kl110", BulbDevice.class },
61             { "kl120", BulbDevice.class },
62             { "kl130", BulbDevice.class },
63             { "re270", RangeExtenderDevice.class },
64             { "unknown", null },
65     });
66     // @formatter:on
67
68     private @Mock Thing thing;
69
70     public static List<Object[]> data() {
71         return TESTS;
72     }
73
74     @ParameterizedTest
75     @MethodSource("data")
76     public void testCorrectClass(String name, Class<?> clazz)
77             throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
78         when(thing.getThingTypeUID()).thenReturn(new ThingTypeUID(TPLinkSmartHomeBindingConstants.BINDING_ID, name));
79         SmartHomeHandler handler = (SmartHomeHandler) factory.createHandler(thing);
80
81         if (clazz == null) {
82             assertNull(handler, name + " should not return any handler but null");
83         } else {
84             assertNotNull(handler, name + " should no return null handler");
85             Field smartHomeDeviceField = SmartHomeHandler.class.getDeclaredField(SMART_HOME_DEVICE_FIELD);
86
87             smartHomeDeviceField.setAccessible(true);
88             assertSame(clazz, smartHomeDeviceField.get(handler).getClass(),
89                     name + " should return expected device class");
90         }
91     }
92 }