]> git.basschouten.com Git - openhab-addons.git/blob
16286650d59fe293efc5e4e1b37a4a87beb1c08a
[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.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;
33
34 /**
35  * Unit tests for {@link SceneGroupChannelBuilder}.
36  *
37  * @author Jacob Laursen - Initial contribution
38  */
39 @NonNullByDefault
40 public class SceneGroupChannelBuilderTest {
41
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);
45
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);
49
50     @BeforeEach
51     private void setUp() {
52         builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
53     }
54
55     @Test
56     public void labelIsCorrect() {
57         List<SceneCollection> sceneCollections = createSceneCollections();
58         List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
59
60         assertEquals(1, channels.size());
61         assertEquals("TestSceneCollection", channels.get(0).getLabel());
62     }
63
64     @Test
65     public void descriptionIsCorrect() {
66         List<SceneCollection> sceneCollections = createSceneCollections();
67         List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
68
69         assertEquals(1, channels.size());
70         assertEquals("Activates the scene group 'TestSceneCollection'", channels.get(0).getDescription());
71     }
72
73     @Test
74     public void groupAndIdAreCorrect() {
75         List<SceneCollection> sceneCollections = createSceneCollections();
76         List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
77
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());
81     }
82
83     @Test
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();
88
89         assertEquals(existingChannels, channels);
90     }
91
92     @Test
93     public void emptyListWhenNoSceneCollections() {
94         List<Channel> channels = builder.build();
95
96         assertEquals(0, channels.size());
97     }
98
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));
104     }
105 }