com.nepxion.discovery.plugin.test.automation.aop.TestInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of discovery-plugin-test-starter-automation Show documentation
Show all versions of discovery-plugin-test-starter-automation Show documentation
Nepxion Discovery is a solution for Spring Cloud with blue green, gray, weight, limitation, circuit breaker, degrade, isolation, monitor, tracing, dye, failover, async agent
The newest version!
package com.nepxion.discovery.plugin.test.automation.aop;
/**
* Title: Nepxion Discovery
* Description: Nepxion Discovery
* Copyright: Copyright (c) 2017-2050
* Company: Nepxion
* @author Haojun Ren
* @version 1.0
*/
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import com.nepxion.discovery.common.entity.FormatType;
import com.nepxion.discovery.plugin.test.automation.annotation.DTest;
import com.nepxion.discovery.plugin.test.automation.annotation.DTestConfig;
import com.nepxion.discovery.plugin.test.automation.constant.TestConstant;
import com.nepxion.discovery.plugin.test.automation.operation.TestOperation;
import com.nepxion.matrix.proxy.aop.AbstractInterceptor;
public class TestInterceptor extends AbstractInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(TestInterceptor.class);
@Autowired
private TestOperation testOperation;
@Value("${" + TestConstant.SPRING_APPLICATION_TEST_CONFIG_OPERATION_AWAIT_TIME + ":3000}")
private Integer configOperationAwaitTime;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
boolean isTestAnnotationPresent = method.isAnnotationPresent(DTest.class);
boolean isTestConfigAnnotationPresent = method.isAnnotationPresent(DTestConfig.class);
if (isTestAnnotationPresent || isTestConfigAnnotationPresent) {
String methodName = getMethodName(invocation);
LOG.info("---------- Run automation testcase :: {}() ----------", methodName);
Object object = null;
if (isTestAnnotationPresent) {
object = invocation.proceed();
} else {
DTestConfig testConfigAnnotation = method.getAnnotation(DTestConfig.class);
String group = convertSpel(invocation, testConfigAnnotation.group());
String serviceId = convertSpel(invocation, testConfigAnnotation.serviceId());
FormatType formatType = testConfigAnnotation.formatType();
String prefix = convertSpel(invocation, testConfigAnnotation.prefix());
String suffix = convertSpel(invocation, testConfigAnnotation.suffix());
String executePath = convertSpel(invocation, testConfigAnnotation.executePath());
String resetPath = convertSpel(invocation, testConfigAnnotation.resetPath());
if (formatType == FormatType.TEXT_FORMAT) {
try {
String type = executePath.substring(executePath.lastIndexOf(".") + 1);
formatType = FormatType.fromString(type);
} catch (Exception e) {
}
}
if (StringUtils.isNotEmpty(prefix)) {
group = prefix + "-" + group;
}
if (StringUtils.isNotEmpty(suffix)) {
serviceId = serviceId + "-" + suffix;
}
testOperation.update(group, serviceId, formatType, executePath);
Thread.sleep(configOperationAwaitTime);
try {
object = invocation.proceed();
} finally {
if (StringUtils.isNotEmpty(resetPath)) {
testOperation.update(group, serviceId, formatType, resetPath);
} else {
testOperation.clear(group, serviceId);
}
Thread.sleep(configOperationAwaitTime);
}
}
LOG.info("* Passed");
return object;
}
return invocation.proceed();
}
private String convertSpel(MethodInvocation invocation, String key) {
String spelKey = null;
try {
spelKey = getSpelKey(invocation, key);
} catch (Exception e) {
spelKey = key;
}
return spelKey;
}
}