Node modification

This commit is contained in:
Yuriy Lytvynchuk 2022-08-08 17:04:10 +03:00
parent 352581a199
commit ef28b19f80
3 changed files with 12 additions and 36 deletions

View File

@ -16,14 +16,12 @@
package org.thingsboard.rule.engine.telemetry; package org.thingsboard.rule.engine.telemetry;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.thingsboard.rule.engine.api.RuleNode; import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNode; import org.thingsboard.rule.engine.api.TbNode;
import org.thingsboard.rule.engine.api.TbNodeConfiguration; import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException; import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.api.util.TbNodeUtils; import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.plugin.ComponentType; import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.TbMsg;
@ -38,9 +36,7 @@ import java.util.stream.Collectors;
name = "delete attributes", name = "delete attributes",
configClazz = TbMsgDeleteAttributesConfiguration.class, configClazz = TbMsgDeleteAttributesConfiguration.class,
nodeDescription = "Delete attributes for Message Originator.", nodeDescription = "Delete attributes for Message Originator.",
nodeDetails = "Allowed scope parameter values: <b>SERVER/CLIENT/SHARED</b>. If no attributes are selected - " + nodeDetails = "Allowed scope parameter values: <b>SERVER/CLIENT/SHARED</b>. Will try to remove attributes by keys from the list",
"message send via <b>Failure</b> chain. If selected attributes successfully deleted - message send via " +
"<b>Success</b> chain, otherwise <b>Failure</b> chain will be used.",
uiResources = {"static/rulenode/rulenode-core-config.js"}, uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbActionNodeDeleteAttributesConfig", configDirective = "tbActionNodeDeleteAttributesConfig",
icon = "remove_circle" icon = "remove_circle"
@ -52,29 +48,19 @@ public class TbMsgDeleteAttributes implements TbNode {
@Override @Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, TbMsgDeleteAttributesConfiguration.class); this.config = TbNodeUtils.convert(configuration, TbMsgDeleteAttributesConfiguration.class);
if (CollectionUtils.isEmpty(config.getKeys())) {
throw new IllegalArgumentException("Attribute keys list is empty!");
}
} }
@Override @Override
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException { public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
List<String> keysPatterns = config.getKeys(); List<String> keysToDelete = config.getKeys().stream()
String scope = TbNodeUtils.processPattern(config.getScope(), msg); .map(keyPattern -> TbNodeUtils.processPattern(keyPattern, msg))
if (DataConstants.SERVER_SCOPE.equals(scope) || .distinct()
DataConstants.CLIENT_SCOPE.equals(scope) || .filter(StringUtils::isNotBlank)
DataConstants.SHARED_SCOPE.equals(scope)) { .collect(Collectors.toList());
List<String> keysToDelete = keysPatterns.stream() if (keysToDelete.isEmpty()) {
.map(keyPattern -> TbNodeUtils.processPattern(keyPattern, msg)) ctx.tellFailure(msg, new RuntimeException("Selected keys patterns have invalid values!"));
.distinct()
.filter(StringUtils::isNotBlank)
.collect(Collectors.toList());
if (keysToDelete.isEmpty()) {
throw new RuntimeException("Selected keys patterns have invalid values!");
}
ctx.getTelemetryService().deleteAndNotify(ctx.getTenantId(), msg.getOriginator(), scope, keysToDelete, new TelemetryNodeCallback(ctx, msg));
} else { } else {
ctx.tellFailure(msg, new IllegalArgumentException("Unsupported attributes scope '" + scope + "'! Only 'SERVER_SCOPE', 'CLIENT_SCOPE' or 'SHARED_SCOPE' are allowed!")); ctx.getTelemetryService().deleteAndNotify(ctx.getTenantId(), msg.getOriginator(), config.getScope(), keysToDelete, new TelemetryNodeCallback(ctx, msg));
} }
} }

View File

@ -27,14 +27,12 @@ public class TbMsgDeleteAttributesConfiguration implements NodeConfiguration<TbM
private String scope; private String scope;
private List<String> keys; private List<String> keys;
private Boolean useScopeAsPattern;
@Override @Override
public TbMsgDeleteAttributesConfiguration defaultConfiguration() { public TbMsgDeleteAttributesConfiguration defaultConfiguration() {
TbMsgDeleteAttributesConfiguration configuration = new TbMsgDeleteAttributesConfiguration(); TbMsgDeleteAttributesConfiguration configuration = new TbMsgDeleteAttributesConfiguration();
configuration.setScope(DataConstants.SERVER_SCOPE); configuration.setScope(DataConstants.SERVER_SCOPE);
configuration.setKeys(Collections.emptyList()); configuration.setKeys(Collections.emptyList());
configuration.setUseScopeAsPattern(false);
return configuration; return configuration;
} }
} }

View File

@ -37,7 +37,6 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
@ -93,13 +92,6 @@ public class TbMsgDeleteAttributesTest {
assertThat(node.config).isEqualTo(config); assertThat(node.config).isEqualTo(config);
} }
@Test
void givenDefaultConfig_whenInit_thenFail() {
config.setKeys(Collections.emptyList());
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
assertThatThrownBy(() -> node.init(ctx, nodeConfiguration)).isInstanceOf(IllegalArgumentException.class);
}
@Test @Test
void givenDefaultConfig_whenVerify_thenOK() { void givenDefaultConfig_whenVerify_thenOK() {
TbMsgDeleteAttributesConfiguration defaultConfig = new TbMsgDeleteAttributesConfiguration().defaultConfiguration(); TbMsgDeleteAttributesConfiguration defaultConfig = new TbMsgDeleteAttributesConfiguration().defaultConfiguration();
@ -128,9 +120,9 @@ public class TbMsgDeleteAttributesTest {
@Test @Test
void giveInvalidScope_whenOnMsg_thenTellFailure() throws Exception { void giveInvalidScope_whenOnMsg_thenTellFailure() throws Exception {
final TbMsgMetaData metaData = new TbMsgMetaData(); final TbMsgMetaData metaData = new TbMsgMetaData();
final String data = "{}"; final String data = "{\"TestAttribute_1\": \"\", \"TestAttribute_2\": \"\"}";
config.setScope("INVALID_SCOPE"); config.setKeys(List.of("$[TestAttribute_1]", "$[TestAttribute_2]"));
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config)); nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
node.init(ctx, nodeConfiguration); node.init(ctx, nodeConfiguration);
@ -143,6 +135,6 @@ public class TbMsgDeleteAttributesTest {
verify(ctx, times(1)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture()); verify(ctx, times(1)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture());
assertThat(newMsgCaptor.getValue()).isSameAs(msg); assertThat(newMsgCaptor.getValue()).isSameAs(msg);
assertThat(exceptionCaptor.getValue()).isInstanceOf(IllegalArgumentException.class); assertThat(exceptionCaptor.getValue()).isInstanceOf(RuntimeException.class);
} }
} }