org.springframework.cloud.config.server.environment.EnvironmentRepository Java Examples

The following examples show how to use org.springframework.cloud.config.server.environment.EnvironmentRepository. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoEnvironmentRepositoryTests.java    From spring-cloud-config-server-mongodb with Apache License 2.0 6 votes vote down vote up
@Test
public void defaultRepo() {
	// Prepare context
	Map<String, Object> props = new HashMap<>();
	props.put("spring.data.mongodb.database", "testdb");
	context = new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).properties(props).run();
	// Prepare test
	MongoTemplate mongoTemplate = this.context.getBean(MongoTemplate.class);
	mongoTemplate.dropCollection("testapp");
	MongoPropertySource ps = new MongoPropertySource();
	ps.getSource().put("testkey", "testval");
	mongoTemplate.save(ps, "testapp");
	// Test
	EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("testapp", "default", null);
	assertEquals("testapp-default", environment.getPropertySources().get(0).getName());
	assertEquals(1, environment.getPropertySources().size());
	assertEquals(true, environment.getPropertySources().get(0).getSource().containsKey("testkey"));
	assertEquals("testval", environment.getPropertySources().get(0).getSource().get("testkey"));
}
 
Example #2
Source File: MongoEnvironmentRepositoryTests.java    From spring-cloud-config-server-mongodb with Apache License 2.0 6 votes vote down vote up
@Test
public void nestedPropertySource() {
	// Prepare context
	Map<String, Object> props = new HashMap<>();
	props.put("spring.data.mongodb.database", "testdb");
	context = new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).properties(props).run();
	// Prepare test
	MongoTemplate mongoTemplate = this.context.getBean(MongoTemplate.class);
	mongoTemplate.dropCollection("testapp");
	MongoPropertySource ps = new MongoPropertySource();
	Map<String, String> inner = new HashMap<String, String>();
	inner.put("inner", "value");
	ps.getSource().put("outer", inner);
	mongoTemplate.save(ps, "testapp");
	// Test
	EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("testapp", "default", null);
	assertEquals("testapp-default", environment.getPropertySources().get(0).getName());
	assertEquals(1, environment.getPropertySources().size());
	assertEquals(true, environment.getPropertySources().get(0).getSource().containsKey("outer.inner"));
	assertEquals("value", environment.getPropertySources().get(0).getSource().get("outer.inner"));
}
 
Example #3
Source File: MongoEnvironmentRepositoryTests.java    From spring-cloud-config-server-mongodb with Apache License 2.0 6 votes vote down vote up
@Test
public void repoWithProfileAndLabelInSource() {
	// Prepare context
	Map<String, Object> props = new HashMap<>();
	props.put("spring.data.mongodb.database", "testdb");
	context = new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).properties(props).run();
	// Prepare test
	MongoTemplate mongoTemplate = this.context.getBean(MongoTemplate.class);
	mongoTemplate.dropCollection("testapp");
	MongoPropertySource ps = new MongoPropertySource();
	ps.setProfile("confprofile");
	ps.setLabel("conflabel");
	ps.getSource().put("profile", "sourceprofile");
	ps.getSource().put("label", "sourcelabel");
	mongoTemplate.save(ps, "testapp");
	// Test
	EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("testapp", "confprofile", "conflabel");
	assertEquals(1, environment.getPropertySources().size());
	assertEquals("testapp-confprofile-conflabel", environment.getPropertySources().get(0).getName());
	assertEquals(true, environment.getPropertySources().get(0).getSource().containsKey("profile"));
	assertEquals("sourceprofile", environment.getPropertySources().get(0).getSource().get("profile"));
	assertEquals(true, environment.getPropertySources().get(0).getSource().containsKey("label"));
	assertEquals("sourcelabel", environment.getPropertySources().get(0).getSource().get("label"));
}
 
