2 * Copyright (c) 2010-2022 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.hdpowerview;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.mock;
18 import java.util.ArrayList;
19 import java.util.Base64;
20 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
26 import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider;
27 import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection;
28 import org.openhab.binding.hdpowerview.internal.builders.SceneGroupChannelBuilder;
29 import org.openhab.binding.hdpowerview.providers.MockedLocaleProvider;
30 import org.openhab.binding.hdpowerview.providers.MockedTranslationProvider;
31 import org.openhab.core.thing.Channel;
32 import org.openhab.core.thing.ChannelGroupUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.type.AutoUpdatePolicy;
35 import org.osgi.framework.Bundle;
38 * Unit tests for {@link SceneGroupChannelBuilder}.
40 * @author Jacob Laursen - Initial contribution
43 public class SceneGroupChannelBuilderTest {
45 private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
46 new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneGroupChannelBuilderTest.class.getSimpleName()),
47 HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);
49 private static final HDPowerViewTranslationProvider TRANSLATION_PROVIDER = new HDPowerViewTranslationProvider(
50 mock(Bundle.class), new MockedTranslationProvider(), new MockedLocaleProvider());
52 private SceneGroupChannelBuilder builder = SceneGroupChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
55 private void setUp() {
56 builder = SceneGroupChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
60 public void labelIsCorrect() {
61 List<SceneCollection> sceneCollections = createSceneCollections();
62 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
64 assertEquals(1, channels.size());
65 assertEquals("TestSceneCollection", channels.get(0).getLabel());
69 public void descriptionIsCorrect() {
70 List<SceneCollection> sceneCollections = createSceneCollections();
71 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
73 assertEquals(1, channels.size());
74 assertEquals("Activates the scene group 'TestSceneCollection'", channels.get(0).getDescription());
78 public void groupAndIdAreCorrect() {
79 List<SceneCollection> sceneCollections = createSceneCollections();
80 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
82 assertEquals(1, channels.size());
83 assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
84 assertEquals(Integer.toString(sceneCollections.get(0).id), channels.get(0).getUID().getIdWithoutGroup());
88 public void autoUpdatePolicyIsCorrect() {
89 List<SceneCollection> sceneCollections = createSceneCollections();
90 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
92 assertEquals(1, channels.size());
93 assertEquals(AutoUpdatePolicy.VETO, channels.get(0).getAutoUpdatePolicy());
97 public void suppliedListIsUsed() {
98 List<SceneCollection> sceneCollections = createSceneCollections();
99 List<Channel> existingChannels = new ArrayList<>(0);
100 List<Channel> channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels).build();
102 assertEquals(existingChannels, channels);
106 public void emptyListWhenNoSceneCollections() {
107 List<Channel> channels = builder.build();
109 assertEquals(0, channels.size());
112 private List<SceneCollection> createSceneCollections() {
113 SceneCollection sceneCollection = new SceneCollection();
114 sceneCollection.id = 1;
115 sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes());
116 return new ArrayList<>(List.of(sceneCollection));