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