mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
add sentinel datasource test case
This commit is contained in:
parent
3ab6b1dc63
commit
6a758dd42f
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
|
import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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();
|
||||||
|
|
||||||
|
assertEquals("ApolloDataSourceFactoryBean getObject error", apolloDataSource,
|
||||||
|
factoryBean.getObject());
|
||||||
|
assertEquals("ApolloDataSource read source value was wrong", "{}",
|
||||||
|
factoryBean.getObject().readSource());
|
||||||
|
assertEquals("ApolloDataSource converter was wrong", converter,
|
||||||
|
factoryBean.getConverter());
|
||||||
|
assertEquals("ApolloDataSourceFactoryBean flowRuleKey was wrong", flowRuleKey,
|
||||||
|
factoryBean.getFlowRulesKey());
|
||||||
|
assertEquals("ApolloDataSourceFactoryBean namespace was wrong", namespace,
|
||||||
|
factoryBean.getNamespaceName());
|
||||||
|
assertEquals("ApolloDataSourceFactoryBean defaultFlowValue was wrong",
|
||||||
|
defaultFlowValue, factoryBean.getDefaultFlowRuleValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,230 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.ApolloDataSourceProperties;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.FileDataSourceProperties;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.ZookeeperDataSourceProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
public class DataSourcePropertiesConfigurationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFileAttr() {
|
||||||
|
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
|
||||||
|
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
|
||||||
|
dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
|
||||||
|
FileDataSourceProperties fileDataSourceProperties = buildFileProperties();
|
||||||
|
|
||||||
|
dataSourcePropertiesConfiguration.setFile(fileDataSourceProperties);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"DataSourcePropertiesConfiguration valid field size was wrong after set file attribute",
|
||||||
|
1, dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration file properties was null after set file attribute",
|
||||||
|
dataSourcePropertiesConfiguration.getFile());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration valid properties was null after set file attribute",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosAttr() {
|
||||||
|
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
|
||||||
|
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
|
||||||
|
dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = buildNacosProperties();
|
||||||
|
|
||||||
|
dataSourcePropertiesConfiguration.setNacos(nacosDataSourceProperties);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"DataSourcePropertiesConfiguration valid field size was wrong after set nacos attribute",
|
||||||
|
1, dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration nacos properties was null after set nacos attribute",
|
||||||
|
dataSourcePropertiesConfiguration.getNacos());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration valid properties was null after set nacos attribute",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testZKAttr() {
|
||||||
|
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
|
||||||
|
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
|
||||||
|
dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
|
||||||
|
ZookeeperDataSourceProperties zookeeperDataSourceProperties = buildZKProperties();
|
||||||
|
|
||||||
|
dataSourcePropertiesConfiguration.setZk(zookeeperDataSourceProperties);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"DataSourcePropertiesConfiguration valid field size was wrong after set zk attribute",
|
||||||
|
1, dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration zk properties was null after set zk attribute",
|
||||||
|
dataSourcePropertiesConfiguration.getZk());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration valid properties was null after set zk attribute",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApolloAttr() {
|
||||||
|
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
|
||||||
|
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
|
||||||
|
dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
|
||||||
|
ApolloDataSourceProperties apolloDataSourceProperties = buildApolloProperties();
|
||||||
|
|
||||||
|
dataSourcePropertiesConfiguration.setApollo(apolloDataSourceProperties);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"DataSourcePropertiesConfiguration valid field size was wrong after set apollo attribute",
|
||||||
|
1, dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration apollo properties was null after set apollo attribute",
|
||||||
|
dataSourcePropertiesConfiguration.getApollo());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration valid properties was null after set apollo attribute",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMultiAttr() {
|
||||||
|
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
|
||||||
|
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
|
||||||
|
dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
|
||||||
|
FileDataSourceProperties fileDataSourceProperties = buildFileProperties();
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = buildNacosProperties();
|
||||||
|
|
||||||
|
dataSourcePropertiesConfiguration.setFile(fileDataSourceProperties);
|
||||||
|
dataSourcePropertiesConfiguration.setNacos(nacosDataSourceProperties);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"DataSourcePropertiesConfiguration valid field size was wrong after set file and nacos attribute",
|
||||||
|
2, dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNull(
|
||||||
|
"DataSourcePropertiesConfiguration valid properties was not null after set file and nacos attribute",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFileConstructor() {
|
||||||
|
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
|
||||||
|
buildFileProperties());
|
||||||
|
assertEquals(
|
||||||
|
"DataSourcePropertiesConfiguration file constructor valid field size was wrong",
|
||||||
|
1, dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration file constructor valid properties was null",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosConstructor() {
|
||||||
|
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
|
||||||
|
buildNacosProperties());
|
||||||
|
assertEquals(
|
||||||
|
"DataSourcePropertiesConfiguration nacos constructor valid field size was wrong",
|
||||||
|
1, dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration nacos constructor valid properties was null",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApolloConstructor() {
|
||||||
|
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
|
||||||
|
buildApolloProperties());
|
||||||
|
assertEquals(
|
||||||
|
"DataSourcePropertiesConfiguration apollo constructor valid field size was wrong",
|
||||||
|
1, dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration apollo constructor valid properties was null",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testZKConstructor() {
|
||||||
|
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
|
||||||
|
buildZKProperties());
|
||||||
|
assertEquals(
|
||||||
|
"DataSourcePropertiesConfiguration zk constructor valid field size was wrong",
|
||||||
|
1, dataSourcePropertiesConfiguration.getValidField().size());
|
||||||
|
assertNotNull(
|
||||||
|
"DataSourcePropertiesConfiguration zk constructor valid properties was null",
|
||||||
|
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileDataSourceProperties buildFileProperties() {
|
||||||
|
FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
|
||||||
|
|
||||||
|
fileDataSourceProperties.setFile("/tmp/test.json");
|
||||||
|
fileDataSourceProperties.setBufSize(1024);
|
||||||
|
fileDataSourceProperties.setRecommendRefreshMs(2000);
|
||||||
|
return fileDataSourceProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
private NacosDataSourceProperties buildNacosProperties() {
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
|
||||||
|
nacosDataSourceProperties.setServerAddr("127.0.0.1:8848");
|
||||||
|
nacosDataSourceProperties.setDataId("sentinel");
|
||||||
|
nacosDataSourceProperties.setGroupId("custom-group");
|
||||||
|
return nacosDataSourceProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApolloDataSourceProperties buildApolloProperties() {
|
||||||
|
ApolloDataSourceProperties apolloDataSourceProperties = new ApolloDataSourceProperties();
|
||||||
|
apolloDataSourceProperties.setFlowRulesKey("test-key");
|
||||||
|
apolloDataSourceProperties.setDefaultFlowRuleValue("dft-val");
|
||||||
|
apolloDataSourceProperties.setNamespaceName("namespace");
|
||||||
|
return apolloDataSourceProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ZookeeperDataSourceProperties buildZKProperties() {
|
||||||
|
ZookeeperDataSourceProperties zookeeperDataSourceProperties = new ZookeeperDataSourceProperties();
|
||||||
|
|
||||||
|
zookeeperDataSourceProperties.setServerAddr("localhost:2181");
|
||||||
|
zookeeperDataSourceProperties.setPath("/path");
|
||||||
|
return zookeeperDataSourceProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,180 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
|
import org.codehaus.jackson.type.TypeReference;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.ApolloDataSourceProperties;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.FileDataSourceProperties;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.ZookeeperDataSourceProperties;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ZookeeperDataSourceFactoryBean;
|
||||||
|
import org.springframework.util.ResourceUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
|
assertEquals("Apollo flow rule key was wrong", "test-key",
|
||||||
|
apolloDataSourceProperties.getFlowRulesKey());
|
||||||
|
assertEquals("Apollo namespace was wrong", "namespace",
|
||||||
|
apolloDataSourceProperties.getNamespaceName());
|
||||||
|
assertEquals("Apollo default data type was wrong", "json",
|
||||||
|
apolloDataSourceProperties.getDataType());
|
||||||
|
assertEquals("Apollo rule type was wrong", RuleType.DEGRADE,
|
||||||
|
apolloDataSourceProperties.getRuleType());
|
||||||
|
assertEquals("Apollo default flow value was wrong", "dft-val",
|
||||||
|
apolloDataSourceProperties.getDefaultFlowRuleValue());
|
||||||
|
assertEquals("Apollo factory bean was wrong",
|
||||||
|
ApolloDataSourceFactoryBean.class.getName(),
|
||||||
|
apolloDataSourceProperties.getFactoryBeanName());
|
||||||
|
assertNull("Apollo converterClass was not null",
|
||||||
|
apolloDataSourceProperties.getConverterClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
|
||||||
|
assertEquals("ZK serverAddr was wrong", "localhost:2181",
|
||||||
|
zookeeperDataSourceProperties.getServerAddr());
|
||||||
|
assertEquals("ZK groupId was wrong", "groupId",
|
||||||
|
zookeeperDataSourceProperties.getGroupId());
|
||||||
|
assertEquals("ZK dataId was wrong", "dataId",
|
||||||
|
zookeeperDataSourceProperties.getDataId());
|
||||||
|
assertEquals("ZK path was wrong", "/path",
|
||||||
|
zookeeperDataSourceProperties.getPath());
|
||||||
|
assertEquals("ZK factory bean was wrong",
|
||||||
|
ZookeeperDataSourceFactoryBean.class.getName(),
|
||||||
|
zookeeperDataSourceProperties.getFactoryBeanName());
|
||||||
|
assertEquals("ZK custom converter class was wrong", "test.ConverterClass",
|
||||||
|
zookeeperDataSourceProperties.getConverterClass());
|
||||||
|
assertEquals("ZK rule type was wrong", RuleType.AUTHORITY,
|
||||||
|
zookeeperDataSourceProperties.getRuleType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFileDefaultValue() {
|
||||||
|
FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
|
||||||
|
|
||||||
|
fileDataSourceProperties.setFile("/tmp/test.json");
|
||||||
|
fileDataSourceProperties.setRuleType(RuleType.PARAM_FLOW);
|
||||||
|
|
||||||
|
assertEquals("File path was wrong", "/tmp/test.json",
|
||||||
|
fileDataSourceProperties.getFile());
|
||||||
|
assertEquals("File charset was wrong", "utf-8",
|
||||||
|
fileDataSourceProperties.getCharset());
|
||||||
|
assertEquals("File refresh time was wrong", 3000L,
|
||||||
|
fileDataSourceProperties.getRecommendRefreshMs());
|
||||||
|
assertEquals("File buf size was wrong", 1024 * 1024,
|
||||||
|
fileDataSourceProperties.getBufSize());
|
||||||
|
assertEquals("File factory bean was wrong",
|
||||||
|
FileRefreshableDataSourceFactoryBean.class.getName(),
|
||||||
|
fileDataSourceProperties.getFactoryBeanName());
|
||||||
|
assertEquals("File rule type was wrong", RuleType.PARAM_FLOW,
|
||||||
|
fileDataSourceProperties.getRuleType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFileCustomValue() {
|
||||||
|
FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
|
||||||
|
|
||||||
|
fileDataSourceProperties.setFile("/tmp/test.json");
|
||||||
|
fileDataSourceProperties.setBufSize(1024);
|
||||||
|
fileDataSourceProperties.setRecommendRefreshMs(2000);
|
||||||
|
fileDataSourceProperties.setCharset("ISO8859-1");
|
||||||
|
|
||||||
|
assertEquals("File path was wrong", "/tmp/test.json",
|
||||||
|
fileDataSourceProperties.getFile());
|
||||||
|
assertEquals("File charset was wrong", "ISO8859-1",
|
||||||
|
fileDataSourceProperties.getCharset());
|
||||||
|
assertEquals("File refresh time was wrong", 2000L,
|
||||||
|
fileDataSourceProperties.getRecommendRefreshMs());
|
||||||
|
assertEquals("File buf size was wrong", 1024,
|
||||||
|
fileDataSourceProperties.getBufSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
assertEquals("DataSourceProperties postRegister error",
|
||||||
|
fileRefreshableDataSource.loadConfig(), FlowRuleManager.getRules());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,109 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
|
import org.codehaus.jackson.type.TypeReference;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.util.ResourceUtils;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
|
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
public class FileRefreshableDataSourceFactoryBeanTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFile() throws Exception {
|
||||||
|
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(
|
||||||
|
TestConfig.class);
|
||||||
|
assertNotNull("FileRefreshableDataSourceFactoryBean was not created",
|
||||||
|
annotationConfigApplicationContext.getBean("fileBean"));
|
||||||
|
FileRefreshableDataSource fileRefreshableDataSource = annotationConfigApplicationContext
|
||||||
|
.getBean("fileBean", FileRefreshableDataSource.class);
|
||||||
|
assertEquals("FileRefreshableDataSourceFactoryBean flow rule size was wrong", 1,
|
||||||
|
((List<FlowRule>) fileRefreshableDataSource.loadConfig()).size());
|
||||||
|
FileRefreshableDataSourceFactoryBean factoryBean = annotationConfigApplicationContext
|
||||||
|
.getBean("&fileBean", FileRefreshableDataSourceFactoryBean.class);
|
||||||
|
assertEquals("FileRefreshableDataSourceFactoryBean buf size was wrong", 1024,
|
||||||
|
factoryBean.getBufSize());
|
||||||
|
assertEquals("FileRefreshableDataSourceFactoryBean charset was wrong", "utf-8",
|
||||||
|
factoryBean.getCharset());
|
||||||
|
assertEquals("FileRefreshableDataSourceFactoryBean recommendRefreshMs was wrong",
|
||||||
|
2000, factoryBean.getRecommendRefreshMs());
|
||||||
|
assertNotNull("FileRefreshableDataSourceFactoryBean file was null",
|
||||||
|
factoryBean.getFile());
|
||||||
|
assertNotNull("FileRefreshableDataSourceFactoryBean converter was null",
|
||||||
|
factoryBean.getConverter());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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,115 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.converter.SentinelConverter;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
|
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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("{}");
|
||||||
|
|
||||||
|
assertEquals("NacosDataSourceFactoryBean getObject was wrong", nacosDataSource,
|
||||||
|
factoryBean.getObject());
|
||||||
|
assertEquals("NacosDataSource read source value was wrong", "{}",
|
||||||
|
factoryBean.getObject().readSource());
|
||||||
|
assertEquals("NacosDataSource converter was wrong", converter,
|
||||||
|
factoryBean.getConverter());
|
||||||
|
assertEquals("NacosDataSourceFactoryBean dataId was wrong", dataId,
|
||||||
|
factoryBean.getDataId());
|
||||||
|
assertEquals("NacosDataSourceFactoryBean groupId was wrong", groupId,
|
||||||
|
factoryBean.getGroupId());
|
||||||
|
assertEquals("NacosDataSourceFactoryBean serverAddr was wrong", serverAddr,
|
||||||
|
factoryBean.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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("{}");
|
||||||
|
|
||||||
|
assertEquals("NacosDataSourceFactoryBean getObject was wrong", nacosDataSource,
|
||||||
|
factoryBean.getObject());
|
||||||
|
assertEquals("NacosDataSource read source value was wrong", "{}",
|
||||||
|
factoryBean.getObject().readSource());
|
||||||
|
assertEquals("NacosDataSource converter was wrong", converter,
|
||||||
|
factoryBean.getConverter());
|
||||||
|
assertEquals("NacosDataSourceFactoryBean dataId was wrong", dataId,
|
||||||
|
factoryBean.getDataId());
|
||||||
|
assertEquals("NacosDataSourceFactoryBean groupId was wrong", groupId,
|
||||||
|
factoryBean.getGroupId());
|
||||||
|
assertEquals("NacosDataSourceFactoryBean namespace was wrong", namespace,
|
||||||
|
factoryBean.getNamespace());
|
||||||
|
assertEquals("NacosDataSourceFactoryBean endpoint was wrong", endpoint,
|
||||||
|
factoryBean.getEndpoint());
|
||||||
|
assertEquals("NacosDataSourceFactoryBean ak was wrong", accessKey,
|
||||||
|
factoryBean.getAccessKey());
|
||||||
|
assertEquals("NacosDataSourceFactoryBean sk was wrong", secretKey,
|
||||||
|
factoryBean.getSecretKey());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,140 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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");
|
||||||
|
|
||||||
|
assertEquals("Nacos groupId was wrong", "custom-group",
|
||||||
|
nacosDataSourceProperties.getGroupId());
|
||||||
|
assertEquals("Nacos dataId was wrong", "sentinel",
|
||||||
|
nacosDataSourceProperties.getDataId());
|
||||||
|
assertEquals("Nacos default data type was wrong", "xml",
|
||||||
|
nacosDataSourceProperties.getDataType());
|
||||||
|
assertEquals("Nacos rule type was wrong", RuleType.FLOW,
|
||||||
|
nacosDataSourceProperties.getRuleType());
|
||||||
|
assertEquals("Nacos default factory bean was wrong",
|
||||||
|
NacosDataSourceFactoryBean.class.getName(),
|
||||||
|
nacosDataSourceProperties.getFactoryBeanName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosWithProperties() {
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
|
||||||
|
nacosDataSourceProperties.setAccessKey("ak");
|
||||||
|
nacosDataSourceProperties.setSecretKey("sk");
|
||||||
|
nacosDataSourceProperties.setEndpoint("endpoint");
|
||||||
|
nacosDataSourceProperties.setNamespace("namespace");
|
||||||
|
nacosDataSourceProperties.setRuleType(RuleType.SYSTEM);
|
||||||
|
|
||||||
|
assertEquals("Nacos ak was wrong", "ak",
|
||||||
|
nacosDataSourceProperties.getAccessKey());
|
||||||
|
assertEquals("Nacos sk was wrong", "sk",
|
||||||
|
nacosDataSourceProperties.getSecretKey());
|
||||||
|
assertEquals("Nacos endpoint was wrong", "endpoint",
|
||||||
|
nacosDataSourceProperties.getEndpoint());
|
||||||
|
assertEquals("Nacos namespace was wrong", "namespace",
|
||||||
|
nacosDataSourceProperties.getNamespace());
|
||||||
|
assertEquals("Nacos rule type was wrong", RuleType.SYSTEM,
|
||||||
|
nacosDataSourceProperties.getRuleType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosWithEDASAndWithoutSystemProperties() {
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
|
||||||
|
.buildByEDAS(RuleType.DEGRADE.getName());
|
||||||
|
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
|
||||||
|
nacosDataSourceProperties.getGroupId());
|
||||||
|
assertEquals("Nacos dataId was wrong", "null-" + RuleType.DEGRADE.getName(),
|
||||||
|
nacosDataSourceProperties.getDataId());
|
||||||
|
assertEquals("Nacos default data type was wrong", "json",
|
||||||
|
nacosDataSourceProperties.getDataType());
|
||||||
|
assertEquals("Nacos rule type was wrong", RuleType.DEGRADE,
|
||||||
|
nacosDataSourceProperties.getRuleType());
|
||||||
|
assertNull("Nacos ak was not null", nacosDataSourceProperties.getAccessKey());
|
||||||
|
assertNull("Nacos sk was not null", nacosDataSourceProperties.getSecretKey());
|
||||||
|
assertNull("Nacos endpoint was not null",
|
||||||
|
nacosDataSourceProperties.getEndpoint());
|
||||||
|
assertNull("Nacos namespace was not null",
|
||||||
|
nacosDataSourceProperties.getNamespace());
|
||||||
|
assertNull("Nacos serverAddr was not null",
|
||||||
|
nacosDataSourceProperties.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosWithEDASDegradeAndWithoutSystemProperties() {
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
|
||||||
|
.buildDegradeByEDAS();
|
||||||
|
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
|
||||||
|
nacosDataSourceProperties.getGroupId());
|
||||||
|
assertEquals("Nacos dataId was wrong", "null-" + RuleType.DEGRADE.getName(),
|
||||||
|
nacosDataSourceProperties.getDataId());
|
||||||
|
assertEquals("Nacos default data type was wrong", "json",
|
||||||
|
nacosDataSourceProperties.getDataType());
|
||||||
|
assertEquals("Nacos rule type was wrong", RuleType.DEGRADE,
|
||||||
|
nacosDataSourceProperties.getRuleType());
|
||||||
|
assertNull("Nacos ak was not null", nacosDataSourceProperties.getAccessKey());
|
||||||
|
assertNull("Nacos sk was not null", nacosDataSourceProperties.getSecretKey());
|
||||||
|
assertNull("Nacos endpoint was not null",
|
||||||
|
nacosDataSourceProperties.getEndpoint());
|
||||||
|
assertNull("Nacos namespace was not null",
|
||||||
|
nacosDataSourceProperties.getNamespace());
|
||||||
|
assertNull("Nacos serverAddr was not null",
|
||||||
|
nacosDataSourceProperties.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosWithEDASFlowAndWithoutSystemProperties() {
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
|
||||||
|
.buildFlowByEDAS();
|
||||||
|
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
|
||||||
|
nacosDataSourceProperties.getGroupId());
|
||||||
|
assertEquals("Nacos dataId was wrong", "null-" + RuleType.FLOW.getName(),
|
||||||
|
nacosDataSourceProperties.getDataId());
|
||||||
|
assertEquals("Nacos default data type was wrong", "json",
|
||||||
|
nacosDataSourceProperties.getDataType());
|
||||||
|
assertEquals("Nacos rule type was wrong", RuleType.FLOW,
|
||||||
|
nacosDataSourceProperties.getRuleType());
|
||||||
|
assertNull("Nacos ak was not null", nacosDataSourceProperties.getAccessKey());
|
||||||
|
assertNull("Nacos sk was not null", nacosDataSourceProperties.getSecretKey());
|
||||||
|
assertNull("Nacos endpoint was not null",
|
||||||
|
nacosDataSourceProperties.getEndpoint());
|
||||||
|
assertNull("Nacos namespace was not null",
|
||||||
|
nacosDataSourceProperties.getNamespace());
|
||||||
|
assertNull("Nacos serverAddr was not null",
|
||||||
|
nacosDataSourceProperties.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,151 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
public class NacosDataSourcePropertiesWithSystemPropertiesTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosWithSystemProperties() {
|
||||||
|
setSystemProperties();
|
||||||
|
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
|
||||||
|
nacosDataSourceProperties.setServerAddr("127.0.0.1:8848");
|
||||||
|
nacosDataSourceProperties.setGroupId("custom-group");
|
||||||
|
nacosDataSourceProperties.setDataId("sentinel");
|
||||||
|
nacosDataSourceProperties.preCheck("test-ds");
|
||||||
|
|
||||||
|
assertEquals("Nacos groupId was wrong", "custom-group",
|
||||||
|
nacosDataSourceProperties.getGroupId());
|
||||||
|
assertEquals("Nacos dataId was wrong", "sentinel",
|
||||||
|
nacosDataSourceProperties.getDataId());
|
||||||
|
assertEquals("Nacos default data type was wrong", "json",
|
||||||
|
nacosDataSourceProperties.getDataType());
|
||||||
|
assertEquals("Nacos ak was wrong", "ak",
|
||||||
|
nacosDataSourceProperties.getAccessKey());
|
||||||
|
assertEquals("Nacos sk was wrong", "sk",
|
||||||
|
nacosDataSourceProperties.getSecretKey());
|
||||||
|
assertEquals("Nacos endpoint was wrong", "endpoint",
|
||||||
|
nacosDataSourceProperties.getEndpoint());
|
||||||
|
assertEquals("Nacos namespace was wrong", "namespace",
|
||||||
|
nacosDataSourceProperties.getNamespace());
|
||||||
|
assertNull("Nacos serverAddr was not null",
|
||||||
|
nacosDataSourceProperties.getServerAddr());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosWithEDASAndSystemProperties() {
|
||||||
|
setSystemProperties();
|
||||||
|
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
|
||||||
|
.buildByEDAS(RuleType.FLOW.getName());
|
||||||
|
|
||||||
|
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
|
||||||
|
nacosDataSourceProperties.getGroupId());
|
||||||
|
assertEquals("Nacos dataId was wrong", "project-name-" + RuleType.FLOW.getName(),
|
||||||
|
nacosDataSourceProperties.getDataId());
|
||||||
|
assertEquals("Nacos default data type was wrong", "json",
|
||||||
|
nacosDataSourceProperties.getDataType());
|
||||||
|
assertEquals("Nacos rule type was wrong", RuleType.FLOW,
|
||||||
|
nacosDataSourceProperties.getRuleType());
|
||||||
|
assertEquals("Nacos ak was wrong", "ak",
|
||||||
|
nacosDataSourceProperties.getAccessKey());
|
||||||
|
assertEquals("Nacos sk was wrong", "sk",
|
||||||
|
nacosDataSourceProperties.getSecretKey());
|
||||||
|
assertEquals("Nacos endpoint was wrong", "endpoint",
|
||||||
|
nacosDataSourceProperties.getEndpoint());
|
||||||
|
assertEquals("Nacos namespace was wrong", "namespace",
|
||||||
|
nacosDataSourceProperties.getNamespace());
|
||||||
|
assertNull("Nacos serverAddr was not null",
|
||||||
|
nacosDataSourceProperties.getServerAddr());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosWithEDASDegradeAndSystemProperties() {
|
||||||
|
setSystemProperties();
|
||||||
|
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
|
||||||
|
.buildDegradeByEDAS();
|
||||||
|
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
|
||||||
|
nacosDataSourceProperties.getGroupId());
|
||||||
|
assertEquals("Nacos dataId was wrong",
|
||||||
|
"project-name-" + RuleType.DEGRADE.getName(),
|
||||||
|
nacosDataSourceProperties.getDataId());
|
||||||
|
assertEquals("Nacos default data type was wrong", "json",
|
||||||
|
nacosDataSourceProperties.getDataType());
|
||||||
|
assertEquals("Nacos rule type was wrong", RuleType.DEGRADE,
|
||||||
|
nacosDataSourceProperties.getRuleType());
|
||||||
|
assertEquals("Nacos ak was wrong", "ak",
|
||||||
|
nacosDataSourceProperties.getAccessKey());
|
||||||
|
assertEquals("Nacos sk was wrong", "sk",
|
||||||
|
nacosDataSourceProperties.getSecretKey());
|
||||||
|
assertEquals("Nacos endpoint was wrong", "endpoint",
|
||||||
|
nacosDataSourceProperties.getEndpoint());
|
||||||
|
assertEquals("Nacos namespace was wrong", "namespace",
|
||||||
|
nacosDataSourceProperties.getNamespace());
|
||||||
|
assertNull("Nacos serverAddr was not null",
|
||||||
|
nacosDataSourceProperties.getServerAddr());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosWithEDASFlowAndSystemProperties() {
|
||||||
|
setSystemProperties();
|
||||||
|
|
||||||
|
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
|
||||||
|
.buildFlowByEDAS();
|
||||||
|
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
|
||||||
|
nacosDataSourceProperties.getGroupId());
|
||||||
|
assertEquals("Nacos dataId was wrong", "project-name-" + RuleType.FLOW.getName(),
|
||||||
|
nacosDataSourceProperties.getDataId());
|
||||||
|
assertEquals("Nacos default data type was wrong", "json",
|
||||||
|
nacosDataSourceProperties.getDataType());
|
||||||
|
assertEquals("Nacos rule type was wrong", RuleType.FLOW,
|
||||||
|
nacosDataSourceProperties.getRuleType());
|
||||||
|
assertEquals("Nacos ak was wrong", "ak",
|
||||||
|
nacosDataSourceProperties.getAccessKey());
|
||||||
|
assertEquals("Nacos sk was wrong", "sk",
|
||||||
|
nacosDataSourceProperties.getSecretKey());
|
||||||
|
assertEquals("Nacos endpoint was wrong", "endpoint",
|
||||||
|
nacosDataSourceProperties.getEndpoint());
|
||||||
|
assertEquals("Nacos namespace was wrong", "namespace",
|
||||||
|
nacosDataSourceProperties.getNamespace());
|
||||||
|
assertNull("Nacos serverAddr was not null",
|
||||||
|
nacosDataSourceProperties.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setSystemProperties() {
|
||||||
|
System.setProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT,
|
||||||
|
"endpoint");
|
||||||
|
System.setProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_NAMESPACE,
|
||||||
|
"namespace");
|
||||||
|
System.setProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_AK, "ak");
|
||||||
|
System.setProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_SK, "sk");
|
||||||
|
System.setProperty(SentinelDataSourceConstants.PROJECT_NAME, "project-name");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
|
*/
|
||||||
|
public class RuleTypeTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetByName() {
|
||||||
|
assertFalse("empty str rule name was not null",
|
||||||
|
RuleType.getByName("").isPresent());
|
||||||
|
assertFalse("test rule name was not null",
|
||||||
|
RuleType.getByName("test").isPresent());
|
||||||
|
assertFalse("param_flow rule name was not null",
|
||||||
|
RuleType.getByName("param_flow").isPresent());
|
||||||
|
assertFalse("param rule name was not null",
|
||||||
|
RuleType.getByName("param").isPresent());
|
||||||
|
assertFalse("FLOW rule name was not null",
|
||||||
|
RuleType.getByName("FLOW").isPresent());
|
||||||
|
assertTrue("flow rule name was null", RuleType.getByName("flow").isPresent());
|
||||||
|
assertTrue("degrade rule name was null",
|
||||||
|
RuleType.getByName("degrade").isPresent());
|
||||||
|
assertTrue("param-flow rule name was null",
|
||||||
|
RuleType.getByName("param-flow").isPresent());
|
||||||
|
assertTrue("system rule name was null", RuleType.getByName("system").isPresent());
|
||||||
|
assertTrue("authority rule name was null",
|
||||||
|
RuleType.getByName("authority").isPresent());
|
||||||
|
assertEquals("flow rule name was not equals RuleType.FLOW", RuleType.FLOW,
|
||||||
|
RuleType.getByName("flow").get());
|
||||||
|
assertEquals("flow rule name was not equals RuleType.DEGRADE", RuleType.DEGRADE,
|
||||||
|
RuleType.getByName("degrade").get());
|
||||||
|
assertEquals("flow rule name was not equals RuleType.PARAM_FLOW",
|
||||||
|
RuleType.PARAM_FLOW, RuleType.getByName("param-flow").get());
|
||||||
|
assertEquals("flow rule name was not equals RuleType.SYSTEM", RuleType.SYSTEM,
|
||||||
|
RuleType.getByName("system").get());
|
||||||
|
assertEquals("flow rule name was not equals RuleType.AUTHORITY",
|
||||||
|
RuleType.AUTHORITY, RuleType.getByName("authority").get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetByClass() {
|
||||||
|
assertFalse("Object.class type type was not null",
|
||||||
|
RuleType.getByClass(Object.class).isPresent());
|
||||||
|
assertFalse("AbstractRule.class rule type was not null",
|
||||||
|
RuleType.getByClass(AbstractRule.class).isPresent());
|
||||||
|
assertTrue("FlowRule.class rule type was null",
|
||||||
|
RuleType.getByClass(FlowRule.class).isPresent());
|
||||||
|
assertTrue("DegradeRule.class rule type was null",
|
||||||
|
RuleType.getByClass(DegradeRule.class).isPresent());
|
||||||
|
assertTrue("ParamFlowRule.class rule type was null",
|
||||||
|
RuleType.getByClass(ParamFlowRule.class).isPresent());
|
||||||
|
assertTrue("SystemRule.class rule type was null",
|
||||||
|
RuleType.getByClass(SystemRule.class).isPresent());
|
||||||
|
assertTrue("AuthorityRule.class rule type was null",
|
||||||
|
RuleType.getByClass(AuthorityRule.class).isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,131 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
|
||||||
|
import org.springframework.util.ResourceUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 = jsonConverter
|
||||||
|
.convert(readFileContent("classpath: flowrule.json"));
|
||||||
|
assertEquals("json converter flow rule size was wrong", 1, flowRules.size());
|
||||||
|
assertEquals("json converter flow rule resource name was wrong", "resource",
|
||||||
|
flowRules.get(0).getResource());
|
||||||
|
assertEquals("json converter flow rule limit app was wrong", "default",
|
||||||
|
flowRules.get(0).getLimitApp());
|
||||||
|
assertEquals("json converter flow rule count was wrong", "1.0",
|
||||||
|
String.valueOf(flowRules.get(0).getCount()));
|
||||||
|
assertEquals("json converter flow rule control behavior was wrong",
|
||||||
|
RuleConstant.CONTROL_BEHAVIOR_DEFAULT,
|
||||||
|
flowRules.get(0).getControlBehavior());
|
||||||
|
assertEquals("json converter flow rule strategy was wrong",
|
||||||
|
RuleConstant.STRATEGY_DIRECT, flowRules.get(0).getStrategy());
|
||||||
|
assertEquals("json converter flow rule grade was wrong",
|
||||||
|
RuleConstant.FLOW_GRADE_QPS, flowRules.get(0).getGrade());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConverterEmptyContent() {
|
||||||
|
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
||||||
|
List<FlowRule> flowRules = jsonConverter.convert("");
|
||||||
|
assertEquals("json converter flow rule size was not empty", 0, flowRules.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 = jsonConverter
|
||||||
|
.convert(readFileContent("classpath: flowrule.xml"));
|
||||||
|
assertEquals("xml converter flow rule size was wrong", 2, flowRules.size());
|
||||||
|
assertEquals("xml converter flow rule1 resource name was wrong", "resource",
|
||||||
|
flowRules.get(0).getResource());
|
||||||
|
assertEquals("xml converter flow rule2 limit app was wrong", "default",
|
||||||
|
flowRules.get(0).getLimitApp());
|
||||||
|
assertEquals("xml converter flow rule1 count was wrong", "1.0",
|
||||||
|
String.valueOf(flowRules.get(0).getCount()));
|
||||||
|
assertEquals("xml converter flow rule1 control behavior was wrong",
|
||||||
|
RuleConstant.CONTROL_BEHAVIOR_DEFAULT,
|
||||||
|
flowRules.get(0).getControlBehavior());
|
||||||
|
assertEquals("xml converter flow rule1 strategy was wrong",
|
||||||
|
RuleConstant.STRATEGY_DIRECT, flowRules.get(0).getStrategy());
|
||||||
|
assertEquals("xml converter flow rule1 grade was wrong",
|
||||||
|
RuleConstant.FLOW_GRADE_QPS, flowRules.get(0).getGrade());
|
||||||
|
|
||||||
|
assertEquals("xml converter flow rule2 resource name was wrong", "test",
|
||||||
|
flowRules.get(1).getResource());
|
||||||
|
assertEquals("xml converter flow rule2 limit app was wrong", "default",
|
||||||
|
flowRules.get(1).getLimitApp());
|
||||||
|
assertEquals("xml converter flow rule2 count was wrong", "1.0",
|
||||||
|
String.valueOf(flowRules.get(1).getCount()));
|
||||||
|
assertEquals("xml converter flow rule2 control behavior was wrong",
|
||||||
|
RuleConstant.CONTROL_BEHAVIOR_DEFAULT,
|
||||||
|
flowRules.get(1).getControlBehavior());
|
||||||
|
assertEquals("xml converter flow rule2 strategy was wrong",
|
||||||
|
RuleConstant.STRATEGY_DIRECT, flowRules.get(1).getStrategy());
|
||||||
|
assertEquals("xml converter flow rule2 grade was wrong",
|
||||||
|
RuleConstant.FLOW_GRADE_QPS, flowRules.get(1).getGrade());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readFileContent(String file) {
|
||||||
|
try {
|
||||||
|
return FileUtils.readFileToString(
|
||||||
|
ResourceUtils.getFile(StringUtils.trimAllWhitespace(file)));
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
|
||||||
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ZookeeperDataSourceFactoryBean;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
|
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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();
|
||||||
|
|
||||||
|
assertEquals("ZookeeperDataSource getObject was wrong", zookeeperDataSource,
|
||||||
|
factoryBean.getObject());
|
||||||
|
assertEquals("ZookeeperDataSource read source value was wrong", "{}",
|
||||||
|
factoryBean.getObject().readSource());
|
||||||
|
assertEquals("ZookeeperDataSourceFactoryBean dataId was wrong", dataId,
|
||||||
|
factoryBean.getDataId());
|
||||||
|
assertEquals("ZookeeperDataSourceFactoryBean converter was wrong", converter,
|
||||||
|
factoryBean.getConverter());
|
||||||
|
assertEquals("ZookeeperDataSourceFactoryBean groupId was wrong", groupId,
|
||||||
|
factoryBean.getGroupId());
|
||||||
|
assertEquals("ZookeeperDataSourceFactoryBean serverAddr was wrong", serverAddr,
|
||||||
|
factoryBean.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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();
|
||||||
|
|
||||||
|
assertEquals("ZookeeperDataSource value was wrong", zookeeperDataSource,
|
||||||
|
factoryBean.getObject());
|
||||||
|
assertEquals("ZookeeperDataSource read source value was wrong", "{}",
|
||||||
|
factoryBean.getObject().readSource());
|
||||||
|
assertEquals("ZookeeperDataSourceFactoryBean converter was wrong", converter,
|
||||||
|
factoryBean.getConverter());
|
||||||
|
assertEquals("ZookeeperDataSourceFactoryBean path was wrong", path,
|
||||||
|
factoryBean.getPath());
|
||||||
|
assertEquals("ZookeeperDataSourceFactoryBean serverAddr was wrong", serverAddr,
|
||||||
|
factoryBean.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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>
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user