@ -606,6 +606,59 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes
} ) ;
}
@Test
public void testSimpleCalculatedFieldWhenCtxBecameUninitialized ( ) throws Exception {
Device testDevice = createDevice ( "Test device" , "1234567890" ) ;
CalculatedField calculatedField = new CalculatedField ( ) ;
calculatedField . setEntityId ( testDevice . getId ( ) ) ;
calculatedField . setType ( CalculatedFieldType . SIMPLE ) ;
calculatedField . setName ( "M + 1" ) ;
calculatedField . setDebugSettings ( DebugSettings . all ( ) ) ;
SimpleCalculatedFieldConfiguration config = new SimpleCalculatedFieldConfiguration ( ) ;
Argument argument = new Argument ( ) ;
ReferencedEntityKey refEntityKey = new ReferencedEntityKey ( "m" , ArgumentType . TS_LATEST , null ) ;
argument . setRefEntityKey ( refEntityKey ) ;
config . setArguments ( Map . of ( "m" , argument ) ) ;
config . setExpression ( "m + 1" ) ;
Output output = new Output ( ) ;
output . setName ( "m1" ) ;
output . setType ( OutputType . TIME_SERIES ) ;
output . setDecimalsByDefault ( 0 ) ;
config . setOutput ( output ) ;
calculatedField . setConfiguration ( config ) ;
calculatedField = doPost ( "/api/calculatedField" , calculatedField , CalculatedField . class ) ;
doPost ( "/api/plugins/telemetry/DEVICE/" + testDevice . getUuidId ( ) + "/timeseries/" + DataConstants . SERVER_SCOPE , JacksonUtil . toJsonNode ( "{\"m\":1}" ) ) ;
await ( ) . alias ( "create CF -> ctx is initialized -> perform calculation" ) . atMost ( TIMEOUT , TimeUnit . SECONDS )
. pollInterval ( POLL_INTERVAL , TimeUnit . SECONDS )
. untilAsserted ( ( ) - > {
ObjectNode m1 = getLatestTelemetry ( testDevice . getId ( ) , "m1" ) ;
assertThat ( m1 ) . isNotNull ( ) ;
assertThat ( m1 . get ( "m1" ) . get ( 0 ) . get ( "value" ) . asText ( ) ) . isEqualTo ( "2" ) ;
} ) ;
config . setExpression ( "m m" ) ;
calculatedField . setConfiguration ( config ) ;
calculatedField = doPost ( "/api/calculatedField" , calculatedField , CalculatedField . class ) ;
doPost ( "/api/plugins/telemetry/DEVICE/" + testDevice . getUuidId ( ) + "/timeseries/" + DataConstants . SERVER_SCOPE , JacksonUtil . toJsonNode ( "{\"m\":2}" ) ) ;
await ( ) . alias ( "update CF -> ctx is not initialized -> no calculation performed" ) . atMost ( TIMEOUT , TimeUnit . SECONDS )
. pollInterval ( POLL_INTERVAL , TimeUnit . SECONDS )
. untilAsserted ( ( ) - > {
ObjectNode m1 = getLatestTelemetry ( testDevice . getId ( ) , "m1" ) ;
assertThat ( m1 ) . isNotNull ( ) ;
assertThat ( m1 . get ( "m1" ) . get ( 0 ) . get ( "value" ) . asText ( ) ) . isEqualTo ( "2" ) ;
} ) ;
}
private ObjectNode getLatestTelemetry ( EntityId entityId , String . . . keys ) throws Exception {
return doGetAsync ( "/api/plugins/telemetry/" + entityId . getEntityType ( ) + "/" + entityId . getId ( ) + "/values/timeseries?keys=" + String . join ( "," , keys ) , ObjectNode . class ) ;
}