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

The following examples show how to use org.springframework.cloud.config.environment.PropertySource. 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: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void arrayOverridenInYaml() throws Exception {
	// Add original values first source
	Map<String, Object> oneMap = new LinkedHashMap<String, Object>();
	oneMap.put("a.b[0]", "c");
	oneMap.put("a.b[1]", "d");
	oneMap.put("a.b[2]", "z");
	this.environment.add(new PropertySource("one", oneMap));

	// Add overridden values in second source
	Map<String, Object> twoMap = new LinkedHashMap<String, Object>();
	twoMap.put("a.b[0]", "f");
	twoMap.put("a.b[1]", "h");
	this.environment.addFirst(new PropertySource("two", twoMap));

	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
	String yaml = this.controller.yaml("foo", "bar", false).getBody();

	// Result will not contain original, extra values from oneMap
	assertThat(yaml).isEqualTo("a:\n  b:\n  - f\n  - h\n");
}
 
Example #2
Source File: PassthruEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void originTrackedPropertySourceWithoutOriginWorks() {
	MockEnvironment mockEnvironment = new MockEnvironment();
	mockEnvironment.setProperty("normalKey", "normalValue");
	mockEnvironment.getPropertySources()
			.addFirst(new OriginTrackedMapPropertySource("myorigintrackedsource",
					Collections.singletonMap("keyNoOrigin", "valueNoOrigin")));
	PassthruEnvironmentRepository repository = new PassthruEnvironmentRepository(
			mockEnvironment);
	Environment environment = repository.findOne("testapp", "default", "master",
			true);
	assertThat(environment).isNotNull();
	List<PropertySource> propertySources = environment.getPropertySources();
	assertThat(propertySources).hasSize(2);
	for (PropertySource propertySource : propertySources) {
		Map source = propertySource.getSource();
		if (propertySource.getName().equals("myorigintrackedsource")) {
			assertThat(source).containsEntry("keyNoOrigin", "valueNoOrigin");
		}
		else if (propertySource.getName().equals("mockProperties")) {
			assertThat(source).containsEntry("normalKey", "normalValue");
		}
	}
}
 
Example #3
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 #4
Source File: ConfigServicePropertySourceLocator.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
private void log(Environment result) {
	if (logger.isInfoEnabled()) {
		logger.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()));
	}
	if (logger.isDebugEnabled()) {
		List<PropertySource> propertySourceList = result.getPropertySources();
		if (propertySourceList != null) {
			int propertyCount = 0;
			for (PropertySource propertySource : propertySourceList) {
				propertyCount += propertySource.getSource().size();
			}
			logger.debug(String.format(
					"Environment %s has %d property sources with %d properties.",
					result.getName(), result.getPropertySources().size(),
					propertyCount));
		}

	}
}
 
Example #5
Source File: CipherEnvironmentEncryptorTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecryptEnvironmentIncludeOrigin() {
	// given
	String secret = randomUUID().toString();

	// when
	Environment environment = new Environment("name", "profile", "label");
	String encrypted = "{cipher}" + this.textEncryptor.encrypt(secret);
	environment.add(new PropertySource("a",
			Collections.<Object, Object>singletonMap(environment.getName(),
					new PropertyValueDescriptor(encrypted, "encrypted value"))));

	// then
	assertThat(this.encryptor.decrypt(environment).getPropertySources().get(0)
			.getSource().get(environment.getName())).isEqualTo(secret);
}
 
Example #6
Source File: CasquatchEnvironmentRepository.java    From casquatch with Apache License 2.0 6 votes vote down vote up
/**
    * Implements findOne for repository interface
    * @param application application name
    * @param profile profile name
    * @param label optional label
    */			
public Environment findOne(String application, String profile, String label) {
	logger.debug("Loading Configuration for "+application+"."+profile+"."+label);
	
	Environment environment = new Environment(application,profile);
	
	Configuration searchConfig = new Configuration();
	searchConfig.setApplication(application);
	searchConfig.setProfile(profile);
	searchConfig.setLabel(label);
		
	List<Configuration> configList = db.getAllById(Configuration.class, searchConfig);
	
	final Map<String, String> properties = new HashMap<>();
	configList.forEach((Configuration conf) -> properties.put(conf.getKey(), conf.getValue()));
       environment.add(new PropertySource(application+"-"+label, properties));
	return environment;
}
 
Example #7
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void propertyOverrideInYamlMultipleValues() throws Exception {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("A", "Y");
	map.put("S", 2);
	map.put("Y", 0);
	this.environment.add(new PropertySource("one", map));
	map = new LinkedHashMap<String, Object>();
	map.put("A", "Z");
	map.put("S", 3);
	this.environment.addFirst(new PropertySource("two", map));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
	String yaml = this.controller.yaml("foo", "bar", false).getBody();
	assertThat(yaml).isEqualTo("A: Z\nS: 3\nY: 0\n");
}
 
