[hdpowerview] Refactor dynamic channels (#11853)

* Extract dynamic channel creation to separate classes.
* Avoid double list allocations.
* Add test coverage for scenarios with no channels built.
* Extract common builder stuff to super class.
* Fix grammar.
* Reduce constructor access modifiers.
* Removed unneeded this keyword for protected method.
* Fix null annotation issues.

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen
2022-01-17 13:41:33 +01:00
committed by GitHub
parent 8b3bb313eb
commit e44dfe7dbc
13 changed files with 1123 additions and 172 deletions

View File

@@ -0,0 +1,264 @@
/**
* Copyright (c) 2010-2022 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.NonNullByDefault;
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.api.responses.Scenes.Scene;
import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents;
import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents.ScheduledEvent;
import org.openhab.binding.hdpowerview.internal.builders.AutomationChannelBuilder;
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 AutomationChannelBuilder}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class AutomationChannelBuilderTest {
private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
new ThingUID(HDPowerViewBindingConstants.BINDING_ID, AutomationChannelBuilderTest.class.getSimpleName()),
HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED);
private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
private AutomationChannelBuilder builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
private List<Scene> scenes = new ArrayList<>();
private List<SceneCollection> sceneCollections = new ArrayList<>();
@BeforeEach
private void setUp() {
builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
Scene scene = new Scene();
scene.id = 1;
scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes());
scenes = new ArrayList<>(List.of(scene));
SceneCollection sceneCollection = new SceneCollection();
sceneCollection.id = 2;
sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes());
sceneCollections = new ArrayList<>(List.of(sceneCollection));
}
@Test
public void sceneSunriseWeekends() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
scheduledEvent.daySaturday = true;
scheduledEvent.daySunday = true;
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
assertEquals(1, channels.size());
assertEquals("TestScene, At sunrise, Weekends", channels.get(0).getLabel());
}
@Test
public void sceneSunsetWeekdays() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNSET);
scheduledEvent.dayMonday = true;
scheduledEvent.dayTuesday = true;
scheduledEvent.dayWednesday = true;
scheduledEvent.dayThursday = true;
scheduledEvent.dayFriday = true;
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
assertEquals(1, channels.size());
assertEquals("TestScene, At sunset, Weekdays", channels.get(0).getLabel());
}
@Test
public void sceneTimeAllDays() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_TIME);
scheduledEvent.dayMonday = true;
scheduledEvent.dayTuesday = true;
scheduledEvent.dayWednesday = true;
scheduledEvent.dayThursday = true;
scheduledEvent.dayFriday = true;
scheduledEvent.daySaturday = true;
scheduledEvent.daySunday = true;
scheduledEvent.hour = 6;
scheduledEvent.minute = 30;
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
assertEquals(1, channels.size());
assertEquals("TestScene, 06:30, All days", channels.get(0).getLabel());
}
@Test
public void sceneMinutesBeforeSunriseMondayTuesday() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
scheduledEvent.dayMonday = true;
scheduledEvent.dayTuesday = true;
scheduledEvent.minute = -15;
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
assertEquals(1, channels.size());
assertEquals("TestScene, 15m before sunrise, Mon, Tue", channels.get(0).getLabel());
}
@Test
public void sceneHoursMinutesAfterSunriseMonday() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
scheduledEvent.dayMonday = true;
scheduledEvent.minute = 61;
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
assertEquals(1, channels.size());
assertEquals("TestScene, 1hr 1m after sunrise, Mon", channels.get(0).getLabel());
}
@Test
public void sceneMinutesBeforeSunsetWednesdayThursdayFriday() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNSET);
scheduledEvent.dayWednesday = true;
scheduledEvent.dayThursday = true;
scheduledEvent.dayFriday = true;
scheduledEvent.minute = -59;
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
assertEquals(1, channels.size());
assertEquals("TestScene, 59m before sunset, Wed, Thu, Fri", channels.get(0).getLabel());
}
@Test
public void sceneHourAfterSunsetFridaySaturdaySunday() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNSET);
scheduledEvent.dayFriday = true;
scheduledEvent.daySaturday = true;
scheduledEvent.daySunday = true;
scheduledEvent.minute = 60;
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
assertEquals(1, channels.size());
assertEquals("TestScene, 1hr after sunset, Fri, Sat, Sun", channels.get(0).getLabel());
}
@Test
public void sceneCollection() {
ScheduledEvent scheduledEvent = createScheduledEventWithSceneCollection(
ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withSceneCollections(sceneCollections).withScheduledEvents(scheduledEvents)
.build();
assertEquals(1, channels.size());
assertEquals("TestSceneCollection, At sunrise, ", channels.get(0).getLabel());
}
@Test
public void suppliedListIsUsed() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> existingChannels = new ArrayList<>(0);
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents)
.withChannels(existingChannels).build();
assertEquals(existingChannels, channels);
}
@Test
public void emptyListWhenNoScheduledEvents() {
List<Channel> channels = builder.build();
assertEquals(0, channels.size());
}
@Test
public void emptyListWhenNoScenesOrSceneCollections() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScheduledEvents(scheduledEvents).build();
assertEquals(0, channels.size());
}
@Test
public void emptyListWhenNoSceneForScheduledEvent() {
ScheduledEvent scheduledEvent = createScheduledEventWithSceneCollection(
ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
assertEquals(0, channels.size());
}
@Test
public void emptyListWhenNoSceneCollectionForScheduledEvent() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withSceneCollections(sceneCollections).withScheduledEvents(scheduledEvents)
.build();
assertEquals(0, channels.size());
}
@Test
public void groupAndIdAreCorrect() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
scheduledEvent.id = 42;
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
assertEquals(1, channels.size());
assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
assertEquals(Integer.toString(scheduledEvent.id), channels.get(0).getUID().getIdWithoutGroup());
}
private ScheduledEvent createScheduledEventWithScene(int eventType) {
ScheduledEvent scheduledEvent = new ScheduledEvent();
scheduledEvent.id = 1;
scheduledEvent.sceneId = scenes.get(0).id;
scheduledEvent.eventType = eventType;
return scheduledEvent;
}
private ScheduledEvent createScheduledEventWithSceneCollection(int eventType) {
ScheduledEvent scheduledEvent = new ScheduledEvent();
scheduledEvent.id = 1;
scheduledEvent.sceneCollectionId = sceneCollections.get(0).id;
scheduledEvent.eventType = eventType;
return scheduledEvent;
}
}

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2010-2022 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 java.util.Locale;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.i18n.LocaleProvider;
/**
* Locale provider for unit tests.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class LocaleProviderForTests implements LocaleProvider {
public Locale getLocale() {
return Locale.ENGLISH;
}
}

View File

@@ -0,0 +1,105 @@
/**
* Copyright (c) 2010-2022 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.NonNullByDefault;
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
*/
@NonNullByDefault
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 static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
private SceneChannelBuilder builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
@BeforeEach
private void setUp() {
builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
}
@Test
public void labelIsCorrect() {
List<Scene> scenes = createScenes();
List<Channel> channels = builder.withScenes(scenes).build();
assertEquals(1, channels.size());
assertEquals("TestScene", channels.get(0).getLabel());
}
@Test
public void descriptionIsCorrect() {
List<Scene> scenes = createScenes();
List<Channel> channels = builder.withScenes(scenes).build();
assertEquals(1, channels.size());
assertEquals("Activates the scene 'TestScene'", channels.get(0).getDescription());
}
@Test
public void groupAndIdAreCorrect() {
List<Scene> scenes = createScenes();
List<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<Scene> scenes = createScenes();
List<Channel> existingChannels = new ArrayList<>(0);
List<Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build();
assertEquals(existingChannels, channels);
}
@Test
public void emptyListWhenNoScenes() {
List<Channel> channels = builder.build();
assertEquals(0, channels.size());
}
private List<Scene> createScenes() {
Scene scene = new Scene();
scene.id = 1;
scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes());
return new ArrayList<>(List.of(scene));
}
}

