增加单元测试模块

This commit is contained in:
bryan31
2021-03-30 16:52:20 +08:00
parent 1735c95cd1
commit 0ab3d2f4cc
14 changed files with 172 additions and 42 deletions

View File

@@ -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);
}

View File

@@ -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();

View File

@@ -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");
}
}

View File

@@ -36,6 +36,12 @@
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -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<Slot> 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<Slot> 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"));
}
}

View File

@@ -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<Slot> 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<Slot> 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"));
}
}

View File

@@ -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<Slot> response= flowExecutor.execute("chain2", "it's a request");
System.out.println(response);
}
}

View File

@@ -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"));
}
}

View File

@@ -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;
}
}

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {