added test
This commit is contained in:
parent
bb570add28
commit
916dc6cb6e
@ -61,7 +61,8 @@ public class SimpleCalculatedFieldState extends BaseCalculatedFieldState {
|
|||||||
double value = switch (kvEntry.getDataType()) {
|
double value = switch (kvEntry.getDataType()) {
|
||||||
case LONG -> kvEntry.getLongValue().map(Long::doubleValue).orElseThrow();
|
case LONG -> kvEntry.getLongValue().map(Long::doubleValue).orElseThrow();
|
||||||
case DOUBLE -> kvEntry.getDoubleValue().orElseThrow();
|
case DOUBLE -> kvEntry.getDoubleValue().orElseThrow();
|
||||||
case BOOLEAN, STRING, JSON -> Double.parseDouble(kvEntry.getValueAsString());
|
case BOOLEAN -> kvEntry.getBooleanValue().map(b -> b ? 1.0 : 0.0).orElseThrow();
|
||||||
|
case STRING, JSON -> Double.parseDouble(kvEntry.getValueAsString());
|
||||||
};
|
};
|
||||||
expr.setVariable(entry.getKey(), value);
|
expr.setVariable(entry.getKey(), value);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
|
|||||||
@ -34,6 +34,7 @@ import org.thingsboard.server.common.data.cf.configuration.SimpleCalculatedField
|
|||||||
import org.thingsboard.server.common.data.id.AssetId;
|
import org.thingsboard.server.common.data.id.AssetId;
|
||||||
import org.thingsboard.server.common.data.id.DeviceId;
|
import org.thingsboard.server.common.data.id.DeviceId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
|
import org.thingsboard.server.common.data.kv.BooleanDataEntry;
|
||||||
import org.thingsboard.server.common.data.kv.DoubleDataEntry;
|
import org.thingsboard.server.common.data.kv.DoubleDataEntry;
|
||||||
import org.thingsboard.server.common.data.kv.LongDataEntry;
|
import org.thingsboard.server.common.data.kv.LongDataEntry;
|
||||||
import org.thingsboard.server.common.data.kv.StringDataEntry;
|
import org.thingsboard.server.common.data.kv.StringDataEntry;
|
||||||
@ -143,7 +144,7 @@ public class SimpleCalculatedFieldStateTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testPerformCalculationWhenPassedNotNumber() {
|
void testPerformCalculationWhenPassedString() {
|
||||||
state.arguments = new HashMap<>(Map.of(
|
state.arguments = new HashMap<>(Map.of(
|
||||||
"key1", key1ArgEntry,
|
"key1", key1ArgEntry,
|
||||||
"key2", new SingleValueArgumentEntry(System.currentTimeMillis() - 9, new StringDataEntry("key2", "string"), 124L),
|
"key2", new SingleValueArgumentEntry(System.currentTimeMillis() - 9, new StringDataEntry("key2", "string"), 124L),
|
||||||
@ -155,6 +156,23 @@ public class SimpleCalculatedFieldStateTest {
|
|||||||
.hasMessage("Argument 'key2' is not a number.");
|
.hasMessage("Argument 'key2' is not a number.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPerformCalculationWhenPassedBoolean() throws ExecutionException, InterruptedException {
|
||||||
|
state.arguments = new HashMap<>(Map.of(
|
||||||
|
"key1", key1ArgEntry,
|
||||||
|
"key2", new SingleValueArgumentEntry(System.currentTimeMillis() - 9, new BooleanDataEntry("key2", true), 124L),// true is parsed as 1
|
||||||
|
"key3", key3ArgEntry
|
||||||
|
));
|
||||||
|
|
||||||
|
CalculatedFieldResult result = state.performCalculation(ctx).get();
|
||||||
|
|
||||||
|
assertThat(result).isNotNull();
|
||||||
|
Output output = getCalculatedFieldConfig().getOutput();
|
||||||
|
assertThat(result.getType()).isEqualTo(output.getType());
|
||||||
|
assertThat(result.getScope()).isEqualTo(output.getScope());
|
||||||
|
assertThat(result.getResult()).isEqualTo(JacksonUtil.valueToTree(Map.of("output", 35)));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testPerformCalculationWhenDecimalsByDefault() throws ExecutionException, InterruptedException {
|
void testPerformCalculationWhenDecimalsByDefault() throws ExecutionException, InterruptedException {
|
||||||
state.arguments = new HashMap<>(Map.of(
|
state.arguments = new HashMap<>(Map.of(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user