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.core.thing.Channel;
30 import org.openhab.core.thing.ChannelGroupUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.type.AutoUpdatePolicy;
33 import org.osgi.framework.Bundle;
36 * Unit tests for {@link SceneGroupChannelBuilder}.
38 * @author Jacob Laursen - Initial contribution
41 public class SceneGroupChannelBuilderTest {
43 private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
44 new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneGroupChannelBuilderTest.class.getSimpleName()),
45 HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);
47 private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
48 mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
49 private SceneGroupChannelBuilder builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
52 private void setUp() {
53 builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
57 public void labelIsCorrect() {
58 List<SceneCollection> sceneCollections = createSceneCollections();
59 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
61 assertEquals(1, channels.size());
62 assertEquals("TestSceneCollection", channels.get(0).getLabel());
66 public void descriptionIsCorrect() {
67 List<SceneCollection> sceneCollections = createSceneCollections();
68 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
70 assertEquals(1, channels.size());
71 assertEquals("Activates the scene group 'TestSceneCollection'", channels.get(0).getDescription());
75 public void groupAndIdAreCorrect() {
76 List<SceneCollection> sceneCollections = createSceneCollections();
77 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
79 assertEquals(1, channels.size());
80 assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
81 assertEquals(Integer.toString(sceneCollections.get(0).id), channels.get(0).getUID().getIdWithoutGroup());
85 public void autoUpdatePolicyIsCorrect() {
86 List<SceneCollection> sceneCollections = createSceneCollections();
87 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
89 assertEquals(1, channels.size());
90 assertEquals(AutoUpdatePolicy.VETO, channels.get(0).getAutoUpdatePolicy());
94 public void suppliedListIsUsed() {
95 List<SceneCollection> sceneCollections = createSceneCollections();
96 List<Channel> existingChannels = new ArrayList<>(0);
97 List<Channel> channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels).build();
99 assertEquals(existingChannels, channels);
103 public void emptyListWhenNoSceneCollections() {
104 List<Channel> channels = builder.build();
106 assertEquals(0, channels.size());
109 private List<SceneCollection> createSceneCollections() {
110 SceneCollection sceneCollection = new SceneCollection();
111 sceneCollection.id = 1;
112 sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes());
113 return new ArrayList<>(List.of(sceneCollection));