View File

@@ -0,0 +1,105 @@
/**
* Copyright (c) 2010-2022 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.NonNullByDefault;
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
*/
@NonNullByDefault
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 static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
private SceneGroupChannelBuilder builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
@BeforeEach
private void setUp() {
builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
}
@Test
public void labelIsCorrect() {
List<SceneCollection> sceneCollections = createSceneCollections();
List<Channel> channels = builder.withSceneCollections(sceneCollections).build();
assertEquals(1, channels.size());
assertEquals("TestSceneCollection", channels.get(0).getLabel());
}
@Test
public void descriptionIsCorrect() {
List<SceneCollection> sceneCollections = createSceneCollections();
List<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<SceneCollection> sceneCollections = createSceneCollections();
List<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<SceneCollection> sceneCollections = createSceneCollections();
List<Channel> existingChannels = new ArrayList<>(0);
List<Channel> channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels).build();
assertEquals(existingChannels, channels);
}
@Test
public void emptyListWhenNoSceneCollections() {
List<Channel> channels = builder.build();
assertEquals(0, channels.size());
}
private List<SceneCollection> createSceneCollections() {
SceneCollection sceneCollection = new SceneCollection();
sceneCollection.id = 1;
sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes());
return new ArrayList<>(List.of(sceneCollection));
}
}

View File

@@ -0,0 +1,66 @@
/**
* Copyright (c) 2010-2022 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 java.util.Map.entry;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.i18n.TranslationProvider;
import org.osgi.framework.Bundle;
/**
* Translation provider for unit tests.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class TranslationProviderForTests implements TranslationProvider {
private final static Map<String, String> 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"),
entry("dynamic-channel.automation.hour-minute", "{0}hr {1}m"),
entry("dynamic-channel.automation.at-sunrise", "At sunrise"),
entry("dynamic-channel.automation.before-sunrise", "{0} before sunrise"),
entry("dynamic-channel.automation.after-sunrise", "{0} after sunrise"),
entry("dynamic-channel.automation.at-sunset", "At sunset"),
entry("dynamic-channel.automation.before-sunset", "{0} before sunset"),
entry("dynamic-channel.automation.after-sunset", "{0} after sunset"),
entry("dynamic-channel.automation.weekdays", "Weekdays"),
entry("dynamic-channel.automation.weekends", "Weekends"),
entry("dynamic-channel.automation.all-days", "All days"));
public TranslationProviderForTests() {
}
@Nullable
public String getText(@Nullable Bundle bundle, @Nullable String key, @Nullable String defaultText,
@Nullable Locale locale) {
return "";
}
@Nullable
public String getText(@Nullable Bundle bundle, @Nullable String key, @Nullable String defaultText,
@Nullable Locale locale, @Nullable Object @Nullable... arguments) {
String text = texts.get(key);
return MessageFormat.format(text != null ? text : key, arguments);
}
}