org.springframework.cloud.config.environment.Environment Java Examples

The following examples show how to use org.springframework.cloud.config.environment.Environment. 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: CredhubEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRetrieveDefaultsWhenNoLabelNorProfileProvided() {
	stubCredentials("/my-application/default/master", "toggles", "key1", "value1");

	Environment environment = this.credhubEnvironmentRepository
			.findOne("my-application", null, null);

	assertThat(environment.getName()).isEqualTo("my-application");
	assertThat(environment.getProfiles()).containsExactly("default");
	assertThat(environment.getLabel()).isEqualTo("master");

	assertThat(environment.getPropertySources().size()).isEqualTo(1);
	assertThat(environment.getPropertySources().get(0).getName())
			.isEqualTo("credhub-my-application-default-master");
	assertThat(environment.getPropertySources().get(0).getSource())
			.isEqualTo(singletonMap("key1", "value1"));
}
 
Example #2
Source File: CompositeIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void contextLoads() {
	ResponseEntity<Environment> response = new TestRestTemplate().exchange(
			"http://localhost:" + this.port + "/foo/development/", HttpMethod.GET,
			getV2AcceptEntity(), Environment.class);
	Environment environment = response.getBody();
	assertThat(3).isEqualTo(environment.getPropertySources().size());
	assertThat("overrides")
			.isEqualTo(environment.getPropertySources().get(0).getName());
	assertThat(environment.getPropertySources().get(1).getName()
			.contains("config-repo")
			&& !environment.getPropertySources().get(1).getName()
					.contains("svn-config-repo")).isTrue();
	assertThat(environment.getPropertySources().get(2).getName()
			.contains("svn-config-repo")).isTrue();
	ConfigServerTestUtils.assertConfigEnabled(environment);
}
 
Example #3
Source File: MultipleJGitEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Override
public Environment findOne(String application, String profile, String label,
		boolean includeOrigin) {

	if (this.pattern == null || this.pattern.length == 0) {
		return null;
	}

	if (PatternMatchUtils.simpleMatch(this.pattern,
			application + "/" + profile)) {
		return super.findOne(application, profile, label, includeOrigin);
	}

	return null;

}
 
Example #4
Source File: SentinelRuleLocator.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void log(Environment result) {

        RecordLog.info(String.format(
            "Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s",
            result.getName(),
            result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()),
            result.getLabel(), result.getVersion(), result.getState()));

        List<PropertySource> propertySourceList = result.getPropertySources();
        if (propertySourceList != null) {
            int propertyCount = 0;
            for (PropertySource propertySource : propertySourceList) {
                propertyCount += propertySource.getSource().size();
            }
            RecordLog.info("[SentinelRuleLocator] Environment {} has {} property sources with {} properties",
                result.getName(), result.getPropertySources().size(), propertyCount);
        }
    }
 
Example #5
Source File: MultiModuleConfigServicePropertySourceLocator.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
private Environment postProcessResult(Environment result) {
    if (preprocessor == null) {
        return result;
    }

    result.getPropertySources().forEach(ps -> {
        Map<?, ?> source = ps.getSource();

        new HashSet<>(source.keySet()).forEach(key -> {
            Object value = ps.getSource().get(key);
            if (key instanceof String && value instanceof String) {
                String processedKey = preprocessor.process((String) key);
                String processedValue = preprocessor.process((String) value);

                if (!key.equals(processedKey) || !value.equals(processedValue)) {
                    logger.info("preprocessor processed config: key={}, value={}, " +
                            "REPLACED KEY={}, VALUE={}", key, value, processedKey, processedValue);
                    ps.getSource().remove(key);
                    ((Map<Object, Object>) source).put(processedKey, processedValue);
                }
            }
        });
    });
    return result;
}
 
Example #6
Source File: CompositeIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void contextLoads() {
	ResponseEntity<Environment> response = new TestRestTemplate().exchange(
			"http://localhost:" + this.port + "/foo/development/", HttpMethod.GET,
			getV2AcceptEntity(), Environment.class);
	Environment environment = response.getBody();
	assertThat(environment.getPropertySources()).hasSize(3);
	assertThat("overrides")
			.isEqualTo(environment.getPropertySources().get(0).getName());
	assertThat(environment.getPropertySources().get(1).getName()
			.contains("config-repo")
			&& !environment.getPropertySources().get(1).getName()
					.contains("svn-config-repo")).isTrue();
	assertThat(environment.getPropertySources().get(2).getName()
			.contains("svn-config-repo")).isTrue();
	ConfigServerTestUtils.assertConfigEnabled(environment);
}
 
Example #7
Source File: JGitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void findOne_CloneOnStartTrue_FindOneSuccess() throws Exception {
	ConfigServerTestUtils.prepareLocalRepo();
	String uri = ConfigServerTestUtils.copyLocalRepo("config-copy");
	this.context = new SpringApplicationBuilder(TestConfiguration.class)
			.web(WebApplicationType.NONE)
			.run("--spring.cloud.config.server.git.uri=" + uri,
					"--spring.cloud.config.server.git.cloneOnStart=true");
	EnvironmentRepository repository = this.context
			.getBean(JGitEnvironmentRepository.class);
	assertThat(((JGitEnvironmentRepository) repository).isCloneOnStart()).isTrue();
	Environment environment = repository.findOne("bar", "staging", "master");
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
	assertThat(environment.getName()).isEqualTo("bar");
	assertThat(environment.getProfiles()).isEqualTo(new String[] { "staging" });
	assertThat(environment.getLabel()).isEqualTo("master");
}
 
Example #8
Source File: MultipleJGitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void mappingRepoWithJustUri() throws IOException {
	String defaultRepoUri = ConfigServerTestUtils.prepareLocalRepo("config-repo");
	String test1RepoUri = ConfigServerTestUtils.prepareLocalRepo("test1-config-repo");

	Map<String, Object> repoMapping = new LinkedHashMap<String, Object>();
	repoMapping.put("spring.cloud.config.server.git.repos.test1-svc.uri",
			test1RepoUri);
	this.context = new SpringApplicationBuilder(TestConfiguration.class)
			.web(WebApplicationType.NONE)
			.properties("spring.cloud.config.server.git.uri:" + defaultRepoUri)
			.properties(repoMapping).run();
	EnvironmentRepository repository = this.context
			.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("test1-svc", "staging", "master");
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
}
 
Example #9
Source File: JGitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void findOne_FileAddedToRepo_FindOneSuccess() throws Exception {
	ConfigServerTestUtils.prepareLocalRepo();
	String uri = ConfigServerTestUtils.copyLocalRepo("config-copy");
	this.context = new SpringApplicationBuilder(TestConfiguration.class)
			.web(WebApplicationType.NONE)
			.run("--spring.cloud.config.server.git.uri=" + uri,
					"--spring.cloud.config.server.git.cloneOnStart=true");
	EnvironmentRepository repository = this.context
			.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("bar", "staging", "master");
	assertThat(environment.getPropertySources().get(0).getSource().get("foo"))
			.isEqualTo("bar");
	Git git = Git.open(ResourceUtils.getFile(uri).getAbsoluteFile());
	git.checkout().setName("master").call();
	StreamUtils.copy("foo: foo", Charset.defaultCharset(),
			new FileOutputStream(ResourceUtils.getFile(uri + "/bar.properties")));
	git.add().addFilepattern("bar.properties").call();
	git.commit().setMessage("Updated for pull").call();
	environment = repository.findOne("bar", "staging", "master");
	assertThat(environment.getPropertySources().get(0).getSource().get("foo"))
			.isEqualTo("foo");
}
 
Example #10
Source File: MultipleJGitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void mappingRepoWithProfiles() throws IOException {
	String defaultRepoUri = ConfigServerTestUtils.prepareLocalRepo("config-repo");
	String test1RepoUri = ConfigServerTestUtils.prepareLocalRepo("test1-config-repo");

	Map<String, Object> repoMapping = new LinkedHashMap<String, Object>();
	repoMapping.put("spring.cloud.config.server.git.repos[test1].pattern[0]",
			"*/staging,*");
	repoMapping.put("spring.cloud.config.server.git.repos[test1].pattern[1]",
			"*/*,staging");
	repoMapping.put("spring.cloud.config.server.git.repos[test1].pattern[2]",
			"*/staging");
	repoMapping.put("spring.cloud.config.server.git.repos[test1].uri", test1RepoUri);
	this.context = new SpringApplicationBuilder(TestConfiguration.class)
			.web(WebApplicationType.NONE)
			.properties("spring.cloud.config.server.git.uri:" + defaultRepoUri)
			.properties(repoMapping).run();
	EnvironmentRepository repository = this.context
			.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("test1-svc", "cloud,staging",
			"master");
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
	environment = repository.findOne("test1-svc", "staging,cloud", "master");
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
}
 
Example #11
Source File: JGitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test(expected = NoSuchLabelException.class)
public void testShouldFailIfRemoteBranchWasDeleted() throws Exception {
	JGitConfigServerTestData testData = JGitConfigServerTestData
			.prepareClonedGitRepository(Collections.singleton(
					"spring.cloud.config.server.git.deleteUntrackedBranches=true"),
					TestConfiguration.class);

	String branchToDelete = "branchToDelete";
	testData.getServerGit().getGit().branchCreate().setName(branchToDelete).call();

	// checkout and simulate regular flow
	Environment environment = testData.getRepository().findOne("bar", "staging",
			"branchToDelete");
	assertThat(environment).isNotNull();

	// remove branch
	testData.getServerGit().getGit().branchDelete().setBranchNames(branchToDelete)
			.call();

	// test
	testData.getRepository().findOne("bar", "staging", "branchToDelete");
}
 
Example #12
Source File: SVNKitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void update() throws Exception {
	String uri = ConfigServerTestUtils.prepareLocalSvnRepo(
			"src/test/resources/svn-config-repo", "target/config");
	this.context = new SpringApplicationBuilder(TestConfiguration.class)
			.web(WebApplicationType.NONE).profiles("subversion")
			.run("--spring.cloud.config.server.svn.uri=" + uri);
	EnvironmentRepository repository = this.context
			.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("bar", "staging", "trunk");
	assertThat(environment.getPropertySources().get(0).getSource().get("foo"))
			.isEqualTo("bar");
	updateRepoForUpdate(uri);
	environment = repository.findOne("bar", "staging", "trunk");
	assertThat(environment.getPropertySources().get(0).getSource().get("foo"))
			.isEqualTo("foo");
}
 
Example #13
Source File: CompositeEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Override
public Environment findOne(String application, String profile, String label,
		boolean includeOrigin) {
	Environment env = new Environment(application, new String[] { profile }, label,
			null, null);
	if (this.environmentRepositories.size() == 1) {
		Environment envRepo = this.environmentRepositories.get(0).findOne(application,
				profile, label, includeOrigin);
		env.addAll(envRepo.getPropertySources());
		env.setVersion(envRepo.getVersion());
		env.setState(envRepo.getState());
	}
	else {
		for (EnvironmentRepository repo : environmentRepositories) {
			env.addAll(repo.findOne(application, profile, label, includeOrigin)
					.getPropertySources());
		}
	}
	return env;
}
 
Example #14
Source File: JGitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldReturnEnvironmentFromLocalBranchInCaseRemoteDeleted()
		throws Exception {
	JGitConfigServerTestData testData = JGitConfigServerTestData
			.prepareClonedGitRepository(TestConfiguration.class);

	String branchToDelete = "branchToDelete";
	testData.getServerGit().getGit().branchCreate().setName(branchToDelete).call();

	Environment environment = testData.getRepository().findOne("bar", "staging",
			"branchToDelete");
	assertThat(environment).isNotNull();

	testData.getServerGit().getGit().branchDelete().setBranchNames(branchToDelete)
			.call();
	testData.getRepository().findOne("bar", "staging", "branchToDelete");
	assertThat(environment).isNotNull();
}
 
