73 lines
2.7 KiB
Java
73 lines
2.7 KiB
Java
package tech.easyflow.manuagent.config;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
import org.springframework.boot.env.YamlPropertySourceLoader;
|
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
|
import org.springframework.core.env.PropertySource;
|
|
import org.springframework.core.io.ClassPathResource;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
/**
|
|
* 验证应用主密钥沿用本地配置文件的读取方式。
|
|
*/
|
|
class AppPropertiesValidationTest {
|
|
|
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
|
.withUserConfiguration(PropertiesConfiguration.class)
|
|
.withPropertyValues(
|
|
"app.data-root=file:../data",
|
|
"app.dashscope-key-file=./dashscope_key.txt",
|
|
"app.master-key=unit-test-master-key",
|
|
"app.admin-username=admin",
|
|
"app.admin-password=admin123",
|
|
"app.sandbox-image=runtime:test",
|
|
"app.sandbox-network=bridge",
|
|
"app.run-timeout=1m");
|
|
|
|
/**
|
|
* 默认应用配置必须直接提供主密钥,使本地启动不依赖额外环境变量。
|
|
*
|
|
* @throws IOException application.yml 无法读取时抛出
|
|
*/
|
|
@Test
|
|
void shouldProvideMasterKeyInApplicationConfiguration() throws IOException {
|
|
YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
|
|
List<PropertySource<?>> sources = loader.load(
|
|
"application.yml",
|
|
new ClassPathResource("application.yml"));
|
|
|
|
assertThat(sources)
|
|
.extracting(source -> source.getProperty("app.master-key"))
|
|
.singleElement()
|
|
.isInstanceOf(String.class)
|
|
.asString()
|
|
.isNotBlank()
|
|
.doesNotContain("APP_MASTER_KEY");
|
|
}
|
|
|
|
/**
|
|
* 提供非空加密主密钥后,配置属性应可以正常绑定并供密钥组件使用。
|
|
*/
|
|
@Test
|
|
void shouldBindConfiguredMasterKey() {
|
|
contextRunner.run(context -> {
|
|
assertThat(context).hasNotFailed();
|
|
assertThat(context.getBean(AppProperties.class).masterKey())
|
|
.isEqualTo("unit-test-master-key");
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 注册应用配置属性,复用生产环境的 Spring Boot 配置绑定流程。
|
|
*/
|
|
@Configuration(proxyBeanMethods = false)
|
|
@EnableConfigurationProperties(AppProperties.class)
|
|
static class PropertiesConfiguration {
|
|
}
|
|
}
|