From af52ea282c9f08066f54522829cfe320513518b4 Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Mon, 20 Nov 2023 18:15:25 +0200 Subject: [PATCH] updated sql upgrade script & fixed rule chain templates & fixed upgrade in rule node & minor changes to the tests --- .../json/edge/rule_chains/edge_root_rule_chain.json | 2 +- .../tenant/device_profile/rule_chain_template.json | 2 +- .../json/tenant/rule_chains/root_rule_chain.json | 2 +- .../service/install/SqlDatabaseUpgradeService.java | 3 ++- .../rule/engine/telemetry/TbMsgAttributesNode.java | 12 ++++++------ .../engine/telemetry/TbMsgAttributesNodeTest.java | 11 ++++++----- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json b/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json index 99625b3188..6b7603c026 100644 --- a/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json +++ b/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, diff --git a/application/src/main/data/json/tenant/device_profile/rule_chain_template.json b/application/src/main/data/json/tenant/device_profile/rule_chain_template.json index a6fb16af3d..0256a2ccf2 100644 --- a/application/src/main/data/json/tenant/device_profile/rule_chain_template.json +++ b/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, diff --git a/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json b/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json index a96b1ba01f..0b70d087e7 100644 --- a/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json +++ b/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, diff --git a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java index 5b1904953b..3527822c4a 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/SqlDatabaseUpgradeService.java +++ b/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."); diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNode.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNode.java index 59ff53557b..66fb0fbde9 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNode.java +++ b/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; } diff --git a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNodeTest.java b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNodeTest.java index e3949e1581..477f50128a 100644 --- a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNodeTest.java +++ b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/telemetry/TbMsgAttributesNodeTest.java @@ -137,15 +137,16 @@ class TbMsgAttributesNodeTest { private static Stream 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