parent
bd4b39297e
commit
60bb8a7c2c
File diff suppressed because one or more lines are too long
@ -0,0 +1,45 @@
|
||||
package com.tencent.cloud.polaris.config.configdata;
|
||||
|
||||
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
|
||||
import org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor;
|
||||
import org.springframework.cloud.util.PropertyUtils;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* PolarisConfigDataMissingEnvironmentPostProcessor to check if miss PolarisConfigData config,if miss config
|
||||
* will throw {@link ImportException}.
|
||||
*
|
||||
* @author wlx
|
||||
* @see ConfigDataMissingEnvironmentPostProcessor
|
||||
* @see ConfigDataMissingEnvironmentPostProcessor.ImportException
|
||||
*/
|
||||
public class PolarisConfigDataMissingEnvironmentPostProcessor extends ConfigDataMissingEnvironmentPostProcessor {
|
||||
|
||||
/**
|
||||
* run after {@link ConfigDataEnvironmentPostProcessor}.
|
||||
*/
|
||||
public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER + 1;
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return ORDER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldProcessEnvironment(Environment environment) {
|
||||
// if using bootstrap or legacy processing don't run
|
||||
if (!PropertyUtils.bootstrapEnabled(environment) && !PropertyUtils.useLegacyProcessing(environment)) {
|
||||
boolean configEnabled = environment.getProperty("spring.cloud.polaris.config.enabled", Boolean.class, true);
|
||||
boolean importCheckEnabled = environment.getProperty("spring.cloud.polaris.config.import-check.enabled", Boolean.class, true);
|
||||
return configEnabled && importCheckEnabled;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPrefix() {
|
||||
return PolarisConfigDataLocationResolver.PREFIX;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.tencent.cloud.polaris.config.configdata;
|
||||
|
||||
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
|
||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||
import org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor;
|
||||
|
||||
/**
|
||||
* Class for most {@code FailureAnalyzer} implementations, to analyze ImportException when
|
||||
* miss Polaris configData config.
|
||||
*
|
||||
* @author wlx
|
||||
* @see AbstractFailureAnalyzer
|
||||
*/
|
||||
public class PolarisImportExceptionFailureAnalyzer extends
|
||||
AbstractFailureAnalyzer<ConfigDataMissingEnvironmentPostProcessor.ImportException> {
|
||||
|
||||
@Override
|
||||
protected FailureAnalysis analyze(Throwable rootFailure, ConfigDataMissingEnvironmentPostProcessor.ImportException cause) {
|
||||
String description;
|
||||
if (cause.missingPrefix) {
|
||||
description = "The spring.config.import property is missing a " + PolarisConfigDataLocationResolver.PREFIX
|
||||
+ " entry";
|
||||
}
|
||||
else {
|
||||
description = "No spring.config.import property has been defined";
|
||||
}
|
||||
String action = "Add a spring.config.import=polaris property to your configuration.\n"
|
||||
+ "\tIf configuration is not required add spring.config.import=optional:polaris instead.\n"
|
||||
+ "\tTo disable this check, set spring.cloud.polaris.config.import-check.enabled=false.";
|
||||
return new FailureAnalysis(description, action, cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,310 @@
|
||||
package com.tencent.cloud.polaris.config.adapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
|
||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
|
||||
import com.tencent.polaris.configuration.api.core.ConfigFileService;
|
||||
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.boot.context.config.Profiles;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisConfigFilePuller}.
|
||||
*
|
||||
* @author wlx
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PolarisConfigFilePullerTest {
|
||||
|
||||
@Mock
|
||||
private PolarisConfigProperties polarisConfigProperties;
|
||||
@Mock
|
||||
private PolarisContextProperties polarisContextProperties;
|
||||
@Mock
|
||||
private ConfigFileService configFileService;
|
||||
@Mock
|
||||
private PolarisPropertySourceManager polarisPropertySourceManager;
|
||||
@Mock
|
||||
private Environment environment;
|
||||
@Mock
|
||||
private Profiles profiles;
|
||||
|
||||
private final String testNamespace = "testNamespace";
|
||||
private final String testServiceName = "testServiceName";
|
||||
private final String polarisConfigPropertySourceName = "polaris-config";
|
||||
|
||||
@Test
|
||||
public void testBootstrapPullInternalConfigFiles() {
|
||||
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
|
||||
polarisPropertySourceManager, environment);
|
||||
|
||||
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||
|
||||
// application.properties
|
||||
Map<String, Object> applicationProperties = new HashMap<>();
|
||||
applicationProperties.put("k1", "v1");
|
||||
applicationProperties.put("k2", "v2");
|
||||
applicationProperties.put("k3", "v3");
|
||||
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
|
||||
.thenReturn(propertiesFile);
|
||||
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
when(environment.getActiveProfiles()).thenReturn(new String[]{});
|
||||
|
||||
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
|
||||
puller.initInternalConfigFiles(compositePropertySource);
|
||||
|
||||
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
|
||||
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
|
||||
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBootstrapPullInternalConfigFilesWithProfile() {
|
||||
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
|
||||
polarisPropertySourceManager, environment);
|
||||
|
||||
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||
|
||||
// application.properties
|
||||
Map<String, Object> applicationProperties = new HashMap<>();
|
||||
applicationProperties.put("k1", "v1");
|
||||
applicationProperties.put("k2", "v2");
|
||||
applicationProperties.put("k3", "v3");
|
||||
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
|
||||
.thenReturn(propertiesFile);
|
||||
|
||||
// application-dev.properties
|
||||
Map<String, Object> devProperties = new HashMap<>();
|
||||
devProperties.put("k1", "v11");
|
||||
ConfigKVFile devFile = new MockedConfigKVFile(devProperties);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application-dev.properties"))
|
||||
.thenReturn(devFile);
|
||||
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application-dev.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap-dev.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
when(environment.getActiveProfiles()).thenReturn(new String[]{"dev"});
|
||||
|
||||
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
|
||||
puller.initInternalConfigFiles(compositePropertySource);
|
||||
|
||||
Assert.assertEquals("v11", compositePropertySource.getProperty("k1"));
|
||||
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
|
||||
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBootstrapPullCustomConfigFilesWithProfile() {
|
||||
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
|
||||
polarisPropertySourceManager, environment);
|
||||
|
||||
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||
|
||||
List<ConfigFileGroup> customFiles = new LinkedList<>();
|
||||
ConfigFileGroup configFileGroup = new ConfigFileGroup();
|
||||
String customGroup = "group1";
|
||||
configFileGroup.setName(customGroup);
|
||||
String customFile1 = "file1.properties";
|
||||
String customFile2 = "file2.properties";
|
||||
configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2));
|
||||
customFiles.add(configFileGroup);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(customFiles);
|
||||
when(environment.getActiveProfiles()).thenReturn(new String[]{});
|
||||
|
||||
// file1.properties
|
||||
Map<String, Object> file1Map = new HashMap<>();
|
||||
file1Map.put("k1", "v1");
|
||||
file1Map.put("k2", "v2");
|
||||
ConfigKVFile file1 = new MockedConfigKVFile(file1Map);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1);
|
||||
|
||||
// file2.properties
|
||||
Map<String, Object> file2Map = new HashMap<>();
|
||||
file2Map.put("k1", "v11");
|
||||
file2Map.put("k3", "v3");
|
||||
ConfigKVFile file2 = new MockedConfigKVFile(file2Map);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile2)).thenReturn(file2);
|
||||
|
||||
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
|
||||
puller.initCustomPolarisConfigFiles(compositePropertySource, customFiles);
|
||||
|
||||
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
|
||||
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
|
||||
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigDataPullInternalConfigFiles() {
|
||||
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
|
||||
polarisPropertySourceManager);
|
||||
|
||||
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||
|
||||
// application.properties
|
||||
Map<String, Object> applicationProperties = new HashMap<>();
|
||||
applicationProperties.put("k1", "v1");
|
||||
applicationProperties.put("k2", "v2");
|
||||
applicationProperties.put("k3", "v3");
|
||||
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
|
||||
.thenReturn(propertiesFile);
|
||||
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
|
||||
when(profiles.getActive()).thenReturn(Lists.newArrayList());
|
||||
|
||||
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
|
||||
puller.initInternalConfigFiles(compositePropertySource, profiles, testServiceName);
|
||||
|
||||
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
|
||||
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
|
||||
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigDataPullInternalConfigFilesWithProfile() {
|
||||
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
|
||||
polarisPropertySourceManager);
|
||||
|
||||
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||
|
||||
// application.properties
|
||||
Map<String, Object> applicationProperties = new HashMap<>();
|
||||
applicationProperties.put("k1", "v1");
|
||||
applicationProperties.put("k2", "v2");
|
||||
applicationProperties.put("k3", "v3");
|
||||
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
|
||||
.thenReturn(propertiesFile);
|
||||
|
||||
// application-dev.properties
|
||||
Map<String, Object> devProperties = new HashMap<>();
|
||||
devProperties.put("k1", "v11");
|
||||
ConfigKVFile devFile = new MockedConfigKVFile(devProperties);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application-dev.properties"))
|
||||
.thenReturn(devFile);
|
||||
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application-dev.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap-dev.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
List<String> active = new ArrayList<>();
|
||||
active.add("dev");
|
||||
when(profiles.getActive()).thenReturn(active);
|
||||
|
||||
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
|
||||
puller.initInternalConfigFiles(compositePropertySource, profiles, testServiceName);
|
||||
|
||||
Assert.assertEquals("v11", compositePropertySource.getProperty("k1"));
|
||||
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
|
||||
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigDataPullCustomConfigFilesWithProfile() {
|
||||
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
|
||||
polarisPropertySourceManager);
|
||||
|
||||
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||
|
||||
List<ConfigFileGroup> customFiles = new LinkedList<>();
|
||||
ConfigFileGroup configFileGroup = new ConfigFileGroup();
|
||||
String customGroup = "group1";
|
||||
configFileGroup.setName(customGroup);
|
||||
String customFile1 = "file1.properties";
|
||||
String customFile2 = "file2.properties";
|
||||
configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2));
|
||||
customFiles.add(configFileGroup);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(customFiles);
|
||||
when(profiles.getActive()).thenReturn(Lists.newArrayList());
|
||||
|
||||
// file1.properties
|
||||
Map<String, Object> file1Map = new HashMap<>();
|
||||
file1Map.put("k1", "v1");
|
||||
file1Map.put("k2", "v2");
|
||||
ConfigKVFile file1 = new MockedConfigKVFile(file1Map);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1);
|
||||
|
||||
// file2.properties
|
||||
Map<String, Object> file2Map = new HashMap<>();
|
||||
file2Map.put("k1", "v11");
|
||||
file2Map.put("k3", "v3");
|
||||
ConfigKVFile file2 = new MockedConfigKVFile(file2Map);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile2)).thenReturn(file2);
|
||||
|
||||
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
|
||||
puller.initCustomPolarisConfigFiles(compositePropertySource, customFiles);
|
||||
|
||||
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
|
||||
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
|
||||
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
|
||||
}
|
||||
}
|
@ -0,0 +1,270 @@
|
||||
package com.tencent.cloud.polaris.config.configdata;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
|
||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
|
||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import com.tencent.polaris.configuration.api.core.ConfigFileService;
|
||||
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
|
||||
import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.boot.ConfigurableBootstrapContext;
|
||||
import org.springframework.boot.context.config.ConfigData;
|
||||
import org.springframework.boot.context.config.ConfigDataLoaderContext;
|
||||
import org.springframework.boot.context.config.Profiles;
|
||||
import org.springframework.boot.logging.DeferredLogs;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisConfigDataLoader}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/16 4:09 下午
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PolarisConfigDataLoaderTest {
|
||||
|
||||
@Mock
|
||||
private ConfigDataLoaderContext context;
|
||||
@Mock
|
||||
private PolarisConfigDataResource polarisConfigDataResource;
|
||||
@Mock
|
||||
private ConfigurableBootstrapContext bootstrapContext;
|
||||
@Mock
|
||||
private PolarisConfigProperties polarisConfigProperties;
|
||||
@Mock
|
||||
private PolarisContextProperties polarisContextProperties;
|
||||
@Mock
|
||||
private ConfigFileService configFileService;
|
||||
@Mock
|
||||
private Profiles profiles;
|
||||
|
||||
private static SDKContext sdkContext;
|
||||
|
||||
private final String testNamespace = "testNamespace";
|
||||
private final String testServiceName = "testServiceName";
|
||||
|
||||
@Test
|
||||
public void loadConfigDataInternalConfigFilesTest() {
|
||||
try (MockedStatic<ConfigFileServiceFactory> mockedStatic = mockStatic(ConfigFileServiceFactory.class)) {
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||
Map<String, Object> applicationProperties = new HashMap<>();
|
||||
applicationProperties.put("k1", "v1");
|
||||
applicationProperties.put("k2", "v2");
|
||||
applicationProperties.put("k3", "v3");
|
||||
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
|
||||
.thenReturn(propertiesFile);
|
||||
|
||||
mockSDKContext();
|
||||
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
|
||||
when(bootstrapContext.isRegistered(eq(SDKContext.class))).thenReturn(false);
|
||||
when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext);
|
||||
|
||||
when(bootstrapContext.isRegistered(eq(PolarisPropertySourceManager.class))).thenReturn(false);
|
||||
when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager());
|
||||
|
||||
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
when(profiles.getActive()).thenReturn(Lists.newArrayList());
|
||||
|
||||
PolarisConfigDataLoader polarisConfigDataLoader = new PolarisConfigDataLoader(new DeferredLogs());
|
||||
when(polarisConfigDataResource.getPolarisConfigProperties()).thenReturn(polarisConfigProperties);
|
||||
when(polarisConfigDataResource.getPolarisContextProperties()).thenReturn(polarisContextProperties);
|
||||
when(polarisConfigDataResource.getServiceName()).thenReturn(testServiceName);
|
||||
when(polarisConfigDataResource.getProfiles()).thenReturn(profiles);
|
||||
|
||||
mockedStatic.when(() -> {
|
||||
ConfigFileServiceFactory.createConfigFileService(sdkContext);
|
||||
}).thenReturn(this.configFileService);
|
||||
|
||||
ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource);
|
||||
List<PropertySource<?>> propertySources = configData.getPropertySources();
|
||||
Optional<PropertySource<?>> propertySource = propertySources.stream().findFirst();
|
||||
if (propertySource.isPresent()) {
|
||||
PropertySource<?> source = propertySource.get();
|
||||
Assert.assertTrue(source instanceof CompositePropertySource);
|
||||
CompositePropertySource compositePropertySource = (CompositePropertySource) source;
|
||||
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
|
||||
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
|
||||
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadConfigDataInternalConfigFilesTestWithProfile() {
|
||||
try (MockedStatic<ConfigFileServiceFactory> mockedStatic = mockStatic(ConfigFileServiceFactory.class)) {
|
||||
Map<String, Object> applicationProperties = new HashMap<>();
|
||||
applicationProperties.put("k1", "v1");
|
||||
applicationProperties.put("k2", "v2");
|
||||
applicationProperties.put("k3", "v3");
|
||||
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
|
||||
.thenReturn(propertiesFile);
|
||||
|
||||
// application-dev.properties
|
||||
Map<String, Object> devProperties = new HashMap<>();
|
||||
devProperties.put("k1", "v11");
|
||||
ConfigKVFile devFile = new MockedConfigKVFile(devProperties);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application-dev.properties"))
|
||||
.thenReturn(devFile);
|
||||
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application-dev.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap-dev.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
List<String> active = new ArrayList<>();
|
||||
active.add("dev");
|
||||
when(profiles.getActive()).thenReturn(active);
|
||||
|
||||
mockSDKContext();
|
||||
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
|
||||
when(bootstrapContext.isRegistered(eq(SDKContext.class))).thenReturn(false);
|
||||
when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext);
|
||||
|
||||
when(bootstrapContext.isRegistered(eq(PolarisPropertySourceManager.class))).thenReturn(false);
|
||||
when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager());
|
||||
|
||||
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
|
||||
PolarisConfigDataLoader polarisConfigDataLoader = new PolarisConfigDataLoader(new DeferredLogs());
|
||||
when(polarisConfigDataResource.getPolarisConfigProperties()).thenReturn(polarisConfigProperties);
|
||||
when(polarisConfigDataResource.getPolarisContextProperties()).thenReturn(polarisContextProperties);
|
||||
when(polarisConfigDataResource.getServiceName()).thenReturn(testServiceName);
|
||||
when(polarisConfigDataResource.getProfiles()).thenReturn(profiles);
|
||||
|
||||
|
||||
mockedStatic.when(() -> {
|
||||
ConfigFileServiceFactory.createConfigFileService(sdkContext);
|
||||
}).thenReturn(this.configFileService);
|
||||
|
||||
ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource);
|
||||
List<PropertySource<?>> propertySources = configData.getPropertySources();
|
||||
Optional<PropertySource<?>> propertySource = propertySources.stream().findFirst();
|
||||
if (propertySource.isPresent()) {
|
||||
PropertySource<?> source = propertySource.get();
|
||||
Assert.assertTrue(source instanceof CompositePropertySource);
|
||||
CompositePropertySource compositePropertySource = (CompositePropertySource) source;
|
||||
Assert.assertEquals("v11", compositePropertySource.getProperty("k1"));
|
||||
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
|
||||
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadConfigDataCustomConfigFilesTestWithProfile() {
|
||||
try (MockedStatic<ConfigFileServiceFactory> mockedStatic = mockStatic(ConfigFileServiceFactory.class)) {
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
|
||||
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
|
||||
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
|
||||
|
||||
String customGroup = "group1";
|
||||
String customFile1 = "file1.properties";
|
||||
when(polarisConfigDataResource.getFileName()).thenReturn(customFile1);
|
||||
when(polarisConfigDataResource.getGroupName()).thenReturn(customGroup);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
when(profiles.getActive()).thenReturn(Lists.newArrayList());
|
||||
|
||||
// file1.properties
|
||||
Map<String, Object> file1Map = new HashMap<>();
|
||||
file1Map.put("k1", "v1");
|
||||
file1Map.put("k2", "v2");
|
||||
file1Map.put("k3", "v3");
|
||||
ConfigKVFile file1 = new MockedConfigKVFile(file1Map);
|
||||
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1);
|
||||
|
||||
|
||||
mockSDKContext();
|
||||
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
|
||||
when(bootstrapContext.isRegistered(eq(SDKContext.class))).thenReturn(false);
|
||||
when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext);
|
||||
|
||||
when(bootstrapContext.isRegistered(eq(PolarisPropertySourceManager.class))).thenReturn(false);
|
||||
when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager());
|
||||
|
||||
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
|
||||
when(polarisContextProperties.getService()).thenReturn(testServiceName);
|
||||
|
||||
when(polarisConfigProperties.getGroups()).thenReturn(null);
|
||||
when(profiles.getActive()).thenReturn(Lists.newArrayList());
|
||||
|
||||
PolarisConfigDataLoader polarisConfigDataLoader = new PolarisConfigDataLoader(new DeferredLogs());
|
||||
when(polarisConfigDataResource.getPolarisConfigProperties()).thenReturn(polarisConfigProperties);
|
||||
when(polarisConfigDataResource.getPolarisContextProperties()).thenReturn(polarisContextProperties);
|
||||
when(polarisConfigDataResource.getServiceName()).thenReturn(testServiceName);
|
||||
when(polarisConfigDataResource.getProfiles()).thenReturn(profiles);
|
||||
|
||||
mockedStatic.when(() -> {
|
||||
ConfigFileServiceFactory.createConfigFileService(sdkContext);
|
||||
}).thenReturn(this.configFileService);
|
||||
|
||||
ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource);
|
||||
List<PropertySource<?>> propertySources = configData.getPropertySources();
|
||||
Optional<PropertySource<?>> propertySource = propertySources.stream().findFirst();
|
||||
if (propertySource.isPresent()) {
|
||||
PropertySource<?> source = propertySource.get();
|
||||
Assert.assertTrue(source instanceof CompositePropertySource);
|
||||
CompositePropertySource compositePropertySource = (CompositePropertySource) source;
|
||||
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
|
||||
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
|
||||
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
if (sdkContext != null) {
|
||||
sdkContext.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private void mockSDKContext() {
|
||||
if (sdkContext == null) {
|
||||
sdkContext = SDKContext.initContext();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.tencent.cloud.polaris.config.configdata;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisConfigDataLocationResolver}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/16 4:10 下午
|
||||
*/
|
||||
public class PolarisConfigDataLocationResolverTest {
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package com.tencent.cloud.polaris.config.configdata;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisConfigDataMissingEnvironmentPostProcessor}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/16 4:09 下午
|
||||
*/
|
||||
public class PolarisConfigDataMissingEnvironmentPostProcessorTest {
|
||||
|
||||
@Test
|
||||
public void missConfigData() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
SpringApplication app = mock(SpringApplication.class);
|
||||
PolarisConfigDataMissingEnvironmentPostProcessor processor = new PolarisConfigDataMissingEnvironmentPostProcessor();
|
||||
assertThatThrownBy(() -> processor.postProcessEnvironment(environment, app))
|
||||
.isInstanceOf(PolarisConfigDataMissingEnvironmentPostProcessor.ImportException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bootstrapEnabledTest() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.cloud.bootstrap.enabled", "true");
|
||||
SpringApplication app = mock(SpringApplication.class);
|
||||
PolarisConfigDataMissingEnvironmentPostProcessor processor = new PolarisConfigDataMissingEnvironmentPostProcessor();
|
||||
// if bootstrap enabled,don't throw ImportException
|
||||
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void legacyProcessingTest() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.config.use-legacy-processing", "true");
|
||||
SpringApplication app = mock(SpringApplication.class);
|
||||
PolarisConfigDataMissingEnvironmentPostProcessor processor = new PolarisConfigDataMissingEnvironmentPostProcessor();
|
||||
// if use-legacy-processing,don't throw ImportException
|
||||
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closeImportCheck() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.cloud.polaris.config.import-check.enabled", "false");
|
||||
SpringApplication app = mock(SpringApplication.class);
|
||||
PolarisConfigDataMissingEnvironmentPostProcessor processor = new PolarisConfigDataMissingEnvironmentPostProcessor();
|
||||
// if import-check.enabled is false,don't throw ImportException
|
||||
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closePolarisConfig() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.cloud.polaris.config.enabled", "false");
|
||||
SpringApplication app = mock(SpringApplication.class);
|
||||
PolarisConfigDataMissingEnvironmentPostProcessor processor = new PolarisConfigDataMissingEnvironmentPostProcessor();
|
||||
// if polaris.config is false,don't throw ImportException
|
||||
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalConfigDataImport() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.config.import", "polaris");
|
||||
SpringApplication app = mock(SpringApplication.class);
|
||||
PolarisConfigDataMissingEnvironmentPostProcessor processor = new PolarisConfigDataMissingEnvironmentPostProcessor();
|
||||
// config polaris config import ,don't throw ImportException
|
||||
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void importOtherConfigDataWithoutPolaris() {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.config.import", "file:application.properties");
|
||||
SpringApplication app = mock(SpringApplication.class);
|
||||
PolarisConfigDataMissingEnvironmentPostProcessor processor = new PolarisConfigDataMissingEnvironmentPostProcessor();
|
||||
assertThatThrownBy(() -> processor.postProcessEnvironment(environment, app))
|
||||
.isInstanceOf(PolarisConfigDataMissingEnvironmentPostProcessor.ImportException.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.tencent.cloud.polaris.config.configdata;
|
||||
|
||||
import com.tencent.polaris.api.utils.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisImportExceptionFailureAnalyzer}.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/16 4:11 下午
|
||||
*/
|
||||
public class PolarisImportExceptionFailureAnalyzerTest {
|
||||
|
||||
@Test
|
||||
public void failureAnalyzerTest() {
|
||||
SpringApplication app = mock(SpringApplication.class);
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
PolarisConfigDataMissingEnvironmentPostProcessor processor = new PolarisConfigDataMissingEnvironmentPostProcessor();
|
||||
assertThatThrownBy(() -> processor.postProcessEnvironment(environment, app))
|
||||
.isInstanceOf(PolarisConfigDataMissingEnvironmentPostProcessor.ImportException.class);
|
||||
Throwable throwable = catchThrowable(() -> processor.postProcessEnvironment(environment, app));
|
||||
PolarisImportExceptionFailureAnalyzer failureAnalyzer = new PolarisImportExceptionFailureAnalyzer();
|
||||
FailureAnalysis analyze = failureAnalyzer.analyze(throwable);
|
||||
assertThat(StringUtils.isNotBlank(analyze.getAction())).isTrue();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue