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.Scenes.Scene;
28 import org.openhab.binding.hdpowerview.internal.builders.SceneChannelBuilder;
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 SceneChannelBuilder}.
40 * @author Jacob Laursen - Initial contribution
43 public class SceneChannelBuilderTest {
45 private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
46 new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneChannelBuilderTest.class.getSimpleName()),
47 HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);
49 private static final HDPowerViewTranslationProvider TRANSLATION_PROVIDER = new HDPowerViewTranslationProvider(
50 mock(Bundle.class), new MockedTranslationProvider(), new MockedLocaleProvider());
52 private SceneChannelBuilder builder = SceneChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
55 private void setUp() {
56 builder = SceneChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
60 public void labelIsCorrect() {
61 List<Scene> scenes = createScenes();
62 List<Channel> channels = builder.withScenes(scenes).build();
64 assertEquals(1, channels.size());
65 assertEquals("TestScene", channels.get(0).getLabel());
69 public void descriptionIsCorrect() {
70 List<Scene> scenes = createScenes();
71 List<Channel> channels = builder.withScenes(scenes).build();
73 assertEquals(1, channels.size());
74 assertEquals("Activates the scene 'TestScene'", channels.get(0).getDescription());
78 public void groupAndIdAreCorrect() {
79 List<Scene> scenes = createScenes();
80 List<Channel> channels = builder.withScenes(scenes).build();
82 assertEquals(1, channels.size());
83 assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
84 assertEquals(Integer.toString(scenes.get(0).id), channels.get(0).getUID().getIdWithoutGroup());
88 public void autoUpdatePolicyIsCorrect() {
89 List<Scene> scenes = createScenes();
90 List<Channel> channels = builder.withScenes(scenes).build();
92 assertEquals(1, channels.size());
93 assertEquals(AutoUpdatePolicy.VETO, channels.get(0).getAutoUpdatePolicy());
97 public void suppliedListIsUsed() {
98 List<Scene> scenes = createScenes();
99 List<Channel> existingChannels = new ArrayList<>(0);
100 List<Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build();
102 assertEquals(existingChannels, channels);
106 public void emptyListWhenNoScenes() {
107 List<Channel> channels = builder.build();
109 assertEquals(0, channels.size());
112 private List<Scene> createScenes() {
113 Scene scene = new Scene();
115 scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes());
116 return new ArrayList<>(List.of(scene));