]> git.basschouten.com Git - openhab-addons.git/blob
13046e212302bc29218700693590d341c623643c
[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.yeelight.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.when;
17 import static org.openhab.binding.yeelight.internal.YeelightBindingConstants.PARAMETER_DEVICE_ID;
18
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.extension.ExtendWith;
24 import org.junit.jupiter.params.ParameterizedTest;
25 import org.junit.jupiter.params.provider.MethodSource;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.mockito.junit.jupiter.MockitoSettings;
29 import org.mockito.quality.Strictness;
30 import org.openhab.binding.yeelight.internal.handler.YeelightCeilingHandler;
31 import org.openhab.binding.yeelight.internal.handler.YeelightCeilingWithAmbientHandler;
32 import org.openhab.binding.yeelight.internal.handler.YeelightCeilingWithNightHandler;
33 import org.openhab.binding.yeelight.internal.handler.YeelightColorHandler;
34 import org.openhab.binding.yeelight.internal.handler.YeelightStripeHandler;
35 import org.openhab.binding.yeelight.internal.handler.YeelightWhiteHandler;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.binding.ThingHandler;
40
41 /**
42  * Unit tests for {@link YeelightHandlerFactory}
43  *
44  * @author Viktor Koop - Initial contribution
45  * @author Nikita Pogudalov - Added YeelightCeilingWithNightHandler for Ceiling 1
46  */
47 @ExtendWith(MockitoExtension.class)
48 @MockitoSettings(strictness = Strictness.LENIENT)
49 public class YeelightHandlerFactoryTest {
50
51     private static final List<Object[]> TESTS = Arrays.asList(
52             new Object[][] { { "dolphin", YeelightWhiteHandler.class }, { "ct_bulb", YeelightWhiteHandler.class },
53                     { "wonder", YeelightColorHandler.class }, { "stripe", YeelightStripeHandler.class },
54                     { "ceiling", YeelightCeilingHandler.class }, { "ceiling3", YeelightCeilingWithNightHandler.class },
55                     { "ceiling1", YeelightCeilingWithNightHandler.class }, { "desklamp", YeelightCeilingHandler.class },
56                     { "ceiling4", YeelightCeilingWithAmbientHandler.class }, { "unknown", null } });
57
58     private final YeelightHandlerFactory factory = new YeelightHandlerFactory();
59
60     private @Mock Thing thing;
61
62     public static List<Object[]> data() {
63         return TESTS;
64     }
65
66     @BeforeEach
67     public void setUp() {
68         Configuration configuration = new Configuration();
69         configuration.put(PARAMETER_DEVICE_ID, "");
70
71         when(thing.getConfiguration()).thenReturn(configuration);
72     }
73
74     @ParameterizedTest
75     @MethodSource("data")
76     public void testCorrectClass(String name, Class<?> clazz) {
77         when(thing.getThingTypeUID()).thenReturn(new ThingTypeUID(YeelightBindingConstants.BINDING_ID, name));
78         ThingHandler handler = factory.createHandler(thing);
79
80         if (null == clazz) {
81             assertNull(handler, name + " should not return any handler but null");
82         } else {
83             assertNotNull(handler, name + " should no return null handler");
84             assertEquals(clazz, handler.getClass(), " should be correct matcher");
85
86             assertEquals(thing, handler.getThing());
87         }
88     }
89 }