Skip to content

Commit

Permalink
[hdpowerview] Fix output to console from tests and eliminate warnings (
Browse files Browse the repository at this point in the history
…openhab#12824)

* Avoid logging to console
* Fix redundant null checks

Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur authored and psmedley committed Feb 23, 2023
1 parent 89c4c58 commit c00e918
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
import org.openhab.core.thing.ChannelGroupUID;
import org.openhab.core.thing.ThingUID;
import org.osgi.framework.Bundle;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

/**
* Unit tests for {@link AutomationChannelBuilder}.
Expand All @@ -54,6 +58,8 @@ public class AutomationChannelBuilderTest {

@BeforeEach
private void setUp() {
final Logger logger = (Logger) LoggerFactory.getLogger(AutomationChannelBuilder.class);
logger.setLevel(Level.OFF);
builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);

Scene scene = new Scene();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,21 @@ public void testOnlineCommunication() {
List<ShadeData> shadesData = shadesX.shadeData;
assertNotNull(shadesData);

if (shadesData != null) {
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);
}
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());
Expand All @@ -152,17 +150,15 @@ public void testOnlineCommunication() {
List<Scene> scenesData = scenes.sceneData;
assertNotNull(scenesData);

if (scenesData != null) {
assertTrue(!scenesData.isEmpty());
Scene sceneZero = scenesData.get(0);
assertNotNull(sceneZero);
sceneId = sceneZero.id;
assertTrue(sceneId > 0);
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);
}
for (Scene scene : scenesData) {
String sceneName = scene.getName();
assertNotNull(sceneName);
}
} catch (HubException e) {
fail(e.getMessage());
Expand All @@ -187,31 +183,27 @@ public void testOnlineCommunication() {
Integer capabilitiesValue = shadeData.capabilities;
assertNotNull(capabilitiesValue);

if (positions != null && capabilitiesValue != null) {
Capabilities capabilities = new ShadeCapabilitiesDatabase()
.getCapabilities(capabilitiesValue.intValue());
Capabilities capabilities = new ShadeCapabilitiesDatabase()
.getCapabilities(capabilitiesValue.intValue());

State pos = positions.getState(capabilities, PRIMARY_POSITION);
assertEquals(PercentType.class, pos.getClass());
State pos = positions.getState(capabilities, PRIMARY_POSITION);
assertEquals(PercentType.class, pos.getClass());

int position = ((PercentType) pos).intValue();
position = position + ((position <= 10) ? 5 : -5);
int position = ((PercentType) pos).intValue();
position = position + ((position <= 10) ? 5 : -5);

ShadePosition targetPosition = new ShadePosition().setPosition(capabilities, PRIMARY_POSITION,
position);
assertNotNull(targetPosition);
ShadePosition targetPosition = new ShadePosition().setPosition(capabilities, PRIMARY_POSITION,
position);
assertNotNull(targetPosition);

if (allowShadeMovementCommands) {
webTargets.moveShade(shadeId, targetPosition);
if (allowShadeMovementCommands) {
webTargets.moveShade(shadeId, targetPosition);

ShadeData newData = webTargets.getShade(shadeId);
ShadePosition actualPosition = newData.positions;
assertNotNull(actualPosition);
if (actualPosition != null) {
assertEquals(targetPosition.getState(capabilities, PRIMARY_POSITION),
actualPosition.getState(capabilities, PRIMARY_POSITION));
}
}
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) {
Expand Down Expand Up @@ -273,16 +265,12 @@ public void sceneResponseIsParsedCorrectly() throws JsonParseException {

Scenes scenes = gson.fromJson(json, Scenes.class);
assertNotNull(scenes);
if (scenes != null) {
List<Scene> sceneData = scenes.sceneData;
assertNotNull(sceneData);
if (sceneData != null) {
assertEquals(4, sceneData.size());
Scene scene = sceneData.get(0);
assertEquals("Door Open", scene.getName());
assertEquals(18097, scene.id);
}
}
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);
}

/**
Expand All @@ -297,17 +285,13 @@ public void sceneCollectionResponseIsParsedCorrectly() throws JsonParseException
SceneCollections sceneCollections = gson.fromJson(json, SceneCollections.class);
assertNotNull(sceneCollections);

if (sceneCollections != null) {
List<SceneCollection> sceneCollectionData = sceneCollections.sceneCollectionData;
assertNotNull(sceneCollectionData);
if (sceneCollectionData != null) {
assertEquals(1, sceneCollectionData.size());
List<SceneCollection> sceneCollectionData = sceneCollections.sceneCollectionData;
assertNotNull(sceneCollectionData);
assertEquals(1, sceneCollectionData.size());

SceneCollection sceneCollection = sceneCollectionData.get(0);
assertEquals("Børn op", sceneCollection.getName());
assertEquals(27119, sceneCollection.id);
}
}
SceneCollection sceneCollection = sceneCollectionData.get(0);
assertEquals("Børn op", sceneCollection.getName());
assertEquals(27119, sceneCollection.id);
}

/**
Expand All @@ -321,79 +305,67 @@ public void duetteTopDownBottomUpShadeIsParsedCorrectly() throws JsonParseExcept

Shades shades = gson.fromJson(json, Shades.class);
assertNotNull(shades);
if (shades != null) {
List<ShadeData> shadesData = shades.shadeData;
assertNotNull(shadesData);
List<ShadeData> shadesData = shades.shadeData;
assertNotNull(shadesData);

if (shadesData != null) {
assertEquals(1, shadesData.size());
ShadeData shadeData = shadesData.get(0);
assertNotNull(shadeData);
assertEquals(1, shadesData.size());
ShadeData shadeData = shadesData.get(0);
assertNotNull(shadeData);

assertEquals("Gardin 1", shadeData.getName());
assertEquals(63778, shadeData.id);
assertEquals("Gardin 1", shadeData.getName());
assertEquals(63778, shadeData.id);

ShadePosition shadePos = shadeData.positions;
assertNotNull(shadePos);
ShadePosition shadePos = shadeData.positions;
assertNotNull(shadePos);

if (shadePos != null) {
Integer capabilitiesValue = shadeData.capabilities;
assertNotNull(capabilitiesValue);
if (capabilitiesValue != null) {
assertEquals(7, capabilitiesValue.intValue());
ShadeCapabilitiesDatabase db = new ShadeCapabilitiesDatabase();
Capabilities capabilities = db.getCapabilities(capabilitiesValue);

State pos = shadePos.getState(capabilities, PRIMARY_POSITION);
assertEquals(PercentType.class, pos.getClass());
assertEquals(59, ((PercentType) pos).intValue());

pos = shadePos.getState(capabilities, SECONDARY_POSITION);
assertEquals(PercentType.class, pos.getClass());
assertEquals(35, ((PercentType) pos).intValue());

pos = shadePos.getState(capabilities, VANE_TILT_POSITION);
assertEquals(UnDefType.class, pos.getClass());

assertEquals(3, shadeData.batteryStatus);

assertEquals(4, shadeData.signalStrength);

assertEquals(8, shadeData.type);

assertTrue(db.isTypeInDatabase(shadeData.type));
assertTrue(db.isCapabilitiesInDatabase(capabilitiesValue.intValue()));

assertEquals(db.getType(shadeData.type).getCapabilities(), capabilitiesValue.intValue());

assertTrue(db.getCapabilities(capabilitiesValue.intValue()).supportsSecondary());
assertNotEquals(db.getType(shadeData.type).getCapabilities(), capabilitiesValue.intValue() + 1);

// ==== when changing position1, position2 value is not changed (vice-versa) ====
ShadePosition shadePosition = shadeData.positions;
assertNotNull(shadePosition);
if (shadePosition != null) {
// ==== position2 ====
State position2Old = shadePosition.getState(capabilities, SECONDARY_POSITION);
shadePosition.setPosition(capabilities, PRIMARY_POSITION, 99);
State position2New = shadePosition.getState(capabilities, SECONDARY_POSITION);
assertEquals(PercentType.class, position2Old.getClass());
assertEquals(PercentType.class, position2New.getClass());
assertEquals(((PercentType) position2Old).intValue(),
((PercentType) position2New).intValue());

// ==== position2 ====
State position1Old = shadePosition.getState(capabilities, PRIMARY_POSITION);
shadePosition.setPosition(capabilities, SECONDARY_POSITION, 99);
State position1New = shadePosition.getState(capabilities, PRIMARY_POSITION);
assertEquals(PercentType.class, position1Old.getClass());
assertEquals(PercentType.class, position1New.getClass());
assertEquals(((PercentType) position1Old).intValue(),
((PercentType) position1New).intValue());
}
}
}
}
}
Integer capabilitiesValue = shadeData.capabilities;
assertNotNull(capabilitiesValue);
assertEquals(7, capabilitiesValue.intValue());
ShadeCapabilitiesDatabase db = new ShadeCapabilitiesDatabase();
Capabilities capabilities = db.getCapabilities(capabilitiesValue);

State pos = shadePos.getState(capabilities, PRIMARY_POSITION);
assertEquals(PercentType.class, pos.getClass());
assertEquals(59, ((PercentType) pos).intValue());

pos = shadePos.getState(capabilities, SECONDARY_POSITION);
assertEquals(PercentType.class, pos.getClass());
assertEquals(35, ((PercentType) pos).intValue());

pos = shadePos.getState(capabilities, VANE_TILT_POSITION);
assertEquals(UnDefType.class, pos.getClass());

assertEquals(3, shadeData.batteryStatus);

assertEquals(4, shadeData.signalStrength);

assertEquals(8, shadeData.type);

assertTrue(db.isTypeInDatabase(shadeData.type));
assertTrue(db.isCapabilitiesInDatabase(capabilitiesValue.intValue()));

assertEquals(db.getType(shadeData.type).getCapabilities(), capabilitiesValue.intValue());

assertTrue(db.getCapabilities(capabilitiesValue.intValue()).supportsSecondary());
assertNotEquals(db.getType(shadeData.type).getCapabilities(), capabilitiesValue.intValue() + 1);

// ==== when changing position1, position2 value is not changed (vice-versa) ====
ShadePosition shadePosition = shadeData.positions;
assertNotNull(shadePosition);
// ==== position2 ====
State position2Old = shadePosition.getState(capabilities, SECONDARY_POSITION);
shadePosition.setPosition(capabilities, PRIMARY_POSITION, 99);
State position2New = shadePosition.getState(capabilities, SECONDARY_POSITION);
assertEquals(PercentType.class, position2Old.getClass());
assertEquals(PercentType.class, position2New.getClass());
assertEquals(((PercentType) position2Old).intValue(), ((PercentType) position2New).intValue());

// ==== position2 ====
State position1Old = shadePosition.getState(capabilities, PRIMARY_POSITION);
shadePosition.setPosition(capabilities, SECONDARY_POSITION, 99);
State position1New = shadePosition.getState(capabilities, PRIMARY_POSITION);
assertEquals(PercentType.class, position1Old.getClass());
assertEquals(PercentType.class, position1New.getClass());
assertEquals(((PercentType) position1Old).intValue(), ((PercentType) position1New).intValue());
}
}

0 comments on commit c00e918

Please sign in to comment.