From 0ab3d2f4cc8c0e62e00d0809e057e5214da183b6 Mon Sep 17 00:00:00 2001 From: bryan31 Date: Tue, 30 Mar 2021 16:52:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/aop/ICmpAroundAspect.java | 4 +- .../yomahub/liteflow/core/NodeComponent.java | 4 +- .../flowtest/aspect/ComponentAspect.java | 4 +- liteflow-test/pom.xml | 6 ++ .../liteflow/test/aop/LFCustomAOPTest.java | 53 +++++++++++++++++ .../liteflow/test/aop/LFGlobalAOPTest.java | 57 +++++++++++++++++++ .../liteflow/test/aop/LiteflowAOPTest.java | 31 ---------- .../liteflow/test/aop/aspect/CmpAspect.java | 18 ++++++ .../test/aop/aspect/CustomAspect.java | 27 +++++++++ .../test/aop/cmp1/{AComp.java => ACmp.java} | 2 +- .../test/aop/cmp1/{BComp.java => BCmp.java} | 2 +- .../test/aop/cmp1/{CComp.java => CCmp.java} | 2 +- .../test/aop/cmp2/{DComp.java => DCmp.java} | 2 +- .../test/aop/cmp2/{EComp.java => ECmp.java} | 2 +- 14 files changed, 172 insertions(+), 42 deletions(-) create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFGlobalAOPTest.java delete mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CmpAspect.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CustomAspect.java rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/{AComp.java => ACmp.java} (90%) rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/{BComp.java => BCmp.java} (90%) rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/{CComp.java => CCmp.java} (90%) rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/{DComp.java => DCmp.java} (90%) rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/{EComp.java => ECmp.java} (90%) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/aop/ICmpAroundAspect.java b/liteflow-core/src/main/java/com/yomahub/liteflow/aop/ICmpAroundAspect.java index 7a2421d17..410a959c4 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/aop/ICmpAroundAspect.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/aop/ICmpAroundAspect.java @@ -16,7 +16,7 @@ import com.yomahub.liteflow.entity.data.Slot; */ public interface ICmpAroundAspect { - void beforeProcess(Slot slot); + void beforeProcess(String nodeId, Slot slot); - void afterProcess(Slot slot); + void afterProcess(String nodeId, Slot slot); } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java index 927e4a17a..79865b752 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java @@ -53,14 +53,14 @@ public abstract class NodeComponent { // process前置处理 if (ObjectUtil.isNotNull(ComponentScaner.cmpAroundAspect)) { - ComponentScaner.cmpAroundAspect.beforeProcess(slot); + ComponentScaner.cmpAroundAspect.beforeProcess(this.getNodeId(), slot); } process(); // process后置处理 if (ObjectUtil.isNotNull(ComponentScaner.cmpAroundAspect)) { - ComponentScaner.cmpAroundAspect.afterProcess(slot); + ComponentScaner.cmpAroundAspect.afterProcess(this.getNodeId(), slot); } stopWatch.stop(); diff --git a/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/aspect/ComponentAspect.java b/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/aspect/ComponentAspect.java index aba478de7..48898ec45 100644 --- a/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/aspect/ComponentAspect.java +++ b/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/aspect/ComponentAspect.java @@ -7,12 +7,12 @@ import org.springframework.stereotype.Component; @Component public class ComponentAspect implements ICmpAroundAspect { @Override - public void beforeProcess(Slot slot) { + public void beforeProcess(String nodeId, Slot slot) { System.out.println("before process"); } @Override - public void afterProcess(Slot slot) { + public void afterProcess(String nodeId, Slot slot) { System.out.println("after process"); } } diff --git a/liteflow-test/pom.xml b/liteflow-test/pom.xml index fc6a4c25a..89c137087 100644 --- a/liteflow-test/pom.xml +++ b/liteflow-test/pom.xml @@ -36,6 +36,12 @@ ${project.parent.version} + + org.aspectj + aspectjweaver + 1.8.13 + + org.springframework.boot spring-boot-starter-test diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java new file mode 100644 index 000000000..b3e9e9033 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java @@ -0,0 +1,53 @@ +package com.yomahub.liteflow.test.aop; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.entity.data.Slot; +import com.yomahub.liteflow.test.aop.aspect.CustomAspect; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +/** + * 切面场景单元测试 + * @author Bryan.Zhang + */ +@RunWith(SpringRunner.class) +@ActiveProfiles("aop") +@SpringBootTest(classes = LFCustomAOPTest.class) +@EnableAutoConfiguration +@Import(CustomAspect.class) +@ComponentScan({"com.yomahub.liteflow.test.aop.cmp1","com.yomahub.liteflow.test.aop.cmp2"}) +public class LFCustomAOPTest { + + @Resource + private FlowExecutor flowExecutor; + + //测试自定义AOP,串行场景 + @Test + public void testCustomAopS() throws Exception{ + LiteflowResponse response= flowExecutor.execute("chain1", "it's a request"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("before_after", response.getData().getData("a")); + Assert.assertEquals("before_after", response.getData().getData("b")); + Assert.assertEquals("before_after", response.getData().getData("c")); + } + + //测试自定义AOP,并行场景 + @Test + public void testGlobalAopP() throws Exception{ + LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("before_after", response.getData().getData("a")); + Assert.assertEquals("before_after", response.getData().getData("b")); + Assert.assertEquals("before_after", response.getData().getData("c")); + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFGlobalAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFGlobalAOPTest.java new file mode 100644 index 000000000..d060bfc00 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFGlobalAOPTest.java @@ -0,0 +1,57 @@ +package com.yomahub.liteflow.test.aop; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.entity.data.Slot; +import com.yomahub.liteflow.test.aop.aspect.CmpAspect; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +/** + * 切面场景单元测试 + * @author Bryan.Zhang + */ +@RunWith(SpringRunner.class) +@ActiveProfiles("aop") +@SpringBootTest(classes = LFGlobalAOPTest.class) +@EnableAutoConfiguration +@Import(CmpAspect.class) +@ComponentScan({"com.yomahub.liteflow.test.aop.cmp1","com.yomahub.liteflow.test.aop.cmp2"}) +public class LFGlobalAOPTest { + + @Resource + private FlowExecutor flowExecutor; + + //测试全局AOP,串行场景 + @Test + public void testGlobalAopS() throws Exception{ + LiteflowResponse response= flowExecutor.execute("chain1", "it's a request"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("before_after", response.getData().getData("a")); + Assert.assertEquals("before_after", response.getData().getData("b")); + Assert.assertEquals("before_after", response.getData().getData("c")); + Assert.assertEquals("before_after", response.getData().getData("d")); + Assert.assertEquals("before_after", response.getData().getData("e")); + } + + //测试全局AOP,并行场景 + @Test + public void testGlobalAopP() throws Exception{ + LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("before_after", response.getData().getData("a")); + Assert.assertEquals("before_after", response.getData().getData("b")); + Assert.assertEquals("before_after", response.getData().getData("c")); + Assert.assertEquals("before_after", response.getData().getData("d")); + Assert.assertEquals("before_after", response.getData().getData("e")); + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java deleted file mode 100644 index 609d22ef4..000000000 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.yomahub.liteflow.test.aop; - -import com.yomahub.liteflow.core.FlowExecutor; -import com.yomahub.liteflow.entity.data.LiteflowResponse; -import com.yomahub.liteflow.entity.data.Slot; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; - -import javax.annotation.Resource; - -@RunWith(SpringRunner.class) -@ActiveProfiles("aop") -@SpringBootTest(classes = LiteflowAOPTest.class) -@EnableAutoConfiguration -@ComponentScan -public class LiteflowAOPTest { - - @Resource - private FlowExecutor flowExecutor; - - @Test - public void testAop() throws Exception{ - LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); - System.out.println(response); - } -} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CmpAspect.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CmpAspect.java new file mode 100644 index 000000000..028d5801b --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CmpAspect.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.aop.aspect; + +import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.aop.ICmpAroundAspect; +import com.yomahub.liteflow.entity.data.Slot; +import org.springframework.stereotype.Component; + +public class CmpAspect implements ICmpAroundAspect { + @Override + public void beforeProcess(String nodeId, Slot slot) { + slot.setData(nodeId, "before"); + } + + @Override + public void afterProcess(String nodeId, Slot slot) { + slot.setData(nodeId, StrUtil.format("{}_{}", slot.getData(nodeId), "after")); + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CustomAspect.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CustomAspect.java new file mode 100644 index 000000000..7277a8ac5 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CustomAspect.java @@ -0,0 +1,27 @@ +package com.yomahub.liteflow.test.aop.aspect; + +import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.entity.data.Slot; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; + +@Aspect +public class CustomAspect { + + @Pointcut("execution(* com.yomahub.liteflow.test.aop.cmp1.*.process())") + public void cut() { + } + + @Around("cut()") + public Object around(ProceedingJoinPoint jp) throws Throwable { + NodeComponent cmp = (NodeComponent) jp.getThis(); + Slot slot = cmp.getSlot(); + slot.setData(cmp.getNodeId(), "before"); + Object returnObj = jp.proceed(); + slot.setData(cmp.getNodeId(), StrUtil.format("{}_{}", slot.getData(cmp.getNodeId()), "after")); + return returnObj; + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/ACmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/ACmp.java index 5e1716956..7fd947163 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/ACmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("a") -public class AComp extends NodeComponent { +public class ACmp extends NodeComponent { @Override public void process() { diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BCmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BCmp.java index 3e42b4ce4..d99d2a1e6 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BCmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("b") -public class BComp extends NodeComponent { +public class BCmp extends NodeComponent { @Override public void process() { diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CCmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CCmp.java index 63a6d37f5..7a2181438 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CCmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("c") -public class CComp extends NodeComponent { +public class CCmp extends NodeComponent { @Override public void process() { diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DCmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DCmp.java index e10c6b155..eb8ae7648 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DCmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("d") -public class DComp extends NodeComponent { +public class DCmp extends NodeComponent { @Override public void process() { diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/ECmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/ECmp.java index dca2147a5..ce0319d44 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/ECmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("e") -public class EComp extends NodeComponent { +public class ECmp extends NodeComponent { @Override public void process() {