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