[hdpowerview] Refactor tests (#13175)

* Move test providers to dedicated folder
* Simplify resource loading
* Clarify what is being tested
* Extract online communication test to separate class
* Fix SAT findings
* Rename variable to comply with naming convention

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2022-07-30 20:43:17 +02:00 committed by GitHub
parent 8d2bb6bf62
commit c7f8507cae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 280 additions and 247 deletions

View File

@ -99,7 +99,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
private List<Scene> sceneCache = new CopyOnWriteArrayList<>();
private List<SceneCollection> sceneCollectionCache = new CopyOnWriteArrayList<>();
private List<ScheduledEvent> scheduledEventCache = new CopyOnWriteArrayList<>();
private Instant UserDataUpdated = Instant.MIN;
private Instant userDataUpdated = Instant.MIN;
private Boolean deprecatedChannelsCreated = false;
private final ChannelTypeUID sceneChannelTypeUID = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID,
@ -145,7 +145,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
}
} catch (HubMaintenanceException e) {
// exceptions are logged in HDPowerViewWebTargets
UserDataUpdated = Instant.MIN;
userDataUpdated = Instant.MIN;
} catch (NumberFormatException | HubException e) {
logger.debug("Unexpected error {}", e.getMessage());
}
@ -169,7 +169,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
hardRefreshPositionInterval = config.hardRefresh;
hardRefreshBatteryLevelInterval = config.hardRefreshBatteryLevel;
initializeChannels();
UserDataUpdated = Instant.MIN;
userDataUpdated = Instant.MIN;
updateStatus(ThingStatus.UNKNOWN);
schedulePoll();
@ -309,17 +309,17 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
}
} catch (HubMaintenanceException e) {
// exceptions are logged in HDPowerViewWebTargets
UserDataUpdated = Instant.MIN;
userDataUpdated = Instant.MIN;
} catch (HubException e) {
logger.warn("Error connecting to bridge: {}", e.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
UserDataUpdated = Instant.MIN;
userDataUpdated = Instant.MIN;
}
}
private void updateUserDataProperties()
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
if (UserDataUpdated.isAfter(Instant.now().minus(firmwareVersionValidityPeriod))) {
if (userDataUpdated.isAfter(Instant.now().minus(firmwareVersionValidityPeriod))) {
return;
}
@ -342,7 +342,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
properties.put(HDPowerViewBindingConstants.PROPERTY_HUB_NAME, hubName);
}
updateProperties(properties);
UserDataUpdated = Instant.now();
userDataUpdated = Instant.now();
}
private void updateFirmwareProperties(Map<String, String> properties, HubFirmware firmwareVersions) {

View File

@ -29,6 +29,8 @@ 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.binding.hdpowerview.providers.MockedLocaleProvider;
import org.openhab.binding.hdpowerview.providers.MockedTranslationProvider;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelGroupUID;
import org.openhab.core.thing.ThingUID;
@ -50,9 +52,10 @@ public class AutomationChannelBuilderTest {
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 static final HDPowerViewTranslationProvider TRANSLATION_PROVIDER = new HDPowerViewTranslationProvider(
mock(Bundle.class), new MockedTranslationProvider(), new MockedLocaleProvider());
private AutomationChannelBuilder builder = AutomationChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
private List<Scene> scenes = new ArrayList<>();
private List<SceneCollection> sceneCollections = new ArrayList<>();
@ -60,7 +63,7 @@ public class AutomationChannelBuilderTest {
private void setUp() {
final Logger logger = (Logger) LoggerFactory.getLogger(AutomationChannelBuilder.class);
logger.setLevel(Level.OFF);
builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
builder = AutomationChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
Scene scene = new Scene();
scene.id = 1;

View File

@ -16,16 +16,13 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.hdpowerview.internal.api.CoordinateSystem.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.Objects;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient;
import org.junit.jupiter.api.Test;
import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections;
import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection;
@ -35,15 +32,11 @@ import org.openhab.binding.hdpowerview.internal.api.responses.Shades;
import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
import org.openhab.binding.hdpowerview.internal.exceptions.HubMaintenanceException;
import org.openhab.binding.hdpowerview.internal.exceptions.HubProcessingException;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
/**
* Unit tests for HD PowerView binding.
@ -54,190 +47,19 @@ import com.google.gson.JsonParseException;
@NonNullByDefault
public class HDPowerViewJUnitTests {
private static final Pattern VALID_IP_V4_ADDRESS = Pattern
.compile("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b");
private Gson gson = new Gson();
/*
* load a test JSON string from a file.
*/
private String loadJson(String fileName) {
try {
return Files.readAllLines(Paths.get(String.format("src/test/resources/%s.json", fileName))).stream()
.collect(Collectors.joining());
} catch (IOException e) {
fail(e.getMessage());
}
return "";
}
/**
* Run a series of ONLINE tests on the communication with a hub.
*
* @param hubIPAddress must be a valid hub IP address to run the
* tests on; or an INVALID IP address to
* suppress the tests
* @param allowShadeMovementCommands set to true if you accept that the tests
* shall physically move the shades
*/
@Test
public void testOnlineCommunication() {
/*
* NOTE: in order to actually run these tests you must have a hub physically
* available, and its IP address must be correctly configured in the
* "hubIPAddress" string constant e.g. "192.168.1.123"
*/
String hubIPAddress = "192.168.1.xxx";
/*
* NOTE: set allowShadeMovementCommands = true if you accept physically moving
* the shades during these tests
*/
boolean allowShadeMovementCommands = false;
if (VALID_IP_V4_ADDRESS.matcher(hubIPAddress).matches()) {
// ==== initialize stuff ====
HttpClient client = new HttpClient();
assertNotNull(client);
// ==== start the client ====
try {
client.start();
assertTrue(client.isStarted());
} catch (Exception e) {
fail(e.getMessage());
private <T> T getObjectFromJson(String filename, Class<T> clazz) throws IOException {
try (InputStream inputStream = HDPowerViewJUnitTests.class.getResourceAsStream(filename)) {
if (inputStream == null) {
throw new IOException("inputstream is null");
}
HDPowerViewWebTargets webTargets = new HDPowerViewWebTargets(client, hubIPAddress);
assertNotNull(webTargets);
int shadeId = 0;
ShadePosition shadePos = null;
Shades shadesX = null;
// ==== get all shades ====
try {
shadesX = webTargets.getShades();
assertNotNull(shadesX);
List<ShadeData> shadesData = shadesX.shadeData;
assertNotNull(shadesData);
assertTrue(!shadesData.isEmpty());
ShadeData shadeData;
shadeData = shadesData.get(0);
assertNotNull(shadeData);
assertTrue(shadeData.getName().length() > 0);
shadePos = shadeData.positions;
assertNotNull(shadePos);
ShadeData shadeZero = shadesData.get(0);
assertNotNull(shadeZero);
shadeId = shadeZero.id;
assertNotEquals(0, shadeId);
for (ShadeData shadexData : shadesData) {
String shadeName = shadexData.getName();
assertNotNull(shadeName);
}
} catch (HubException e) {
fail(e.getMessage());
}
// ==== get all scenes ====
int sceneId = 0;
try {
Scenes scenes = webTargets.getScenes();
assertNotNull(scenes);
List<Scene> scenesData = scenes.sceneData;
assertNotNull(scenesData);
assertTrue(!scenesData.isEmpty());
Scene sceneZero = scenesData.get(0);
assertNotNull(sceneZero);
sceneId = sceneZero.id;
assertTrue(sceneId > 0);
for (Scene scene : scenesData) {
String sceneName = scene.getName();
assertNotNull(sceneName);
}
} catch (HubException e) {
fail(e.getMessage());
}
// ==== refresh a specific shade ====
ShadeData shadeData = null;
try {
assertNotEquals(0, shadeId);
shadeData = webTargets.refreshShadePosition(shadeId);
} catch (HubException e) {
fail(e.getMessage());
}
// ==== move a specific shade ====
try {
assertNotEquals(0, shadeId);
if (shadeData != null) {
ShadePosition positions = shadeData.positions;
assertNotNull(positions);
Integer capabilitiesValue = shadeData.capabilities;
assertNotNull(capabilitiesValue);
Capabilities capabilities = new ShadeCapabilitiesDatabase()
.getCapabilities(capabilitiesValue.intValue());
State pos = positions.getState(capabilities, PRIMARY_POSITION);
assertEquals(PercentType.class, pos.getClass());
int position = ((PercentType) pos).intValue();
position = position + ((position <= 10) ? 5 : -5);
ShadePosition targetPosition = new ShadePosition().setPosition(capabilities, PRIMARY_POSITION,
position);
assertNotNull(targetPosition);
if (allowShadeMovementCommands) {
webTargets.moveShade(shadeId, targetPosition);
ShadeData newData = webTargets.getShade(shadeId);
ShadePosition actualPosition = newData.positions;
assertNotNull(actualPosition);
assertEquals(targetPosition.getState(capabilities, PRIMARY_POSITION),
actualPosition.getState(capabilities, PRIMARY_POSITION));
}
}
} catch (HubException e) {
fail(e.getMessage());
}
// ==== activate a specific scene ====
if (allowShadeMovementCommands) {
try {
assertNotNull(sceneId);
webTargets.activateScene(sceneId);
} catch (HubProcessingException | HubMaintenanceException e) {
fail(e.getMessage());
}
}
// ==== test stop command ====
if (allowShadeMovementCommands) {
try {
assertNotNull(sceneId);
webTargets.stopShade(shadeId);
} catch (HubException e) {
fail(e.getMessage());
}
}
// ==== stop the client ====
if (client.isRunning()) {
try {
client.stop();
} catch (Exception e) {
fail(e.getMessage());
}
byte[] bytes = inputStream.readAllBytes();
if (bytes == null) {
throw new IOException("Resulting byte-array empty");
}
String json = new String(bytes, StandardCharsets.UTF_8);
return Objects.requireNonNull(gson.fromJson(json, clazz));
}
}
@ -245,45 +67,34 @@ public class HDPowerViewJUnitTests {
* Test generic JSON shades response.
*/
@Test
public void shadeResponseIsParsedCorrectly() throws JsonParseException {
final Gson gson = new Gson();
Shades shades;
String json = loadJson("shades");
assertNotEquals("", json);
shades = gson.fromJson(json, Shades.class);
assertNotNull(shades);
public void shadeNameIsDecoded() throws IOException {
Shades shades = getObjectFromJson("shades.json", Shades.class);
List<ShadeData> shadeData = shades.shadeData;
assertNotNull(shadeData);
assertEquals(3, shadeData.size());
ShadeData shade = shadeData.get(0);
assertEquals("Shade 2", shade.getName());
}
/**
* Test generic JSON scene response.
*/
@Test
public void sceneResponseIsParsedCorrectly() throws JsonParseException {
final Gson gson = new Gson();
String json = loadJson("scenes");
assertNotEquals("", json);
Scenes scenes = gson.fromJson(json, Scenes.class);
assertNotNull(scenes);
public void sceneNameIsDecoded() throws IOException {
Scenes scenes = getObjectFromJson("scenes.json", Scenes.class);
List<Scene> sceneData = scenes.sceneData;
assertNotNull(sceneData);
assertEquals(4, sceneData.size());
Scene scene = sceneData.get(0);
assertEquals("Door Open", scene.getName());
assertEquals(18097, scene.id);
}
/**
* Test generic JSON scene collection response.
*/
@Test
public void sceneCollectionResponseIsParsedCorrectly() throws JsonParseException {
final Gson gson = new Gson();
String json = loadJson("sceneCollections");
assertNotEquals("", json);
SceneCollections sceneCollections = gson.fromJson(json, SceneCollections.class);
assertNotNull(sceneCollections);
public void sceneCollectionNameIsDecoded() throws IOException {
SceneCollections sceneCollections = getObjectFromJson("sceneCollections.json", SceneCollections.class);
List<SceneCollection> sceneCollectionData = sceneCollections.sceneCollectionData;
assertNotNull(sceneCollectionData);
@ -291,20 +102,14 @@ public class HDPowerViewJUnitTests {
SceneCollection sceneCollection = sceneCollectionData.get(0);
assertEquals("Børn op", sceneCollection.getName());
assertEquals(27119, sceneCollection.id);
}
/**
* Test the JSON parsing for a duette top down bottom up shade.
*/
@Test
public void duetteTopDownBottomUpShadeIsParsedCorrectly() throws JsonParseException {
final Gson gson = new Gson();
String json = loadJson("duette");
assertNotEquals("", json);
Shades shades = gson.fromJson(json, Shades.class);
assertNotNull(shades);
public void duetteTopDownBottomUpShadeIsParsedCorrectly() throws IOException {
Shades shades = getObjectFromJson("duette.json", Shades.class);
List<ShadeData> shadesData = shades.shadeData;
assertNotNull(shadesData);

View File

@ -0,0 +1,219 @@
/**
* 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.*;
import static org.openhab.binding.hdpowerview.internal.api.CoordinateSystem.*;
import java.util.List;
import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient;
import org.junit.jupiter.api.Test;
import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
import org.openhab.binding.hdpowerview.internal.api.responses.Scenes;
import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene;
import org.openhab.binding.hdpowerview.internal.api.responses.Shades;
import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
import org.openhab.binding.hdpowerview.internal.exceptions.HubMaintenanceException;
import org.openhab.binding.hdpowerview.internal.exceptions.HubProcessingException;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.types.State;
/**
* Unit tests for HD PowerView binding.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
public class OnlineCommunicationTest {
private static final Pattern VALID_IP_V4_ADDRESS = Pattern
.compile("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b");
/**
* Run a series of ONLINE tests on the communication with a hub.
*
* @param hubIPAddress must be a valid hub IP address to run the
* tests on; or an INVALID IP address to
* suppress the tests
* @param allowShadeMovementCommands set to true if you accept that the tests
* shall physically move the shades
*/
@Test
public void testOnlineCommunication() {
/*
* NOTE: in order to actually run these tests you must have a hub physically
* available, and its IP address must be correctly configured in the
* "hubIPAddress" string constant e.g. "192.168.1.123"
*/
String hubIPAddress = "192.168.1.xxx";
/*
* NOTE: set allowShadeMovementCommands = true if you accept physically moving
* the shades during these tests
*/
boolean allowShadeMovementCommands = false;
if (VALID_IP_V4_ADDRESS.matcher(hubIPAddress).matches()) {
// ==== initialize stuff ====
HttpClient client = new HttpClient();
assertNotNull(client);
// ==== start the client ====
try {
client.start();
assertTrue(client.isStarted());
} catch (Exception e) {
fail(e.getMessage());
}
HDPowerViewWebTargets webTargets = new HDPowerViewWebTargets(client, hubIPAddress);
assertNotNull(webTargets);
int shadeId = 0;
ShadePosition shadePos = null;
Shades shadesX = null;
// ==== get all shades ====
try {
shadesX = webTargets.getShades();
assertNotNull(shadesX);
List<ShadeData> shadesData = shadesX.shadeData;
assertNotNull(shadesData);
assertTrue(!shadesData.isEmpty());
ShadeData shadeData;
shadeData = shadesData.get(0);
assertNotNull(shadeData);
assertTrue(shadeData.getName().length() > 0);
shadePos = shadeData.positions;
assertNotNull(shadePos);
ShadeData shadeZero = shadesData.get(0);
assertNotNull(shadeZero);
shadeId = shadeZero.id;
assertNotEquals(0, shadeId);
for (ShadeData shadexData : shadesData) {
String shadeName = shadexData.getName();
assertNotNull(shadeName);
}
} catch (HubException e) {
fail(e.getMessage());
}
// ==== get all scenes ====
int sceneId = 0;
try {
Scenes scenes = webTargets.getScenes();
assertNotNull(scenes);
List<Scene> scenesData = scenes.sceneData;
assertNotNull(scenesData);
assertTrue(!scenesData.isEmpty());
Scene sceneZero = scenesData.get(0);
assertNotNull(sceneZero);
sceneId = sceneZero.id;
assertTrue(sceneId > 0);
for (Scene scene : scenesData) {
String sceneName = scene.getName();
assertNotNull(sceneName);
}
} catch (HubException e) {
fail(e.getMessage());
}
// ==== refresh a specific shade ====
ShadeData shadeData = null;
try {
assertNotEquals(0, shadeId);
shadeData = webTargets.refreshShadePosition(shadeId);
} catch (HubException e) {
fail(e.getMessage());
}
// ==== move a specific shade ====
try {
assertNotEquals(0, shadeId);
if (shadeData != null) {
ShadePosition positions = shadeData.positions;
assertNotNull(positions);
Integer capabilitiesValue = shadeData.capabilities;
assertNotNull(capabilitiesValue);
Capabilities capabilities = new ShadeCapabilitiesDatabase()
.getCapabilities(capabilitiesValue.intValue());
State pos = positions.getState(capabilities, PRIMARY_POSITION);
assertEquals(PercentType.class, pos.getClass());
int position = ((PercentType) pos).intValue();
position = position + ((position <= 10) ? 5 : -5);
ShadePosition targetPosition = new ShadePosition().setPosition(capabilities, PRIMARY_POSITION,
position);
assertNotNull(targetPosition);
if (allowShadeMovementCommands) {
webTargets.moveShade(shadeId, targetPosition);
ShadeData newData = webTargets.getShade(shadeId);
ShadePosition actualPosition = newData.positions;
assertNotNull(actualPosition);
assertEquals(targetPosition.getState(capabilities, PRIMARY_POSITION),
actualPosition.getState(capabilities, PRIMARY_POSITION));
}
}
} catch (HubException e) {
fail(e.getMessage());
}
// ==== activate a specific scene ====
if (allowShadeMovementCommands) {
try {
assertNotNull(sceneId);
webTargets.activateScene(sceneId);
} catch (HubProcessingException | HubMaintenanceException e) {
fail(e.getMessage());
}
}
// ==== test stop command ====
if (allowShadeMovementCommands) {
try {
assertNotNull(sceneId);
webTargets.stopShade(shadeId);
} catch (HubException e) {
fail(e.getMessage());
}
}
// ==== stop the client ====
if (client.isRunning()) {
try {
client.stop();
} catch (Exception e) {
fail(e.getMessage());
}
}
}
}
}

View File

@ -26,6 +26,8 @@ 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.binding.hdpowerview.providers.MockedLocaleProvider;
import org.openhab.binding.hdpowerview.providers.MockedTranslationProvider;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelGroupUID;
import org.openhab.core.thing.ThingUID;
@ -44,13 +46,14 @@ public class SceneChannelBuilderTest {
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);
private static final HDPowerViewTranslationProvider TRANSLATION_PROVIDER = new HDPowerViewTranslationProvider(
mock(Bundle.class), new MockedTranslationProvider(), new MockedLocaleProvider());
private SceneChannelBuilder builder = SceneChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
@BeforeEach
private void setUp() {
builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
builder = SceneChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
}
@Test

View File

@ -26,6 +26,8 @@ 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.binding.hdpowerview.providers.MockedLocaleProvider;
import org.openhab.binding.hdpowerview.providers.MockedTranslationProvider;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelGroupUID;
import org.openhab.core.thing.ThingUID;
@ -44,13 +46,14 @@ public class SceneGroupChannelBuilderTest {
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);
private static final HDPowerViewTranslationProvider TRANSLATION_PROVIDER = new HDPowerViewTranslationProvider(
mock(Bundle.class), new MockedTranslationProvider(), new MockedLocaleProvider());
private SceneGroupChannelBuilder builder = SceneGroupChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
@BeforeEach
private void setUp() {
builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
builder = SceneGroupChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
}
@Test

View File

@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.hdpowerview;
package org.openhab.binding.hdpowerview.providers;
import java.util.Locale;
@ -23,7 +23,7 @@ import org.openhab.core.i18n.LocaleProvider;
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class LocaleProviderForTests implements LocaleProvider {
public class MockedLocaleProvider implements LocaleProvider {
public Locale getLocale() {
return Locale.ENGLISH;
}

View File

@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.hdpowerview;
package org.openhab.binding.hdpowerview.providers;
import static java.util.Map.entry;
@ -29,9 +29,9 @@ import org.osgi.framework.Bundle;
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class TranslationProviderForTests implements TranslationProvider {
public class MockedTranslationProvider implements TranslationProvider {
private final static Map<String, String> texts = Map.ofEntries(
private static final 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}''"),
@ -48,7 +48,7 @@ public class TranslationProviderForTests implements TranslationProvider {
entry("dynamic-channel.automation.weekends", "Weekends"),
entry("dynamic-channel.automation.all-days", "All days"));
public TranslationProviderForTests() {
public MockedTranslationProvider() {
}
@Nullable
@ -60,7 +60,7 @@ public class TranslationProviderForTests implements TranslationProvider {
@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);
String text = TEXTS.get(key);
return MessageFormat.format(text != null ? text : key, arguments);
}
}