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