2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.knx.internal.channel;
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.*;
21 import java.util.Objects;
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;
33 * @author Holger Friedrich - Initial Contribution
37 class KNXChannelFactoryTest {
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)
45 public void testNullChannelUidFails() {
46 Channel channel = Objects.requireNonNull(mock(Channel.class));
48 assertThrows(IllegalArgumentException.class, () -> {
49 KNXChannelFactory.createKnxChannel(channel);
54 public void testInvalidChannelUidFails() {
55 Channel channel = Objects.requireNonNull(mock(Channel.class));
56 when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("a:b:c"));
58 assertThrows(IllegalArgumentException.class, () -> {
59 KNXChannelFactory.createKnxChannel(channel);
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");
76 assertNotNull(KNXChannelFactory.createKnxChannel(channel));