]> git.basschouten.com Git - openhab-addons.git/blob
94065aa07f0436dc031f920a54a3f647a230178e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.hdpowerview;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.mock;
17
18 import java.util.ArrayList;
19 import java.util.Base64;
20 import java.util.List;
21
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.osgi.framework.Bundle;
33
34 /**
35  * Unit tests for {@link SceneChannelBuilder}.
36  *
37  * @author Jacob Laursen - Initial contribution
38  */
39 @NonNullByDefault
40 public class SceneChannelBuilderTest {
41
42     private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
43             new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneChannelBuilderTest.class.getSimpleName()),
44             HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);
45
46     private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
47             mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
48     private SceneChannelBuilder builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
49
50     @BeforeEach
51     private void setUp() {
52         builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
53     }
54
55     @Test
56     public void labelIsCorrect() {
57         List<Scene> scenes = createScenes();
58         List<Channel> channels = builder.withScenes(scenes).build();
59
60         assertEquals(1, channels.size());
61         assertEquals("TestScene", channels.get(0).getLabel());
62     }
63
64     @Test
65     public void descriptionIsCorrect() {
66         List<Scene> scenes = createScenes();
67         List<Channel> channels = builder.withScenes(scenes).build();
68
69         assertEquals(1, channels.size());
70         assertEquals("Activates the scene 'TestScene'", channels.get(0).getDescription());
71     }
72
73     @Test
74     public void groupAndIdAreCorrect() {
75         List<Scene> scenes = createScenes();
76         List<Channel> channels = builder.withScenes(scenes).build();
77
78         assertEquals(1, channels.size());
79         assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
80         assertEquals(Integer.toString(scenes.get(0).id), channels.get(0).getUID().getIdWithoutGroup());
81     }
82
83     @Test
84     public void suppliedListIsUsed() {
85         List<Scene> scenes = createScenes();
86         List<Channel> existingChannels = new ArrayList<>(0);
87         List<Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build();
88
89         assertEquals(existingChannels, channels);
90     }
91
92     @Test
93     public void emptyListWhenNoScenes() {
94         List<Channel> channels = builder.build();
95
96         assertEquals(0, channels.size());
97     }
98
99     private List<Scene> createScenes() {
100         Scene scene = new Scene();
101         scene.id = 1;
102         scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes());
103         return new ArrayList<>(List.of(scene));
104     }
105 }