Feature: add AssemblyFlow to support tsf (#992)
parent
97f5ec54d0
commit
d68bf433a3
@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-starter-tencent-tsf-adapter-plugin</artifactId>
|
||||
<name>Spring Cloud Starter Tencent TSF Plugin</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring Cloud Tencent dependencies start -->
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-rpc-enhancement</artifactId>
|
||||
</dependency>
|
||||
<!-- Spring Cloud Tencent dependencies end -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.tsf.adapter.config;
|
||||
|
||||
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration;
|
||||
import com.tencent.polaris.api.exception.PolarisException;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* PolarisTsfAdapterAutoConfiguration.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnPolarisEnabled
|
||||
@AutoConfigureAfter(RpcEnhancementAutoConfiguration.class)
|
||||
public class PolarisTsfAdapterAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public PolarisTsfFlowConfigModifier polarisFlowConfigModifier() throws PolarisException {
|
||||
return new PolarisTsfFlowConfigModifier();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
33
spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/BlockingLoadBalancerClientAspect.java → spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-tsf-adapter-plugin/src/main/java/com/tencent/cloud/tsf/adapter/config/PolarisTsfFlowConfigModifier.java
33
spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/BlockingLoadBalancerClientAspect.java → spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-tsf-adapter-plugin/src/main/java/com/tencent/cloud/tsf/adapter/config/PolarisTsfFlowConfigModifier.java
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.tsf.adapter.config;
|
||||
|
||||
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* PolarisTsfAdapterAutoConfigurationTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class PolarisTsfAdapterAutoConfigurationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
PolarisContextAutoConfiguration.class,
|
||||
PolarisTsfAdapterAutoConfiguration.class
|
||||
));
|
||||
|
||||
@Test
|
||||
public void testDefaultInitialization() {
|
||||
this.contextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(PolarisTsfFlowConfigModifier.class);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.tsf.adapter.config;
|
||||
|
||||
import com.tencent.polaris.assembly.flow.AssemblyFlow;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
|
||||
/**
|
||||
* TsfAssemblyFlow.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class TsfAssemblyFlow implements AssemblyFlow {
|
||||
@Override
|
||||
public String getName() {
|
||||
return PolarisTsfFlowConfigModifier.TSF_FLOW_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSDKContext(SDKContext sdkContext) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.tsf.adapter.config;
|
||||
|
||||
import com.tencent.polaris.api.flow.DiscoveryFlow;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
|
||||
/**
|
||||
* TsfDiscoveryFlow.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class TsfDiscoveryFlow implements DiscoveryFlow {
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return PolarisTsfFlowConfigModifier.TSF_FLOW_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSDKContext(SDKContext sdkContext) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.tsf.adapter.config;
|
||||
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import com.tencent.polaris.router.api.flow.RouterFlow;
|
||||
|
||||
/**
|
||||
* TsfRouterFlow.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class TsfRouterFlow implements RouterFlow {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return PolarisTsfFlowConfigModifier.TSF_FLOW_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSDKContext(SDKContext sdkContext) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
com.tencent.cloud.tsf.adapter.config.TsfDiscoveryFlow
|
@ -0,0 +1 @@
|
||||
com.tencent.cloud.tsf.adapter.config.TsfAssemblyFlow
|
@ -0,0 +1 @@
|
||||
com.tencent.cloud.tsf.adapter.config.TsfRouterFlow
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.feign;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import feign.Request;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.openfeign.loadbalancer.LoadBalancerFeignRequestTransformer;
|
||||
|
||||
import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE;
|
||||
|
||||
/**
|
||||
* PolarisLoadBalancerFeignRequestTransformer.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class PolarisLoadBalancerFeignRequestTransformer implements LoadBalancerFeignRequestTransformer {
|
||||
|
||||
/**
|
||||
* Transform Request, add Loadbalancer ServiceInstance to MetadataContext.
|
||||
* @param request request
|
||||
* @param instance instance
|
||||
* @return HttpRequest
|
||||
*/
|
||||
@Override
|
||||
public Request transformRequest(Request request, ServiceInstance instance) {
|
||||
if (instance != null) {
|
||||
MetadataContextHolder.get().setLoadbalancer(LOAD_BALANCER_SERVICE_INSTANCE, instance);
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.filter;
|
||||
|
||||
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
|
||||
/**
|
||||
* EnhancedReactiveFilter.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class EnhancedReactiveFilter implements WebFilter, Ordered {
|
||||
|
||||
private final EnhancedPluginRunner pluginRunner;
|
||||
|
||||
public EnhancedReactiveFilter(EnhancedPluginRunner pluginRunner) {
|
||||
this.pluginRunner = pluginRunner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
|
||||
|
||||
EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder()
|
||||
.httpHeaders(exchange.getRequest().getHeaders())
|
||||
.httpMethod(exchange.getRequest().getMethod())
|
||||
.url(exchange.getRequest().getURI())
|
||||
.build();
|
||||
enhancedPluginContext.setRequest(enhancedRequestContext);
|
||||
|
||||
enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance());
|
||||
|
||||
// Run pre enhanced plugins.
|
||||
pluginRunner.run(EnhancedPluginType.Server.PRE, enhancedPluginContext);
|
||||
|
||||
long startMillis = System.currentTimeMillis();
|
||||
return chain.filter(exchange)
|
||||
.doOnSuccess(v -> {
|
||||
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||
|
||||
EnhancedResponseContext enhancedResponseContext = EnhancedResponseContext.builder()
|
||||
.httpStatus(exchange.getResponse().getRawStatusCode())
|
||||
.httpHeaders(exchange.getResponse().getHeaders())
|
||||
.build();
|
||||
enhancedPluginContext.setResponse(enhancedResponseContext);
|
||||
|
||||
// Run post enhanced plugins.
|
||||
pluginRunner.run(EnhancedPluginType.Server.POST, enhancedPluginContext);
|
||||
})
|
||||
.doOnError(e -> {
|
||||
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||
enhancedPluginContext.setThrowable(e);
|
||||
// Run exception enhanced plugins.
|
||||
pluginRunner.run(EnhancedPluginType.Server.EXCEPTION, enhancedPluginContext);
|
||||
})
|
||||
.doFinally(v -> {
|
||||
// Run finally enhanced plugins.
|
||||
pluginRunner.run(EnhancedPluginType.Server.FINALLY, enhancedPluginContext);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return MetadataConstant.OrderConstant.WEB_FILTER_ORDER + 1;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.filter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
/**
|
||||
* EnhancedServletFilter.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@Order(MetadataConstant.OrderConstant.WEB_FILTER_ORDER + 1)
|
||||
public class EnhancedServletFilter extends OncePerRequestFilter {
|
||||
|
||||
private final EnhancedPluginRunner pluginRunner;
|
||||
|
||||
public EnhancedServletFilter(EnhancedPluginRunner pluginRunner) {
|
||||
this.pluginRunner = pluginRunner;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
|
||||
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
Enumeration<String> requestHeaderNames = request.getHeaderNames();
|
||||
if (requestHeaderNames != null) {
|
||||
while (requestHeaderNames.hasMoreElements()) {
|
||||
String requestHeaderName = requestHeaderNames.nextElement();
|
||||
requestHeaders.addAll(requestHeaderName, Collections.list(request.getHeaders(requestHeaderName)));
|
||||
}
|
||||
}
|
||||
EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder()
|
||||
.httpHeaders(requestHeaders)
|
||||
.httpMethod(HttpMethod.valueOf(request.getMethod()))
|
||||
.url(URI.create(request.getRequestURL().toString()))
|
||||
.build();
|
||||
enhancedPluginContext.setRequest(enhancedRequestContext);
|
||||
|
||||
enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance());
|
||||
|
||||
// Run pre enhanced plugins.
|
||||
pluginRunner.run(EnhancedPluginType.Server.PRE, enhancedPluginContext);
|
||||
|
||||
long startMillis = System.currentTimeMillis();
|
||||
try {
|
||||
filterChain.doFilter(request, response);
|
||||
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
Collection<String> responseHeaderNames = response.getHeaderNames();
|
||||
if (responseHeaderNames != null) {
|
||||
for (String responseHeaderName : responseHeaderNames) {
|
||||
responseHeaders.addAll(responseHeaderName, new ArrayList<>(response.getHeaders(responseHeaderName)));
|
||||
}
|
||||
}
|
||||
EnhancedResponseContext enhancedResponseContext = EnhancedResponseContext.builder()
|
||||
.httpStatus(response.getStatus())
|
||||
.httpHeaders(responseHeaders)
|
||||
.build();
|
||||
enhancedPluginContext.setResponse(enhancedResponseContext);
|
||||
|
||||
// Run post enhanced plugins.
|
||||
pluginRunner.run(EnhancedPluginType.Server.POST, enhancedPluginContext);
|
||||
}
|
||||
catch (ServletException | IOException e) {
|
||||
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||
enhancedPluginContext.setThrowable(e);
|
||||
// Run exception enhanced plugins.
|
||||
pluginRunner.run(EnhancedPluginType.Server.EXCEPTION, enhancedPluginContext);
|
||||
throw e;
|
||||
}
|
||||
finally {
|
||||
// Run finally enhanced plugins.
|
||||
pluginRunner.run(EnhancedPluginType.Server.FINALLY, enhancedPluginContext);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* PluginOrderConstant.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class PluginOrderConstant {
|
||||
|
||||
public static class ClientPluginOrder {
|
||||
/**
|
||||
* order for
|
||||
* {@link com.tencent.cloud.polaris.circuitbreaker.reporter.SuccessCircuitBreakerReporter}
|
||||
* and
|
||||
* {@link com.tencent.cloud.polaris.circuitbreaker.reporter.ExceptionCircuitBreakerReporter}.
|
||||
*/
|
||||
public static final int CONSUMER_REPORTER_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 1;
|
||||
/**
|
||||
* order for
|
||||
* {@link com.tencent.cloud.rpc.enhancement.plugin.reporter.SuccessPolarisReporter}
|
||||
* and
|
||||
* {@link com.tencent.cloud.rpc.enhancement.plugin.reporter.ExceptionPolarisReporter}.
|
||||
*/
|
||||
public static final int CIRCUIT_BREAKER_REPORTER_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 2;
|
||||
/**
|
||||
* order for
|
||||
* {@link com.tencent.cloud.rpc.enhancement.plugin.assembly.client.AssemblyClientPreHook}
|
||||
* and
|
||||
* {@link com.tencent.cloud.rpc.enhancement.plugin.assembly.client.AssemblyClientPostHook}
|
||||
* and
|
||||
* {@link com.tencent.cloud.rpc.enhancement.plugin.assembly.client.AssemblyClientExceptionHook}.
|
||||
*/
|
||||
public static final int ASSEMBLY_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 3;
|
||||
}
|
||||
|
||||
public static class ServerPluginOrder {
|
||||
/**
|
||||
* order for
|
||||
* {@link com.tencent.cloud.rpc.enhancement.plugin.assembly.server.AssemblyServerPreHook}
|
||||
* and
|
||||
* {@link com.tencent.cloud.rpc.enhancement.plugin.assembly.server.AssemblyServerPostHook}
|
||||
* and
|
||||
* {@link com.tencent.cloud.rpc.enhancement.plugin.assembly.server.AssemblyServerExceptionHook}.
|
||||
*/
|
||||
public static final int ASSEMBLY_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 1;
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin.assembly;
|
||||
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.api.rpc.MetadataProvider;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
/**
|
||||
* AssemblyMetadataProvider.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class AssemblyMetadataProvider implements MetadataProvider {
|
||||
|
||||
private final ServiceInstance serviceInstance;
|
||||
|
||||
private final String namespace;
|
||||
|
||||
public AssemblyMetadataProvider(ServiceInstance localServiceInstance, String namespace) {
|
||||
this.serviceInstance = localServiceInstance;
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMetadata(String key) {
|
||||
return serviceInstance.getMetadata().get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServiceKey getLocalService() {
|
||||
return new ServiceKey(namespace, serviceInstance.getServiceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalIp() {
|
||||
return serviceInstance.getHost();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin.assembly;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.google.common.net.HttpHeaders;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.api.rpc.RequestContext;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
/**
|
||||
* AssemblyRequestContext.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class AssemblyRequestContext implements RequestContext {
|
||||
|
||||
private final EnhancedRequestContext requestContext;
|
||||
|
||||
private final ServiceKey callerService;
|
||||
|
||||
private final String callerIp;
|
||||
|
||||
private final Map<String, String> cookies;
|
||||
|
||||
public AssemblyRequestContext(EnhancedRequestContext requestContext, ServiceKey callerService, String callerIp) {
|
||||
this.requestContext = requestContext;
|
||||
this.callerService = callerService;
|
||||
this.callerIp = callerIp;
|
||||
this.cookies = new HashMap<>();
|
||||
List<String> allCookies =
|
||||
Optional.ofNullable(requestContext.getHttpHeaders().get(HttpHeaders.COOKIE))
|
||||
.orElse(new ArrayList<>())
|
||||
.stream()
|
||||
.flatMap(it -> Arrays.stream(it.split(";")))
|
||||
.toList();
|
||||
allCookies.forEach(cookie -> {
|
||||
String[] cookieKV = cookie.split("=");
|
||||
if (cookieKV.length == 2) {
|
||||
cookies.put(cookieKV[0], cookieKV[1]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethod() {
|
||||
return requestContext.getHttpMethod().name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMethod(String method) {
|
||||
requestContext.setHttpMethod(HttpMethod.valueOf(method));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeader(String key) {
|
||||
return requestContext.getHttpHeaders().getFirst(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeader(String key, String value) {
|
||||
requestContext.getHttpHeaders().set(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> listHeaderKeys() {
|
||||
return requestContext.getHttpHeaders().keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCookie(String key) {
|
||||
return this.cookies.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCookie(String key, String value) {
|
||||
this.cookies.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> listCookieKeys() {
|
||||
return this.cookies.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCallerIp() {
|
||||
return callerIp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServiceKey getCallerService() {
|
||||
return callerService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
return requestContext.getUrl();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin.assembly;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.PolarisEnhancedPluginUtils;
|
||||
import com.tencent.polaris.api.pojo.RetStatus;
|
||||
import com.tencent.polaris.api.rpc.ResponseContext;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* AssemblyResponseContext.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class AssemblyResponseContext implements ResponseContext {
|
||||
|
||||
private final EnhancedResponseContext responseContext;
|
||||
|
||||
private final Throwable throwable;
|
||||
|
||||
private final RetStatus retStatus;
|
||||
|
||||
public AssemblyResponseContext(@Nullable EnhancedResponseContext responseContext, @Nullable Throwable throwable) {
|
||||
this.responseContext = responseContext;
|
||||
this.throwable = throwable;
|
||||
if (responseContext == null) {
|
||||
this.retStatus = PolarisEnhancedPluginUtils.getRetStatusFromRequest(null, null, throwable);
|
||||
}
|
||||
else {
|
||||
this.retStatus = PolarisEnhancedPluginUtils.getRetStatusFromRequest(responseContext.getHttpHeaders(), responseContext.getHttpStatus(), throwable);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRetCode() {
|
||||
if (responseContext == null) {
|
||||
return null;
|
||||
}
|
||||
return this.responseContext.getHttpStatus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeader(String key) {
|
||||
if (responseContext == null) {
|
||||
return null;
|
||||
}
|
||||
return this.responseContext.getHttpHeaders().getFirst(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> listHeaders() {
|
||||
if (responseContext == null) {
|
||||
return null;
|
||||
}
|
||||
return this.responseContext.getHttpHeaders().keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Throwable getThrowable() {
|
||||
return this.throwable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RetStatus getRetStatus() {
|
||||
return this.retStatus;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin.assembly.client;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.PolarisEnhancedPluginUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyMetadataProvider;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyResponseContext;
|
||||
import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import com.tencent.polaris.assembly.api.pojo.AfterRequest;
|
||||
import com.tencent.polaris.assembly.api.pojo.Capability;
|
||||
|
||||
import static com.tencent.cloud.rpc.enhancement.plugin.PluginOrderConstant.ClientPluginOrder.ASSEMBLY_PLUGIN_ORDER;
|
||||
|
||||
/**
|
||||
* AssemblyClientExceptionHook.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class AssemblyClientExceptionHook implements EnhancedPlugin {
|
||||
|
||||
private final AssemblyAPI assemblyAPI;
|
||||
|
||||
private final InstanceTransformer instanceTransformer;
|
||||
|
||||
public AssemblyClientExceptionHook(AssemblyAPI assemblyAPI, InstanceTransformer instanceTransformer) {
|
||||
this.assemblyAPI = assemblyAPI;
|
||||
this.instanceTransformer = instanceTransformer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnhancedPluginType getType() {
|
||||
return EnhancedPluginType.Client.EXCEPTION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(EnhancedPluginContext context) {
|
||||
|
||||
AfterRequest afterRequest = new AfterRequest();
|
||||
afterRequest.setCapabilities(new Capability[]{Capability.ALL});
|
||||
afterRequest.setRequestContext(new AssemblyRequestContext(
|
||||
context.getRequest(),
|
||||
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||
context.getLocalServiceInstance().getHost()
|
||||
));
|
||||
afterRequest.setResponseContext(new AssemblyResponseContext(null, context.getThrowable()));
|
||||
afterRequest.setMetadataProvider(new AssemblyMetadataProvider(context.getLocalServiceInstance(), MetadataContext.LOCAL_NAMESPACE));
|
||||
afterRequest.setDelay(context.getDelay());
|
||||
afterRequest.setRouteLabels(PolarisEnhancedPluginUtils.getLabelMap(context.getRequest().getHttpHeaders()));
|
||||
// TargetService and TargetInstance only exist in client side
|
||||
afterRequest.setTargetService(new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getTargetServiceInstance().getServiceId()));
|
||||
afterRequest.setTargetInstance(instanceTransformer.transform(context.getTargetServiceInstance()));
|
||||
|
||||
assemblyAPI.afterCallService(afterRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return ASSEMBLY_PLUGIN_ORDER;
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin.assembly.client;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.PolarisEnhancedPluginUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyMetadataProvider;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyResponseContext;
|
||||
import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import com.tencent.polaris.assembly.api.pojo.AfterRequest;
|
||||
import com.tencent.polaris.assembly.api.pojo.Capability;
|
||||
|
||||
import static com.tencent.cloud.rpc.enhancement.plugin.PluginOrderConstant.ClientPluginOrder.ASSEMBLY_PLUGIN_ORDER;
|
||||
|
||||
/**
|
||||
* AssemblyClientPostHook.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class AssemblyClientPostHook implements EnhancedPlugin {
|
||||
|
||||
private final AssemblyAPI assemblyAPI;
|
||||
|
||||
private final InstanceTransformer instanceTransformer;
|
||||
|
||||
public AssemblyClientPostHook(AssemblyAPI assemblyAPI, InstanceTransformer instanceTransformer) {
|
||||
this.assemblyAPI = assemblyAPI;
|
||||
this.instanceTransformer = instanceTransformer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnhancedPluginType getType() {
|
||||
return EnhancedPluginType.Client.POST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(EnhancedPluginContext context) {
|
||||
|
||||
AfterRequest afterRequest = new AfterRequest();
|
||||
afterRequest.setCapabilities(new Capability[]{Capability.ALL});
|
||||
afterRequest.setRequestContext(new AssemblyRequestContext(
|
||||
context.getRequest(),
|
||||
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||
context.getLocalServiceInstance().getHost()
|
||||
));
|
||||
afterRequest.setResponseContext(new AssemblyResponseContext(context.getResponse(), null));
|
||||
afterRequest.setMetadataProvider(new AssemblyMetadataProvider(context.getLocalServiceInstance(), MetadataContext.LOCAL_NAMESPACE));
|
||||
afterRequest.setDelay(context.getDelay());
|
||||
afterRequest.setRouteLabels(PolarisEnhancedPluginUtils.getLabelMap(context.getRequest().getHttpHeaders()));
|
||||
// TargetService and TargetInstance only exist in client side
|
||||
afterRequest.setTargetService(new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getTargetServiceInstance().getServiceId()));
|
||||
afterRequest.setTargetInstance(instanceTransformer.transform(context.getTargetServiceInstance()));
|
||||
|
||||
assemblyAPI.afterCallService(afterRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return ASSEMBLY_PLUGIN_ORDER;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin.assembly.client;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyMetadataProvider;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import com.tencent.polaris.assembly.api.pojo.BeforeRequest;
|
||||
import com.tencent.polaris.assembly.api.pojo.Capability;
|
||||
|
||||
import static com.tencent.cloud.rpc.enhancement.plugin.PluginOrderConstant.ClientPluginOrder.ASSEMBLY_PLUGIN_ORDER;
|
||||
|
||||
/**
|
||||
* AssemblyClientPreHook.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class AssemblyClientPreHook implements EnhancedPlugin {
|
||||
|
||||
private final AssemblyAPI assemblyAPI;
|
||||
|
||||
public AssemblyClientPreHook(AssemblyAPI assemblyAPI) {
|
||||
this.assemblyAPI = assemblyAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnhancedPluginType getType() {
|
||||
return EnhancedPluginType.Client.PRE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(EnhancedPluginContext context) {
|
||||
|
||||
BeforeRequest beforeRequest = new BeforeRequest();
|
||||
beforeRequest.setCapabilities(new Capability[]{Capability.ALL});
|
||||
beforeRequest.setRequestContext(new AssemblyRequestContext(
|
||||
context.getRequest(),
|
||||
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||
context.getLocalServiceInstance().getHost()
|
||||
));
|
||||
beforeRequest.setMetadataProvider(new AssemblyMetadataProvider(context.getLocalServiceInstance(), MetadataContext.LOCAL_NAMESPACE));
|
||||
// TargetService only exist in client side
|
||||
beforeRequest.setTargetService(new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getTargetServiceInstance().getServiceId()));
|
||||
|
||||
assemblyAPI.beforeCallService(beforeRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return ASSEMBLY_PLUGIN_ORDER;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin.assembly.server;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.PolarisEnhancedPluginUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyMetadataProvider;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyResponseContext;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import com.tencent.polaris.assembly.api.pojo.AfterRequest;
|
||||
import com.tencent.polaris.assembly.api.pojo.Capability;
|
||||
|
||||
import static com.tencent.cloud.rpc.enhancement.plugin.PluginOrderConstant.ServerPluginOrder.ASSEMBLY_PLUGIN_ORDER;
|
||||
|
||||
|
||||
/**
|
||||
* AssemblyServerExceptionHook.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class AssemblyServerExceptionHook implements EnhancedPlugin {
|
||||
|
||||
private final AssemblyAPI assemblyAPI;
|
||||
|
||||
public AssemblyServerExceptionHook(AssemblyAPI assemblyAPI) {
|
||||
this.assemblyAPI = assemblyAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnhancedPluginType getType() {
|
||||
return EnhancedPluginType.Server.EXCEPTION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(EnhancedPluginContext context) {
|
||||
AfterRequest afterRequest = new AfterRequest();
|
||||
afterRequest.setCapabilities(new Capability[]{Capability.ALL});
|
||||
afterRequest.setRequestContext(new AssemblyRequestContext(
|
||||
context.getRequest(),
|
||||
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||
context.getLocalServiceInstance().getHost()
|
||||
));
|
||||
afterRequest.setResponseContext(new AssemblyResponseContext(null, context.getThrowable()));
|
||||
afterRequest.setMetadataProvider(new AssemblyMetadataProvider(context.getLocalServiceInstance(), MetadataContext.LOCAL_NAMESPACE));
|
||||
afterRequest.setDelay(context.getDelay());
|
||||
afterRequest.setRouteLabels(PolarisEnhancedPluginUtils.getLabelMap(context.getRequest().getHttpHeaders()));
|
||||
|
||||
assemblyAPI.afterProcess(afterRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return ASSEMBLY_PLUGIN_ORDER;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin.assembly.server;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.PolarisEnhancedPluginUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyMetadataProvider;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyResponseContext;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import com.tencent.polaris.assembly.api.pojo.AfterRequest;
|
||||
import com.tencent.polaris.assembly.api.pojo.Capability;
|
||||
|
||||
import static com.tencent.cloud.rpc.enhancement.plugin.PluginOrderConstant.ServerPluginOrder.ASSEMBLY_PLUGIN_ORDER;
|
||||
|
||||
/**
|
||||
* AssemblyServerPostHook.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class AssemblyServerPostHook implements EnhancedPlugin {
|
||||
|
||||
private final AssemblyAPI assemblyAPI;
|
||||
|
||||
public AssemblyServerPostHook(AssemblyAPI assemblyAPI) {
|
||||
this.assemblyAPI = assemblyAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnhancedPluginType getType() {
|
||||
return EnhancedPluginType.Server.POST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(EnhancedPluginContext context) {
|
||||
AfterRequest afterRequest = new AfterRequest();
|
||||
afterRequest.setCapabilities(new Capability[]{Capability.ALL});
|
||||
afterRequest.setRequestContext(new AssemblyRequestContext(
|
||||
context.getRequest(),
|
||||
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||
context.getLocalServiceInstance().getHost()
|
||||
));
|
||||
afterRequest.setResponseContext(new AssemblyResponseContext(context.getResponse(), null));
|
||||
afterRequest.setMetadataProvider(new AssemblyMetadataProvider(context.getLocalServiceInstance(), MetadataContext.LOCAL_NAMESPACE));
|
||||
afterRequest.setDelay(context.getDelay());
|
||||
afterRequest.setRouteLabels(PolarisEnhancedPluginUtils.getLabelMap(context.getRequest().getHttpHeaders()));
|
||||
|
||||
assemblyAPI.afterProcess(afterRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return ASSEMBLY_PLUGIN_ORDER;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin.assembly.server;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyMetadataProvider;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import com.tencent.polaris.assembly.api.pojo.BeforeRequest;
|
||||
import com.tencent.polaris.assembly.api.pojo.Capability;
|
||||
|
||||
import static com.tencent.cloud.rpc.enhancement.plugin.PluginOrderConstant.ServerPluginOrder.ASSEMBLY_PLUGIN_ORDER;
|
||||
|
||||
/**
|
||||
* AssemblyServerPreHook.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class AssemblyServerPreHook implements EnhancedPlugin {
|
||||
|
||||
private final AssemblyAPI assemblyAPI;
|
||||
|
||||
public AssemblyServerPreHook(AssemblyAPI assemblyAPI) {
|
||||
this.assemblyAPI = assemblyAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnhancedPluginType getType() {
|
||||
return EnhancedPluginType.Server.PRE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(EnhancedPluginContext context) {
|
||||
BeforeRequest beforeRequest = new BeforeRequest();
|
||||
beforeRequest.setCapabilities(new Capability[]{Capability.ALL});
|
||||
beforeRequest.setRequestContext(new AssemblyRequestContext(
|
||||
context.getRequest(),
|
||||
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||
context.getLocalServiceInstance().getHost()
|
||||
));
|
||||
beforeRequest.setMetadataProvider(new AssemblyMetadataProvider(context.getLocalServiceInstance(), MetadataContext.LOCAL_NAMESPACE));
|
||||
|
||||
assemblyAPI.beforeProcess(beforeRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return ASSEMBLY_PLUGIN_ORDER;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.feign;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import feign.Request;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE;
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* PolarisLoadBalancerFeignRequestTransformerTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class PolarisLoadBalancerFeignRequestTransformerTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
|
||||
private PolarisLoadBalancerFeignRequestTransformer transformer = new PolarisLoadBalancerFeignRequestTransformer();
|
||||
|
||||
@Mock
|
||||
private Request clientRequest;
|
||||
|
||||
@Mock
|
||||
private ServiceInstance serviceInstance;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
MetadataLocalProperties metadataLocalProperties = mock(MetadataLocalProperties.class);
|
||||
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
|
||||
doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class);
|
||||
doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Throwable {
|
||||
transformer.transformRequest(clientRequest, serviceInstance);
|
||||
assertThat(MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE)).isEqualTo(serviceInstance);
|
||||
}
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.client.AssemblyClientExceptionHook;
|
||||
import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* AssemblyClientExceptionHookTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AssemblyClientExceptionHookTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
@InjectMocks
|
||||
private AssemblyClientExceptionHook assemblyClientExceptionHook;
|
||||
@Mock
|
||||
private AssemblyAPI assemblyAPI;
|
||||
@Mock
|
||||
private InstanceTransformer instanceTransformer;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
RpcEnhancementReporterProperties reporterProperties = mock(RpcEnhancementReporterProperties.class);
|
||||
doReturn(reporterProperties)
|
||||
.when(applicationContext).getBean(RpcEnhancementReporterProperties.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
|
||||
.thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetName() {
|
||||
assertThat(assemblyClientExceptionHook.getName()).isEqualTo(AssemblyClientExceptionHook.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testType() {
|
||||
assertThat(assemblyClientExceptionHook.getType()).isEqualTo(EnhancedPluginType.Client.EXCEPTION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() {
|
||||
EnhancedPluginContext pluginContext = new EnhancedPluginContext();
|
||||
EnhancedRequestContext request = EnhancedRequestContext.builder()
|
||||
.httpMethod(HttpMethod.GET)
|
||||
.url(URI.create("http://0.0.0.0/"))
|
||||
.httpHeaders(new HttpHeaders())
|
||||
.build();
|
||||
request.toString();
|
||||
EnhancedResponseContext response = EnhancedResponseContext.builder()
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
response.toString();
|
||||
|
||||
DefaultServiceInstance targetServiceInstance = new DefaultServiceInstance();
|
||||
targetServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
DefaultServiceInstance localServiceInstance = new DefaultServiceInstance();
|
||||
localServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
pluginContext.setRequest(request);
|
||||
pluginContext.setResponse(response);
|
||||
pluginContext.setTargetServiceInstance(targetServiceInstance);
|
||||
pluginContext.setLocalServiceInstance(localServiceInstance);
|
||||
pluginContext.setThrowable(new RuntimeException());
|
||||
|
||||
assemblyClientExceptionHook.run(pluginContext);
|
||||
assemblyClientExceptionHook.getOrder();
|
||||
assemblyClientExceptionHook.getName();
|
||||
assemblyClientExceptionHook.getType();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandlerThrowable() {
|
||||
// mock request
|
||||
EnhancedRequestContext request = mock(EnhancedRequestContext.class);
|
||||
// mock response
|
||||
EnhancedResponseContext response = mock(EnhancedResponseContext.class);
|
||||
|
||||
EnhancedPluginContext context = new EnhancedPluginContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
assemblyClientExceptionHook.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.client.AssemblyClientPostHook;
|
||||
import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* AssemblyClientPostHookTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AssemblyClientPostHookTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
@InjectMocks
|
||||
private AssemblyClientPostHook assemblyClientPostHook;
|
||||
@Mock
|
||||
private AssemblyAPI assemblyAPI;
|
||||
@Mock
|
||||
private InstanceTransformer instanceTransformer;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
RpcEnhancementReporterProperties reporterProperties = mock(RpcEnhancementReporterProperties.class);
|
||||
doReturn(reporterProperties)
|
||||
.when(applicationContext).getBean(RpcEnhancementReporterProperties.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
|
||||
.thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetName() {
|
||||
assertThat(assemblyClientPostHook.getName()).isEqualTo(AssemblyClientPostHook.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testType() {
|
||||
assertThat(assemblyClientPostHook.getType()).isEqualTo(EnhancedPluginType.Client.POST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() {
|
||||
EnhancedPluginContext pluginContext = new EnhancedPluginContext();
|
||||
EnhancedRequestContext request = EnhancedRequestContext.builder()
|
||||
.httpMethod(HttpMethod.GET)
|
||||
.url(URI.create("http://0.0.0.0/"))
|
||||
.httpHeaders(new HttpHeaders())
|
||||
.build();
|
||||
request.toString();
|
||||
EnhancedResponseContext response = EnhancedResponseContext.builder()
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
response.toString();
|
||||
|
||||
DefaultServiceInstance targetServiceInstance = new DefaultServiceInstance();
|
||||
targetServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
DefaultServiceInstance localServiceInstance = new DefaultServiceInstance();
|
||||
localServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
pluginContext.setRequest(request);
|
||||
pluginContext.setResponse(response);
|
||||
pluginContext.setTargetServiceInstance(targetServiceInstance);
|
||||
pluginContext.setLocalServiceInstance(localServiceInstance);
|
||||
pluginContext.setThrowable(new RuntimeException());
|
||||
|
||||
assemblyClientPostHook.run(pluginContext);
|
||||
assemblyClientPostHook.getOrder();
|
||||
assemblyClientPostHook.getName();
|
||||
assemblyClientPostHook.getType();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandlerThrowable() {
|
||||
// mock request
|
||||
EnhancedRequestContext request = mock(EnhancedRequestContext.class);
|
||||
// mock response
|
||||
EnhancedResponseContext response = mock(EnhancedResponseContext.class);
|
||||
|
||||
EnhancedPluginContext context = new EnhancedPluginContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
assemblyClientPostHook.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.client.AssemblyClientPreHook;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* AssemblyClientPreHookTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AssemblyClientPreHookTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
@InjectMocks
|
||||
private AssemblyClientPreHook assemblyClientPreHook;
|
||||
@Mock
|
||||
private AssemblyAPI assemblyAPI;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
RpcEnhancementReporterProperties reporterProperties = mock(RpcEnhancementReporterProperties.class);
|
||||
doReturn(reporterProperties)
|
||||
.when(applicationContext).getBean(RpcEnhancementReporterProperties.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
|
||||
.thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetName() {
|
||||
assertThat(assemblyClientPreHook.getName()).isEqualTo(AssemblyClientPreHook.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testType() {
|
||||
assertThat(assemblyClientPreHook.getType()).isEqualTo(EnhancedPluginType.Client.PRE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() {
|
||||
EnhancedPluginContext pluginContext = new EnhancedPluginContext();
|
||||
EnhancedRequestContext request = EnhancedRequestContext.builder()
|
||||
.httpMethod(HttpMethod.GET)
|
||||
.url(URI.create("http://0.0.0.0/"))
|
||||
.httpHeaders(new HttpHeaders())
|
||||
.build();
|
||||
request.toString();
|
||||
EnhancedResponseContext response = EnhancedResponseContext.builder()
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
response.toString();
|
||||
|
||||
DefaultServiceInstance targetServiceInstance = new DefaultServiceInstance();
|
||||
targetServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
DefaultServiceInstance localServiceInstance = new DefaultServiceInstance();
|
||||
localServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
pluginContext.setRequest(request);
|
||||
pluginContext.setResponse(response);
|
||||
pluginContext.setTargetServiceInstance(targetServiceInstance);
|
||||
pluginContext.setLocalServiceInstance(localServiceInstance);
|
||||
pluginContext.setThrowable(new RuntimeException());
|
||||
|
||||
assemblyClientPreHook.run(pluginContext);
|
||||
assemblyClientPreHook.getOrder();
|
||||
assemblyClientPreHook.getName();
|
||||
assemblyClientPreHook.getType();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandlerThrowable() {
|
||||
// mock request
|
||||
EnhancedRequestContext request = mock(EnhancedRequestContext.class);
|
||||
// mock response
|
||||
EnhancedResponseContext response = mock(EnhancedResponseContext.class);
|
||||
|
||||
EnhancedPluginContext context = new EnhancedPluginContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
assemblyClientPreHook.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyMetadataProvider;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* AssemblyMetadataProviderTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AssemblyMetadataProviderTest {
|
||||
|
||||
@Test
|
||||
public void testAssemblyMetadataProvider() {
|
||||
ServiceInstance serviceInstance = Mockito.mock(ServiceInstance.class);
|
||||
Map<String, String> metadata = new HashMap<>() {{
|
||||
put("k", "v");
|
||||
}};
|
||||
doReturn(metadata).when(serviceInstance).getMetadata();
|
||||
doReturn("0.0.0.0").when(serviceInstance).getHost();
|
||||
doReturn("test").when(serviceInstance).getServiceId();
|
||||
AssemblyMetadataProvider assemblyMetadataProvider = new AssemblyMetadataProvider(serviceInstance, "test");
|
||||
assertThat(assemblyMetadataProvider.getMetadata("k")).isEqualTo("v");
|
||||
assertThat(assemblyMetadataProvider.getLocalIp()).isEqualTo("0.0.0.0");
|
||||
assertThat(assemblyMetadataProvider.getLocalService()).isEqualTo(new ServiceKey("test", "test"));
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* AssemblyRequestContextTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AssemblyRequestContextTest {
|
||||
|
||||
@Test
|
||||
public void testAssemblyRequestContext() {
|
||||
URI uri = URI.create("http://0.0.0.0/");
|
||||
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.add("a", "a");
|
||||
httpHeaders.add(HttpHeaders.COOKIE, "cookies-k1=cookies-v1;cookies-k2=cookies-v2");
|
||||
|
||||
EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder()
|
||||
.httpMethod(HttpMethod.GET)
|
||||
.url(uri)
|
||||
.httpHeaders(httpHeaders)
|
||||
.build();
|
||||
|
||||
ServiceKey callerService = new ServiceKey("test", "test");
|
||||
AssemblyRequestContext assemblyRequestContext = new AssemblyRequestContext(
|
||||
enhancedRequestContext,
|
||||
callerService,
|
||||
"0.0.0.0"
|
||||
);
|
||||
|
||||
assertThat(assemblyRequestContext.getURI()).isEqualTo(uri);
|
||||
|
||||
assertThat(assemblyRequestContext.getHeader("a")).isEqualTo("a");
|
||||
assemblyRequestContext.setHeader("b", "b");
|
||||
assertThat(assemblyRequestContext.listHeaderKeys()).isEqualTo(new HashSet<>(Arrays.asList(HttpHeaders.COOKIE, "a", "b")));
|
||||
|
||||
assertThat(assemblyRequestContext.getMethod()).isEqualTo(HttpMethod.GET.toString());
|
||||
assemblyRequestContext.setMethod(HttpMethod.OPTIONS.name());
|
||||
assertThat(assemblyRequestContext.getMethod()).isEqualTo(HttpMethod.OPTIONS.toString());
|
||||
|
||||
assertThat(assemblyRequestContext.getCookie("cookies-k1")).isEqualTo("cookies-v1");
|
||||
assertThat(assemblyRequestContext.getCookie("cookies-k2")).isEqualTo("cookies-v2");
|
||||
assemblyRequestContext.setCookie("cookies-k3", "cookies-v3");
|
||||
assertThat(assemblyRequestContext.listCookieKeys()).isEqualTo(new HashSet<>(Arrays.asList("cookies-k1", "cookies-k2", "cookies-k3")));
|
||||
|
||||
assertThat(assemblyRequestContext.getCallerService()).isEqualTo(callerService);
|
||||
assertThat(assemblyRequestContext.getCallerIp()).isEqualTo("0.0.0.0");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyResponseContext;
|
||||
import com.tencent.polaris.api.pojo.RetStatus;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* AssemblyResponseContextTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AssemblyResponseContextTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
RpcEnhancementReporterProperties reporterProperties = mock(RpcEnhancementReporterProperties.class);
|
||||
doReturn(reporterProperties)
|
||||
.when(applicationContext).getBean(RpcEnhancementReporterProperties.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
|
||||
.thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssemblyResponseContext() {
|
||||
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.add("a", "a");
|
||||
|
||||
EnhancedResponseContext enhancedResponseContext = EnhancedResponseContext.builder()
|
||||
.httpHeaders(httpHeaders)
|
||||
.httpStatus(HttpStatus.OK.value())
|
||||
.build();
|
||||
|
||||
AssemblyResponseContext assemblyResponseContext = new AssemblyResponseContext(enhancedResponseContext, null);
|
||||
assertThat(assemblyResponseContext.getHeader("a")).isEqualTo("a");
|
||||
assertThat(assemblyResponseContext.getRetCode()).isEqualTo(HttpStatus.OK.value());
|
||||
assertThat(assemblyResponseContext.getThrowable()).isEqualTo(null);
|
||||
assertThat(assemblyResponseContext.getRetStatus()).isEqualTo(RetStatus.RetSuccess);
|
||||
assertThat(assemblyResponseContext.listHeaders()).isEqualTo(new HashSet<>(List.of("a")));
|
||||
|
||||
Throwable e = new SocketTimeoutException();
|
||||
assemblyResponseContext = new AssemblyResponseContext(null, e);
|
||||
assertThat(assemblyResponseContext.getHeader("a")).isEqualTo(null);
|
||||
assertThat(assemblyResponseContext.getRetCode()).isEqualTo(null);
|
||||
assertThat(assemblyResponseContext.getThrowable()).isEqualTo(e);
|
||||
assertThat(assemblyResponseContext.getRetStatus()).isEqualTo(RetStatus.RetTimeout);
|
||||
assertThat(assemblyResponseContext.listHeaders()).isEqualTo(null);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.server.AssemblyServerExceptionHook;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* AssemblyServerExceptionHookTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AssemblyServerExceptionHookTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
@InjectMocks
|
||||
private AssemblyServerExceptionHook assemblyServerExceptionHook;
|
||||
@Mock
|
||||
private AssemblyAPI assemblyAPI;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
RpcEnhancementReporterProperties reporterProperties = mock(RpcEnhancementReporterProperties.class);
|
||||
doReturn(reporterProperties)
|
||||
.when(applicationContext).getBean(RpcEnhancementReporterProperties.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
|
||||
.thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetName() {
|
||||
assertThat(assemblyServerExceptionHook.getName()).isEqualTo(AssemblyServerExceptionHook.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testType() {
|
||||
assertThat(assemblyServerExceptionHook.getType()).isEqualTo(EnhancedPluginType.Server.EXCEPTION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() {
|
||||
EnhancedPluginContext pluginContext = new EnhancedPluginContext();
|
||||
EnhancedRequestContext request = EnhancedRequestContext.builder()
|
||||
.httpMethod(HttpMethod.GET)
|
||||
.url(URI.create("http://0.0.0.0/"))
|
||||
.httpHeaders(new HttpHeaders())
|
||||
.build();
|
||||
request.toString();
|
||||
EnhancedResponseContext response = EnhancedResponseContext.builder()
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
response.toString();
|
||||
|
||||
DefaultServiceInstance targetServiceInstance = new DefaultServiceInstance();
|
||||
targetServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
DefaultServiceInstance localServiceInstance = new DefaultServiceInstance();
|
||||
localServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
pluginContext.setRequest(request);
|
||||
pluginContext.setResponse(response);
|
||||
pluginContext.setTargetServiceInstance(targetServiceInstance);
|
||||
pluginContext.setLocalServiceInstance(localServiceInstance);
|
||||
pluginContext.setThrowable(new RuntimeException());
|
||||
|
||||
assemblyServerExceptionHook.run(pluginContext);
|
||||
assemblyServerExceptionHook.getOrder();
|
||||
assemblyServerExceptionHook.getName();
|
||||
assemblyServerExceptionHook.getType();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandlerThrowable() {
|
||||
// mock request
|
||||
EnhancedRequestContext request = mock(EnhancedRequestContext.class);
|
||||
// mock response
|
||||
EnhancedResponseContext response = mock(EnhancedResponseContext.class);
|
||||
|
||||
EnhancedPluginContext context = new EnhancedPluginContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
assemblyServerExceptionHook.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.server.AssemblyServerPostHook;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* AssemblyServerPostHookTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AssemblyServerPostHookTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
@InjectMocks
|
||||
private AssemblyServerPostHook assemblyServerPostHook;
|
||||
@Mock
|
||||
private AssemblyAPI assemblyAPI;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
RpcEnhancementReporterProperties reporterProperties = mock(RpcEnhancementReporterProperties.class);
|
||||
doReturn(reporterProperties)
|
||||
.when(applicationContext).getBean(RpcEnhancementReporterProperties.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
|
||||
.thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetName() {
|
||||
assertThat(assemblyServerPostHook.getName()).isEqualTo(AssemblyServerPostHook.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testType() {
|
||||
assertThat(assemblyServerPostHook.getType()).isEqualTo(EnhancedPluginType.Server.POST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() {
|
||||
EnhancedPluginContext pluginContext = new EnhancedPluginContext();
|
||||
EnhancedRequestContext request = EnhancedRequestContext.builder()
|
||||
.httpMethod(HttpMethod.GET)
|
||||
.url(URI.create("http://0.0.0.0/"))
|
||||
.httpHeaders(new HttpHeaders())
|
||||
.build();
|
||||
request.toString();
|
||||
EnhancedResponseContext response = EnhancedResponseContext.builder()
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
response.toString();
|
||||
|
||||
DefaultServiceInstance targetServiceInstance = new DefaultServiceInstance();
|
||||
targetServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
DefaultServiceInstance localServiceInstance = new DefaultServiceInstance();
|
||||
localServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
pluginContext.setRequest(request);
|
||||
pluginContext.setResponse(response);
|
||||
pluginContext.setTargetServiceInstance(targetServiceInstance);
|
||||
pluginContext.setLocalServiceInstance(localServiceInstance);
|
||||
pluginContext.setThrowable(new RuntimeException());
|
||||
|
||||
assemblyServerPostHook.run(pluginContext);
|
||||
assemblyServerPostHook.getOrder();
|
||||
assemblyServerPostHook.getName();
|
||||
assemblyServerPostHook.getType();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandlerThrowable() {
|
||||
// mock request
|
||||
EnhancedRequestContext request = mock(EnhancedRequestContext.class);
|
||||
// mock response
|
||||
EnhancedResponseContext response = mock(EnhancedResponseContext.class);
|
||||
|
||||
EnhancedPluginContext context = new EnhancedPluginContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
assemblyServerPostHook.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.rpc.enhancement.plugin;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.assembly.server.AssemblyServerPreHook;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* AssemblyServerPreHookTest.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AssemblyServerPreHookTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
@InjectMocks
|
||||
private AssemblyServerPreHook assemblyServerPreHook;
|
||||
@Mock
|
||||
private AssemblyAPI assemblyAPI;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
RpcEnhancementReporterProperties reporterProperties = mock(RpcEnhancementReporterProperties.class);
|
||||
doReturn(reporterProperties)
|
||||
.when(applicationContext).getBean(RpcEnhancementReporterProperties.class);
|
||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
|
||||
.thenReturn(applicationContext);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetName() {
|
||||
assertThat(assemblyServerPreHook.getName()).isEqualTo(AssemblyServerPreHook.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testType() {
|
||||
assertThat(assemblyServerPreHook.getType()).isEqualTo(EnhancedPluginType.Server.PRE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() {
|
||||
EnhancedPluginContext pluginContext = new EnhancedPluginContext();
|
||||
EnhancedRequestContext request = EnhancedRequestContext.builder()
|
||||
.httpMethod(HttpMethod.GET)
|
||||
.url(URI.create("http://0.0.0.0/"))
|
||||
.httpHeaders(new HttpHeaders())
|
||||
.build();
|
||||
request.toString();
|
||||
EnhancedResponseContext response = EnhancedResponseContext.builder()
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
response.toString();
|
||||
|
||||
DefaultServiceInstance targetServiceInstance = new DefaultServiceInstance();
|
||||
targetServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
DefaultServiceInstance localServiceInstance = new DefaultServiceInstance();
|
||||
localServiceInstance.setServiceId(SERVICE_PROVIDER);
|
||||
|
||||
pluginContext.setRequest(request);
|
||||
pluginContext.setResponse(response);
|
||||
pluginContext.setTargetServiceInstance(targetServiceInstance);
|
||||
pluginContext.setLocalServiceInstance(localServiceInstance);
|
||||
pluginContext.setThrowable(new RuntimeException());
|
||||
|
||||
assemblyServerPreHook.run(pluginContext);
|
||||
assemblyServerPreHook.getOrder();
|
||||
assemblyServerPreHook.getName();
|
||||
assemblyServerPreHook.getType();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandlerThrowable() {
|
||||
// mock request
|
||||
EnhancedRequestContext request = mock(EnhancedRequestContext.class);
|
||||
// mock response
|
||||
EnhancedResponseContext response = mock(EnhancedResponseContext.class);
|
||||
|
||||
EnhancedPluginContext context = new EnhancedPluginContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
assemblyServerPreHook.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue