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.osgi.framework.Bundle;
35 * Unit tests for {@link SceneGroupChannelBuilder}.
37 * @author Jacob Laursen - Initial contribution
40 public class SceneGroupChannelBuilderTest {
42 private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
43 new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneGroupChannelBuilderTest.class.getSimpleName()),
44 HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);
46 private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
47 mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
48 private SceneGroupChannelBuilder builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
51 private void setUp() {
52 builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
56 public void labelIsCorrect() {
57 List<SceneCollection> sceneCollections = createSceneCollections();
58 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
60 assertEquals(1, channels.size());
61 assertEquals("TestSceneCollection", channels.get(0).getLabel());
65 public void descriptionIsCorrect() {
66 List<SceneCollection> sceneCollections = createSceneCollections();
67 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
69 assertEquals(1, channels.size());
70 assertEquals("Activates the scene group 'TestSceneCollection'", channels.get(0).getDescription());
74 public void groupAndIdAreCorrect() {
75 List<SceneCollection> sceneCollections = createSceneCollections();
76 List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
78 assertEquals(1, channels.size());
79 assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
80 assertEquals(Integer.toString(sceneCollections.get(0).id), channels.get(0).getUID().getIdWithoutGroup());
84 public void suppliedListIsUsed() {
85 List<SceneCollection> sceneCollections = createSceneCollections();
86 List<Channel> existingChannels = new ArrayList<>(0);
87 List<Channel> channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels).build();
89 assertEquals(existingChannels, channels);
93 public void emptyListWhenNoSceneCollections() {
94 List<Channel> channels = builder.build();
96 assertEquals(0, channels.size());
99 private List<SceneCollection> createSceneCollections() {
100 SceneCollection sceneCollection = new SceneCollection();
101 sceneCollection.id = 1;
102 sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes());
103 return new ArrayList<>(List.of(sceneCollection));