diff --git a/liteflow-rule-plugin/liteflow-rule-apollo/src/main/java/com/yomahub/liteflow/parser/apollo/util/ApolloParseHelper.java b/liteflow-rule-plugin/liteflow-rule-apollo/src/main/java/com/yomahub/liteflow/parser/apollo/util/ApolloParseHelper.java index 11e2557c8..9fcd1fa1a 100644 --- a/liteflow-rule-plugin/liteflow-rule-apollo/src/main/java/com/yomahub/liteflow/parser/apollo/util/ApolloParseHelper.java +++ b/liteflow-rule-plugin/liteflow-rule-apollo/src/main/java/com/yomahub/liteflow/parser/apollo/util/ApolloParseHelper.java @@ -2,6 +2,7 @@ package com.yomahub.liteflow.parser.apollo.util; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import com.ctrip.framework.apollo.Config; @@ -14,6 +15,7 @@ import com.yomahub.liteflow.enums.NodeTypeEnum; import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.parser.apollo.exception.ApolloException; import com.yomahub.liteflow.parser.apollo.vo.ApolloParserConfigVO; +import com.yomahub.liteflow.spi.holder.ContextAwareHolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,12 +51,23 @@ public class ApolloParseHelper { public ApolloParseHelper(ApolloParserConfigVO apolloParserConfigVO) { this.apolloParserConfigVO = apolloParserConfigVO; + try { - chainConfig = ConfigService.getConfig(apolloParserConfigVO.getChainNamespace()); - String scriptNamespace; - // scriptConfig is optional - if (StrUtil.isNotBlank(scriptNamespace = apolloParserConfigVO.getScriptNamespace())) { - scriptConfig = ConfigService.getConfig(scriptNamespace); + try{ + //这里本身对于程序运行来说没有什么意义,拿到的永远是null + //其实config对象也没有注入到spring容器中 + //这里这样写的目的是为了单测中的mockito,当有@MockBean的时候,这里就能拿到了 + this.chainConfig = ContextAwareHolder.loadContextAware().getBean("chainConfig"); + this.scriptConfig = ContextAwareHolder.loadContextAware().getBean("scriptConfig"); + }catch (Exception ignored){} + + if (ObjectUtil.isNull(chainConfig)){ + chainConfig = ConfigService.getConfig(apolloParserConfigVO.getChainNamespace()); + String scriptNamespace; + // scriptConfig is optional + if (StrUtil.isNotBlank(scriptNamespace = apolloParserConfigVO.getScriptNamespace())) { + scriptConfig = ConfigService.getConfig(scriptNamespace); + } } } catch (Exception e) { throw new ApolloException(e.getMessage()); diff --git a/liteflow-rule-plugin/pom.xml b/liteflow-rule-plugin/pom.xml index 1cb201872..02de13a6d 100644 --- a/liteflow-rule-plugin/pom.xml +++ b/liteflow-rule-plugin/pom.xml @@ -15,6 +15,7 @@ liteflow-rule-sql liteflow-rule-nacos liteflow-rule-etcd + liteflow-rule-apollo liteflow-rule-plugin diff --git a/liteflow-testcase-el/liteflow-testcase-el-apollo-springboot/src/test/java/com/yomahub/liteflow/test/apollo/ApolloWithXmlELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-apollo-springboot/src/test/java/com/yomahub/liteflow/test/apollo/ApolloWithXmlELSpringbootTest.java index fa9e229c8..e464dcba1 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-apollo-springboot/src/test/java/com/yomahub/liteflow/test/apollo/ApolloWithXmlELSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-apollo-springboot/src/test/java/com/yomahub/liteflow/test/apollo/ApolloWithXmlELSpringbootTest.java @@ -1,19 +1,30 @@ package com.yomahub.liteflow.test.apollo; +import cn.hutool.core.util.StrUtil; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigService; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.flow.LiteflowResponse; import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; 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.junit4.SpringRunner; - import javax.annotation.Resource; +import java.util.List; +import java.util.Set; + +import static org.mockito.Mockito.*; /** * @Description: @@ -27,30 +38,38 @@ import javax.annotation.Resource; @ComponentScan({"com.yomahub.liteflow.test.apollo.cmp"}) public class ApolloWithXmlELSpringbootTest { + @MockBean(name = "chainConfig") + private Config chainConfig; + + @MockBean(name = "scriptConfig") + private Config scriptConfig; @Resource private FlowExecutor flowExecutor; + @Before + public void setUp(){ + MockitoAnnotations.initMocks(this); + } + @After public void after() { FlowBus.cleanCache(); } - @Test - public void testApolloWithXml1() throws InterruptedException { + public void testApolloWithXml1(){ + Set chainNameList = Sets.newHashSet("chain1"); + Set scriptNodeValueList = Sets.newHashSet("s1:script:脚本s1"); + when(chainConfig.getPropertyNames()).thenReturn(chainNameList); + when(scriptConfig.getPropertyNames()).thenReturn(scriptNodeValueList); + + String chain1Data = "THEN(a, b, c, s1);"; + String scriptNodeValue = "defaultContext.setData(\"test\",\"hello\");"; + when(chainConfig.getProperty(anyString(), anyString())).thenReturn(chain1Data); + when(scriptConfig.getProperty(anyString(), anyString())).thenReturn(scriptNodeValue); + LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); Assert.assertEquals("a==>b==>c==>s1[脚本s1]", response.getExecuteStepStrWithoutTime()); } - - - @Test - public void testApolloWithXml2() throws InterruptedException { - while (true) { - LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); - System.out.println("liteflow step : " + response.getExecuteStepStrWithoutTime()); - Thread.sleep(2000l); - } - } - } diff --git a/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java index 283edd5fe..eee5c3b23 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java @@ -43,7 +43,6 @@ public class EtcdWithXmlELSpringbootTest extends BaseTest { private static final String SCRIPT_PATH = "/liteflow/script"; - @Before public void setUp(){ MockitoAnnotations.initMocks(this); diff --git a/liteflow-testcase-el/pom.xml b/liteflow-testcase-el/pom.xml index 3c05f7f32..32610be3c 100644 --- a/liteflow-testcase-el/pom.xml +++ b/liteflow-testcase-el/pom.xml @@ -28,6 +28,7 @@ liteflow-testcase-el-sql-springboot liteflow-testcase-el-nacos-springboot liteflow-testcase-el-etcd-springboot + liteflow-testcase-el-apollo-springboot