From a8472e7747565f0905f4f3aa3d9b307979c87eae Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 28 Dec 2021 13:22:32 +0100 Subject: [PATCH] Add test coverage for SceneChannelBuilder and SceneGroupChannelBuilder. Signed-off-by: Jacob Laursen --- .../AutomationChannelBuilderTest.java | 2 +- .../hdpowerview/SceneChannelBuilderTest.java | 106 +++++++++++++++++ .../SceneGroupChannelBuilderTest.java | 107 ++++++++++++++++++ .../TranslationProviderForTests.java | 2 + 4 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java create mode 100644 bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java index abc5d65c36d77..0aa63e7aecf39 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/AutomationChannelBuilderTest.java @@ -216,7 +216,7 @@ public void suppliedListIsUsed() { } @Test - public void scheduledEventGroupAndIdAreCorrect() { + public void groupAndIdAreCorrect() { ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE); scheduledEvent.id = 42; List scheduledEvents = new ArrayList<>(List.of(scheduledEvent)); diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java new file mode 100644 index 0000000000000..829a4f7031d59 --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneChannelBuilderTest.java @@ -0,0 +1,106 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; +import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene; +import org.openhab.binding.hdpowerview.internal.builders.SceneChannelBuilder; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelGroupUID; +import org.openhab.core.thing.ThingUID; +import org.osgi.framework.Bundle; + +/** + * Unit tests for {@link SceneChannelBuilder}. + * + * @author Jacob Laursen - Initial contribution + */ +public class SceneChannelBuilderTest { + + private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID( + new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneChannelBuilderTest.class.getSimpleName()), + HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE); + + private HDPowerViewTranslationProvider translationProvider; + private SceneChannelBuilder builder; + + @BeforeEach + private void setUp() { + translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(), + new LocaleProviderForTests()); + builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); + } + + @Test + public void labelIsCorrect() { + List scenes = createScenes(); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).build(); + + assertEquals(1, channels.size()); + assertEquals("TestScene", channels.get(0).getLabel()); + } + + @Test + public void descriptionIsCorrect() { + List scenes = createScenes(); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).build(); + + assertEquals(1, channels.size()); + assertEquals("Activates the scene 'TestScene'", channels.get(0).getDescription()); + } + + @Test + public void groupAndIdAreCorrect() { + List scenes = createScenes(); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).build(); + + assertEquals(1, channels.size()); + assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId()); + assertEquals(Integer.toString(scenes.get(0).id), channels.get(0).getUID().getIdWithoutGroup()); + } + + @Test + public void suppliedListIsUsed() { + List scenes = createScenes(); + List existingChannels = new ArrayList<>(0); + + @NonNull + List<@NonNull Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build(); + + assertEquals(existingChannels, channels); + } + + private List createScenes() { + Scene scene = new Scene(); + scene.id = 1; + scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes()); + return new ArrayList<>(List.of(scene)); + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java new file mode 100644 index 0000000000000..669454d2b4807 --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/SceneGroupChannelBuilderTest.java @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hdpowerview; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants; +import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider; +import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection; +import org.openhab.binding.hdpowerview.internal.builders.SceneGroupChannelBuilder; +import org.openhab.core.thing.Channel; +import org.openhab.core.thing.ChannelGroupUID; +import org.openhab.core.thing.ThingUID; +import org.osgi.framework.Bundle; + +/** + * Unit tests for {@link SceneGroupChannelBuilder}. + * + * @author Jacob Laursen - Initial contribution + */ +public class SceneGroupChannelBuilderTest { + + private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID( + new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneGroupChannelBuilderTest.class.getSimpleName()), + HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE); + + private HDPowerViewTranslationProvider translationProvider; + private SceneGroupChannelBuilder builder; + + @BeforeEach + private void setUp() { + translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(), + new LocaleProviderForTests()); + builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID); + } + + @Test + public void labelIsCorrect() { + List sceneCollections = createSceneCollections(); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).build(); + + assertEquals(1, channels.size()); + assertEquals("TestSceneCollection", channels.get(0).getLabel()); + } + + @Test + public void descriptionIsCorrect() { + List sceneCollections = createSceneCollections(); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).build(); + + assertEquals(1, channels.size()); + assertEquals("Activates the scene group 'TestSceneCollection'", channels.get(0).getDescription()); + } + + @Test + public void groupAndIdAreCorrect() { + List sceneCollections = createSceneCollections(); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).build(); + + assertEquals(1, channels.size()); + assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId()); + assertEquals(Integer.toString(sceneCollections.get(0).id), channels.get(0).getUID().getIdWithoutGroup()); + } + + @Test + public void suppliedListIsUsed() { + List sceneCollections = createSceneCollections(); + List existingChannels = new ArrayList<>(0); + + @NonNull + List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels) + .build(); + + assertEquals(existingChannels, channels); + } + + private List createSceneCollections() { + SceneCollection sceneCollection = new SceneCollection(); + sceneCollection.id = 1; + sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes()); + return new ArrayList<>(List.of(sceneCollection)); + } +} diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/TranslationProviderForTests.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/TranslationProviderForTests.java index d2223e9c9c8b3..eec340c3a44a3 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/TranslationProviderForTests.java +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/TranslationProviderForTests.java @@ -32,6 +32,8 @@ public class TranslationProviderForTests implements TranslationProvider { private final static Map texts = Map.ofEntries( + entry("dynamic-channel.scene-activate.description", "Activates the scene ''{0}''"), + entry("dynamic-channel.scene-group-activate.description", "Activates the scene group ''{0}''"), entry("dynamic-channel.automation-enabled.description", "Enables/disables the automation ''{0}''"), entry("dynamic-channel.automation-enabled.label", "{0}, {1}, {2}"), entry("dynamic-channel.automation.hour", "{0}hr"), entry("dynamic-channel.automation.minute", "{0}m"),