|
|
@ -20,15 +20,10 @@ import com.github.benmanes.caffeine.cache.Cache; |
|
|
import org.junit.Assert; |
|
|
import org.junit.Assert; |
|
|
import org.junit.Ignore; |
|
|
import org.junit.Ignore; |
|
|
import org.junit.jupiter.api.Test; |
|
|
import org.junit.jupiter.api.Test; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.test.context.TestPropertySource; |
|
|
import org.springframework.test.context.TestPropertySource; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
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.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 org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
|
|
|
|
|
|
import java.io.Serializable; |
|
|
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.assertThat; |
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
|
import static org.thingsboard.server.common.data.msg.TbMsgType.POST_TELEMETRY_REQUEST; |
|
|
|
|
|
|
|
|
|
|
|
@DaoSqlTest |
|
|
@DaoSqlTest |
|
|
@TestPropertySource(properties = { |
|
|
@TestPropertySource(properties = { |
|
|
@ -52,10 +46,7 @@ import static org.thingsboard.server.common.data.msg.TbMsgType.POST_TELEMETRY_RE |
|
|
"tbel.max_errors=2", |
|
|
"tbel.max_errors=2", |
|
|
"tbel.compiled_scripts_cache_size=100" |
|
|
"tbel.compiled_scripts_cache_size=100" |
|
|
}) |
|
|
}) |
|
|
class TbelInvokeServiceTest extends AbstractControllerTest { |
|
|
class TbelInvokeServiceTest extends AbstractTbelInvokeTest { |
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private TbelInvokeService invokeService; |
|
|
|
|
|
|
|
|
|
|
|
@Value("${tbel.max_errors}") |
|
|
@Value("${tbel.max_errors}") |
|
|
private int maxJsErrors; |
|
|
private int maxJsErrors; |
|
|
@ -69,14 +60,14 @@ class TbelInvokeServiceTest extends AbstractControllerTest { |
|
|
for (int i = 0; i < 100; i++) { |
|
|
for (int i = 0; i < 100; i++) { |
|
|
msg.put("temperature", i); |
|
|
msg.put("temperature", i); |
|
|
boolean expected = i > 20; |
|
|
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); |
|
|
Assert.assertEquals(expected, result); |
|
|
} |
|
|
} |
|
|
long startTs = System.currentTimeMillis(); |
|
|
long startTs = System.currentTimeMillis(); |
|
|
for (int i = 0; i < iterations; i++) { |
|
|
for (int i = 0; i < iterations; i++) { |
|
|
msg.put("temperature", i); |
|
|
msg.put("temperature", i); |
|
|
boolean expected = i > 20; |
|
|
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); |
|
|
Assert.assertEquals(expected, result); |
|
|
} |
|
|
} |
|
|
long duration = System.currentTimeMillis() - startTs; |
|
|
long duration = System.currentTimeMillis() - startTs; |
|
|
@ -101,7 +92,7 @@ class TbelInvokeServiceTest extends AbstractControllerTest { |
|
|
|
|
|
|
|
|
for (int i = 0; i < maxJsErrors; i++) { |
|
|
for (int i = 0; i < maxJsErrors; i++) { |
|
|
assertThatThrownBy(() -> { |
|
|
assertThatThrownBy(() -> { |
|
|
invokeScript(scriptId, hugeMsg); |
|
|
invokeScriptResultString(scriptId, hugeMsg); |
|
|
}).hasMessageContaining("input arguments exceed maximum"); |
|
|
}).hasMessageContaining("input arguments exceed maximum"); |
|
|
} |
|
|
} |
|
|
assertThatScriptIsBlocked(scriptId); |
|
|
assertThatScriptIsBlocked(scriptId); |
|
|
@ -114,7 +105,7 @@ class TbelInvokeServiceTest extends AbstractControllerTest { |
|
|
|
|
|
|
|
|
for (int i = 0; i < maxJsErrors; i++) { |
|
|
for (int i = 0; i < maxJsErrors; i++) { |
|
|
assertThatThrownBy(() -> { |
|
|
assertThatThrownBy(() -> { |
|
|
invokeScript(scriptId, "{}"); |
|
|
invokeScriptResultString(scriptId, "{}"); |
|
|
}).hasMessageContaining("result exceeds maximum allowed size"); |
|
|
}).hasMessageContaining("result exceeds maximum allowed size"); |
|
|
} |
|
|
} |
|
|
assertThatScriptIsBlocked(scriptId); |
|
|
assertThatScriptIsBlocked(scriptId); |
|
|
@ -182,7 +173,7 @@ class TbelInvokeServiceTest extends AbstractControllerTest { |
|
|
scriptsIds.add(scriptId); |
|
|
scriptsIds.add(scriptId); |
|
|
|
|
|
|
|
|
for (int j = 0; j < i; j++) { |
|
|
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); |
|
|
UUID scriptRemovedFromCache = scriptsIds.get(0); |
|
|
assertThat(compiledScriptsCache.getIfPresent(scriptIdToHash.get(scriptRemovedFromCache))).isNull(); |
|
|
assertThat(compiledScriptsCache.getIfPresent(scriptIdToHash.get(scriptRemovedFromCache))).isNull(); |
|
|
invokeScript(scriptRemovedFromCache, "{ \"temperature\": 12 }"); |
|
|
invokeScriptResultString(scriptRemovedFromCache, "{ \"temperature\": 12 }"); |
|
|
assertThat(compiledScriptsCache.getIfPresent(scriptIdToHash.get(scriptRemovedFromCache))).isNotNull(); |
|
|
assertThat(compiledScriptsCache.getIfPresent(scriptIdToHash.get(scriptRemovedFromCache))).isNotNull(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void assertThatScriptIsBlocked(UUID scriptId) { |
|
|
private void assertThatScriptIsBlocked(UUID scriptId) { |
|
|
assertThatThrownBy(() -> { |
|
|
assertThatThrownBy(() -> { |
|
|
invokeScript(scriptId, "{}"); |
|
|
invokeScriptResultString(scriptId, "{}"); |
|
|
}).hasMessageContaining("invocation is blocked due to maximum error"); |
|
|
}).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(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|