tbel_test_docsIo: add Abstract class
This commit is contained in:
parent
85b0497137
commit
a18105ca59
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Copyright © 2016-2024 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.server.service.script;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.script.api.ScriptType;
|
||||
import org.thingsboard.script.api.tbel.TbelInvokeService;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.controller.AbstractControllerTest;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static org.thingsboard.server.common.data.msg.TbMsgType.POST_TELEMETRY_REQUEST;
|
||||
|
||||
public abstract class AbstractTbelInvokeTest extends AbstractControllerTest {
|
||||
|
||||
@Autowired
|
||||
protected TbelInvokeService invokeService;
|
||||
|
||||
protected UUID evalScript(String script) throws ExecutionException, InterruptedException {
|
||||
return invokeService.eval(TenantId.SYS_TENANT_ID, ScriptType.RULE_NODE_SCRIPT, script, "msg", "metadata", "msgType").get();
|
||||
}
|
||||
|
||||
protected String invokeScriptResultString(UUID scriptId, String str) throws ExecutionException, InterruptedException {
|
||||
var msg = JacksonUtil.fromString(str, Map.class);
|
||||
return invokeScript(scriptId, str).toString();
|
||||
}
|
||||
protected Object invokeScript(UUID scriptId, String str) throws ExecutionException, InterruptedException {
|
||||
var msg = JacksonUtil.fromString(str, Map.class);
|
||||
return invokeService.invokeScript(TenantId.SYS_TENANT_ID, null, scriptId, msg, "{}", POST_TELEMETRY_REQUEST.name()).get();
|
||||
}
|
||||
}
|
||||
@ -16,12 +16,6 @@
|
||||
package org.thingsboard.server.service.script;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.script.api.ScriptType;
|
||||
import org.thingsboard.script.api.tbel.TbelInvokeService;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.controller.AbstractControllerTest;
|
||||
import org.thingsboard.server.dao.service.DaoSqlTest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -30,18 +24,13 @@ import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.thingsboard.server.common.data.msg.TbMsgType.POST_TELEMETRY_REQUEST;
|
||||
|
||||
@DaoSqlTest
|
||||
class TbelInvokeDocsIoTest extends AbstractControllerTest {
|
||||
|
||||
@Autowired
|
||||
private TbelInvokeService invokeService;
|
||||
class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
|
||||
|
||||
private String decoderStr;
|
||||
private String msgStr;
|
||||
@ -635,16 +624,6 @@ class TbelInvokeDocsIoTest extends AbstractControllerTest {
|
||||
Object actual = invokeScript(evalScript(decoderStr), msgStr);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
private UUID evalScript(String script) throws ExecutionException, InterruptedException {
|
||||
return invokeService.eval(TenantId.SYS_TENANT_ID, ScriptType.RULE_NODE_SCRIPT, script, "msg", "metadata", "msgType").get();
|
||||
}
|
||||
|
||||
private Object invokeScript(UUID scriptId, String str) throws ExecutionException, InterruptedException {
|
||||
var msg = JacksonUtil.fromString(str, Map.class);
|
||||
return invokeService.invokeScript(TenantId.SYS_TENANT_ID, null, scriptId, msg, "{}", POST_TELEMETRY_REQUEST.name()).get();
|
||||
}
|
||||
|
||||
private List splice(List oldList, int start, int deleteCount, Object... values) {
|
||||
start = initStartIndex(oldList, start);
|
||||
deleteCount = deleteCount < 0 ? 0 : Math.min(deleteCount, (oldList.size() - start));
|
||||
|
||||
@ -20,15 +20,10 @@ import com.github.benmanes.caffeine.cache.Cache;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.script.api.ScriptType;
|
||||
import org.thingsboard.script.api.tbel.TbelInvokeService;
|
||||
import org.thingsboard.script.api.tbel.TbelScript;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.controller.AbstractControllerTest;
|
||||
import org.thingsboard.server.dao.service.DaoSqlTest;
|
||||
|
||||
import java.io.Serializable;
|
||||
@ -42,7 +37,6 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.thingsboard.server.common.data.msg.TbMsgType.POST_TELEMETRY_REQUEST;
|
||||
|
||||
@DaoSqlTest
|
||||
@TestPropertySource(properties = {
|
||||
@ -52,10 +46,7 @@ import static org.thingsboard.server.common.data.msg.TbMsgType.POST_TELEMETRY_RE
|
||||
"tbel.max_errors=2",
|
||||
"tbel.compiled_scripts_cache_size=100"
|
||||
})
|
||||
class TbelInvokeServiceTest extends AbstractControllerTest {
|
||||
|
||||
@Autowired
|
||||
private TbelInvokeService invokeService;
|
||||
class TbelInvokeServiceTest extends AbstractTbelInvokeTest {
|
||||
|
||||
@Value("${tbel.max_errors}")
|
||||
private int maxJsErrors;
|
||||
@ -69,14 +60,14 @@ class TbelInvokeServiceTest extends AbstractControllerTest {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
msg.put("temperature", i);
|
||||
boolean expected = i > 20;
|
||||
boolean result = Boolean.parseBoolean(invokeScript(scriptId, JacksonUtil.toString(msg)));
|
||||
boolean result = Boolean.parseBoolean(invokeScriptResultString(scriptId, JacksonUtil.toString(msg)));
|
||||
Assert.assertEquals(expected, result);
|
||||
}
|
||||
long startTs = System.currentTimeMillis();
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
msg.put("temperature", i);
|
||||
boolean expected = i > 20;
|
||||
boolean result = Boolean.parseBoolean(invokeScript(scriptId, JacksonUtil.toString(msg)));
|
||||
boolean result = Boolean.parseBoolean(invokeScriptResultString(scriptId, JacksonUtil.toString(msg)));
|
||||
Assert.assertEquals(expected, result);
|
||||
}
|
||||
long duration = System.currentTimeMillis() - startTs;
|
||||
@ -101,7 +92,7 @@ class TbelInvokeServiceTest extends AbstractControllerTest {
|
||||
|
||||
for (int i = 0; i < maxJsErrors; i++) {
|
||||
assertThatThrownBy(() -> {
|
||||
invokeScript(scriptId, hugeMsg);
|
||||
invokeScriptResultString(scriptId, hugeMsg);
|
||||
}).hasMessageContaining("input arguments exceed maximum");
|
||||
}
|
||||
assertThatScriptIsBlocked(scriptId);
|
||||
@ -114,7 +105,7 @@ class TbelInvokeServiceTest extends AbstractControllerTest {
|
||||
|
||||
for (int i = 0; i < maxJsErrors; i++) {
|
||||
assertThatThrownBy(() -> {
|
||||
invokeScript(scriptId, "{}");
|
||||
invokeScriptResultString(scriptId, "{}");
|
||||
}).hasMessageContaining("result exceeds maximum allowed size");
|
||||
}
|
||||
assertThatScriptIsBlocked(scriptId);
|
||||
@ -182,7 +173,7 @@ class TbelInvokeServiceTest extends AbstractControllerTest {
|
||||
scriptsIds.add(scriptId);
|
||||
|
||||
for (int j = 0; j < i; j++) {
|
||||
invokeScript(scriptId, "{ \"temperature\": 12 }"); // so that scriptsIds is ordered by number of invocations
|
||||
invokeScriptResultString(scriptId, "{ \"temperature\": 12 }"); // so that scriptsIds is ordered by number of invocations
|
||||
}
|
||||
}
|
||||
|
||||
@ -201,23 +192,13 @@ class TbelInvokeServiceTest extends AbstractControllerTest {
|
||||
|
||||
UUID scriptRemovedFromCache = scriptsIds.get(0);
|
||||
assertThat(compiledScriptsCache.getIfPresent(scriptIdToHash.get(scriptRemovedFromCache))).isNull();
|
||||
invokeScript(scriptRemovedFromCache, "{ \"temperature\": 12 }");
|
||||
invokeScriptResultString(scriptRemovedFromCache, "{ \"temperature\": 12 }");
|
||||
assertThat(compiledScriptsCache.getIfPresent(scriptIdToHash.get(scriptRemovedFromCache))).isNotNull();
|
||||
}
|
||||
|
||||
private void assertThatScriptIsBlocked(UUID scriptId) {
|
||||
assertThatThrownBy(() -> {
|
||||
invokeScript(scriptId, "{}");
|
||||
invokeScriptResultString(scriptId, "{}");
|
||||
}).hasMessageContaining("invocation is blocked due to maximum error");
|
||||
}
|
||||
|
||||
private UUID evalScript(String script) throws ExecutionException, InterruptedException {
|
||||
return invokeService.eval(TenantId.SYS_TENANT_ID, ScriptType.RULE_NODE_SCRIPT, script, "msg", "metadata", "msgType").get();
|
||||
}
|
||||
|
||||
private String invokeScript(UUID scriptId, String str) throws ExecutionException, InterruptedException {
|
||||
var msg = JacksonUtil.fromString(str, Map.class);
|
||||
return invokeService.invokeScript(TenantId.SYS_TENANT_ID, null, scriptId, msg, "{}", POST_TELEMETRY_REQUEST.name()).get().toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user