From 527f7176c54710b395bbb332bfed3e3a35441657 Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Wed, 1 Jan 2025 14:25:28 +0100 Subject: [PATCH] tbel test refactored to not use sql and all controllers. reflection utils used --- .../script/AbstractTbelInvokeTest.java | 5 ++++- .../service/script/TbelInvokeDocsIoTest.java | 2 -- .../service/script/TbelInvokeServiceTest.java | 19 +++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/service/script/AbstractTbelInvokeTest.java b/application/src/test/java/org/thingsboard/server/service/script/AbstractTbelInvokeTest.java index 11b61f64c4..dcd73bac50 100644 --- a/application/src/test/java/org/thingsboard/server/service/script/AbstractTbelInvokeTest.java +++ b/application/src/test/java/org/thingsboard/server/service/script/AbstractTbelInvokeTest.java @@ -16,8 +16,10 @@ package org.thingsboard.server.service.script; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.script.api.ScriptType; +import org.thingsboard.script.api.tbel.DefaultTbelInvokeService; import org.thingsboard.script.api.tbel.TbelInvokeService; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.controller.AbstractControllerTest; @@ -28,7 +30,8 @@ import java.util.concurrent.ExecutionException; import static org.thingsboard.server.common.data.msg.TbMsgType.POST_TELEMETRY_REQUEST; -public abstract class AbstractTbelInvokeTest extends AbstractControllerTest { +@SpringBootTest(classes = DefaultTbelInvokeService.class) +public abstract class AbstractTbelInvokeTest { @Autowired protected TbelInvokeService invokeService; diff --git a/application/src/test/java/org/thingsboard/server/service/script/TbelInvokeDocsIoTest.java b/application/src/test/java/org/thingsboard/server/service/script/TbelInvokeDocsIoTest.java index d72dd87992..cc51a7063c 100644 --- a/application/src/test/java/org/thingsboard/server/service/script/TbelInvokeDocsIoTest.java +++ b/application/src/test/java/org/thingsboard/server/service/script/TbelInvokeDocsIoTest.java @@ -18,7 +18,6 @@ package org.thingsboard.server.service.script; import org.junit.jupiter.api.Test; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.script.api.tbel.TbDate; -import org.thingsboard.server.dao.service.DaoSqlTest; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -34,7 +33,6 @@ import java.util.concurrent.atomic.AtomicReference; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertEquals; -@DaoSqlTest class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest { private String decoderStr; diff --git a/application/src/test/java/org/thingsboard/server/service/script/TbelInvokeServiceTest.java b/application/src/test/java/org/thingsboard/server/service/script/TbelInvokeServiceTest.java index 071fdf77fc..62170236e0 100644 --- a/application/src/test/java/org/thingsboard/server/service/script/TbelInvokeServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/script/TbelInvokeServiceTest.java @@ -22,9 +22,9 @@ import org.junit.Ignore; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.test.context.TestPropertySource; +import org.springframework.test.util.ReflectionTestUtils; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.script.api.tbel.TbelScript; -import org.thingsboard.server.dao.service.DaoSqlTest; import java.io.Serializable; import java.util.ArrayList; @@ -38,7 +38,6 @@ import java.util.concurrent.TimeUnit; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -@DaoSqlTest @TestPropertySource(properties = { "tbel.max_script_body_size=100", "tbel.max_total_args_size=50", @@ -120,9 +119,9 @@ class TbelInvokeServiceTest extends AbstractTbelInvokeTest { scriptsIds.add(scriptId); } - Map scriptIdToHash = getFieldValue(invokeService, "scriptIdToHash"); - Map scriptMap = getFieldValue(invokeService, "scriptMap"); - Cache compiledScriptsCache = getFieldValue(invokeService, "compiledScriptsCache"); + Map scriptIdToHash = (Map) ReflectionTestUtils.getField(invokeService, "scriptIdToHash"); + Map scriptMap = (Map) ReflectionTestUtils.getField(invokeService, "scriptMap"); + Cache compiledScriptsCache = (Cache) ReflectionTestUtils.getField(invokeService, "compiledScriptsCache"); String scriptHash = scriptIdToHash.get(scriptsIds.get(0)); @@ -140,9 +139,9 @@ class TbelInvokeServiceTest extends AbstractTbelInvokeTest { scriptsIds.add(scriptId); } - Map scriptIdToHash = getFieldValue(invokeService, "scriptIdToHash"); - Map scriptMap = getFieldValue(invokeService, "scriptMap"); - Cache compiledScriptsCache = getFieldValue(invokeService, "compiledScriptsCache"); + Map scriptIdToHash = (Map) ReflectionTestUtils.getField(invokeService, "scriptIdToHash"); + Map scriptMap = (Map) ReflectionTestUtils.getField(invokeService, "scriptMap"); + Cache compiledScriptsCache = (Cache) ReflectionTestUtils.getField(invokeService, "compiledScriptsCache"); String scriptHash = scriptIdToHash.get(scriptsIds.get(0)); for (int i = 0; i < 9; i++) { @@ -163,8 +162,8 @@ class TbelInvokeServiceTest extends AbstractTbelInvokeTest { @Ignore("This test is based on assumption that Caffeine cache is LRU based but in fact it is based on " + "Tiny LFU which is the cause that the tests fail sometime: https://arxiv.org/pdf/1512.00727.pdf") public void whenCompiledScriptsCacheIsTooBig_thenRemoveRarelyUsedScripts() throws Exception { - Map scriptIdToHash = getFieldValue(invokeService, "scriptIdToHash"); - Cache compiledScriptsCache = getFieldValue(invokeService, "compiledScriptsCache"); + Map scriptIdToHash = (Map) ReflectionTestUtils.getField(invokeService, "scriptIdToHash"); + Cache compiledScriptsCache = (Cache) ReflectionTestUtils.getField(invokeService, "compiledScriptsCache"); List scriptsIds = new ArrayList<>(); for (int i = 0; i < 110; i++) { // tbel.compiled_scripts_cache_size = 100