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.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 SceneChannelBuilder}.
38 * @author Jacob Laursen - Initial contribution
41 public class SceneChannelBuilderTest {
43 private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
44 new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneChannelBuilderTest.class.getSimpleName()),
45 HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);
47 private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
48 mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
49 private SceneChannelBuilder builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
52 private void setUp() {
53 builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
57 public void labelIsCorrect() {
58 List<Scene> scenes = createScenes();
59 List<Channel> channels = builder.withScenes(scenes).build();
61 assertEquals(1, channels.size());
62 assertEquals("TestScene", channels.get(0).getLabel());
66 public void descriptionIsCorrect() {
67 List<Scene> scenes = createScenes();
68 List<Channel> channels = builder.withScenes(scenes).build();
70 assertEquals(1, channels.size());
71 assertEquals("Activates the scene 'TestScene'", channels.get(0).getDescription());
75 public void groupAndIdAreCorrect() {
76 List<Scene> scenes = createScenes();
77 List<Channel> channels = builder.withScenes(scenes).build();
79 assertEquals(1, channels.size());
80 assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
81 assertEquals(Integer.toString(scenes.get(0).id), channels.get(0).getUID().getIdWithoutGroup());
85 public void autoUpdatePolicyIsCorrect() {
86 List<Scene> scenes = createScenes();
87 List<Channel> channels = builder.withScenes(scenes).build();
89 assertEquals(1, channels.size());
90 assertEquals(AutoUpdatePolicy.VETO, channels.get(0).getAutoUpdatePolicy());
94 public void suppliedListIsUsed() {
95 List<Scene> scenes = createScenes();
96 List<Channel> existingChannels = new ArrayList<>(0);
97 List<Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build();
99 assertEquals(existingChannels, channels);
103 public void emptyListWhenNoScenes() {
104 List<Channel> channels = builder.build();
106 assertEquals(0, channels.size());
109 private List<Scene> createScenes() {
110 Scene scene = new Scene();
112 scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes());
113 return new ArrayList<>(List.of(scene));