]> git.basschouten.com Git - openhab-addons.git/blob
f5566e4c5b58bf522cd26efe88d57c6b7b2fcfa6
[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.openhab.core.thing.type.AutoUpdatePolicy;
33 import org.osgi.framework.Bundle;
34
35 /**
36  * Unit tests for {@link SceneGroupChannelBuilder}.
37  *
38  * @author Jacob Laursen - Initial contribution
39  */
40 @NonNullByDefault
41 public class SceneGroupChannelBuilderTest {
42
43     private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
44             new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneGroupChannelBuilderTest.class.getSimpleName()),
45             HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);
46
47     private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
48             mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
49     private SceneGroupChannelBuilder builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
50
51     @BeforeEach
52     private void setUp() {
53         builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
54     }
55
56     @Test
57     public void labelIsCorrect() {
58         List<SceneCollection> sceneCollections = createSceneCollections();
59         List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
60
61         assertEquals(1, channels.size());
62         assertEquals("TestSceneCollection", channels.get(0).getLabel());
63     }
64
65     @Test
66     public void descriptionIsCorrect() {
67         List<SceneCollection> sceneCollections = createSceneCollections();
68         List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
69
70         assertEquals(1, channels.size());
71         assertEquals("Activates the scene group 'TestSceneCollection'", channels.get(0).getDescription());
72     }
73
74     @Test
75     public void groupAndIdAreCorrect() {
76         List<SceneCollection> sceneCollections = createSceneCollections();
77         List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
78
79         assertEquals(1, channels.size());
80         assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
81         assertEquals(Integer.toString(sceneCollections.get(0).id), channels.get(0).getUID().getIdWithoutGroup());
82     }
83
84     @Test
85     public void autoUpdatePolicyIsCorrect() {
86         List<SceneCollection> sceneCollections = createSceneCollections();
87         List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
88
89         assertEquals(1, channels.size());
90         assertEquals(AutoUpdatePolicy.VETO, channels.get(0).getAutoUpdatePolicy());
91     }
92
93     @Test
94     public void suppliedListIsUsed() {
95         List<SceneCollection> sceneCollections = createSceneCollections();
96         List<Channel> existingChannels = new ArrayList<>(0);
97         List<Channel> channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels).build();
98
99         assertEquals(existingChannels, channels);
100     }
101
102     @Test
103     public void emptyListWhenNoSceneCollections() {
104         List<Channel> channels = builder.build();
105
106         assertEquals(0, channels.size());
107     }
108
109     private List<SceneCollection> createSceneCollections() {
110         SceneCollection sceneCollection = new SceneCollection();
111         sceneCollection.id = 1;
112         sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes());
113         return new ArrayList<>(List.of(sceneCollection));
114     }
115 }