]> git.basschouten.com Git - openhab-addons.git/blob
78aa0586de09b15a60b929e0d2f5f5cf6294bcbb
[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.knx.internal.channel;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18 import static org.openhab.binding.knx.internal.KNXBindingConstants.*;
19
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.params.ParameterizedTest;
25 import org.junit.jupiter.params.provider.ValueSource;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29
30 /**
31  *
32  * @author Holger Friedrich - Initial Contribution
33  *
34  */
35 @NonNullByDefault
36 class KNXChannelFactoryTest {
37
38     /**
39      * This test checks if channels with invalid channelTypeUID lead to the intended exception.
40      * Side effect is testing if KNXChannelFactory can be instantiated (this is not the case e.g. when types with
41      * duplicate channel types are created)
42      */
43     @Test
44     public void testNullChannelUidFails() {
45         Channel channel = mock(Channel.class);
46
47         assertThrows(IllegalArgumentException.class, () -> {
48             KNXChannelFactory.createKnxChannel(channel);
49         });
50     }
51
52     @Test
53     public void testInvalidChannelUidFails() {
54         Channel channel = mock(Channel.class);
55         when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("a:b:c"));
56
57         assertThrows(IllegalArgumentException.class, () -> {
58             KNXChannelFactory.createKnxChannel(channel);
59         });
60     }
61
62     @ParameterizedTest
63     @ValueSource(strings = { CHANNEL_COLOR, CHANNEL_COLOR_CONTROL, CHANNEL_CONTACT, CHANNEL_CONTACT_CONTROL,
64             CHANNEL_DATETIME, CHANNEL_DATETIME_CONTROL, CHANNEL_DIMMER, CHANNEL_DIMMER_CONTROL, CHANNEL_NUMBER,
65             CHANNEL_NUMBER_CONTROL, CHANNEL_ROLLERSHUTTER, CHANNEL_ROLLERSHUTTER_CONTROL, CHANNEL_STRING,
66             CHANNEL_STRING_CONTROL, CHANNEL_SWITCH, CHANNEL_SWITCH_CONTROL })
67     public void testSuccess(String channeltype) {
68         Channel channel = mock(Channel.class);
69         Configuration configuration = new Configuration(
70                 Map.of("key1", "5.001:<1/2/3+4/5/6+1/5/6", "key2", "1.001:7/1/9+1/1/2"));
71         when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("knx:" + channeltype));
72         when(channel.getConfiguration()).thenReturn(configuration);
73         when(channel.getAcceptedItemType()).thenReturn("none");
74
75         assertNotNull(KNXChannelFactory.createKnxChannel(channel));
76     }
77 }