Merge pull request #13868 from irynamatveieva/fix/cf-arguments

Fixed CF arguments casting Long to Integer to avoid truncation
This commit is contained in:
Viacheslav Klimov 2025-08-14 13:04:23 +03:00 committed by GitHub
commit 6538c357f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 4 deletions

View File

@ -101,6 +101,11 @@ public class SingleValueArgumentEntry implements ArgumentEntry {
} catch (Exception e) {
}
}
if (value instanceof Long longValue) {
if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) {
value = longValue.intValue();
}
}
return new TbelCfSingleValueArg(ts, value);
}

View File

@ -20,7 +20,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.script.api.tbel.DefaultTbelInvokeService;
import org.thingsboard.script.api.tbel.TbelInvokeService;
@ -60,7 +60,7 @@ public class ScriptCalculatedFieldStateTest {
private final DeviceId DEVICE_ID = new DeviceId(UUID.fromString("5512071d-5abc-411d-a907-4cdb6539c2eb"));
private final AssetId ASSET_ID = new AssetId(UUID.fromString("5bc010ae-bcfd-46c8-98b9-8ee8c8955a76"));
private final SingleValueArgumentEntry assetHumidityArgEntry = new SingleValueArgumentEntry(System.currentTimeMillis() - 10, new DoubleDataEntry("assetHumidity", 43.0), 122L);
private final SingleValueArgumentEntry assetHumidityArgEntry = new SingleValueArgumentEntry(System.currentTimeMillis() - 10, new DoubleDataEntry("assetHumidity", 86.0), 122L);
private final TsRollingArgumentEntry deviceTemperatureArgEntry = createRollingArgEntry();
private final long ts = System.currentTimeMillis();
@ -71,7 +71,7 @@ public class ScriptCalculatedFieldStateTest {
@Autowired
private TbelInvokeService tbelInvokeService;
@MockBean
@MockitoBean
private ApiLimitService apiLimitService;
@BeforeEach
@ -133,6 +133,22 @@ public class ScriptCalculatedFieldStateTest {
assertThat(result.getResult()).isEqualTo(JacksonUtil.valueToTree(Map.of("maxDeviceTemperature", 17.0, "assetHumidity", 43.0)));
}
@Test
void testPerformCalculationWithLongEntry() throws ExecutionException, InterruptedException {
state.arguments = new HashMap<>(Map.of(
"deviceTemperature", deviceTemperatureArgEntry,
"assetHumidity", new SingleValueArgumentEntry(System.currentTimeMillis() - 10, new LongDataEntry("a", 45L), 10L)
));
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("maxDeviceTemperature", 17.0, "assetHumidity", 22.5)));
}
@Test
void testIsReadyWhenNotAllArgPresent() {
assertThat(state.isReady()).isFalse();
@ -193,7 +209,7 @@ public class ScriptCalculatedFieldStateTest {
config.setArguments(Map.of("deviceTemperature", argument1, "assetHumidity", argument2));
config.setExpression("return {\"maxDeviceTemperature\": deviceTemperature.max(), \"assetHumidity\": assetHumidity}");
config.setExpression("return {\"maxDeviceTemperature\": deviceTemperature.max(), \"assetHumidity\": assetHumidity / 2 }");
Output output = new Output();
output.setType(OutputType.ATTRIBUTES);