@ -15,6 +15,7 @@
* /
package org.thingsboard.rule.engine.profile ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import com.fasterxml.jackson.databind.node.ObjectNode ;
import com.google.common.util.concurrent.Futures ;
import com.google.common.util.concurrent.ListenableFuture ;
@ -23,7 +24,6 @@ import org.junit.runner.RunWith;
import org.mockito.Mock ;
import org.mockito.Mockito ;
import org.mockito.junit.MockitoJUnitRunner ;
import org.thingsboard.common.util.JacksonUtil ;
import org.thingsboard.rule.engine.api.RuleEngineAlarmService ;
import org.thingsboard.rule.engine.api.RuleEngineDeviceProfileCache ;
import org.thingsboard.rule.engine.api.TbContext ;
@ -90,6 +90,8 @@ import static org.mockito.Mockito.when;
@RunWith ( MockitoJUnitRunner . class )
public class TbDeviceProfileNodeTest {
private static final ObjectMapper mapper = new ObjectMapper ( ) ;
private TbDeviceProfileNode node ;
@Mock
@ -120,10 +122,10 @@ public class TbDeviceProfileNodeTest {
deviceProfile . setProfileData ( deviceProfileData ) ;
Mockito . when ( cache . get ( tenantId , deviceId ) ) . thenReturn ( deviceProfile ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 42 ) ;
TbMsg msg = TbMsg . newMsg ( "123456789" , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
verify ( ctx , Mockito . never ( ) ) . tellFailure ( Mockito . any ( ) , Mockito . any ( ) ) ;
@ -139,10 +141,10 @@ public class TbDeviceProfileNodeTest {
deviceProfile . setProfileData ( deviceProfileData ) ;
Mockito . when ( cache . get ( tenantId , deviceId ) ) . thenReturn ( deviceProfile ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 42 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
verify ( ctx , Mockito . never ( ) ) . tellFailure ( Mockito . any ( ) , Mockito . any ( ) ) ;
@ -196,10 +198,10 @@ public class TbDeviceProfileNodeTest {
TbMsg theMsg = TbMsg . newMsg ( "ALARM" , deviceId , new TbMsgMetaData ( ) , "" ) ;
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) ) . thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 42 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
verify ( ctx ) . enqueueForTellNext ( theMsg , "Alarm Created" ) ;
@ -212,7 +214,7 @@ public class TbDeviceProfileNodeTest {
TbMsg msg2 = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg2 ) ;
verify ( ctx ) . tellSuccess ( msg2 ) ;
verify ( ctx ) . enqueueForTellNext ( theMsg2 , "Alarm Updated" ) ;
@ -290,10 +292,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 21 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -377,10 +379,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 21 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -446,10 +448,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 35 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -540,10 +542,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 35 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -558,7 +560,7 @@ public class TbDeviceProfileNodeTest {
Thread . sleep ( halfOfAlarmDelay ) ;
TbMsg msg2 = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg2 ) ;
verify ( ctx ) . tellSuccess ( msg2 ) ;
@ -664,10 +666,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 150 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -682,7 +684,7 @@ public class TbDeviceProfileNodeTest {
Thread . sleep ( halfOfAlarmDelay ) ;
TbMsg msg2 = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg2 ) ;
verify ( ctx ) . tellSuccess ( msg2 ) ;
@ -773,10 +775,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 150 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -785,7 +787,7 @@ public class TbDeviceProfileNodeTest {
data . put ( "temperature" , 151 ) ;
TbMsg msg2 = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg2 ) ;
verify ( ctx ) . tellSuccess ( msg2 ) ;
@ -889,10 +891,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 150 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -901,7 +903,7 @@ public class TbDeviceProfileNodeTest {
data . put ( "temperature" , 151 ) ;
TbMsg msg2 = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg2 ) ;
verify ( ctx ) . tellSuccess ( msg2 ) ;
@ -985,10 +987,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 35 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -1003,7 +1005,7 @@ public class TbDeviceProfileNodeTest {
Thread . sleep ( halfOfAlarmDelay ) ;
TbMsg msg2 = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg2 ) ;
verify ( ctx ) . tellSuccess ( msg2 ) ;
@ -1083,10 +1085,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 35 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -1165,10 +1167,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 35 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
// Mockito.reset(ctx);
@ -1259,10 +1261,10 @@ public class TbDeviceProfileNodeTest {
TbMsg theMsg = TbMsg . newMsg ( "ALARM" , deviceId , new TbMsgMetaData ( ) , "" ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 35 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -1339,10 +1341,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 25 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -1412,10 +1414,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 40 ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -1495,10 +1497,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 150L ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -1580,10 +1582,10 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . newMsg ( Mockito . any ( ) , Mockito . anyString ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) , Mockito . anyString ( ) ) )
. thenReturn ( theMsg ) ;
ObjectNode data = JacksonUtil . new ObjectNode( ) ;
ObjectNode data = mapper . create ObjectNode( ) ;
data . put ( "temperature" , 150L ) ;
TbMsg msg = TbMsg . newMsg ( SessionMsgType . POST_TELEMETRY_REQUEST . name ( ) , deviceId , new TbMsgMetaData ( ) ,
TbMsgDataType . JSON , JacksonUtil . to String( data ) , null , null ) ;
TbMsgDataType . JSON , mapper . writeValueAs String( data ) , null , null ) ;
node . onMsg ( ctx , msg ) ;
verify ( ctx ) . tellSuccess ( msg ) ;
@ -1598,7 +1600,7 @@ public class TbDeviceProfileNodeTest {
Mockito . when ( ctx . getAlarmService ( ) ) . thenReturn ( alarmService ) ;
Mockito . when ( ctx . getDeviceService ( ) ) . thenReturn ( deviceService ) ;
Mockito . when ( ctx . getAttributesService ( ) ) . thenReturn ( attributesService ) ;
TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration ( JacksonUtil . new ObjectNode( ) ) ;
TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration ( mapper . create ObjectNode( ) ) ;
node = new TbDeviceProfileNode ( ) ;
node . init ( ctx , nodeConfiguration ) ;
}