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