[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:
parent
8d2bb6bf62
commit
c7f8507cae
@ -99,7 +99,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
|
|||||||
private List<Scene> sceneCache = new CopyOnWriteArrayList<>();
|
private List<Scene> sceneCache = new CopyOnWriteArrayList<>();
|
||||||
private List<SceneCollection> sceneCollectionCache = new CopyOnWriteArrayList<>();
|
private List<SceneCollection> sceneCollectionCache = new CopyOnWriteArrayList<>();
|
||||||
private List<ScheduledEvent> scheduledEventCache = new CopyOnWriteArrayList<>();
|
private List<ScheduledEvent> scheduledEventCache = new CopyOnWriteArrayList<>();
|
||||||
private Instant UserDataUpdated = Instant.MIN;
|
private Instant userDataUpdated = Instant.MIN;
|
||||||
private Boolean deprecatedChannelsCreated = false;
|
private Boolean deprecatedChannelsCreated = false;
|
||||||
|
|
||||||
private final ChannelTypeUID sceneChannelTypeUID = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID,
|
private final ChannelTypeUID sceneChannelTypeUID = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID,
|
||||||
@ -145,7 +145,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
|
|||||||
}
|
}
|
||||||
} catch (HubMaintenanceException e) {
|
} catch (HubMaintenanceException e) {
|
||||||
// exceptions are logged in HDPowerViewWebTargets
|
// exceptions are logged in HDPowerViewWebTargets
|
||||||
UserDataUpdated = Instant.MIN;
|
userDataUpdated = Instant.MIN;
|
||||||
} catch (NumberFormatException | HubException e) {
|
} catch (NumberFormatException | HubException e) {
|
||||||
logger.debug("Unexpected error {}", e.getMessage());
|
logger.debug("Unexpected error {}", e.getMessage());
|
||||||
}
|
}
|
||||||
@ -169,7 +169,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
|
|||||||
hardRefreshPositionInterval = config.hardRefresh;
|
hardRefreshPositionInterval = config.hardRefresh;
|
||||||
hardRefreshBatteryLevelInterval = config.hardRefreshBatteryLevel;
|
hardRefreshBatteryLevelInterval = config.hardRefreshBatteryLevel;
|
||||||
initializeChannels();
|
initializeChannels();
|
||||||
UserDataUpdated = Instant.MIN;
|
userDataUpdated = Instant.MIN;
|
||||||
|
|
||||||
updateStatus(ThingStatus.UNKNOWN);
|
updateStatus(ThingStatus.UNKNOWN);
|
||||||
schedulePoll();
|
schedulePoll();
|
||||||
@ -309,17 +309,17 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
|
|||||||
}
|
}
|
||||||
} catch (HubMaintenanceException e) {
|
} catch (HubMaintenanceException e) {
|
||||||
// exceptions are logged in HDPowerViewWebTargets
|
// exceptions are logged in HDPowerViewWebTargets
|
||||||
UserDataUpdated = Instant.MIN;
|
userDataUpdated = Instant.MIN;
|
||||||
} catch (HubException e) {
|
} catch (HubException e) {
|
||||||
logger.warn("Error connecting to bridge: {}", e.getMessage());
|
logger.warn("Error connecting to bridge: {}", e.getMessage());
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
|
||||||
UserDataUpdated = Instant.MIN;
|
userDataUpdated = Instant.MIN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateUserDataProperties()
|
private void updateUserDataProperties()
|
||||||
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
|
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
|
||||||
if (UserDataUpdated.isAfter(Instant.now().minus(firmwareVersionValidityPeriod))) {
|
if (userDataUpdated.isAfter(Instant.now().minus(firmwareVersionValidityPeriod))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -342,7 +342,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
|
|||||||
properties.put(HDPowerViewBindingConstants.PROPERTY_HUB_NAME, hubName);
|
properties.put(HDPowerViewBindingConstants.PROPERTY_HUB_NAME, hubName);
|
||||||
}
|
}
|
||||||
updateProperties(properties);
|
updateProperties(properties);
|
||||||
UserDataUpdated = Instant.now();
|
userDataUpdated = Instant.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateFirmwareProperties(Map<String, String> properties, HubFirmware firmwareVersions) {
|
private void updateFirmwareProperties(Map<String, String> properties, HubFirmware firmwareVersions) {
|
||||||
|
|||||||
@ -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;
|
||||||
import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents.ScheduledEvent;
|
import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents.ScheduledEvent;
|
||||||
import org.openhab.binding.hdpowerview.internal.builders.AutomationChannelBuilder;
|
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.Channel;
|
||||||
import org.openhab.core.thing.ChannelGroupUID;
|
import org.openhab.core.thing.ChannelGroupUID;
|
||||||
import org.openhab.core.thing.ThingUID;
|
import org.openhab.core.thing.ThingUID;
|
||||||
@ -50,9 +52,10 @@ public class AutomationChannelBuilderTest {
|
|||||||
new ThingUID(HDPowerViewBindingConstants.BINDING_ID, AutomationChannelBuilderTest.class.getSimpleName()),
|
new ThingUID(HDPowerViewBindingConstants.BINDING_ID, AutomationChannelBuilderTest.class.getSimpleName()),
|
||||||
HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED);
|
HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED);
|
||||||
|
|
||||||
private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
|
private static final HDPowerViewTranslationProvider TRANSLATION_PROVIDER = new HDPowerViewTranslationProvider(
|
||||||
mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
|
mock(Bundle.class), new MockedTranslationProvider(), new MockedLocaleProvider());
|
||||||
private AutomationChannelBuilder builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
|
|
||||||
|
private AutomationChannelBuilder builder = AutomationChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
|
||||||
private List<Scene> scenes = new ArrayList<>();
|
private List<Scene> scenes = new ArrayList<>();
|
||||||
private List<SceneCollection> sceneCollections = new ArrayList<>();
|
private List<SceneCollection> sceneCollections = new ArrayList<>();
|
||||||
|
|
||||||
@ -60,7 +63,7 @@ public class AutomationChannelBuilderTest {
|
|||||||
private void setUp() {
|
private void setUp() {
|
||||||
final Logger logger = (Logger) LoggerFactory.getLogger(AutomationChannelBuilder.class);
|
final Logger logger = (Logger) LoggerFactory.getLogger(AutomationChannelBuilder.class);
|
||||||
logger.setLevel(Level.OFF);
|
logger.setLevel(Level.OFF);
|
||||||
builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
|
builder = AutomationChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
|
||||||
|
|
||||||
Scene scene = new Scene();
|
Scene scene = new Scene();
|
||||||
scene.id = 1;
|
scene.id = 1;
|
||||||
|
|||||||
@ -16,16 +16,13 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
import static org.openhab.binding.hdpowerview.internal.api.CoordinateSystem.*;
|
import static org.openhab.binding.hdpowerview.internal.api.CoordinateSystem.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.io.InputStream;
|
||||||
import java.nio.file.Paths;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
|
||||||
import org.junit.jupiter.api.Test;
|
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.ShadePosition;
|
||||||
import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections;
|
import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections;
|
||||||
import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection;
|
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.api.responses.Shades.ShadeData;
|
||||||
import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
|
import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
|
||||||
import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
|
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.library.types.PercentType;
|
||||||
import org.openhab.core.types.State;
|
import org.openhab.core.types.State;
|
||||||
import org.openhab.core.types.UnDefType;
|
import org.openhab.core.types.UnDefType;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonParseException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for HD PowerView binding.
|
* Unit tests for HD PowerView binding.
|
||||||
@ -54,190 +47,19 @@ import com.google.gson.JsonParseException;
|
|||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class HDPowerViewJUnitTests {
|
public class HDPowerViewJUnitTests {
|
||||||
|
|
||||||
private static final Pattern VALID_IP_V4_ADDRESS = Pattern
|
private Gson gson = new Gson();
|
||||||
.compile("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b");
|
|
||||||
|
|
||||||
/*
|
private <T> T getObjectFromJson(String filename, Class<T> clazz) throws IOException {
|
||||||
* load a test JSON string from a file.
|
try (InputStream inputStream = HDPowerViewJUnitTests.class.getResourceAsStream(filename)) {
|
||||||
*/
|
if (inputStream == null) {
|
||||||
private String loadJson(String fileName) {
|
throw new IOException("inputstream is null");
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
byte[] bytes = inputStream.readAllBytes();
|
||||||
HDPowerViewWebTargets webTargets = new HDPowerViewWebTargets(client, hubIPAddress);
|
if (bytes == null) {
|
||||||
assertNotNull(webTargets);
|
throw new IOException("Resulting byte-array empty");
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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 generic JSON shades response.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void shadeResponseIsParsedCorrectly() throws JsonParseException {
|
public void shadeNameIsDecoded() throws IOException {
|
||||||
final Gson gson = new Gson();
|
Shades shades = getObjectFromJson("shades.json", Shades.class);
|
||||||
Shades shades;
|
List<ShadeData> shadeData = shades.shadeData;
|
||||||
String json = loadJson("shades");
|
assertNotNull(shadeData);
|
||||||
assertNotEquals("", json);
|
assertEquals(3, shadeData.size());
|
||||||
shades = gson.fromJson(json, Shades.class);
|
ShadeData shade = shadeData.get(0);
|
||||||
assertNotNull(shades);
|
assertEquals("Shade 2", shade.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test generic JSON scene response.
|
* Test generic JSON scene response.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void sceneResponseIsParsedCorrectly() throws JsonParseException {
|
public void sceneNameIsDecoded() throws IOException {
|
||||||
final Gson gson = new Gson();
|
Scenes scenes = getObjectFromJson("scenes.json", Scenes.class);
|
||||||
String json = loadJson("scenes");
|
|
||||||
assertNotEquals("", json);
|
|
||||||
|
|
||||||
Scenes scenes = gson.fromJson(json, Scenes.class);
|
|
||||||
assertNotNull(scenes);
|
|
||||||
List<Scene> sceneData = scenes.sceneData;
|
List<Scene> sceneData = scenes.sceneData;
|
||||||
assertNotNull(sceneData);
|
assertNotNull(sceneData);
|
||||||
assertEquals(4, sceneData.size());
|
assertEquals(4, sceneData.size());
|
||||||
Scene scene = sceneData.get(0);
|
Scene scene = sceneData.get(0);
|
||||||
assertEquals("Door Open", scene.getName());
|
assertEquals("Door Open", scene.getName());
|
||||||
assertEquals(18097, scene.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test generic JSON scene collection response.
|
* Test generic JSON scene collection response.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void sceneCollectionResponseIsParsedCorrectly() throws JsonParseException {
|
public void sceneCollectionNameIsDecoded() throws IOException {
|
||||||
final Gson gson = new Gson();
|
SceneCollections sceneCollections = getObjectFromJson("sceneCollections.json", SceneCollections.class);
|
||||||
String json = loadJson("sceneCollections");
|
|
||||||
assertNotEquals("", json);
|
|
||||||
|
|
||||||
SceneCollections sceneCollections = gson.fromJson(json, SceneCollections.class);
|
|
||||||
assertNotNull(sceneCollections);
|
|
||||||
|
|
||||||
List<SceneCollection> sceneCollectionData = sceneCollections.sceneCollectionData;
|
List<SceneCollection> sceneCollectionData = sceneCollections.sceneCollectionData;
|
||||||
assertNotNull(sceneCollectionData);
|
assertNotNull(sceneCollectionData);
|
||||||
@ -291,20 +102,14 @@ public class HDPowerViewJUnitTests {
|
|||||||
|
|
||||||
SceneCollection sceneCollection = sceneCollectionData.get(0);
|
SceneCollection sceneCollection = sceneCollectionData.get(0);
|
||||||
assertEquals("Børn op", sceneCollection.getName());
|
assertEquals("Børn op", sceneCollection.getName());
|
||||||
assertEquals(27119, sceneCollection.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the JSON parsing for a duette top down bottom up shade.
|
* Test the JSON parsing for a duette top down bottom up shade.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void duetteTopDownBottomUpShadeIsParsedCorrectly() throws JsonParseException {
|
public void duetteTopDownBottomUpShadeIsParsedCorrectly() throws IOException {
|
||||||
final Gson gson = new Gson();
|
Shades shades = getObjectFromJson("duette.json", Shades.class);
|
||||||
String json = loadJson("duette");
|
|
||||||
assertNotEquals("", json);
|
|
||||||
|
|
||||||
Shades shades = gson.fromJson(json, Shades.class);
|
|
||||||
assertNotNull(shades);
|
|
||||||
List<ShadeData> shadesData = shades.shadeData;
|
List<ShadeData> shadesData = shades.shadeData;
|
||||||
assertNotNull(shadesData);
|
assertNotNull(shadesData);
|
||||||
|
|
||||||
|
|||||||
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -26,6 +26,8 @@ import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
|
|||||||
import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider;
|
import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider;
|
||||||
import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene;
|
import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene;
|
||||||
import org.openhab.binding.hdpowerview.internal.builders.SceneChannelBuilder;
|
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.Channel;
|
||||||
import org.openhab.core.thing.ChannelGroupUID;
|
import org.openhab.core.thing.ChannelGroupUID;
|
||||||
import org.openhab.core.thing.ThingUID;
|
import org.openhab.core.thing.ThingUID;
|
||||||
@ -44,13 +46,14 @@ public class SceneChannelBuilderTest {
|
|||||||
new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneChannelBuilderTest.class.getSimpleName()),
|
new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneChannelBuilderTest.class.getSimpleName()),
|
||||||
HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);
|
HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);
|
||||||
|
|
||||||
private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
|
private static final HDPowerViewTranslationProvider TRANSLATION_PROVIDER = new HDPowerViewTranslationProvider(
|
||||||
mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
|
mock(Bundle.class), new MockedTranslationProvider(), new MockedLocaleProvider());
|
||||||
private SceneChannelBuilder builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
|
|
||||||
|
private SceneChannelBuilder builder = SceneChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
private void setUp() {
|
private void setUp() {
|
||||||
builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
|
builder = SceneChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@ -26,6 +26,8 @@ import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
|
|||||||
import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider;
|
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.SceneCollections.SceneCollection;
|
||||||
import org.openhab.binding.hdpowerview.internal.builders.SceneGroupChannelBuilder;
|
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.Channel;
|
||||||
import org.openhab.core.thing.ChannelGroupUID;
|
import org.openhab.core.thing.ChannelGroupUID;
|
||||||
import org.openhab.core.thing.ThingUID;
|
import org.openhab.core.thing.ThingUID;
|
||||||
@ -44,13 +46,14 @@ public class SceneGroupChannelBuilderTest {
|
|||||||
new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneGroupChannelBuilderTest.class.getSimpleName()),
|
new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneGroupChannelBuilderTest.class.getSimpleName()),
|
||||||
HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);
|
HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);
|
||||||
|
|
||||||
private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
|
private static final HDPowerViewTranslationProvider TRANSLATION_PROVIDER = new HDPowerViewTranslationProvider(
|
||||||
mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
|
mock(Bundle.class), new MockedTranslationProvider(), new MockedLocaleProvider());
|
||||||
private SceneGroupChannelBuilder builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
|
|
||||||
|
private SceneGroupChannelBuilder builder = SceneGroupChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
private void setUp() {
|
private void setUp() {
|
||||||
builder = SceneGroupChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
|
builder = SceneGroupChannelBuilder.create(TRANSLATION_PROVIDER, CHANNEL_GROUP_UID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
*
|
*
|
||||||
* SPDX-License-Identifier: EPL-2.0
|
* SPDX-License-Identifier: EPL-2.0
|
||||||
*/
|
*/
|
||||||
package org.openhab.binding.hdpowerview;
|
package org.openhab.binding.hdpowerview.providers;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ import org.openhab.core.i18n.LocaleProvider;
|
|||||||
* @author Jacob Laursen - Initial contribution
|
* @author Jacob Laursen - Initial contribution
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class LocaleProviderForTests implements LocaleProvider {
|
public class MockedLocaleProvider implements LocaleProvider {
|
||||||
public Locale getLocale() {
|
public Locale getLocale() {
|
||||||
return Locale.ENGLISH;
|
return Locale.ENGLISH;
|
||||||
}
|
}
|
||||||
@ -10,7 +10,7 @@
|
|||||||
*
|
*
|
||||||
* SPDX-License-Identifier: EPL-2.0
|
* SPDX-License-Identifier: EPL-2.0
|
||||||
*/
|
*/
|
||||||
package org.openhab.binding.hdpowerview;
|
package org.openhab.binding.hdpowerview.providers;
|
||||||
|
|
||||||
import static java.util.Map.entry;
|
import static java.util.Map.entry;
|
||||||
|
|
||||||
@ -29,9 +29,9 @@ import org.osgi.framework.Bundle;
|
|||||||
* @author Jacob Laursen - Initial contribution
|
* @author Jacob Laursen - Initial contribution
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@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-activate.description", "Activates the scene ''{0}''"),
|
||||||
entry("dynamic-channel.scene-group-activate.description", "Activates the scene group ''{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.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.weekends", "Weekends"),
|
||||||
entry("dynamic-channel.automation.all-days", "All days"));
|
entry("dynamic-channel.automation.all-days", "All days"));
|
||||||
|
|
||||||
public TranslationProviderForTests() {
|
public MockedTranslationProvider() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -60,7 +60,7 @@ public class TranslationProviderForTests implements TranslationProvider {
|
|||||||
@Nullable
|
@Nullable
|
||||||
public String getText(@Nullable Bundle bundle, @Nullable String key, @Nullable String defaultText,
|
public String getText(@Nullable Bundle bundle, @Nullable String key, @Nullable String defaultText,
|
||||||
@Nullable Locale locale, @Nullable Object @Nullable... arguments) {
|
@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);
|
return MessageFormat.format(text != null ? text : key, arguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user