Skip to content

Commit

Permalink
Add test coverage for SceneChannelBuilder and SceneGroupChannelBuilder.
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Jan 2, 2022
1 parent 66c6440 commit a8472e7
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Scene> 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<Scene> 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<Scene> 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<Scene> scenes = createScenes();
List<Channel> existingChannels = new ArrayList<>(0);

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build();

assertEquals(existingChannels, channels);
}

private List<Scene> createScenes() {
Scene scene = new Scene();
scene.id = 1;
scene.name = Base64.getEncoder().encodeToString(("TestScene").getBytes());
return new ArrayList<>(List.of(scene));
}
}
Original file line number Diff line number Diff line change
@@ -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<SceneCollection> 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<SceneCollection> 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<SceneCollection> 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<SceneCollection> sceneCollections = createSceneCollections();
List<Channel> existingChannels = new ArrayList<>(0);

@NonNull
List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections).withChannels(existingChannels)
.build();

assertEquals(existingChannels, channels);
}

private List<SceneCollection> createSceneCollections() {
SceneCollection sceneCollection = new SceneCollection();
sceneCollection.id = 1;
sceneCollection.name = Base64.getEncoder().encodeToString(("TestSceneCollection").getBytes());
return new ArrayList<>(List.of(sceneCollection));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
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"),
Expand Down

0 comments on commit a8472e7

Please sign in to comment.