Example #4
Source File: CompositeEnvironmentBeanFactoryPostProcessor.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
		throws BeansException {
	List<String> typePropertyList = CompositeUtils
			.getCompositeTypeList(this.environment);
	for (int i = 0; i < typePropertyList.size(); i++) {
		String type = typePropertyList.get(i);
		String factoryName = CompositeUtils.getFactoryName(type, beanFactory);

		Type[] factoryTypes = CompositeUtils
				.getEnvironmentRepositoryFactoryTypeParams(beanFactory, factoryName);
		Class<? extends EnvironmentRepositoryProperties> propertiesClass;
		propertiesClass = (Class<? extends EnvironmentRepositoryProperties>) factoryTypes[1];
		EnvironmentRepositoryProperties properties = bindProperties(i,
				propertiesClass, this.environment);

		AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder
				.genericBeanDefinition(EnvironmentRepository.class)
				.setFactoryMethodOnBean("build", factoryName)
				.addConstructorArgValue(properties).getBeanDefinition();
		String beanName = String.format("%s-env-repo%d", type, i);
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
		registry.registerBeanDefinition(beanName, beanDefinition);
	}
}
 
Example #5
Source File: ConfigClientOnIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Bean
public EnvironmentRepository environmentRepository() {
	EnvironmentRepository repository = Mockito.mock(EnvironmentRepository.class);
	given(repository.findOne(anyString(), anyString(), anyString(), anyBoolean()))
			.willReturn(new Environment("", ""));
	return repository;
}
 
Example #6
Source File: ConfigServerMvcConfiguration.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnBean(ResourceRepository.class)
public ResourceController resourceController(ResourceRepository repository,
		EnvironmentRepository envRepository, ConfigServerProperties server) {
	ResourceController controller = new ResourceController(repository,
			encrypted(envRepository, server), this.resourceEncryptorMap);
	controller.setEncryptEnabled(server.getEncrypt().isEnabled());
	controller.setPlainTextEncryptEnabled(server.getEncrypt().isPlainTextEncrypt());
	return controller;
}
 
Example #7
Source File: ConfigServerMvcConfiguration.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Bean
@RefreshScope
public EnvironmentController environmentController(
		EnvironmentRepository envRepository, ConfigServerProperties server) {
	EnvironmentController controller = new EnvironmentController(
			encrypted(envRepository, server), this.objectMapper);
	controller.setStripDocumentFromYaml(server.isStripDocumentFromYaml());
	controller.setAcceptEmpty(server.isAcceptEmpty());
	return controller;
}
 
Example #8
Source File: EnvironmentRepositoryConfiguration.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Primary
@Bean
@ConditionalOnMissingSearchPathLocator
public CompositeEnvironmentRepository compositeEnvironmentRepository(
		List<EnvironmentRepository> environmentRepositories) {
	return new CompositeEnvironmentRepository(environmentRepositories);
}
 
Example #9
Source File: EnvironmentRepositoryConfiguration.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Primary
@Bean
@ConditionalOnSearchPathLocator
public SearchPathCompositeEnvironmentRepository searchPathCompositeEnvironmentRepository(
		List<EnvironmentRepository> environmentRepositories) {
	return new SearchPathCompositeEnvironmentRepository(environmentRepositories);
}
 
Example #10
Source File: EnvironmentRepositoryConfiguration.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty(value = "spring.cloud.config.server.health.enabled",
		matchIfMissing = true)
public ConfigServerHealthIndicator configServerHealthIndicator(
		EnvironmentRepository repository) {
	return new ConfigServerHealthIndicator(repository);
}
 
Example #11
Source File: RefreshableConfigServerIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Bean
public EnvironmentRepository environmentRepository() {
	EnvironmentRepository repository = Mockito.mock(EnvironmentRepository.class);
	Environment environment = new Environment("", "");
	given(repository.findOne(isA(String.class), isA(String.class),
			nullable(String.class), isA(Boolean.class))).willReturn(environment);
	return repository;
}
 
Example #12
Source File: OnSearchPathLocatorPresent.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
		AnnotatedTypeMetadata metadata) {
	ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
	List<String> types = CompositeUtils
			.getCompositeTypeList(context.getEnvironment());

	// get EnvironmentRepository types from registered factories
	List<Class<? extends EnvironmentRepository>> repositoryTypes = new ArrayList<>();
	for (String type : types) {
		String factoryName = CompositeUtils.getFactoryName(type, beanFactory);
		Type[] actualTypeArguments = CompositeUtils
				.getEnvironmentRepositoryFactoryTypeParams(beanFactory, factoryName);
		Class<? extends EnvironmentRepository> repositoryType;
		repositoryType = (Class<? extends EnvironmentRepository>) actualTypeArguments[0];
		repositoryTypes.add(repositoryType);
	}

	boolean required = metadata
			.isAnnotated(ConditionalOnSearchPathLocator.class.getName());
	boolean foundSearchPathLocator = repositoryTypes.stream()
			.anyMatch(SearchPathLocator.class::isAssignableFrom);
	if (required && !foundSearchPathLocator) {
		return ConditionOutcome.noMatch(
				ConditionMessage.forCondition(ConditionalOnSearchPathLocator.class)
						.notAvailable(SearchPathLocator.class.getTypeName()));
	}
	if (!required && foundSearchPathLocator) {
		return ConditionOutcome.noMatch(ConditionMessage
				.forCondition(ConditionalOnMissingSearchPathLocator.class)
				.available(SearchPathLocator.class.getTypeName()));
	}
	return ConditionOutcome.match();
}
 
Example #13
Source File: ResourceController.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
public ResourceController(ResourceRepository resourceRepository,
		EnvironmentRepository environmentRepository,
		Map<String, ResourceEncryptor> resourceEncryptorMap) {
	this.resourceRepository = resourceRepository;
	this.environmentRepository = environmentRepository;
	this.resourceEncryptorMap = resourceEncryptorMap;
	this.helper.setAlwaysUseFullPath(true);
}
 
Example #14
Source File: ConfigClientOffIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Bean
public EnvironmentRepository environmentRepository() {
	EnvironmentRepository repository = Mockito.mock(EnvironmentRepository.class);
	given(repository.findOne(anyString(), anyString(), anyString(), anyBoolean()))
			.willReturn(new Environment("", ""));
	return repository;
}
 
Example #15
Source File: CompositeConfiguration.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
@Autowired
public void setEnvironmentRepos(List<EnvironmentRepository> repos) {
	this.environmentRepos = repos;
}
 
Example #16
Source File: ResourceControllerIntegrationTests.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
@Bean
public EnvironmentRepository environmentRepository() {
	EnvironmentRepository repository = Mockito.mock(EnvironmentRepository.class);
	return repository;
}
 
Example #17
Source File: CustomCompositeEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
@Bean
public EnvironmentRepository environmentRepository() {
	return new CustomEnvironmentRepository(
			new CustomEnvironmentProperties("p"));
}
 
Example #18
Source File: ConfigServerHealthIndicator.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
public ConfigServerHealthIndicator(EnvironmentRepository environmentRepository) {
	this.environmentRepository = environmentRepository;
}
 
Example #19
Source File: SpringConfigServerApplication.java    From casquatch with Apache License 2.0 4 votes vote down vote up
@Bean
@Autowired
public EnvironmentRepository environmentRepository(CasquatchDao db) {
	return new CasquatchEnvironmentRepository(db);
}
 
Example #20
Source File: ResourceController.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
public ResourceController(ResourceRepository resourceRepository,
		EnvironmentRepository environmentRepository) {
	this.resourceRepository = resourceRepository;
	this.environmentRepository = environmentRepository;
	this.helper.setAlwaysUseFullPath(true);
}
 
Example #21
Source File: CloudConfigAutoConfiguration.java    From kork with Apache License 2.0 4 votes vote down vote up
@Bean
CloudConfigResourceService cloudConfigResourceService(
    ResourceRepository resourceRepository, EnvironmentRepository environmentRepository) {
  return new CloudConfigResourceService(resourceRepository, environmentRepository);
}
 
Example #22
Source File: CloudConfigResourceService.java    From kork with Apache License 2.0 4 votes vote down vote up
public CloudConfigResourceService(
    ResourceRepository resourceRepository, EnvironmentRepository environmentRepository) {
  this.resourceRepository = resourceRepository;
  this.environmentRepository = environmentRepository;
}
 
Example #23
Source File: MongoEnvironmentRepositoryConfiguration.java    From spring-cloud-config-server-mongodb with Apache License 2.0 4 votes vote down vote up
@Bean
public EnvironmentRepository environmentRepository() {
	return new MongoEnvironmentRepository(mongoTemplate);
}