From c5e221c9a53d37595c5448fe0af0c61c1a45f174 Mon Sep 17 00:00:00 2001 From: houxinyu Date: Sun, 13 Aug 2023 00:53:20 +0800 Subject: [PATCH] add test --- .../RedisWithXmlELPollSpringbootTest.java | 17 +++- ...edisWithXmlELSubscribeSpringbootTest2.java | 89 +++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELSubscribeSpringbootTest2.java diff --git a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollSpringbootTest.java index 20f7da8fb..f146a7c5e 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollSpringbootTest.java @@ -1,5 +1,6 @@ package com.yomahub.liteflow.test.redis; +import cn.hutool.crypto.digest.DigestUtil; import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.flow.LiteflowResponse; @@ -20,6 +21,8 @@ import javax.annotation.Resource; import java.util.HashSet; import java.util.Set; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; /** @@ -62,12 +65,24 @@ public class RedisWithXmlELPollSpringbootTest extends BaseTest { Set chainNameSet = new HashSet<>(); chainNameSet.add("chain11"); String chainValue = "THEN(a, b, c);"; + Object chainSHA = DigestUtil.sha1Hex(chainValue); + + //SHA值用于测试修改chain的轮询刷新功能 + String changeChainValue = "THEN(a, c);"; + Object changeChainSHA = DigestUtil.sha1Hex(changeChainValue); when(chainJedis.hkeys("pollChainKey")).thenReturn(chainNameSet); - when(chainJedis.hget("pollChainKey", "chain11")).thenReturn(chainValue); + when(chainJedis.hget("pollChainKey", "chain11")).thenReturn(chainValue).thenReturn(changeChainValue); + when(chainJedis.evalsha(anyString(), anyInt(), anyString())).thenReturn(chainSHA).thenReturn(changeChainSHA); LiteflowResponse response = flowExecutor.execute2Resp("chain11", "arg"); Assertions.assertTrue(response.isSuccess()); Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr()); + + flowExecutor.reloadRule(); + + response = flowExecutor.execute2Resp("chain11", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>c", response.getExecuteStepStr()); } /** diff --git a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELSubscribeSpringbootTest2.java b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELSubscribeSpringbootTest2.java new file mode 100644 index 000000000..ff89921ec --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELSubscribeSpringbootTest2.java @@ -0,0 +1,89 @@ +package com.yomahub.liteflow.test.redis; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.slot.DefaultContext; +import com.yomahub.liteflow.test.BaseTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.redisson.api.RMapCache; +import org.redisson.api.RedissonClient; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import javax.annotation.Resource; + +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +/** + * springboot环境下的redis配置源订阅模式功能测试 + * + * @author hxinyu + * @since 2.11.0 + */ +@ExtendWith(SpringExtension.class) +@TestPropertySource(value = "classpath:/redis/application-sub-xml.properties") +@SpringBootTest(classes = RedisWithXmlELSubscribeSpringbootTest2.class) +@EnableAutoConfiguration +@ComponentScan({"com.yomahub.liteflow.test.redis.cmp"}) +public class RedisWithXmlELSubscribeSpringbootTest2 extends BaseTest { + + @MockBean(name = "chainClient") + private static RedissonClient chainClient; + + @MockBean(name = "scriptClient") + private static RedissonClient scriptClient; + + @Resource + private FlowExecutor flowExecutor; + + @BeforeEach + public void setUp() { + MockitoAnnotations.initMocks(this); + } + +/* @BeforeAll + public static void setUpBeforeClass() { + Config config = new Config(); + config.useSingleServer().setAddress("redis://127.0.0.1:6379").setDatabase(1); + redissonClient = Redisson.create(config); + RMapCache chainKey = redissonClient.getMapCache("chainKey"); + RMapCache scriptKey = redissonClient.getMapCache("scriptKey"); + scriptKey.put("s1:script:脚本s1:groovy", "defaultContext.setData(\"test1\",\"hello s1\");"); + scriptKey.put("s2:script:脚本s2:js", "defaultContext.setData(\"test2\",\"hello s2\");"); + scriptKey.put("s3:script:脚本s3", "defaultContext.setData(\"test3\",\"hello s3\");"); + chainKey.put("chain1", "THEN(a, b, c);"); + chainKey.put("chain2", "THEN(a, b, c, s3);"); + chainKey.put("chain3", "THEN(a, b, c, s1, s2);"); + }*/ + + @Test + public void testSubWithXml() throws InterruptedException { + RMapCache chainKey = chainClient.getMapCache(""); + System.out.println(chainKey); + +/* LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr());*/ + } + + @Test + public void testSubWithScriptXml() throws InterruptedException { + LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg"); + DefaultContext context = response.getFirstContextBean(); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("hello s1", context.getData("test1")); + Assertions.assertEquals("a==>b==>c==>s1[脚本s1]==>s2[脚本s2]", response.getExecuteStepStrWithoutTime()); + } +}