Example #15
Source File: PassthruEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Override
public Environment findOne(String application, String profile, String label,
		boolean includeOrigin) {
	Environment result = new Environment(application,
			StringUtils.commaDelimitedListToStringArray(profile), label, null, null);
	for (org.springframework.core.env.PropertySource<?> source : this.environment
			.getPropertySources()) {
		String name = source.getName();
		if (!this.standardSources.contains(name)
				&& source instanceof MapPropertySource) {
			result.add(new PropertySource(name, getMap(source, includeOrigin)));
		}
	}
	return result;

}
 
Example #16
Source File: MultipleJGitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void mappingRepoWithProfileDefaultPatterns() throws IOException {
	String defaultRepoUri = ConfigServerTestUtils.prepareLocalRepo("config-repo");
	String test1RepoUri = ConfigServerTestUtils.prepareLocalRepo("test1-config-repo");

	Map<String, Object> repoMapping = new LinkedHashMap<String, Object>();
	repoMapping.put("spring.cloud.config.server.git.repos[test1].pattern",
			"*/staging");
	repoMapping.put("spring.cloud.config.server.git.repos[test1].uri", test1RepoUri);
	this.context = new SpringApplicationBuilder(TestConfiguration.class)
			.web(WebApplicationType.NONE)
			.properties("spring.cloud.config.server.git.uri:" + defaultRepoUri)
			.properties(repoMapping).run();
	EnvironmentRepository repository = this.context
			.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("test1-svc", "staging,cloud",
			"master");
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
}
 
Example #17
Source File: EnvironmentController.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/{label}/{name}-{profiles}.json")
public ResponseEntity<String> labelledJsonProperties(@PathVariable String name,
		@PathVariable String profiles, @PathVariable String label,
		@RequestParam(defaultValue = "true") boolean resolvePlaceholders)
		throws Exception {
	validateProfiles(profiles);
	Environment environment = labelled(name, profiles, label);
	Map<String, Object> properties = convertToMap(environment);
	String json = this.objectMapper.writeValueAsString(properties);
	if (resolvePlaceholders) {
		json = resolvePlaceholders(prepareEnvironment(environment), json);
	}
	return getSuccess(json, MediaType.APPLICATION_JSON);
}
 
Example #18
Source File: BootstrapConfigServerIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	ResponseEntity<Environment> response = new TestRestTemplate().exchange(
			"http://localhost:" + this.port + "/foo/development/", HttpMethod.GET,
			getV2AcceptEntity(), Environment.class);
	Environment environment = response.getBody();
	assertThat(environment.getPropertySources()).hasSize(2);
	assertOriginTrackedValue(environment, 0, "bar", "foo");
	assertOriginTrackedValue(environment, 1, "info.foo", "bar");
}
 
Example #19
Source File: SubversionConfigServerIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	ResponseEntity<Environment> exchange = new TestRestTemplate().exchange(
			"http://localhost:" + this.port + "/foo/development/", HttpMethod.GET,
			getV2AcceptEntity(), Environment.class);
	Environment environment = exchange.getBody();
	assertThat(environment.getPropertySources().isEmpty()).isFalse();
	assertThat(environment.getPropertySources().get(0).getName())
			.isEqualTo("overrides");
	ConfigServerTestUtils.assertConfigEnabled(environment);
}
 
Example #20
Source File: JdbcEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void basicProperties() {
	Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource),
			new JdbcEnvironmentProperties()).findOne("foo", "bar", "");
	assertThat(env.getName()).isEqualTo("foo");
	assertThat(env.getProfiles()).isEqualTo(new String[] { "default", "bar" });
	assertThat(env.getLabel()).isEqualTo("master");
	assertThat(env.getPropertySources()).isNotEmpty();
	assertThat(env.getPropertySources().get(0).getName()).isEqualTo("foo-bar");
	assertThat(env.getPropertySources().get(0).getSource().get("a.b.c"))
			.isEqualTo("x");
	assertThat(env.getPropertySources().get(1).getName())
			.isEqualTo("application-default");
	assertThat(env.getPropertySources().get(1).getSource().get("a.b")).isEqualTo("y");
}
 
