2 * Copyright (c) 2010-2023 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.internal;
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.builders.SceneChannelBuilder;
26 import org.openhab.binding.hdpowerview.internal.dto.Scene;
27 import org.openhab.binding.hdpowerview.internal.providers.MockedLocaleProvider;
28 import org.openhab.binding.hdpowerview.internal.providers.MockedTranslationProvider;
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 TRANSLATION_PROVIDER = new HDPowerViewTranslationProvider(
48 mock(Bundle.class), new MockedTranslationProvider(), new MockedLocaleProvider());
50 private SceneChannelBuilder builder = SceneChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
54 builder = SceneChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
58 public void labelIsCorrect() {
59 List<Scene> scenes = createScenes();
60 List<Channel> channels = builder.withScenes(scenes).build();
62 assertEquals(1, channels.size());
63 assertEquals("TestScene", channels.get(0).getLabel());
67 public void descriptionIsCorrect() {
68 List<Scene> scenes = createScenes();
69 List<Channel> channels = builder.withScenes(scenes).build();
71 assertEquals(1, channels.size());
72 assertEquals("Activates the scene 'TestScene'", channels.get(0).getDescription());
76 public void groupAndIdAreCorrect() {
77 List<Scene> scenes = createScenes();
78 List<Channel> channels = builder.withScenes(scenes).build();
80 assertEquals(1, channels.size());
81 assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
82 assertEquals(Integer.toString(scenes.get(0).id), channels.get(0).getUID().getIdWithoutGroup());
86 public void autoUpdatePolicyIsCorrect() {
87 List<Scene> scenes = createScenes();
88 List<Channel> channels = builder.withScenes(scenes).build();
90 assertEquals(1, channels.size());
91 assertEquals(AutoUpdatePolicy.VETO, channels.get(0).getAutoUpdatePolicy());
95 public void suppliedListIsUsed() {
96 List<Scene> scenes = createScenes();
97 List<Channel> existingChannels = new ArrayList<>(0);
98 List<Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build();
100 assertEquals(existingChannels, channels);
104 public void emptyListWhenNoScenes() {
105 List<Channel> channels = builder.build();
107 assertEquals(0, channels.size());
110 private List<Scene> createScenes() {
111 Scene scene = new Scene();
113 scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes());
114 return new ArrayList<>(List.of(scene));