mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
format code with maven plugins
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -16,13 +16,10 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
@@ -37,19 +34,16 @@ import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Add this unit test to verify https://github.com/alibaba/spring-cloud-alibaba/pull/838
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { ContextIdSentinelFeignTests.TestConfig.class }, properties = {
|
||||
"feign.sentinel.enabled=true" })
|
||||
@SpringBootTest(classes = { ContextIdSentinelFeignTests.TestConfig.class },
|
||||
properties = { "feign.sentinel.enabled=true" })
|
||||
public class ContextIdSentinelFeignTests {
|
||||
|
||||
@Autowired
|
||||
@@ -60,16 +54,11 @@ public class ContextIdSentinelFeignTests {
|
||||
|
||||
@Test
|
||||
public void testFeignClient() {
|
||||
assertEquals("Sentinel Feign Client fallback success", "echo fallback",
|
||||
echoService.echo("test"));
|
||||
assertEquals("Sentinel Feign Client fallbackFactory success", "foo fallback",
|
||||
fooService.echo("test"));
|
||||
assertNotEquals("ToString method invoke was not in SentinelInvocationHandler",
|
||||
echoService.toString(), fooService.toString());
|
||||
assertNotEquals("HashCode method invoke was not in SentinelInvocationHandler",
|
||||
echoService.hashCode(), fooService.hashCode());
|
||||
assertFalse("Equals method invoke was not in SentinelInvocationHandler",
|
||||
echoService.equals(fooService));
|
||||
assertThat(echoService.echo("test")).isEqualTo("echo fallback");
|
||||
assertThat(fooService.echo("test")).isEqualTo("foo fallback");
|
||||
assertThat(fooService.toString()).isNotEqualTo(echoService.toString());
|
||||
assertThat(fooService.hashCode()).isNotEqualTo(echoService.hashCode());
|
||||
assertThat(echoService.equals(fooService)).isEqualTo(Boolean.FALSE);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -80,16 +69,24 @@ public class ContextIdSentinelFeignTests {
|
||||
|
||||
}
|
||||
|
||||
@FeignClient(contextId = "echoService", name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
|
||||
@FeignClient(contextId = "echoService", name = "service-provider",
|
||||
fallback = EchoServiceFallback.class,
|
||||
configuration = FeignConfiguration.class)
|
||||
public interface EchoService {
|
||||
@GetMapping(value = "/echo/{str}")
|
||||
|
||||
@GetMapping("/echo/{str}")
|
||||
String echo(@PathVariable("str") String str);
|
||||
|
||||
}
|
||||
|
||||
@FeignClient(contextId = "fooService", value = "foo-service", fallbackFactory = CustomFallbackFactory.class, configuration = FeignConfiguration.class)
|
||||
@FeignClient(contextId = "fooService", value = "foo-service",
|
||||
fallbackFactory = CustomFallbackFactory.class,
|
||||
configuration = FeignConfiguration.class)
|
||||
public interface FooService {
|
||||
|
||||
@RequestMapping(path = "echo/{str}")
|
||||
String echo(@RequestParam("str") String param);
|
||||
|
||||
}
|
||||
|
||||
public static class FeignConfiguration {
|
||||
@@ -121,6 +118,7 @@ public class ContextIdSentinelFeignTests {
|
||||
public String echo(@RequestParam("str") String param) {
|
||||
return "foo fallback";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class CustomFallbackFactory
|
||||
@@ -132,6 +130,7 @@ public class ContextIdSentinelFeignTests {
|
||||
public FooService create(Throwable throwable) {
|
||||
return fooService;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -16,18 +16,28 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
|
||||
import com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint;
|
||||
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
|
||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.log.LogBase;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
@@ -45,30 +55,18 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
|
||||
import com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint;
|
||||
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
|
||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.log.LogBase;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
* @author jiashuai.xie
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = {
|
||||
SentinelAutoConfigurationTests.TestConfig.class }, properties = {
|
||||
"spring.cloud.sentinel.filter.order=123",
|
||||
@SpringBootTest(classes = { SentinelAutoConfigurationTests.TestConfig.class },
|
||||
properties = { "spring.cloud.sentinel.filter.order=123",
|
||||
"spring.cloud.sentinel.filter.urlPatterns=/*,/test",
|
||||
"spring.cloud.sentinel.metric.fileSingleSize=9999",
|
||||
"spring.cloud.sentinel.metric.fileTotalCount=100",
|
||||
@@ -79,7 +77,8 @@ import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
"spring.cloud.sentinel.transport.dashboard=http://localhost:8080",
|
||||
"spring.cloud.sentinel.transport.port=9999",
|
||||
"spring.cloud.sentinel.transport.clientIp=1.1.1.1",
|
||||
"spring.cloud.sentinel.transport.heartbeatIntervalMs=20000" }, webEnvironment = RANDOM_PORT)
|
||||
"spring.cloud.sentinel.transport.heartbeatIntervalMs=20000" },
|
||||
webEnvironment = RANDOM_PORT)
|
||||
public class SentinelAutoConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
@@ -131,10 +130,10 @@ public class SentinelAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
assertNotNull("FilterRegistrationBean was not created", filterRegistrationBean);
|
||||
assertNotNull("SentinelProperties was not created", sentinelProperties);
|
||||
assertNotNull("SentinelBeanPostProcessor was not created",
|
||||
sentinelBeanPostProcessor);
|
||||
|
||||
assertThat(filterRegistrationBean).isNotNull();
|
||||
assertThat(filterRegistrationBean).isNotNull();
|
||||
assertThat(sentinelBeanPostProcessor).isNotNull();
|
||||
|
||||
checkSentinelLog();
|
||||
checkSentinelEager();
|
||||
@@ -148,139 +147,110 @@ public class SentinelAutoConfigurationTests {
|
||||
private void checkEndpoint() {
|
||||
SentinelEndpoint sentinelEndpoint = new SentinelEndpoint(sentinelProperties);
|
||||
Map<String, Object> map = sentinelEndpoint.invoke();
|
||||
assertEquals("Endpoint Sentinel log pid was wrong", true, map.get("logUsePid"));
|
||||
assertEquals("Endpoint Sentinel transport console server was wrong",
|
||||
"http://localhost:8080", map.get("consoleServer"));
|
||||
assertEquals("Endpoint Sentinel transport port was wrong", "9999",
|
||||
map.get("clientPort"));
|
||||
assertEquals("Endpoint Sentinel transport heartbeatIntervalMs was wrong", 20000l,
|
||||
map.get("heartbeatIntervalMs"));
|
||||
assertEquals("Endpoint Sentinel transport clientIp was wrong", "1.1.1.1",
|
||||
map.get("clientIp"));
|
||||
assertEquals("Endpoint Sentinel metric file size was wrong", 9999l,
|
||||
map.get("metricsFileSize"));
|
||||
assertEquals("Endpoint Sentinel metric file count was wrong", 100,
|
||||
map.get("totalMetricsFileCount"));
|
||||
assertEquals("Endpoint Sentinel metric file charset was wrong", "UTF-8",
|
||||
map.get("metricsFileCharset"));
|
||||
assertEquals("Endpoint Sentinel block page was wrong", "/error",
|
||||
map.get("blockPage"));
|
||||
|
||||
assertThat(map.get("logUsePid")).isEqualTo(Boolean.TRUE);
|
||||
assertThat(map.get("consoleServer")).isEqualTo("http://localhost:8080");
|
||||
assertThat(map.get("clientPort")).isEqualTo("9999");
|
||||
assertThat(map.get("heartbeatIntervalMs")).isEqualTo(20000L);
|
||||
assertThat(map.get("clientIp")).isEqualTo("1.1.1.1");
|
||||
assertThat(map.get("metricsFileSize")).isEqualTo(9999L);
|
||||
assertThat(map.get("totalMetricsFileCount")).isEqualTo(100);
|
||||
assertThat(map.get("metricsFileCharset")).isEqualTo("UTF-8");
|
||||
assertThat(map.get("blockPage")).isEqualTo("/error");
|
||||
}
|
||||
|
||||
private void checkSentinelFilter() {
|
||||
assertEquals("SentinelProperties filter order was wrong", 123,
|
||||
sentinelProperties.getFilter().getOrder());
|
||||
assertEquals("SentinelProperties filter url pattern size was wrong", 2,
|
||||
sentinelProperties.getFilter().getUrlPatterns().size());
|
||||
assertEquals("SentinelProperties filter url pattern item was wrong", "/*",
|
||||
sentinelProperties.getFilter().getUrlPatterns().get(0));
|
||||
assertEquals("SentinelProperties filter url pattern item was wrong", "/test",
|
||||
sentinelProperties.getFilter().getUrlPatterns().get(1));
|
||||
assertThat(sentinelProperties.getFilter().getOrder()).isEqualTo(123);
|
||||
assertThat(sentinelProperties.getFilter().getUrlPatterns().size()).isEqualTo(2);
|
||||
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0))
|
||||
.isEqualTo("/*");
|
||||
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(1))
|
||||
.isEqualTo("/test");
|
||||
}
|
||||
|
||||
private void checkSentinelMetric() {
|
||||
assertEquals("SentinelProperties metric charset was wrong", "UTF-8",
|
||||
sentinelProperties.getMetric().getCharset());
|
||||
assertEquals("SentinelProperties metric file single size was wrong", "9999",
|
||||
sentinelProperties.getMetric().getFileSingleSize());
|
||||
assertEquals("SentinelProperties metric file total count was wrong", "100",
|
||||
sentinelProperties.getMetric().getFileTotalCount());
|
||||
assertThat(sentinelProperties.getMetric().getCharset()).isEqualTo("UTF-8");
|
||||
assertThat(sentinelProperties.getMetric().getFileSingleSize()).isEqualTo("9999");
|
||||
assertThat(sentinelProperties.getMetric().getFileTotalCount()).isEqualTo("100");
|
||||
}
|
||||
|
||||
private void checkSentinelColdFactor() {
|
||||
assertEquals("SentinelProperties coldFactor was wrong", "3",
|
||||
sentinelProperties.getFlow().getColdFactor());
|
||||
assertThat(sentinelProperties.getFlow().getColdFactor()).isEqualTo("3");
|
||||
}
|
||||
|
||||
private void checkSentinelTransport() {
|
||||
assertEquals("SentinelProperties transport port was wrong", "9999",
|
||||
sentinelProperties.getTransport().getPort());
|
||||
assertEquals("SentinelProperties transport dashboard was wrong",
|
||||
"http://localhost:8080",
|
||||
sentinelProperties.getTransport().getDashboard());
|
||||
assertEquals("SentinelProperties transport clientIp was wrong", "1.1.1.1",
|
||||
sentinelProperties.getTransport().getClientIp());
|
||||
assertEquals("SentinelProperties transport heartbeatIntervalMs was wrong",
|
||||
"20000", sentinelProperties.getTransport().getHeartbeatIntervalMs());
|
||||
assertThat(sentinelProperties.getTransport().getPort()).isEqualTo("9999");
|
||||
assertThat(sentinelProperties.getTransport().getDashboard())
|
||||
.isEqualTo("http://localhost:8080");
|
||||
assertThat(sentinelProperties.getTransport().getClientIp()).isEqualTo("1.1.1.1");
|
||||
assertThat(sentinelProperties.getTransport().getHeartbeatIntervalMs())
|
||||
.isEqualTo("20000");
|
||||
}
|
||||
|
||||
private void checkSentinelEager() {
|
||||
assertEquals("SentinelProperties eager was wrong", true,
|
||||
sentinelProperties.isEager());
|
||||
assertThat(sentinelProperties.isEager()).isEqualTo(true);
|
||||
}
|
||||
|
||||
private void checkSentinelLog() {
|
||||
assertEquals("SentinelProperties log file pid was wrong", true,
|
||||
sentinelProperties.getLog().isSwitchPid());
|
||||
assertThat(sentinelProperties.getLog().isSwitchPid()).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilter() {
|
||||
assertEquals("Sentinel Filter order was wrong", filterRegistrationBean.getOrder(),
|
||||
123);
|
||||
assertEquals("Sentinel Filter url-pattern was wrong",
|
||||
filterRegistrationBean.getUrlPatterns().size(), 2);
|
||||
assertThat(123).isEqualTo(filterRegistrationBean.getOrder());
|
||||
assertThat(2).isEqualTo(filterRegistrationBean.getUrlPatterns().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelSystemProperties() {
|
||||
assertEquals("Sentinel log pid was wrong", true, LogBase.isLogNameUsePid());
|
||||
assertEquals("Sentinel transport console server was wrong",
|
||||
"http://localhost:8080", TransportConfig.getConsoleServer());
|
||||
assertEquals("Sentinel transport port was wrong", "9999",
|
||||
TransportConfig.getPort());
|
||||
assertEquals("Sentinel transport heartbeatIntervalMs was wrong", 20000l,
|
||||
TransportConfig.getHeartbeatIntervalMs().longValue());
|
||||
assertEquals("Sentinel transport clientIp was wrong", "1.1.1.1",
|
||||
TransportConfig.getHeartbeatClientIp());
|
||||
assertEquals("Sentinel metric file size was wrong", 9999,
|
||||
SentinelConfig.singleMetricFileSize());
|
||||
assertEquals("Sentinel metric file count was wrong", 100,
|
||||
SentinelConfig.totalMetricFileCount());
|
||||
assertEquals("Sentinel metric file charset was wrong", "UTF-8",
|
||||
SentinelConfig.charset());
|
||||
assertEquals("Sentinel block page was wrong", "/error",
|
||||
WebServletConfig.getBlockPage());
|
||||
assertThat(LogBase.isLogNameUsePid()).isEqualTo(true);
|
||||
assertThat(TransportConfig.getConsoleServer()).isEqualTo("http://localhost:8080");
|
||||
assertThat(TransportConfig.getPort()).isEqualTo("9999");
|
||||
assertThat(TransportConfig.getHeartbeatIntervalMs().longValue())
|
||||
.isEqualTo(20000L);
|
||||
assertThat(TransportConfig.getHeartbeatClientIp()).isEqualTo("1.1.1.1");
|
||||
assertThat(SentinelConfig.singleMetricFileSize()).isEqualTo(9999);
|
||||
assertThat(SentinelConfig.totalMetricFileCount()).isEqualTo(100);
|
||||
assertThat(SentinelConfig.charset()).isEqualTo("UTF-8");
|
||||
assertThat(WebServletConfig.getBlockPage()).isEqualTo("/error");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlowRestTemplate() {
|
||||
assertEquals("RestTemplate interceptors size was wrong", 2,
|
||||
restTemplate.getInterceptors().size());
|
||||
assertEquals("RestTemplateWithBlockClass interceptors size was wrong", 1,
|
||||
restTemplateWithBlockClass.getInterceptors().size());
|
||||
|
||||
assertThat(restTemplate.getInterceptors().size()).isEqualTo(2);
|
||||
assertThat(restTemplateWithBlockClass.getInterceptors().size()).isEqualTo(1);
|
||||
|
||||
ResponseEntity responseEntityBlock = restTemplateWithBlockClass
|
||||
.getForEntity(flowUrl, String.class);
|
||||
assertEquals("RestTemplateWithBlockClass Sentinel Block Message was wrong",
|
||||
"Oops", responseEntityBlock.getBody());
|
||||
assertEquals(
|
||||
"RestTemplateWithBlockClass Sentinel Block Http Status Code was wrong",
|
||||
HttpStatus.OK, responseEntityBlock.getStatusCode());
|
||||
|
||||
assertThat(responseEntityBlock.getBody()).isEqualTo("Oops");
|
||||
assertThat(responseEntityBlock.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
ResponseEntity responseEntityRaw = restTemplate.getForEntity(flowUrl,
|
||||
String.class);
|
||||
assertEquals("RestTemplate Sentinel Block Message was wrong",
|
||||
"RestTemplate request block by sentinel", responseEntityRaw.getBody());
|
||||
assertEquals("RestTemplate Sentinel Block Http Status Code was wrong",
|
||||
HttpStatus.OK, responseEntityRaw.getStatusCode());
|
||||
|
||||
assertThat(responseEntityRaw.getBody())
|
||||
.isEqualTo("RestTemplate request block by sentinel");
|
||||
assertThat(responseEntityRaw.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalRestTemplate() {
|
||||
assertEquals("RestTemplateWithoutBlockClass interceptors size was wrong", 0,
|
||||
restTemplateWithoutBlockClass.getInterceptors().size());
|
||||
assertThatExceptionOfType(RestClientException.class).isThrownBy(() -> {
|
||||
assertThat(restTemplateWithoutBlockClass.getInterceptors().size()).isEqualTo(0);
|
||||
|
||||
assertThatThrownBy(() -> {
|
||||
restTemplateWithoutBlockClass.getForEntity(flowUrl, String.class);
|
||||
});
|
||||
}).isInstanceOf(RestClientException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFallbackRestTemplate() {
|
||||
ResponseEntity responseEntity = restTemplateWithFallbackClass
|
||||
.getForEntity(degradeUrl, String.class);
|
||||
assertEquals("RestTemplateWithFallbackClass Sentinel Message was wrong",
|
||||
"Oops fallback", responseEntity.getBody());
|
||||
assertEquals("RestTemplateWithFallbackClass Sentinel Http Status Code was wrong",
|
||||
HttpStatus.OK, responseEntity.getStatusCode());
|
||||
|
||||
assertThat(responseEntity.getBody()).isEqualTo("Oops fallback");
|
||||
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -295,13 +265,15 @@ public class SentinelAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(blockHandlerClass = ExceptionUtil.class, blockHandler = "handleException")
|
||||
@SentinelRestTemplate(blockHandlerClass = ExceptionUtil.class,
|
||||
blockHandler = "handleException")
|
||||
RestTemplate restTemplateWithBlockClass() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(fallbackClass = ExceptionUtil.class, fallback = "fallbackException")
|
||||
@SentinelRestTemplate(fallbackClass = ExceptionUtil.class,
|
||||
fallback = "fallbackException")
|
||||
RestTemplate restTemplateWithFallbackClass() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
@@ -314,6 +286,7 @@ public class SentinelAutoConfigurationTests {
|
||||
}
|
||||
|
||||
public static class ExceptionUtil {
|
||||
|
||||
public static SentinelClientHttpResponse handleException(HttpRequest request,
|
||||
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
|
||||
System.out.println("Oops: " + ex.getClass().getCanonicalName());
|
||||
@@ -325,6 +298,7 @@ public class SentinelAutoConfigurationTests {
|
||||
System.out.println("Oops: " + ex.getClass().getCanonicalName());
|
||||
return new SentinelClientHttpResponse("Oops fallback");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -332,6 +306,7 @@ public class SentinelAutoConfigurationTests {
|
||||
@ImportAutoConfiguration({ SentinelAutoConfiguration.class,
|
||||
SentinelWebAutoConfiguration.class, SentinelTestConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -16,24 +16,11 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
|
||||
@@ -41,13 +28,25 @@ import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.util.FilterUtil;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { SentinelBeanAutowiredTests.TestConfig.class }, properties = {
|
||||
"spring.cloud.sentinel.filter.order=111" })
|
||||
@SpringBootTest(classes = { SentinelBeanAutowiredTests.TestConfig.class },
|
||||
properties = { "spring.cloud.sentinel.filter.order=111" })
|
||||
public class SentinelBeanAutowiredTests {
|
||||
|
||||
@Autowired
|
||||
@@ -64,31 +63,27 @@ public class SentinelBeanAutowiredTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
assertNotNull("UrlCleaner was not created", urlCleaner);
|
||||
assertNotNull("UrlBlockHandler was not created", urlBlockHandler);
|
||||
assertNotNull("RequestOriginParser was not created", requestOriginParser);
|
||||
assertNotNull("SentinelProperties was not created", sentinelProperties);
|
||||
assertThat(urlCleaner).isNotNull();
|
||||
assertThat(urlBlockHandler).isNotNull();
|
||||
assertThat(requestOriginParser).isNotNull();
|
||||
assertThat(sentinelProperties).isNotNull();
|
||||
|
||||
checkUrlPattern();
|
||||
}
|
||||
|
||||
private void checkUrlPattern() {
|
||||
assertEquals("SentinelProperties filter order was wrong", 111,
|
||||
sentinelProperties.getFilter().getOrder());
|
||||
assertEquals("SentinelProperties filter url pattern size was wrong", 1,
|
||||
sentinelProperties.getFilter().getUrlPatterns().size());
|
||||
assertEquals("SentinelProperties filter url pattern was wrong", "/*",
|
||||
sentinelProperties.getFilter().getUrlPatterns().get(0));
|
||||
assertThat(sentinelProperties.getFilter().getOrder()).isEqualTo(111);
|
||||
assertThat(sentinelProperties.getFilter().getUrlPatterns().size()).isEqualTo(1);
|
||||
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0))
|
||||
.isEqualTo("/*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeanAutowired() {
|
||||
assertEquals("UrlCleaner was not autowired", urlCleaner,
|
||||
WebCallbackManager.getUrlCleaner());
|
||||
assertEquals("UrlBlockHandler was not autowired", urlBlockHandler,
|
||||
WebCallbackManager.getUrlBlockHandler());
|
||||
assertEquals("RequestOriginParser was not autowired", requestOriginParser,
|
||||
WebCallbackManager.getRequestOriginParser());
|
||||
assertThat(WebCallbackManager.getUrlCleaner()).isEqualTo(urlCleaner);
|
||||
assertThat(WebCallbackManager.getUrlBlockHandler()).isEqualTo(urlBlockHandler);
|
||||
assertThat(WebCallbackManager.getRequestOriginParser())
|
||||
.isEqualTo(requestOriginParser);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -16,12 +16,11 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
|
||||
import com.alibaba.cloud.sentinel.datasource.RuleType;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
@@ -29,8 +28,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
|
||||
import com.alibaba.cloud.sentinel.datasource.RuleType;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
@@ -62,38 +60,31 @@ public class SentinelDataSourceTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
assertNotNull("SentinelProperties was not created", sentinelProperties);
|
||||
assertThat(sentinelProperties).isNotNull();
|
||||
|
||||
checkUrlPattern();
|
||||
}
|
||||
|
||||
private void checkUrlPattern() {
|
||||
assertEquals("SentinelProperties filter order was wrong", Integer.MIN_VALUE,
|
||||
sentinelProperties.getFilter().getOrder());
|
||||
assertEquals("SentinelProperties filter url pattern size was wrong", 1,
|
||||
sentinelProperties.getFilter().getUrlPatterns().size());
|
||||
assertEquals("SentinelProperties filter url pattern was wrong", "/*",
|
||||
sentinelProperties.getFilter().getUrlPatterns().get(0));
|
||||
assertThat(sentinelProperties.getFilter().getOrder())
|
||||
.isEqualTo(Integer.MIN_VALUE);
|
||||
assertThat(sentinelProperties.getFilter().getUrlPatterns().size()).isEqualTo(1);
|
||||
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0))
|
||||
.isEqualTo("/*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDataSource() {
|
||||
assertEquals("DataSource size was wrong", 5,
|
||||
sentinelProperties.getDatasource().size());
|
||||
assertNull("DataSource ds1 apollo is not null",
|
||||
sentinelProperties.getDatasource().get("ds1").getApollo());
|
||||
assertNull("DataSource ds1 nacos is not null",
|
||||
sentinelProperties.getDatasource().get("ds1").getNacos());
|
||||
assertNull("DataSource ds1 zk is not null",
|
||||
sentinelProperties.getDatasource().get("ds1").getZk());
|
||||
assertNotNull("DataSource ds1 file is null",
|
||||
sentinelProperties.getDatasource().get("ds1").getFile());
|
||||
|
||||
assertEquals("DataSource ds1 file dataType was wrong", "json",
|
||||
sentinelProperties.getDatasource().get("ds1").getFile().getDataType());
|
||||
assertEquals("DataSource ds1 file ruleType was wrong", RuleType.FLOW,
|
||||
sentinelProperties.getDatasource().get("ds1").getFile().getRuleType());
|
||||
assertThat(sentinelProperties.getDatasource().size()).isEqualTo(5);
|
||||
assertThat(sentinelProperties.getDatasource().get("ds1").getApollo()).isNull();
|
||||
assertThat(sentinelProperties.getDatasource().get("ds1").getNacos()).isNull();
|
||||
assertThat(sentinelProperties.getDatasource().get("ds1").getZk()).isNull();
|
||||
assertThat(sentinelProperties.getDatasource().get("ds1").getFile()).isNotNull();
|
||||
|
||||
assertThat(sentinelProperties.getDatasource().get("ds1").getFile().getDataType())
|
||||
.isEqualTo("json");
|
||||
assertThat(sentinelProperties.getDatasource().get("ds1").getFile().getRuleType())
|
||||
.isEqualTo(RuleType.FLOW);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -16,17 +16,16 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
@@ -39,17 +38,15 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { SentinelFeignTests.TestConfig.class }, properties = {
|
||||
"feign.sentinel.enabled=true" })
|
||||
@SpringBootTest(classes = { SentinelFeignTests.TestConfig.class },
|
||||
properties = { "feign.sentinel.enabled=true" })
|
||||
public class SentinelFeignTests {
|
||||
|
||||
@Autowired
|
||||
@@ -99,29 +96,26 @@ public class SentinelFeignTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
assertNotNull("EchoService was not created", echoService);
|
||||
assertNotNull("FooService was not created", fooService);
|
||||
assertThat(echoService).isNotNull();
|
||||
assertThat(fooService).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFeignClient() {
|
||||
assertEquals("Sentinel Feign Client fallback success", "echo fallback",
|
||||
echoService.echo("test"));
|
||||
assertEquals("Sentinel Feign Client fallbackFactory success", "foo fallback",
|
||||
fooService.echo("test"));
|
||||
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
|
||||
barService.bar();
|
||||
});
|
||||
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
|
||||
bazService.baz();
|
||||
});
|
||||
assertThat(echoService.echo("test")).isEqualTo("echo fallback");
|
||||
assertThat(fooService.echo("test")).isEqualTo("foo fallback");
|
||||
|
||||
assertNotEquals("ToString method invoke was not in SentinelInvocationHandler",
|
||||
echoService.toString(), fooService.toString());
|
||||
assertNotEquals("HashCode method invoke was not in SentinelInvocationHandler",
|
||||
echoService.hashCode(), fooService.hashCode());
|
||||
assertFalse("Equals method invoke was not in SentinelInvocationHandler",
|
||||
echoService.equals(fooService));
|
||||
assertThatThrownBy(() -> {
|
||||
barService.bar();
|
||||
}).isInstanceOf(Exception.class);
|
||||
|
||||
assertThatThrownBy(() -> {
|
||||
bazService.baz();
|
||||
}).isInstanceOf(Exception.class);
|
||||
|
||||
assertThat(fooService.toString()).isNotEqualTo(echoService.toString());
|
||||
assertThat(fooService.hashCode()).isNotEqualTo(echoService.hashCode());
|
||||
assertThat(echoService.equals(fooService)).isEqualTo(Boolean.FALSE);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -144,29 +138,38 @@ public class SentinelFeignTests {
|
||||
|
||||
@FeignClient(value = "test-service", fallback = EchoServiceFallback.class)
|
||||
public interface EchoService {
|
||||
|
||||
@RequestMapping(path = "echo/{str}")
|
||||
String echo(@RequestParam("str") String param);
|
||||
|
||||
}
|
||||
|
||||
@FeignClient(value = "foo-service", fallbackFactory = CustomFallbackFactory.class)
|
||||
public interface FooService {
|
||||
|
||||
@RequestMapping(path = "echo/{str}")
|
||||
String echo(@RequestParam("str") String param);
|
||||
|
||||
}
|
||||
|
||||
@FeignClient(value = "bar-service")
|
||||
@FeignClient("bar-service")
|
||||
public interface BarService {
|
||||
|
||||
@RequestMapping(path = "bar")
|
||||
String bar();
|
||||
|
||||
}
|
||||
|
||||
public interface BazService {
|
||||
|
||||
@RequestMapping(path = "baz")
|
||||
String baz();
|
||||
|
||||
}
|
||||
|
||||
@FeignClient(value = "baz-service")
|
||||
@FeignClient("baz-service")
|
||||
public interface BazClient extends BazService {
|
||||
|
||||
}
|
||||
|
||||
public static class EchoServiceFallback implements EchoService {
|
||||
@@ -184,6 +187,7 @@ public class SentinelFeignTests {
|
||||
public String echo(@RequestParam("str") String param) {
|
||||
return "foo fallback";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class CustomFallbackFactory
|
||||
@@ -195,6 +199,7 @@ public class SentinelFeignTests {
|
||||
public FooService create(Throwable throwable) {
|
||||
return fooService;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -16,9 +16,12 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
|
||||
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@@ -28,10 +31,7 @@ import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
|
||||
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
@@ -62,8 +62,7 @@ public class SentinelRestTemplateTests {
|
||||
public void testNormal() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
TestConfig5.class);
|
||||
assertEquals("RestTemplate size was wrong", 1,
|
||||
context.getBeansOfType(RestTemplate.class).size());
|
||||
assertThat(context.getBeansOfType(RestTemplate.class).size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test(expected = BeanCreationException.class)
|
||||
@@ -113,6 +112,7 @@ public class SentinelRestTemplateTests {
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig1 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -124,10 +124,12 @@ public class SentinelRestTemplateTests {
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig2 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -139,10 +141,12 @@ public class SentinelRestTemplateTests {
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig3 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -154,10 +158,12 @@ public class SentinelRestTemplateTests {
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig4 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -169,10 +175,12 @@ public class SentinelRestTemplateTests {
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig5 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -180,14 +188,22 @@ public class SentinelRestTemplateTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class, blockHandler = "handleException", fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class, fallback = "fallbackException", urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class, urlCleaner = "clean")
|
||||
@SentinelRestTemplate(
|
||||
blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class,
|
||||
blockHandler = "handleException",
|
||||
fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class,
|
||||
fallback = "fallbackException",
|
||||
urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class,
|
||||
urlCleaner = "clean")
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig6 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -195,14 +211,18 @@ public class SentinelRestTemplateTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class, blockHandler = "handleException1")
|
||||
@SentinelRestTemplate(
|
||||
blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class,
|
||||
blockHandler = "handleException1")
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig7 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -210,14 +230,18 @@ public class SentinelRestTemplateTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class, fallback = "fallbackException1")
|
||||
@SentinelRestTemplate(
|
||||
fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class,
|
||||
fallback = "fallbackException1")
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig8 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -225,14 +249,18 @@ public class SentinelRestTemplateTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class, blockHandler = "handleException2")
|
||||
@SentinelRestTemplate(
|
||||
blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class,
|
||||
blockHandler = "handleException2")
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig9 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -240,14 +268,18 @@ public class SentinelRestTemplateTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class, fallback = "fallbackException2")
|
||||
@SentinelRestTemplate(
|
||||
fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class,
|
||||
fallback = "fallbackException2")
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig10 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -265,10 +297,12 @@ public class SentinelRestTemplateTests {
|
||||
RestTemplate restTemplate2() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig11 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -280,10 +314,12 @@ public class SentinelRestTemplateTests {
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig12 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -295,10 +331,12 @@ public class SentinelRestTemplateTests {
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig13 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -306,14 +344,18 @@ public class SentinelRestTemplateTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class, urlCleaner = "clean1")
|
||||
@SentinelRestTemplate(
|
||||
urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class,
|
||||
urlCleaner = "clean1")
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig14 {
|
||||
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
@@ -321,13 +363,17 @@ public class SentinelRestTemplateTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class, urlCleaner = "clean2")
|
||||
@SentinelRestTemplate(
|
||||
urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class,
|
||||
urlCleaner = "clean2")
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class ExceptionUtil {
|
||||
|
||||
public static SentinelClientHttpResponse handleException(HttpRequest request,
|
||||
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
|
||||
System.out.println("Oops: " + ex.getClass().getCanonicalName());
|
||||
@@ -349,15 +395,18 @@ public class SentinelRestTemplateTests {
|
||||
ClientHttpRequestExecution execution, BlockException ex) {
|
||||
System.out.println("Oops: " + ex.getClass().getCanonicalName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class UrlCleanUtil {
|
||||
|
||||
public static String clean(String url) {
|
||||
return url;
|
||||
}
|
||||
|
||||
public static void clean2(String url) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -21,7 +21,6 @@ import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@@ -29,6 +28,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class TestConverter implements Converter<String, List<ParamFlowRule>> {
|
||||
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
@@ -42,4 +42,5 @@ public class TestConverter implements Converter<String, List<ParamFlowRule>> {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -16,21 +16,10 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel.endpoint;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.alibaba.cloud.sentinel.SentinelProperties;
|
||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
||||
@@ -38,6 +27,17 @@ import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
|
||||
import com.alibaba.csp.sentinel.heartbeat.HeartbeatSenderProvider;
|
||||
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
|
||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test cases for {@link SentinelHealthIndicator}.
|
||||
@@ -171,4 +171,5 @@ public class SentinelHealthIndicatorTests {
|
||||
assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource"))
|
||||
.isEqualTo(new Status(Status.DOWN.getCode(), "fileDataSource2 error"));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user