mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish alibaba/spring-cloud-alibaba/#1283 : Renaming spring-cloud-starter-alibaba to be spring-cloud-alibaba-starters
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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,
|
||||
* 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.alibaba.cloud.sentinel.datasource;
|
||||
|
||||
import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter;
|
||||
import com.alibaba.cloud.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class ApolloDataSourceFactoryBeanTests {
|
||||
|
||||
private String flowRuleKey = "sentinel";
|
||||
|
||||
private String namespace = "namespace";
|
||||
|
||||
private String defaultFlowValue = "{}";
|
||||
|
||||
@Test
|
||||
public void testApolloFactoryBean() throws Exception {
|
||||
ApolloDataSourceFactoryBean factoryBean = spy(new ApolloDataSourceFactoryBean());
|
||||
|
||||
Converter converter = mock(JsonConverter.class);
|
||||
|
||||
factoryBean.setDefaultFlowRuleValue(defaultFlowValue);
|
||||
factoryBean.setFlowRulesKey(flowRuleKey);
|
||||
factoryBean.setNamespaceName(namespace);
|
||||
factoryBean.setConverter(converter);
|
||||
|
||||
ApolloDataSource apolloDataSource = mock(ApolloDataSource.class);
|
||||
|
||||
when(apolloDataSource.readSource()).thenReturn("{}");
|
||||
doReturn(apolloDataSource).when(factoryBean).getObject();
|
||||
|
||||
assertThat(factoryBean.getObject()).isEqualTo(apolloDataSource);
|
||||
assertThat(factoryBean.getObject().readSource()).isEqualTo("{}");
|
||||
assertThat(factoryBean.getConverter()).isEqualTo(converter);
|
||||
assertThat(factoryBean.getFlowRulesKey()).isEqualTo(flowRuleKey);
|
||||
assertThat(factoryBean.getNamespaceName()).isEqualTo(namespace);
|
||||
assertThat(factoryBean.getDefaultFlowRuleValue()).isEqualTo(defaultFlowValue);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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,
|
||||
* 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.alibaba.cloud.sentinel.datasource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.cloud.sentinel.datasource.config.ApolloDataSourceProperties;
|
||||
import com.alibaba.cloud.sentinel.datasource.config.FileDataSourceProperties;
|
||||
import com.alibaba.cloud.sentinel.datasource.config.ZookeeperDataSourceProperties;
|
||||
import com.alibaba.cloud.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean;
|
||||
import com.alibaba.cloud.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean;
|
||||
import com.alibaba.cloud.sentinel.datasource.factorybean.ZookeeperDataSourceFactoryBean;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class DataSourcePropertiesTests {
|
||||
|
||||
@Test
|
||||
public void testApollo() {
|
||||
ApolloDataSourceProperties apolloDataSourceProperties = new ApolloDataSourceProperties();
|
||||
apolloDataSourceProperties.setFlowRulesKey("test-key");
|
||||
apolloDataSourceProperties.setDefaultFlowRuleValue("dft-val");
|
||||
apolloDataSourceProperties.setNamespaceName("namespace");
|
||||
apolloDataSourceProperties.setRuleType(RuleType.DEGRADE);
|
||||
|
||||
assertThat(apolloDataSourceProperties.getFlowRulesKey()).isEqualTo("test-key");
|
||||
assertThat(apolloDataSourceProperties.getNamespaceName()).isEqualTo("namespace");
|
||||
assertThat(apolloDataSourceProperties.getDataType()).isEqualTo("json");
|
||||
assertThat(apolloDataSourceProperties.getRuleType()).isEqualTo(RuleType.DEGRADE);
|
||||
assertThat(apolloDataSourceProperties.getDefaultFlowRuleValue())
|
||||
.isEqualTo("dft-val");
|
||||
assertThat(apolloDataSourceProperties.getFactoryBeanName())
|
||||
.isEqualTo(ApolloDataSourceFactoryBean.class.getName());
|
||||
assertThat(apolloDataSourceProperties.getConverterClass()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZK() {
|
||||
ZookeeperDataSourceProperties zookeeperDataSourceProperties = new ZookeeperDataSourceProperties();
|
||||
|
||||
zookeeperDataSourceProperties.setServerAddr("localhost:2181");
|
||||
zookeeperDataSourceProperties.setGroupId("groupId");
|
||||
zookeeperDataSourceProperties.setDataId("dataId");
|
||||
zookeeperDataSourceProperties.setPath("/path");
|
||||
zookeeperDataSourceProperties.setConverterClass("test.ConverterClass");
|
||||
zookeeperDataSourceProperties.setRuleType(RuleType.AUTHORITY);
|
||||
|
||||
assertThat(zookeeperDataSourceProperties.getServerAddr())
|
||||
.isEqualTo("localhost:2181");
|
||||
assertThat(zookeeperDataSourceProperties.getGroupId()).isEqualTo("groupId");
|
||||
assertThat(zookeeperDataSourceProperties.getDataId()).isEqualTo("dataId");
|
||||
assertThat(zookeeperDataSourceProperties.getPath()).isEqualTo("/path");
|
||||
assertThat(zookeeperDataSourceProperties.getFactoryBeanName())
|
||||
.isEqualTo(ZookeeperDataSourceFactoryBean.class.getName());
|
||||
assertThat(zookeeperDataSourceProperties.getConverterClass())
|
||||
.isEqualTo("test.ConverterClass");
|
||||
assertThat(zookeeperDataSourceProperties.getRuleType())
|
||||
.isEqualTo(RuleType.AUTHORITY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFileDefaultValue() {
|
||||
FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
|
||||
|
||||
fileDataSourceProperties.setFile("/tmp/test.json");
|
||||
fileDataSourceProperties.setRuleType(RuleType.PARAM_FLOW);
|
||||
|
||||
assertThat(fileDataSourceProperties.getFile()).isEqualTo("/tmp/test.json");
|
||||
assertThat(fileDataSourceProperties.getCharset()).isEqualTo("utf-8");
|
||||
assertThat(fileDataSourceProperties.getRecommendRefreshMs()).isEqualTo(3000L);
|
||||
assertThat(fileDataSourceProperties.getBufSize()).isEqualTo(1024 * 1024);
|
||||
assertThat(fileDataSourceProperties.getFactoryBeanName())
|
||||
.isEqualTo(FileRefreshableDataSourceFactoryBean.class.getName());
|
||||
assertThat(fileDataSourceProperties.getRuleType()).isEqualTo(RuleType.PARAM_FLOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFileCustomValue() {
|
||||
FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
|
||||
|
||||
fileDataSourceProperties.setFile("/tmp/test.json");
|
||||
fileDataSourceProperties.setBufSize(1024);
|
||||
fileDataSourceProperties.setRecommendRefreshMs(2000);
|
||||
fileDataSourceProperties.setCharset("ISO8859-1");
|
||||
|
||||
assertThat(fileDataSourceProperties.getFile()).isEqualTo("/tmp/test.json");
|
||||
assertThat(fileDataSourceProperties.getCharset()).isEqualTo("ISO8859-1");
|
||||
assertThat(fileDataSourceProperties.getRecommendRefreshMs()).isEqualTo(2000L);
|
||||
assertThat(fileDataSourceProperties.getBufSize()).isEqualTo(1024);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void testFileException() {
|
||||
FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
|
||||
fileDataSourceProperties.setFile("classpath: 1.json");
|
||||
fileDataSourceProperties.preCheck("test-ds");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPostRegister() throws Exception {
|
||||
FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
|
||||
|
||||
fileDataSourceProperties.setFile("classpath: flowrule.json");
|
||||
fileDataSourceProperties.setRuleType(RuleType.FLOW);
|
||||
|
||||
FileRefreshableDataSource fileRefreshableDataSource = new FileRefreshableDataSource(
|
||||
ResourceUtils
|
||||
.getFile(StringUtils
|
||||
.trimAllWhitespace(fileDataSourceProperties.getFile()))
|
||||
.getAbsolutePath(),
|
||||
new Converter<String, List<FlowRule>>() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public List<FlowRule> convert(String source) {
|
||||
try {
|
||||
return objectMapper.readValue(source,
|
||||
new TypeReference<List<FlowRule>>() {
|
||||
});
|
||||
}
|
||||
catch (IOException e) {
|
||||
// ignore
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
fileDataSourceProperties.postRegister(fileRefreshableDataSource);
|
||||
assertThat(FlowRuleManager.getRules())
|
||||
.isEqualTo(fileRefreshableDataSource.loadConfig());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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,
|
||||
* 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.alibaba.cloud.sentinel.datasource;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.cloud.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class FileRefreshableDataSourceFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void testFile() throws Exception {
|
||||
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(
|
||||
TestConfig.class);
|
||||
assertThat(annotationConfigApplicationContext.getBean("fileBean")).isNotNull();
|
||||
FileRefreshableDataSource fileRefreshableDataSource = annotationConfigApplicationContext
|
||||
.getBean("fileBean", FileRefreshableDataSource.class);
|
||||
assertThat(((List<FlowRule>) fileRefreshableDataSource.loadConfig()).size())
|
||||
.isEqualTo(1);
|
||||
FileRefreshableDataSourceFactoryBean factoryBean = annotationConfigApplicationContext
|
||||
.getBean("&fileBean", FileRefreshableDataSourceFactoryBean.class);
|
||||
assertThat(factoryBean.getBufSize()).isEqualTo(1024);
|
||||
assertThat(factoryBean.getCharset()).isEqualTo("utf-8");
|
||||
assertThat(factoryBean.getRecommendRefreshMs()).isEqualTo(2000);
|
||||
assertThat(factoryBean.getFile()).isNotNull();
|
||||
assertThat(factoryBean.getConverter()).isNotNull();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig {
|
||||
|
||||
@Bean
|
||||
public FileRefreshableDataSourceFactoryBean fileBean() {
|
||||
FileRefreshableDataSourceFactoryBean factoryBean = new FileRefreshableDataSourceFactoryBean();
|
||||
factoryBean.setBufSize(1024);
|
||||
factoryBean.setCharset("utf-8");
|
||||
factoryBean.setRecommendRefreshMs(2000);
|
||||
try {
|
||||
factoryBean.setFile(ResourceUtils.getFile("classpath:flowrule.json")
|
||||
.getAbsolutePath());
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
// ignore
|
||||
}
|
||||
factoryBean.setConverter(buildConverter());
|
||||
return factoryBean;
|
||||
}
|
||||
|
||||
private Converter buildConverter() {
|
||||
return new Converter<String, List<FlowRule>>() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public List<FlowRule> convert(String source) {
|
||||
try {
|
||||
return objectMapper.readValue(source,
|
||||
new TypeReference<List<FlowRule>>() {
|
||||
});
|
||||
}
|
||||
catch (IOException e) {
|
||||
// ignore
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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,
|
||||
* 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.alibaba.cloud.sentinel.datasource;
|
||||
|
||||
import com.alibaba.cloud.sentinel.datasource.converter.SentinelConverter;
|
||||
import com.alibaba.cloud.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class NacosDataSourceFactoryBeanTests {
|
||||
|
||||
private String dataId = "sentinel";
|
||||
|
||||
private String groupId = "DEFAULT_GROUP";
|
||||
|
||||
private String serverAddr = "localhost:8848";
|
||||
|
||||
private String accessKey = "ak";
|
||||
|
||||
private String secretKey = "sk";
|
||||
|
||||
private String endpoint = "endpoint";
|
||||
|
||||
private String namespace = "namespace";
|
||||
|
||||
@Test
|
||||
public void testNacosFactoryBeanServerAddr() throws Exception {
|
||||
NacosDataSourceFactoryBean factoryBean = spy(new NacosDataSourceFactoryBean());
|
||||
|
||||
Converter converter = mock(SentinelConverter.class);
|
||||
|
||||
factoryBean.setDataId(dataId);
|
||||
factoryBean.setGroupId(groupId);
|
||||
factoryBean.setServerAddr(serverAddr);
|
||||
factoryBean.setConverter(converter);
|
||||
|
||||
NacosDataSource nacosDataSource = mock(NacosDataSource.class);
|
||||
|
||||
doReturn(nacosDataSource).when(factoryBean).getObject();
|
||||
when(nacosDataSource.readSource()).thenReturn("{}");
|
||||
|
||||
assertThat(factoryBean.getObject()).isEqualTo(nacosDataSource);
|
||||
assertThat(factoryBean.getObject().readSource()).isEqualTo("{}");
|
||||
assertThat(factoryBean.getConverter()).isEqualTo(converter);
|
||||
assertThat(factoryBean.getDataId()).isEqualTo(dataId);
|
||||
assertThat(factoryBean.getGroupId()).isEqualTo(groupId);
|
||||
assertThat(factoryBean.getServerAddr()).isEqualTo(serverAddr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNacosFactoryBeanProperties() throws Exception {
|
||||
NacosDataSourceFactoryBean factoryBean = spy(new NacosDataSourceFactoryBean());
|
||||
|
||||
Converter converter = mock(SentinelConverter.class);
|
||||
|
||||
factoryBean.setDataId(dataId);
|
||||
factoryBean.setGroupId(groupId);
|
||||
factoryBean.setAccessKey(accessKey);
|
||||
factoryBean.setSecretKey(secretKey);
|
||||
factoryBean.setEndpoint(endpoint);
|
||||
factoryBean.setNamespace(namespace);
|
||||
factoryBean.setConverter(converter);
|
||||
|
||||
NacosDataSource nacosDataSource = mock(NacosDataSource.class);
|
||||
|
||||
doReturn(nacosDataSource).when(factoryBean).getObject();
|
||||
when(nacosDataSource.readSource()).thenReturn("{}");
|
||||
|
||||
assertThat(factoryBean.getObject()).isEqualTo(nacosDataSource);
|
||||
assertThat(factoryBean.getObject().readSource()).isEqualTo("{}");
|
||||
assertThat(factoryBean.getConverter()).isEqualTo(converter);
|
||||
assertThat(factoryBean.getDataId()).isEqualTo(dataId);
|
||||
assertThat(factoryBean.getGroupId()).isEqualTo(groupId);
|
||||
assertThat(factoryBean.getNamespace()).isEqualTo(namespace);
|
||||
assertThat(factoryBean.getEndpoint()).isEqualTo(endpoint);
|
||||
assertThat(factoryBean.getAccessKey()).isEqualTo(accessKey);
|
||||
assertThat(factoryBean.getSecretKey()).isEqualTo(secretKey);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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,
|
||||
* 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.alibaba.cloud.sentinel.datasource;
|
||||
|
||||
import com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties;
|
||||
import com.alibaba.cloud.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class NacosDataSourcePropertiesTests {
|
||||
|
||||
@Test
|
||||
public void testNacosWithAddr() {
|
||||
NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
|
||||
nacosDataSourceProperties.setServerAddr("127.0.0.1:8848");
|
||||
nacosDataSourceProperties.setRuleType(RuleType.FLOW);
|
||||
nacosDataSourceProperties.setDataId("sentinel");
|
||||
nacosDataSourceProperties.setGroupId("custom-group");
|
||||
nacosDataSourceProperties.setDataType("xml");
|
||||
|
||||
assertThat(nacosDataSourceProperties.getGroupId()).isEqualTo("custom-group");
|
||||
assertThat(nacosDataSourceProperties.getDataId()).isEqualTo("sentinel");
|
||||
assertThat(nacosDataSourceProperties.getDataType()).isEqualTo("xml");
|
||||
assertThat(nacosDataSourceProperties.getRuleType()).isEqualTo(RuleType.FLOW);
|
||||
assertThat(nacosDataSourceProperties.getFactoryBeanName())
|
||||
.isEqualTo(NacosDataSourceFactoryBean.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNacosWithProperties() {
|
||||
NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
|
||||
nacosDataSourceProperties.setAccessKey("ak");
|
||||
nacosDataSourceProperties.setSecretKey("sk");
|
||||
nacosDataSourceProperties.setEndpoint("endpoint");
|
||||
nacosDataSourceProperties.setNamespace("namespace");
|
||||
nacosDataSourceProperties.setRuleType(RuleType.SYSTEM);
|
||||
|
||||
assertThat(nacosDataSourceProperties.getAccessKey()).isEqualTo("ak");
|
||||
assertThat(nacosDataSourceProperties.getSecretKey()).isEqualTo("sk");
|
||||
assertThat(nacosDataSourceProperties.getEndpoint()).isEqualTo("endpoint");
|
||||
assertThat(nacosDataSourceProperties.getNamespace()).isEqualTo("namespace");
|
||||
assertThat(nacosDataSourceProperties.getRuleType()).isEqualTo(RuleType.SYSTEM);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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,
|
||||
* 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.alibaba.cloud.sentinel.datasource;
|
||||
|
||||
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.system.SystemRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class RuleTypeTests {
|
||||
|
||||
@Test
|
||||
public void testGetByName() {
|
||||
assertThat(RuleType.getByName("").isPresent()).isEqualTo(Boolean.FALSE);
|
||||
assertThat(RuleType.getByName("test").isPresent()).isEqualTo(Boolean.FALSE);
|
||||
assertThat(RuleType.getByName("param_flow").isPresent()).isEqualTo(Boolean.FALSE);
|
||||
assertThat(RuleType.getByName("param").isPresent()).isEqualTo(Boolean.FALSE);
|
||||
assertThat(RuleType.getByName("FLOW").isPresent()).isEqualTo(Boolean.FALSE);
|
||||
assertThat(RuleType.getByName("flow").isPresent()).isEqualTo(Boolean.TRUE);
|
||||
assertThat(RuleType.getByName("degrade").isPresent()).isEqualTo(Boolean.TRUE);
|
||||
assertThat(RuleType.getByName("param-flow").isPresent()).isEqualTo(Boolean.TRUE);
|
||||
assertThat(RuleType.getByName("system").isPresent()).isEqualTo(Boolean.TRUE);
|
||||
assertThat(RuleType.getByName("authority").isPresent()).isEqualTo(Boolean.TRUE);
|
||||
assertThat(RuleType.getByName("flow").get()).isEqualTo(RuleType.FLOW);
|
||||
assertThat(RuleType.getByName("degrade").get()).isEqualTo(RuleType.DEGRADE);
|
||||
assertThat(RuleType.getByName("param-flow").get()).isEqualTo(RuleType.PARAM_FLOW);
|
||||
assertThat(RuleType.getByName("system").get()).isEqualTo(RuleType.SYSTEM);
|
||||
assertThat(RuleType.getByName("authority").get()).isEqualTo(RuleType.AUTHORITY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetByClass() {
|
||||
assertThat(RuleType.getByClass(Object.class).isPresent())
|
||||
.isEqualTo(Boolean.FALSE);
|
||||
assertThat(RuleType.getByClass(AbstractRule.class).isPresent())
|
||||
.isEqualTo(Boolean.FALSE);
|
||||
assertThat(RuleType.getByClass(FlowRule.class).isPresent())
|
||||
.isEqualTo(Boolean.TRUE);
|
||||
assertThat(RuleType.getByClass(DegradeRule.class).isPresent())
|
||||
.isEqualTo(Boolean.TRUE);
|
||||
assertThat(RuleType.getByClass(ParamFlowRule.class).isPresent())
|
||||
.isEqualTo(Boolean.TRUE);
|
||||
assertThat(RuleType.getByClass(SystemRule.class).isPresent())
|
||||
.isEqualTo(Boolean.TRUE);
|
||||
assertThat(RuleType.getByClass(AuthorityRule.class).isPresent())
|
||||
.isEqualTo(Boolean.TRUE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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,
|
||||
* 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.alibaba.cloud.sentinel.datasource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter;
|
||||
import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class SentinelConverterTests {
|
||||
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private XmlMapper xmlMapper = new XmlMapper();
|
||||
|
||||
@Test
|
||||
public void testJsonConverter() {
|
||||
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
||||
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter
|
||||
.convert(readFileContent("classpath: flowrule.json"));
|
||||
|
||||
assertThat(flowRules.size()).isEqualTo(1);
|
||||
assertThat(flowRules.get(0).getResource()).isEqualTo("resource");
|
||||
assertThat(flowRules.get(0).getLimitApp()).isEqualTo("default");
|
||||
assertThat(String.valueOf(flowRules.get(0).getCount())).isEqualTo("1.0");
|
||||
assertThat(flowRules.get(0).getControlBehavior())
|
||||
.isEqualTo(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
|
||||
assertThat(flowRules.get(0).getStrategy())
|
||||
.isEqualTo(RuleConstant.STRATEGY_DIRECT);
|
||||
assertThat(flowRules.get(0).getGrade()).isEqualTo(RuleConstant.FLOW_GRADE_QPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConverterEmptyContent() {
|
||||
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
||||
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter.convert("");
|
||||
assertThat(flowRules.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void testConverterErrorFormat() {
|
||||
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
||||
jsonConverter.convert(readFileContent("classpath: flowrule-errorformat.json"));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void testConverterErrorContent() {
|
||||
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
||||
jsonConverter.convert(readFileContent("classpath: flowrule-errorcontent.json"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXmlConverter() {
|
||||
XmlConverter jsonConverter = new XmlConverter(xmlMapper, FlowRule.class);
|
||||
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter
|
||||
.convert(readFileContent("classpath: flowrule.xml"));
|
||||
|
||||
assertThat(flowRules.size()).isEqualTo(2);
|
||||
assertThat(flowRules.get(0).getResource()).isEqualTo("resource");
|
||||
assertThat(flowRules.get(0).getLimitApp()).isEqualTo("default");
|
||||
assertThat(String.valueOf(flowRules.get(0).getCount())).isEqualTo("1.0");
|
||||
assertThat(flowRules.get(0).getControlBehavior())
|
||||
.isEqualTo(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
|
||||
assertThat(flowRules.get(0).getStrategy())
|
||||
.isEqualTo(RuleConstant.STRATEGY_DIRECT);
|
||||
assertThat(flowRules.get(0).getGrade()).isEqualTo(RuleConstant.FLOW_GRADE_QPS);
|
||||
|
||||
assertThat(flowRules.get(1).getResource()).isEqualTo("test");
|
||||
assertThat(flowRules.get(1).getLimitApp()).isEqualTo("default");
|
||||
assertThat(String.valueOf(flowRules.get(1).getCount())).isEqualTo("1.0");
|
||||
assertThat(flowRules.get(1).getControlBehavior())
|
||||
.isEqualTo(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
|
||||
assertThat(flowRules.get(1).getStrategy())
|
||||
.isEqualTo(RuleConstant.STRATEGY_DIRECT);
|
||||
assertThat(flowRules.get(1).getGrade()).isEqualTo(RuleConstant.FLOW_GRADE_QPS);
|
||||
}
|
||||
|
||||
private String readFileContent(String file) {
|
||||
try {
|
||||
return FileUtils.readFileToString(
|
||||
ResourceUtils.getFile(StringUtils.trimAllWhitespace(file)));
|
||||
}
|
||||
catch (IOException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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,
|
||||
* 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.alibaba.cloud.sentinel.datasource;
|
||||
|
||||
import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter;
|
||||
import com.alibaba.cloud.sentinel.datasource.factorybean.ZookeeperDataSourceFactoryBean;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class ZookeeperDataSourceFactoryBeanTests {
|
||||
|
||||
private String dataId = "dataId";
|
||||
|
||||
private String groupId = "groupId";
|
||||
|
||||
private String serverAddr = "localhost:2181";
|
||||
|
||||
private String path = "/sentinel";
|
||||
|
||||
@Test
|
||||
public void testZKWithoutPathFactoryBean() throws Exception {
|
||||
ZookeeperDataSourceFactoryBean factoryBean = spy(
|
||||
ZookeeperDataSourceFactoryBean.class);
|
||||
|
||||
Converter converter = mock(XmlConverter.class);
|
||||
|
||||
ZookeeperDataSource zookeeperDataSource = mock(ZookeeperDataSource.class);
|
||||
|
||||
factoryBean.setConverter(converter);
|
||||
factoryBean.setDataId(dataId);
|
||||
factoryBean.setGroupId(groupId);
|
||||
factoryBean.setServerAddr(serverAddr);
|
||||
|
||||
when(zookeeperDataSource.readSource()).thenReturn("{}");
|
||||
doReturn(zookeeperDataSource).when(factoryBean).getObject();
|
||||
|
||||
assertThat(factoryBean.getObject()).isEqualTo(zookeeperDataSource);
|
||||
assertThat(factoryBean.getObject().readSource()).isEqualTo("{}");
|
||||
assertThat(factoryBean.getDataId()).isEqualTo(dataId);
|
||||
assertThat(factoryBean.getConverter()).isEqualTo(converter);
|
||||
assertThat(factoryBean.getGroupId()).isEqualTo(groupId);
|
||||
assertThat(factoryBean.getServerAddr()).isEqualTo(serverAddr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZKWithPathFactoryBean() throws Exception {
|
||||
ZookeeperDataSourceFactoryBean factoryBean = spy(
|
||||
ZookeeperDataSourceFactoryBean.class);
|
||||
|
||||
Converter converter = mock(XmlConverter.class);
|
||||
|
||||
ZookeeperDataSource zookeeperDataSource = mock(ZookeeperDataSource.class);
|
||||
|
||||
factoryBean.setConverter(converter);
|
||||
factoryBean.setPath(path);
|
||||
factoryBean.setServerAddr(serverAddr);
|
||||
|
||||
when(zookeeperDataSource.readSource()).thenReturn("{}");
|
||||
doReturn(zookeeperDataSource).when(factoryBean).getObject();
|
||||
|
||||
assertThat(factoryBean.getObject()).isEqualTo(zookeeperDataSource);
|
||||
assertThat(factoryBean.getObject().readSource()).isEqualTo("{}");
|
||||
assertThat(factoryBean.getConverter()).isEqualTo(converter);
|
||||
assertThat(factoryBean.getPath()).isEqualTo(path);
|
||||
assertThat(factoryBean.getServerAddr()).isEqualTo(serverAddr);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
[
|
||||
{
|
||||
"test": 1,
|
||||
"controlBehavior": 0,
|
||||
"count": 1,
|
||||
"grade": 1,
|
||||
"limitApp": "default",
|
||||
"strategy": 0
|
||||
}
|
||||
]
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"resource": "resource",
|
||||
"controlBehavior": 0,
|
||||
"count": 1,
|
||||
"grade": 1,
|
||||
"limitApp": "default",
|
||||
"strategy": 0
|
||||
}==
|
||||
]
|
||||
@@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"resource": "resource",
|
||||
"controlBehavior": 0,
|
||||
"count": 1,
|
||||
"grade": 1,
|
||||
"limitApp": "default",
|
||||
"strategy": 0
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Rules>
|
||||
<FlowRule>
|
||||
<resource>resource</resource>
|
||||
<controlBehavior>0</controlBehavior>
|
||||
<count>1</count>
|
||||
<grade>1</grade>
|
||||
<limitApp>default</limitApp>
|
||||
<strategy>0</strategy>
|
||||
</FlowRule>
|
||||
<FlowRule>
|
||||
<resource>test</resource>
|
||||
<controlBehavior>0</controlBehavior>
|
||||
<count>1</count>
|
||||
<grade>1</grade>
|
||||
<limitApp>default</limitApp>
|
||||
<strategy>0</strategy>
|
||||
</FlowRule>
|
||||
</Rules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user