Example #21
Source File: MultipleJGitEnvironmentProfilePlaceholderRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void otherMappingRepo() {
	Environment environment = this.repository.findOne("application",
			"test2-config-repo", "master");
	assertThat(environment.getPropertySources().size()).isEqualTo(1);
	assertThat(environment.getPropertySources().get(0).getName())
			.isEqualTo(getUri("*").replace("{profile}", "test2-config-repo")
					+ "/application.properties");
	assertVersion(environment);
}
 
Example #22
Source File: EnvironmentController.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/{label}/{name}-{profiles}.properties")
public ResponseEntity<String> labelledProperties(@PathVariable String name,
		@PathVariable String profiles, @PathVariable String label,
		@RequestParam(defaultValue = "true") boolean resolvePlaceholders)
		throws IOException {
	validateProfiles(profiles);
	Environment environment = labelled(name, profiles, label);
	Map<String, Object> properties = convertToProperties(environment);
	String propertiesString = getPropertiesString(properties);
	if (resolvePlaceholders) {
		propertiesString = resolvePlaceholders(prepareEnvironment(environment),
				propertiesString);
	}
	return getSuccess(propertiesString);
}
 
Example #23
Source File: JGitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void branch() {
	this.repository.setBasedir(this.basedir);
	Environment environment = this.repository.findOne("bar", "staging", "raw");
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
	assertThat(environment.getPropertySources().get(0).getName())
			.isEqualTo(this.repository.getUri() + "/bar.properties");
	assertVersion(environment);
}
 
Example #24
Source File: MultipleJGitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultRepo() {
	Environment environment = this.repository.findOne("bar", "staging", "master");
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
	assertThat(environment.getPropertySources().get(0).getName())
			.isEqualTo(this.repository.getUri() + "/bar.properties");
	assertVersion(environment);
}
 
Example #25
Source File: SVNKitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void basedir() {
	this.repository.setBasedir(this.basedir);
	Environment environment = this.findOne();
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
	assertThat(environment.getPropertySources().get(0).getName()
			.contains("bar.properties")).isTrue();
	assertThat(environment.getPropertySources().get(1).getName()
			.contains("application.yml")).isTrue();
}
 
Example #26
Source File: MultipleJGitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultRepoTwice() {
	Environment environment = this.repository.findOne("bar", "staging", "master");
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
	assertThat(environment.getPropertySources().get(0).getName())
			.isEqualTo(this.repository.getUri() + "/bar.properties");
	assertVersion(environment);
}
 
Example #27
Source File: CustomCompositeEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() {
	Environment environment = new TestRestTemplate().getForObject(
			"http://localhost:" + this.port + "/foo/development/",
			Environment.class);
	List<PropertySource> propertySources = environment.getPropertySources();
	assertThat(3).isEqualTo(propertySources.size());
	assertThat("overrides").isEqualTo(propertySources.get(0).getName());
	assertThat(propertySources.get(1).getName().contains("config-repo")).isTrue();
	assertThat("p").isEqualTo(propertySources.get(2).getName());
}
 
Example #28
Source File: NativeEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void prefixed() {
	this.repository.setSearchLocations("classpath:/test");
	Environment environment = this.repository.findOne("foo", "development", "master");
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
	assertThat(environment.getVersion()).as("version was wrong")
			.isEqualTo("myversion");
}
 
Example #29
Source File: AwsS3EnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void findWithLabel() throws UnsupportedEncodingException {
	setupS3("label1/foo-bar.yml", yamlContent);

	final Environment env = envRepo.findOne("foo", "bar", "label1");

	assertExpectedEnvironment(env, "foo", "label1", null, 1, "bar");
}
 
Example #30
Source File: SVNKitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void vanilla_with_update() {
	this.findOne();
	Environment environment = this.findOne();
	assertThat(environment.getPropertySources().size()).isEqualTo(2);
	assertThat(environment.getPropertySources().get(0).getName()
			.contains("bar.properties")).isTrue();
	assertThat(environment.getPropertySources().get(1).getName()
			.contains("application.yml")).isTrue();
}