修复test bug

This commit is contained in:
eson 2019-07-10 02:04:43 +08:00
parent 7c56c84758
commit e26d0c17a6
3 changed files with 47 additions and 35 deletions

View File

@ -83,12 +83,27 @@ public class ConfigGateway implements RouteDefinitionLocator {
Map<String, Object> configYaml = new Yaml().load((InputStream) inputStream);
if (configYaml != null) {
Map<String, Object> defaultFiltersYaml = (Map<String, Object>) configYaml.get("default-filters");
Map<String, Object> restfulYaml = (Map<String, Object>) configYaml.get("restful");
Map<String, Object> dubboYaml = (Map<String, Object>) configYaml.get("dubbo");
List<String> defaultFiltersYaml = null;
Map<String, Object> restfulYaml = null;
Map<String, Object> dubboYaml = null;
Object unknown;
unknown = configYaml.get("default-filters");
if (unknown != null) {
defaultFiltersYaml = (List<String>) unknown;
}
unknown = configYaml.get("restful");
if (unknown != null) {
restfulYaml = (Map<String, Object>) unknown;
}
unknown = configYaml.get("dubbo");
if (unknown != null) {
dubboYaml = (Map<String, Object>) unknown;
}
List<RouteDefinition> routeList = new ArrayList<RouteDefinition>();
if (restfulYaml != null && !restfulYaml.isEmpty()) {
this.getDefaultFilter(defaultFilters, defaultFiltersYaml);
this.configRestful(routeList, restfulYaml);
@ -110,44 +125,43 @@ public class ConfigGateway implements RouteDefinitionLocator {
/**
*
* @param routeList 路由列表
* @param routeList 路由列表
* @param restfulYaml 配置Yaml.restful对象
*/
@SuppressWarnings("unchecked")
private void configRestful(List<RouteDefinition> routeList, Map<String, Object> restfulYaml) {
if (restfulYaml != null) {
Object unknownRoutes = restfulYaml.get("routes");
if (unknownRoutes != null) {
List<LinkedHashMap<String, List<String>>> routes = (ArrayList<LinkedHashMap<String, List<String>>>) unknownRoutes;
for (LinkedHashMap<String, List<String>> iter : routes) {
Object unknownRoutes = restfulYaml.get("routes");
if (unknownRoutes != null) {
List<LinkedHashMap<String, List<String>>> routes = (ArrayList<LinkedHashMap<String, List<String>>>) unknownRoutes;
for (LinkedHashMap<String, List<String>> iter : routes) {
List<FilterDefinition> filters = new ArrayList<>();
List<PredicateDefinition> predicates = new ArrayList<>();
RouteDefinition rd = new RouteDefinition();
List<FilterDefinition> filters = new ArrayList<>();
List<PredicateDefinition> predicates = new ArrayList<>();
RouteDefinition rd = new RouteDefinition();
// 设置基础属性
this.parseAndSetBase(rd, iter);
// 设置基础属性
this.parseAndSetBase(rd, iter);
// predicates: 下的相关属性
this.parseAndAddPredicates(predicates, iter, "predicates");
// predicates: 下的相关属性
this.parseAndAddPredicates(predicates, iter, "predicates");
// filters: 下的相关属性
this.parseAndAddFilters(filters, iter, "filters");
// filters: 下的相关属性
this.parseAndAddFilters(filters, iter, "filters");
rd.setPredicates(predicates);
rd.setFilters(filters);
rd.setPredicates(predicates);
rd.setFilters(filters);
routeList.add(rd);
}
routeList.add(rd);
}
}
}
/**
*
* @param routeList 路由列表
* @param configDubbo 配置Yaml.dubbo对象
* @param routeList 路由列表
* @param configDubbo 配置Yaml.dubbo对象
*/
@SuppressWarnings("unchecked")
private void configHttp2Dubbo(List<RouteDefinition> routeList, Map<String, Object> configDubbo) {
@ -194,16 +208,14 @@ public class ConfigGateway implements RouteDefinitionLocator {
/**
*
* @param filters {@link #defaultFilters} 全局的默认过滤列表, 可以和Spring Cloud 默认配置一起生效
* @param filters {@link #defaultFilters} 全局的默认过滤列表, 可以和Spring Cloud
* 默认配置一起生效
* @param defaultYaml 配置Yaml.default-filters对象
*/
@SuppressWarnings("unchecked")
private void getDefaultFilter(List<FilterDefinition> filters, Map<String, Object> defaultYaml) {
private void getDefaultFilter(List<FilterDefinition> filters, List<String> defaultFiltersYaml) {
// default-filters: 下的相关设置
Object unknownDefaultFilters = defaultYaml.get("default-filters");
if (unknownDefaultFilters != null) {
List<String> defaultFiltersYaml = (ArrayList<String>) unknownDefaultFilters;
if (defaultFiltersYaml != null) {
for (String filterString : defaultFiltersYaml) {
filters.add(new FilterDefinition(filterString));
}
@ -212,7 +224,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
/***
*
* @param rd 路由列表
* @param rd 路由列表
* @param iter Yaml.restful.routes
*/
private void parseAndSetBase(RouteDefinition rd, LinkedHashMap<String, List<String>> iter) {

View File

@ -1,6 +1,6 @@
default-filters:
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
- AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar
restful:
routes:
- id: path_route

View File

@ -37,7 +37,7 @@ public class TestYaml {
Assert.assertEquals(restfulYaml.getClass(), LinkedHashMap.class);
List<String> defaultFilters = ((Map<String, List<String>>)restfulYaml).get("default-filters");
List<String> defaultFilters = (List<String>)configYaml.get("default-filters");
Assert.assertNotNull(defaultFilters);
Assert.assertEquals(defaultFilters.size(), 1);