Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Fix tests. Add more tests for percentageValue. NumberValue: Check for…
Browse files Browse the repository at this point in the history
… min/max
  • Loading branch information
David Graeff committed Jan 17, 2019
1 parent 0c153b3 commit 4e12a8b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ public void receiveDecimalFractionalTest() throws InterruptedException, Executio

@Test
public void receivePercentageTest() throws InterruptedException, ExecutionException, TimeoutException {
PercentageValue value = new PercentageValue(new BigDecimal(-100), new BigDecimal(100), new BigDecimal(10));
PercentageValue value = new PercentageValue(new BigDecimal(-100), new BigDecimal(100), new BigDecimal(10), null,
null);
ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
c.start(connection, mock(ScheduledExecutorService.class), 100);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void initialize() throws MqttException {

thingHandler.initialize();
ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
assertThat(channelConfig.transformations.get(0).pattern, is(jsonPathPattern));
assertThat(channelConfig.transformationsIn.get(0).pattern, is(jsonPathPattern));
}

@SuppressWarnings("null")
Expand All @@ -118,7 +118,7 @@ public void processMessageWithJSONPath() throws Exception {
ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
channelConfig.setChannelStateUpdateListener(thingHandler);

ChannelStateTransformation transformation = channelConfig.transformations.get(0);
ChannelStateTransformation transformation = channelConfig.transformationsIn.get(0);

byte payload[] = jsonPathJSON.getBytes();
assertThat(transformation.pattern, is(jsonPathPattern));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void illegalNumberCommand() {

@Test(expected = IllegalArgumentException.class)
public void illegalPercentCommand() {
PercentageValue v = new PercentageValue(null, null, null);
PercentageValue v = new PercentageValue(null, null, null, null, null);
v.update(OnOffType.OFF);
}

Expand All @@ -95,7 +95,7 @@ public void illegalOnOffCommand() {

@Test(expected = IllegalArgumentException.class)
public void illegalPercentUpdate() {
PercentageValue v = new PercentageValue(null, null, null);
PercentageValue v = new PercentageValue(null, null, null, null, null);
v.update(new DecimalType(101.0));
}

Expand Down Expand Up @@ -180,16 +180,35 @@ public void rollershutterUpdate() {

@Test
public void percentCalc() {
PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0));
PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
null);
v.update(new DecimalType(110.0));
assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
assertThat(v.getMQTTpublishValue(), is("110"));
v.update(new DecimalType(10.0));
assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
assertThat(v.getMQTTpublishValue(), is("10"));

v.update(OnOffType.ON);
assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
v.update(OnOffType.OFF);
assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
}

@Test
public void percentCustomOnOff() {
PercentageValue v = new PercentageValue(new BigDecimal(0.0), new BigDecimal(100.0), new BigDecimal(1.0), "on",
"off");
v.update(new StringType("on"));
assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
v.update(new StringType("off"));
assertThat((PercentType) v.getChannelState(), is(new PercentType(0)));
}

@Test
public void decimalCalc() {
PercentageValue v = new PercentageValue(new BigDecimal(0.1), new BigDecimal(1.0), new BigDecimal(0.1));
PercentageValue v = new PercentageValue(new BigDecimal(0.1), new BigDecimal(1.0), new BigDecimal(0.1), null,
null);
v.update(new DecimalType(1.0));
assertThat((PercentType) v.getChannelState(), is(new PercentType(100)));
v.update(new DecimalType(0.1));
Expand All @@ -200,7 +219,8 @@ public void decimalCalc() {

@Test(expected = IllegalArgumentException.class)
public void percentCalcInvalid() {
PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0));
PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
null);
v.update(new DecimalType(9.0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.StateDescription;
import org.eclipse.smarthome.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Implements a number value.
*
* If min / max limits are set, values below / above are (almost) silently ignored.
*
* <p>
* Accepts user updates and MQTT state updates from a DecimalType, IncreaseDecreaseType and UpDownType.
* </p>
Expand All @@ -38,6 +42,7 @@
*/
@NonNullByDefault
public class NumberValue extends Value {
private final Logger logger = LoggerFactory.getLogger(NumberValue.class);
private final @Nullable BigDecimal min;
private final @Nullable BigDecimal max;
private final BigDecimal step;
Expand All @@ -50,19 +55,44 @@ public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable
this.step = step == null ? new BigDecimal(1.0) : step;
}

protected boolean checkConditions(BigDecimal newValue, DecimalType oldvalue) {
if (min != null && newValue.compareTo(min) == -1) {
logger.trace("Number not accepted as it is below the configured minimum");
return false;
}
if (max != null && newValue.compareTo(max) == 1) {
logger.trace("Number not accepted as it is above the configured maximum");
return false;
}

return true;
}

@Override
public void update(Command command) throws IllegalArgumentException {
DecimalType oldvalue = (state == UnDefType.UNDEF) ? new DecimalType() : (DecimalType) state;
BigDecimal newValue;
if (command instanceof DecimalType) {
if (!checkConditions(((DecimalType) command).toBigDecimal(), oldvalue)) {
return;
}
state = (DecimalType) command;
} else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
state = new DecimalType(oldvalue.toBigDecimal().add(step));
newValue = oldvalue.toBigDecimal().add(step);
} else {
state = new DecimalType(oldvalue.toBigDecimal().subtract(step));
newValue = oldvalue.toBigDecimal().subtract(step);
}
if (!checkConditions(newValue, oldvalue)) {
return;
}
state = new DecimalType(newValue);
} else {
state = DecimalType.valueOf(command.toString());
newValue = new BigDecimal(command.toString());
if (!checkConditions(newValue, oldvalue)) {
return;
}
state = new DecimalType(newValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.smarthome.core.library.CoreItemFactory;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.IncreaseDecreaseType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.library.types.UpDownType;
Expand Down Expand Up @@ -52,9 +53,9 @@ public class PercentageValue extends Value {

public PercentageValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
@Nullable String onValue, @Nullable String offValue) {
super(CoreItemFactory.DIMMER,
Stream.of(DecimalType.class, IncreaseDecreaseType.class, UpDownType.class, StringType.class)
.collect(Collectors.toList()));
super(CoreItemFactory.DIMMER, Stream
.of(DecimalType.class, IncreaseDecreaseType.class, OnOffType.class, UpDownType.class, StringType.class)
.collect(Collectors.toList()));
this.onValue = onValue;
this.offValue = offValue;
this.min = min == null ? 0.0 : min.doubleValue();
Expand Down Expand Up @@ -82,6 +83,8 @@ public void update(Command command) throws IllegalArgumentException {
double v = oldvalue.doubleValue() - step;
state = new PercentType(new BigDecimal(v >= min ? v : min));
}
} else if (command instanceof OnOffType) {
state = ((OnOffType) command) == OnOffType.ON ? PercentType.HUNDRED : PercentType.ZERO;
} else if (command instanceof UpDownType) {
if (((UpDownType) command) == UpDownType.UP) {
final double v = oldvalue.doubleValue() + step;
Expand Down

0 comments on commit 4e12a8b

Please sign in to comment.