Browse Source

updated sql upgrade script & fixed rule chain templates & fixed upgrade in rule node & minor changes to the tests

pull/9652/head
ShvaykaD 3 years ago
parent
commit
af52ea282c
  1. 2
      application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json
  2. 2
      application/src/main/data/json/tenant/device_profile/rule_chain_template.json
  3. 2
      application/src/main/data/json/tenant/rule_chains/root_rule_chain.json
  4. 3
      application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java
  5. 12
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNode.java
  6. 11
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNodeTest.java

2
application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json

@ -48,7 +48,7 @@
"type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode",
"name": "Save Client Attributes",
"debugMode": false,
"configurationVersion": 1,
"configurationVersion": 2,
"configuration": {
"scope": "CLIENT_SCOPE",
"notifyDevice": false,

2
application/src/main/data/json/tenant/device_profile/rule_chain_template.json

@ -32,7 +32,7 @@
"type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode",
"name": "Save Client Attributes",
"debugMode": false,
"configurationVersion": 1,
"configurationVersion": 2,
"configuration": {
"scope": "CLIENT_SCOPE",
"notifyDevice": false,

2
application/src/main/data/json/tenant/rule_chains/root_rule_chain.json

@ -31,7 +31,7 @@
"type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode",
"name": "Save Client Attributes",
"debugMode": false,
"configurationVersion": 1,
"configurationVersion": 2,
"configuration": {
"scope": "CLIENT_SCOPE",
"notifyDevice": false,

3
application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java

@ -801,11 +801,12 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService
"configuration = (configuration::jsonb || jsonb_build_object(" +
"'notifyDevice', CASE WHEN configuration::jsonb ->> 'notifyDevice' = 'false' THEN false ELSE true END, " +
"'sendAttributesUpdatedNotification', CASE WHEN configuration::jsonb ->> 'sendAttributesUpdatedNotification' = 'true' THEN true ELSE false END, " +
"'updateAttributesOnlyOnValueChange', CASE WHEN configuration::jsonb ->> 'updateAttributesOnlyOnValueChange' = 'true' THEN true ELSE false END" +
"'updateAttributesOnlyOnValueChange', CASE WHEN configuration::jsonb ->> 'updateAttributesOnlyOnValueChange' = 'false' THEN false ELSE true END" +
")::jsonb)::varchar, " +
"configuration_version = 2 " +
"WHERE type = 'org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode' AND configuration_version = 1;");
} catch (Exception e) {
log.warn("Failed to execute update script for save attributes rule nodes due to: ", e);
}
conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3006002;");
log.info("Schema updated to version 3.6.2.");

12
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNode.java

@ -173,11 +173,11 @@ public class TbMsgAttributesNode implements TbNode {
}
case 1:
// update notifyDevice. set true if null or property doesn't exist for backward-compatibility.
hasChanges = fixEscapedBooleanConfigParameters(oldConfiguration, NOTIFY_DEVICE_KEY, hasChanges, true);
hasChanges = fixEscapedBooleanConfigParameter(oldConfiguration, NOTIFY_DEVICE_KEY, hasChanges, true);
// update sendAttributesUpdatedNotification.
hasChanges = fixEscapedBooleanConfigParameters(oldConfiguration, SEND_ATTRIBUTES_UPDATED_NOTIFICATION_KEY, hasChanges, false);
hasChanges = fixEscapedBooleanConfigParameter(oldConfiguration, SEND_ATTRIBUTES_UPDATED_NOTIFICATION_KEY, hasChanges, false);
// update updateAttributesOnlyOnValueChange.
hasChanges = fixEscapedBooleanConfigParameters(oldConfiguration, UPDATE_ATTRIBUTES_ONLY_ON_VALUE_CHANGE_KEY, hasChanges, false);
hasChanges = fixEscapedBooleanConfigParameter(oldConfiguration, UPDATE_ATTRIBUTES_ONLY_ON_VALUE_CHANGE_KEY, hasChanges, true);
break;
default:
break;
@ -185,18 +185,18 @@ public class TbMsgAttributesNode implements TbNode {
return new TbPair<>(hasChanges, oldConfiguration);
}
private static boolean fixEscapedBooleanConfigParameters(JsonNode oldConfiguration, String boolKey, boolean hasChanges, boolean defaultValue) {
private boolean fixEscapedBooleanConfigParameter(JsonNode oldConfiguration, String boolKey, boolean hasChanges, boolean valueIfNull) {
if (oldConfiguration.hasNonNull(boolKey)) {
var value = oldConfiguration.get(boolKey);
if (value.isTextual()) {
hasChanges = true;
((ObjectNode) oldConfiguration)
.put(boolKey, value.asBoolean(defaultValue));
.put(boolKey, value.asBoolean(valueIfNull));
}
} else {
hasChanges = true;
((ObjectNode) oldConfiguration)
.put(boolKey, defaultValue);
.put(boolKey, valueIfNull);
}
return hasChanges;
}

11
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNodeTest.java

@ -137,15 +137,16 @@ class TbMsgAttributesNodeTest {
private static Stream<Arguments> provideNotifyDeviceMdValue() {
return Stream.of(
Arguments.of(null, true),
Arguments.of(true, true),
Arguments.of(false, false)
Arguments.of("null", false),
Arguments.of("true", true),
Arguments.of("false", false)
);
}
// Notify device backward-compatibility test
@ParameterizedTest
@MethodSource("provideNotifyDeviceMdValue")
void testNotifyDeviceArgumentForSaveAndNotify(Boolean mdValue, boolean expectedArgumentValue) throws TbNodeException {
void givenNotifyDeviceMdValue_whenSaveAndNotify_thenVerifyExpectedArgumentForNotifyDeviceInSaveAndNotifyMethod(String mdValue, boolean expectedArgumentValue) throws TbNodeException {
var ctxMock = mock(TbContext.class);
var telemetryServiceMock = mock(RuleEngineTelemetryService.class);
TbMsgAttributesNode node = spy(TbMsgAttributesNode.class);
@ -164,7 +165,7 @@ class TbMsgAttributesNodeTest {
TbMsgMetaData md = new TbMsgMetaData();
if (mdValue != null) {
md.putValue(NOTIFY_DEVICE_METADATA_KEY, mdValue.toString());
md.putValue(NOTIFY_DEVICE_METADATA_KEY, mdValue);
}
// dummy list with one ts kv to pass the empty list check.
var testTbMsg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, ORIGINATOR_ID, md, TbMsg.EMPTY_STRING);
@ -264,7 +265,7 @@ class TbMsgAttributesNodeTest {
ObjectNode upgradedConfig = (ObjectNode) upgradeResult.getSecond();
assertThat(upgradedConfig.get(NOTIFY_DEVICE_KEY).asBoolean()).as("pre condition has [false] for key " + NOTIFY_DEVICE_KEY).isTrue();
assertThat(upgradedConfig.get(SEND_ATTRIBUTES_UPDATED_NOTIFICATION_KEY).asBoolean()).as("pre condition has [true] for key " + SEND_ATTRIBUTES_UPDATED_NOTIFICATION_KEY).isFalse();
assertThat(upgradedConfig.get(UPDATE_ATTRIBUTES_ONLY_ON_VALUE_CHANGE_KEY).asBoolean()).as("pre condition has [true] for key " + UPDATE_ATTRIBUTES_ONLY_ON_VALUE_CHANGE_KEY).isFalse();
assertThat(upgradedConfig.get(UPDATE_ATTRIBUTES_ONLY_ON_VALUE_CHANGE_KEY).asBoolean()).as("pre condition has [false] for key " + UPDATE_ATTRIBUTES_ONLY_ON_VALUE_CHANGE_KEY).isTrue();
}
@Test

Loading…
Cancel
Save