|
|
@ -18,6 +18,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
package com.tencent.cloud.polaris.config.adapter;
|
|
|
|
package com.tencent.cloud.polaris.config.adapter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
@ -37,11 +38,13 @@ import org.junit.runner.RunWith;
|
|
|
|
import org.mockito.Mock;
|
|
|
|
import org.mockito.Mock;
|
|
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.TypeConverter;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
|
|
import org.springframework.cloud.context.refresh.ContextRefresher;
|
|
|
|
import org.springframework.cloud.context.refresh.ContextRefresher;
|
|
|
|
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -69,12 +72,31 @@ public class PolarisPropertiesSourceAutoRefresherTest {
|
|
|
|
private final String testFileName = "application.properties";
|
|
|
|
private final String testFileName = "application.properties";
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testConfigFileChanged() {
|
|
|
|
public void testConfigFileChanged() throws Exception {
|
|
|
|
PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties,
|
|
|
|
PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties,
|
|
|
|
polarisPropertySourceManager, contextRefresher, springValueRegistry, placeholderHelper);
|
|
|
|
polarisPropertySourceManager, contextRefresher, springValueRegistry, placeholderHelper);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class);
|
|
|
|
|
|
|
|
ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);
|
|
|
|
|
|
|
|
TypeConverter typeConverter = mock(TypeConverter.class);
|
|
|
|
|
|
|
|
when(beanFactory.getTypeConverter()).thenReturn(typeConverter);
|
|
|
|
|
|
|
|
when(applicationContext.getBeanFactory()).thenReturn(beanFactory);
|
|
|
|
|
|
|
|
refresher.setApplicationContext(applicationContext);
|
|
|
|
|
|
|
|
when(typeConverter.convertIfNecessary(any(), any(), (Field) any())).thenReturn("v11");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Collection<SpringValue> springValues = new ArrayList<>();
|
|
|
|
|
|
|
|
MockedConfigChange mockedConfigChange = new MockedConfigChange();
|
|
|
|
|
|
|
|
mockedConfigChange.setK1("v1");
|
|
|
|
|
|
|
|
Field field = mockedConfigChange.getClass().getDeclaredField("k1");
|
|
|
|
|
|
|
|
SpringValue springValue = new SpringValue("v1", "placeholder", mockedConfigChange, "mockedConfigChange", field, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
springValues.add(springValue);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when(springValueRegistry.get(any(), any())).thenReturn(springValues);
|
|
|
|
when(polarisConfigProperties.isAutoRefresh()).thenReturn(true);
|
|
|
|
when(polarisConfigProperties.isAutoRefresh()).thenReturn(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> content = new HashMap<>();
|
|
|
|
Map<String, Object> content = new HashMap<>();
|
|
|
|
content.put("k1", "v1");
|
|
|
|
content.put("k1", "v1");
|
|
|
|
content.put("k2", "v2");
|
|
|
|
content.put("k2", "v2");
|
|
|
@ -98,42 +120,8 @@ public class PolarisPropertiesSourceAutoRefresherTest {
|
|
|
|
|
|
|
|
|
|
|
|
file.fireChangeListener(event);
|
|
|
|
file.fireChangeListener(event);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("v11", polarisPropertySource.getProperty("k1"));
|
|
|
|
Assert.assertEquals("v11", mockedConfigChange.getK1());
|
|
|
|
Assert.assertEquals("v3", polarisPropertySource.getProperty("k3"));
|
|
|
|
|
|
|
|
Assert.assertNull(polarisPropertySource.getProperty("k2"));
|
|
|
|
|
|
|
|
Assert.assertEquals("v4", polarisPropertySource.getProperty("k4"));
|
|
|
|
|
|
|
|
verify(contextRefresher).refresh();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void testNewConfigFile() {
|
|
|
|
|
|
|
|
PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties,
|
|
|
|
|
|
|
|
polarisPropertySourceManager, contextRefresher, springValueRegistry, placeholderHelper);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when(polarisConfigProperties.isAutoRefresh()).thenReturn(true);
|
|
|
|
|
|
|
|
Collection<SpringValue> springValues = new ArrayList<>();
|
|
|
|
|
|
|
|
SpringValue springValue = mock(SpringValue.class);
|
|
|
|
|
|
|
|
springValues.add(springValue);
|
|
|
|
|
|
|
|
when(springValueRegistry.get(any(), any())).thenReturn(springValues);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> emptyContent = new HashMap<>();
|
|
|
|
|
|
|
|
MockedConfigKVFile file = new MockedConfigKVFile(emptyContent);
|
|
|
|
|
|
|
|
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName,
|
|
|
|
|
|
|
|
file, emptyContent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when(polarisPropertySourceManager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", null, "v1", ChangeType.ADDED);
|
|
|
|
|
|
|
|
Map<String, ConfigPropertyChangeInfo> changeInfos = new HashMap<>();
|
|
|
|
|
|
|
|
changeInfos.put("k1", changeInfo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConfigKVFileChangeEvent event = new ConfigKVFileChangeEvent(changeInfos);
|
|
|
|
|
|
|
|
refresher.onApplicationEvent(null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file.fireChangeListener(event);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Assert.assertEquals("v1", polarisPropertySource.getProperty("k1"));
|
|
|
|
|
|
|
|
verify(contextRefresher).refresh();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|