TbLogNode.java standard implementation without calling JS when JsScript equals default config
This commit is contained in:
		
							parent
							
								
									5c6a5bcb6a
								
							
						
					
					
						commit
						ca8df0130f
					
				@ -20,6 +20,7 @@ import com.google.common.util.concurrent.Futures;
 | 
			
		||||
import com.google.common.util.concurrent.MoreExecutors;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.checkerframework.checker.nullness.qual.Nullable;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleNode;
 | 
			
		||||
import org.thingsboard.rule.engine.api.ScriptEngine;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbContext;
 | 
			
		||||
@ -30,8 +31,6 @@ import org.thingsboard.rule.engine.api.util.TbNodeUtils;
 | 
			
		||||
import org.thingsboard.server.common.data.plugin.ComponentType;
 | 
			
		||||
import org.thingsboard.server.common.msg.TbMsg;
 | 
			
		||||
 | 
			
		||||
import static org.thingsboard.common.util.DonAsynchron.withCallback;
 | 
			
		||||
 | 
			
		||||
@Slf4j
 | 
			
		||||
@RuleNode(
 | 
			
		||||
        type = ComponentType.ACTION,
 | 
			
		||||
@ -49,15 +48,22 @@ public class TbLogNode implements TbNode {
 | 
			
		||||
 | 
			
		||||
    private TbLogNodeConfiguration config;
 | 
			
		||||
    private ScriptEngine jsEngine;
 | 
			
		||||
    private boolean standard;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
 | 
			
		||||
        this.config = TbNodeUtils.convert(configuration, TbLogNodeConfiguration.class);
 | 
			
		||||
        this.jsEngine = ctx.createJsScriptEngine(config.getJsScript());
 | 
			
		||||
        this.standard = new TbLogNodeConfiguration().defaultConfiguration().getJsScript().equals(config.getJsScript());
 | 
			
		||||
        this.jsEngine = this.standard ? null : ctx.createJsScriptEngine(config.getJsScript());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onMsg(TbContext ctx, TbMsg msg) {
 | 
			
		||||
        if (standard) {
 | 
			
		||||
            logStandard(ctx, msg);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ctx.logJsEvalRequest();
 | 
			
		||||
        Futures.addCallback(jsEngine.executeToStringAsync(msg), new FutureCallback<String>() {
 | 
			
		||||
            @Override
 | 
			
		||||
@ -75,6 +81,17 @@ public class TbLogNode implements TbNode {
 | 
			
		||||
        }, MoreExecutors.directExecutor()); //usually js responses runs on js callback executor
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void logStandard(TbContext ctx, TbMsg msg) {
 | 
			
		||||
        log.info(toLogMessage(msg));
 | 
			
		||||
        ctx.tellSuccess(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    String toLogMessage(TbMsg msg) {
 | 
			
		||||
        return "\n" +
 | 
			
		||||
                "Incoming message:\n" + msg.getData() + "\n" +
 | 
			
		||||
                "Incoming metadata:\n" + JacksonUtil.toString(msg.getMetaData().getData());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void destroy() {
 | 
			
		||||
        if (jsEngine != null) {
 | 
			
		||||
 | 
			
		||||
@ -26,7 +26,7 @@ public class TbLogNodeConfiguration implements NodeConfiguration {
 | 
			
		||||
    @Override
 | 
			
		||||
    public TbLogNodeConfiguration defaultConfiguration() {
 | 
			
		||||
        TbLogNodeConfiguration configuration = new TbLogNodeConfiguration();
 | 
			
		||||
        configuration.setJsScript("return 'Incoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);");
 | 
			
		||||
        configuration.setJsScript("return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);");
 | 
			
		||||
        return configuration;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,80 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2022 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.rule.engine.action;
 | 
			
		||||
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.msg.TbMsg;
 | 
			
		||||
import org.thingsboard.server.common.msg.TbMsgMetaData;
 | 
			
		||||
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThat;
 | 
			
		||||
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class TbLogNodeTest {
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void givenMsg_whenToLog_thenReturnString() {
 | 
			
		||||
        TbLogNode node = new TbLogNode();
 | 
			
		||||
        String data = "{\"key\": \"value\"}";
 | 
			
		||||
        TbMsgMetaData metaData = new TbMsgMetaData(Map.of("mdKey1", "mdValue1", "mdKey2", "23"));
 | 
			
		||||
        TbMsg msg = TbMsg.newMsg("POST_TELEMETRY", TenantId.SYS_TENANT_ID, metaData, data);
 | 
			
		||||
 | 
			
		||||
        String logMessage = node.toLogMessage(msg);
 | 
			
		||||
        log.info(logMessage);
 | 
			
		||||
 | 
			
		||||
        assertThat(logMessage).isEqualTo("\n" +
 | 
			
		||||
                "Incoming message:\n" +
 | 
			
		||||
                "{\"key\": \"value\"}\n" +
 | 
			
		||||
                "Incoming metadata:\n" +
 | 
			
		||||
                "{\"mdKey1\":\"mdValue1\",\"mdKey2\":\"23\"}");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void givenEmptyDataMsg_whenToLog_thenReturnString() {
 | 
			
		||||
        TbLogNode node = new TbLogNode();
 | 
			
		||||
        TbMsgMetaData metaData = new TbMsgMetaData(Collections.emptyMap());
 | 
			
		||||
        TbMsg msg = TbMsg.newMsg("POST_TELEMETRY", TenantId.SYS_TENANT_ID, metaData, "");
 | 
			
		||||
 | 
			
		||||
        String logMessage = node.toLogMessage(msg);
 | 
			
		||||
        log.info(logMessage);
 | 
			
		||||
 | 
			
		||||
        assertThat(logMessage).isEqualTo("\n" +
 | 
			
		||||
                "Incoming message:\n" +
 | 
			
		||||
                "\n" +
 | 
			
		||||
                "Incoming metadata:\n" +
 | 
			
		||||
                "{}");
 | 
			
		||||
    }
 | 
			
		||||
    @Test
 | 
			
		||||
    void givenNullDataMsg_whenToLog_thenReturnString() {
 | 
			
		||||
        TbLogNode node = new TbLogNode();
 | 
			
		||||
        TbMsgMetaData metaData = new TbMsgMetaData(Collections.emptyMap());
 | 
			
		||||
        TbMsg msg = TbMsg.newMsg("POST_TELEMETRY", TenantId.SYS_TENANT_ID, metaData, null);
 | 
			
		||||
 | 
			
		||||
        String logMessage = node.toLogMessage(msg);
 | 
			
		||||
        log.info(logMessage);
 | 
			
		||||
 | 
			
		||||
        assertThat(logMessage).isEqualTo("\n" +
 | 
			
		||||
                "Incoming message:\n" +
 | 
			
		||||
                "null\n" +
 | 
			
		||||
                "Incoming metadata:\n" +
 | 
			
		||||
                "{}");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user