Example #8
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 #9
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void arrayObObjectAtTopLevelInYaml() throws Exception {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("document[0].a", "c");
	map.put("document[1].a", "d");
	this.environment.add(new PropertySource("one", map));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
	String yaml = this.controller.yaml("foo", "bar", false).getBody();
	assertThat(yaml).isEqualTo("- a: c\n- a: d\n");
}
 
Example #10
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void yamlWithProperties() throws Exception {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("org.springframework", "WARN");
	map.put("org.springframework.cloud", "ERROR");
	this.environment.add(new PropertySource("abo", map));
	when(this.repository.findOne("ay", "äzöq", null, false))
			.thenReturn(this.environment);
	System.out.println("this.controller = " + this.controller);
	String yaml = this.controller.yaml("ay", "äzöq", false).getBody();
	assertThat(yaml).isEqualTo(
			"org:\n  springframework: WARN\n  springframework.cloud: ERROR\n");
}
 
Example #11
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void arrayAtTopLevelInYaml() throws Exception {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("document[0]", "c");
	map.put("document[1]", "d");
	this.environment.add(new PropertySource("one", map));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
	String yaml = this.controller.yaml("foo", "bar", false).getBody();
	assertThat(yaml).isEqualTo("- c\n- d\n");
}
 
Example #12
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void arrayOfObjectInYaml() throws Exception {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("a.b[0].c", "d");
	map.put("a.b[0].d", "e");
	map.put("a.b[1].c", "d");
	this.environment.add(new PropertySource("one", map));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
	String yaml = this.controller.yaml("foo", "bar", false).getBody();
	assertThat("a:\n  b:\n  - d: e\n    c: d\n  - c: d\n".equals(yaml)
			|| "a:\n  b:\n  - c: d\n    d: e\n  - c: d\n".equals(yaml))
					.as("Wrong output: " + yaml).isTrue();
}
 
Example #13
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void nestedArraysOfObjectInJson() throws Exception {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("a.b[0].c", "x");
	map.put("a.b[0].d[0]", "xx");
	map.put("a.b[0].d[1]", "yy");
	map.put("a.b[1].c", "y");
	map.put("a.b[1].e[0].d", "z");
	this.environment.add(new PropertySource("one", map));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
	String json = this.controller.jsonProperties("foo", "bar", false).getBody();
	assertThat(json).as("Wrong output: " + json).isEqualTo(
			"{\"a\":{\"b\":[{\"c\":\"x\",\"d\":[\"xx\",\"yy\"]},{\"c\":\"y\",\"e\":[{\"d\":\"z\"}]}]}}");
}
 
Example #14
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void arrayOfObjectAtTopLevelInYaml() throws Exception {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("b[0].c", "d");
	map.put("b[1].c", "d");
	this.environment.add(new PropertySource("one", map));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
	String yaml = this.controller.yaml("foo", "bar", false).getBody();
	assertThat(yaml).isEqualTo("b:\n- c: d\n- c: d\n");
}
 
Example #15
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void arrayOfObjectNestedLevelInYaml() throws Exception {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("x.a.b[0].c", "d");
	map.put("x.a.b[1].c", "d");
	this.environment.add(new PropertySource("one", map));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
	String yaml = this.controller.yaml("foo", "bar", false).getBody();
	assertThat(yaml).isEqualTo("x:\n  a:\n    b:\n    - c: d\n    - c: d\n");
}
 
Example #16
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
private void whenPlaceholders() {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("foo", "bar");
	this.environment.add(new PropertySource("one", map));
	this.environment.addFirst(
			new PropertySource("two", Collections.singletonMap("a.b.c", "${foo}")));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
}
 
Example #17
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
private void whenPlaceholdersSystemProps() {
	System.setProperty("foo", "bar");
	this.environment.addFirst(
			new PropertySource("two", Collections.singletonMap("a.b.c", "${foo}")));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
}
 
Example #18
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
private void whenPlaceholdersSystemPropsWithDefault() {
	System.setProperty("foo", "bar");
	this.environment.addFirst(new PropertySource("two",
			Collections.singletonMap("a.b.c", "${foo:spam}")));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
}
 
Example #19
Source File: EnvironmentControllerIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void propertiesLabelWhenApplicationNameContainsHyphen() throws Exception {
	Environment environment = new Environment("foo-bar", "default");
	environment.add(new PropertySource("foo", new HashMap<>()));
	when(this.repository.findOne("foo-bar", "default", "label", false))
			.thenReturn(this.environment);
	this.mvc.perform(MockMvcRequestBuilders.get("/label/foo-bar-default.properties"))
			.andExpect(MockMvcResultMatchers.status().isOk());
	verify(this.repository).findOne("foo-bar", "default", "label", false);
}
 
Example #20
Source File: EnvironmentEncryptorEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void allowOverrideFalse() throws Exception {
	this.controller.setOverrides(Collections.singletonMap("foo", "bar"));
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("a.b.c", "d");
	this.environment.add(new PropertySource("one", map));
	when(this.repository.findOne("foo", "bar", "master", false))
			.thenReturn(this.environment);
	assertThat(this.controller.findOne("foo", "bar", "master", false)
			.getPropertySources().get(0).getSource().toString())
					.isEqualTo("{foo=bar}");
}
 
Example #21
Source File: EnvironmentEncryptorEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void overrideWithEscapedPlaceholders() throws Exception {
	this.controller.setOverrides(Collections.singletonMap("foo", "$\\{bar}"));
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("bar", "foo");
	this.environment.add(new PropertySource("one", map));
	when(this.repository.findOne("foo", "bar", "master", false))
			.thenReturn(this.environment);
	assertThat(this.controller.findOne("foo", "bar", "master", false)
			.getPropertySources().get(0).getSource().toString())
					.isEqualTo("{foo=${bar}}");
}
 
Example #22
Source File: EnvironmentControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void arrayInYaml() throws Exception {
	Map<String, Object> map = new LinkedHashMap<String, Object>();
	map.put("a.b[0]", "c");
	map.put("a.b[1]", "d");
	this.environment.add(new PropertySource("one", map));
	when(this.repository.findOne("foo", "bar", null, false))
			.thenReturn(this.environment);
	String yaml = this.controller.yaml("foo", "bar", false).getBody();
	assertThat(yaml).isEqualTo("a:\n  b:\n  - c\n  - d\n");
}
 
Example #23
Source File: EnvironmentControllerIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void environmentWithApplicationContainingSlash() throws Exception {
	Environment environment = new Environment("foo/app", "default");
	environment.add(new PropertySource("foo", new HashMap<>()));
	when(this.repository.findOne("foo/app", "default", null, false))
			.thenReturn(environment);
	this.mvc.perform(MockMvcRequestBuilders.get("/foo(_)app/default"))
			.andExpect(MockMvcResultMatchers.status().isOk())
			.andExpect(MockMvcResultMatchers.content()
					.string(Matchers.containsString("\"propertySources\":")));
}
 
Example #24
Source File: AwsS3EnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
private void assertExpectedEnvironment(Environment env, String applicationName,
		String label, String version, int propertySourceCount, String... profiles) {
	assertThat(env.getName()).isEqualTo(applicationName);
	assertThat(env.getProfiles()).isEqualTo(profiles);
	assertThat(env.getLabel()).isEqualTo(label);
	assertThat(env.getVersion()).isEqualTo(version);
	assertThat(env.getPropertySources().size()).isEqualTo(propertySourceCount);
	for (PropertySource ps : env.getPropertySources()) {
		assertThat(ps.getSource()).isEqualTo(expectedProperties);
	}
}
 
Example #25
Source File: CipherEnvironmentEncryptorTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecryptEnvironment() {
	// given
	String secret = randomUUID().toString();

	// when
	Environment environment = new Environment("name", "profile", "label");
	environment.add(new PropertySource("a", Collections.<Object, Object>singletonMap(
			environment.getName(), "{cipher}" + this.textEncryptor.encrypt(secret))));

	// then
	assertThat(this.encryptor.decrypt(environment).getPropertySources().get(0)
			.getSource().get(environment.getName())).isEqualTo(secret);
}
 
Example #26
Source File: CipherEnvironmentEncryptorTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecryptEnvironmentWithKey() {
	// given
	String secret = randomUUID().toString();

	// when
	Environment environment = new Environment("name", "profile", "label");
	environment.add(new PropertySource("a",
			Collections.<Object, Object>singletonMap(environment.getName(),
					"{cipher}{key:test}" + this.textEncryptor.encrypt(secret))));

	// then
	assertThat(this.encryptor.decrypt(environment).getPropertySources().get(0)
			.getSource().get(environment.getName())).isEqualTo(secret);
}
 
Example #27
Source File: CipherEnvironmentEncryptorTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToUseNullAsPropertyValue() {

	// when
	Environment environment = new Environment("name", "profile", "label");
	environment.add(new PropertySource("a",
			Collections.<Object, Object>singletonMap(environment.getName(), null)));

	// then
	assertThat(this.encryptor.decrypt(environment).getPropertySources().get(0)
			.getSource().get(environment.getName())).isEqualTo(null);
}
 
Example #28
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 #29
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 #30
Source File: CustomCompositeEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Override
public Environment findOne(String application, String profile, String label,
		boolean includeOrigin) {
	Environment e = new Environment("test", new String[0], "label", "version",
			"state");
	PropertySource p = new PropertySource(this.properties.getPropertySourceName(),
			new HashMap<>());
	e.add(p);
	return e;
}