geroza/BAEL-395-Archaius_With_Spring_Cloud_Introduction (#4968)
* * added examples for archaius introduction, with basic configuration, extra configurations, adn setting up an additional config source * * fixed indentation in XML files
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.spring.cloud.archaius.basic;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BasicArchaiusApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BasicArchaiusApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.spring.cloud.archaius.basic.controller;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.netflix.config.DynamicIntProperty;
|
||||
import com.netflix.config.DynamicPropertyFactory;
|
||||
import com.netflix.config.DynamicStringProperty;
|
||||
|
||||
@RestController
|
||||
public class ConfigPropertiesController {
|
||||
|
||||
@Value("${baeldung.archaius.properties.one:not found!}")
|
||||
private String propertyOneWithValue;
|
||||
|
||||
@Value("${baeldung.archaius.properties.two:not found!}")
|
||||
private String propertyTwoWithValue;
|
||||
|
||||
@Value("${baeldung.archaius.properties.three:not found!}")
|
||||
private String propertyThreeWithValue;
|
||||
|
||||
@Value("${baeldung.archaius.properties.four:not found!}")
|
||||
private String propertyFourWithValue;
|
||||
|
||||
private DynamicStringProperty propertyOneWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getStringProperty("baeldung.archaius.properties.one", "not found!");
|
||||
|
||||
private DynamicStringProperty propertyTwoWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getStringProperty("baeldung.archaius.properties.two", "not found!");
|
||||
|
||||
private DynamicStringProperty propertyThreeWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getStringProperty("baeldung.archaius.properties.three", "not found!");
|
||||
|
||||
private DynamicStringProperty propertyFourWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getStringProperty("baeldung.archaius.properties.four", "not found!");
|
||||
|
||||
private DynamicIntProperty intPropertyWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getIntProperty("baeldung.archaius.properties.int", 0);
|
||||
|
||||
@GetMapping("/properties-from-value")
|
||||
public Map<String, String> getPropertiesFromValue() {
|
||||
Map<String, String> properties = new HashMap<>();
|
||||
properties.put("baeldung.archaius.properties.one", propertyOneWithValue);
|
||||
properties.put("baeldung.archaius.properties.two", propertyTwoWithValue);
|
||||
properties.put("baeldung.archaius.properties.three", propertyThreeWithValue);
|
||||
properties.put("baeldung.archaius.properties.four", propertyFourWithValue);
|
||||
return properties;
|
||||
}
|
||||
|
||||
@GetMapping("/properties-from-dynamic")
|
||||
public Map<String, String> getPropertiesFromDynamic() {
|
||||
Map<String, String> properties = new HashMap<>();
|
||||
properties.put(propertyOneWithDynamic.getName(), propertyOneWithDynamic.get());
|
||||
properties.put(propertyTwoWithDynamic.getName(), propertyTwoWithDynamic.get());
|
||||
properties.put(propertyThreeWithDynamic.getName(), propertyThreeWithDynamic.get());
|
||||
properties.put(propertyFourWithDynamic.getName(), propertyFourWithDynamic.get());
|
||||
return properties;
|
||||
}
|
||||
|
||||
@GetMapping("/int-property")
|
||||
public Map<String, Integer> getIntPropertyFromDynamic() {
|
||||
Map<String, Integer> properties = new HashMap<>();
|
||||
properties.put(intPropertyWithDynamic.getName(), intPropertyWithDynamic.get());
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
server.port=8080
|
||||
baeldung.archaius.properties.one=one FROM:application.properties
|
||||
baeldung.archaius.properties.two=two FROM:application.properties
|
||||
@@ -0,0 +1,4 @@
|
||||
baeldung.archaius.properties.one=one FROM:config.properties
|
||||
baeldung.archaius.properties.three=three FROM:config.properties
|
||||
|
||||
baeldung.archaius.properties.int=1
|
||||
@@ -0,0 +1,2 @@
|
||||
baeldung.archaius.properties.one=one FROM:other.properties
|
||||
baeldung.archaius.properties.four=four FROM:other.properties
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.spring.cloud.archaius.basic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import com.netflix.config.DynamicPropertyFactory;
|
||||
import com.netflix.config.DynamicStringProperty;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
public class ArchaiusBasicConfigurationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ConfigurableApplicationContext context;
|
||||
|
||||
private DynamicStringProperty testPropertyWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getStringProperty("baeldung.archaius.test.properties.one", "not found!");
|
||||
|
||||
@Test
|
||||
public void givenIntialPropertyValue_whenPropertyChanges_thenArchaiusRetrievesNewValue() {
|
||||
String initialValue = testPropertyWithDynamic.get();
|
||||
|
||||
TestPropertyValues.of("baeldung.archaius.test.properties.one=new-value")
|
||||
.applyTo(context);
|
||||
context.publishEvent(new EnvironmentChangeEvent(Collections.singleton("baeldung.archaius.test.properties.one")));
|
||||
String finalValue = testPropertyWithDynamic.get();
|
||||
|
||||
assertThat(initialValue).isEqualTo("test-one");
|
||||
assertThat(finalValue).isEqualTo("new-value");
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.spring.cloud.archaius.basic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class ArchaiusBasicConfigurationLiveTest {
|
||||
|
||||
private static final String BASE_URL = "http://localhost:8080";
|
||||
|
||||
private static final String DYNAMIC_PROPERTIES_URL = "/properties-from-dynamic";
|
||||
private static final Map<String, String> EXPECTED_ARCHAIUS_PROPERTIES = createExpectedArchaiusProperties();
|
||||
|
||||
private static Map<String, String> createExpectedArchaiusProperties() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("baeldung.archaius.properties.one", "one FROM:application.properties");
|
||||
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
|
||||
map.put("baeldung.archaius.properties.three", "three FROM:config.properties");
|
||||
map.put("baeldung.archaius.properties.four", "not found!");
|
||||
return map;
|
||||
}
|
||||
|
||||
private static final String VALUE_PROPERTIES_URL = "/properties-from-value";
|
||||
private static final Map<String, String> EXPECTED_VALUE_PROPERTIES = createExpectedValueProperties();
|
||||
|
||||
private static Map<String, String> createExpectedValueProperties() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("baeldung.archaius.properties.one", "one FROM:application.properties");
|
||||
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
|
||||
map.put("baeldung.archaius.properties.three", "not found!");
|
||||
map.put("baeldung.archaius.properties.four", "not found!");
|
||||
return map;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
ConfigurableApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate template;
|
||||
|
||||
private <T> Map<T, T> exchangeAsMap(String uri, ParameterizedTypeReference<Map<T, T>> responseType) {
|
||||
return template.exchange(uri, HttpMethod.GET, null, responseType)
|
||||
.getBody();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDefaultConfigurationSetup_whenRequestProperties_thenEndpointRetrievesValuesInFiles() {
|
||||
Map<String, String> initialResponse = this.exchangeAsMap(BASE_URL + DYNAMIC_PROPERTIES_URL, new ParameterizedTypeReference<Map<String, String>>() {
|
||||
});
|
||||
|
||||
assertThat(initialResponse).containsAllEntriesOf(EXPECTED_ARCHAIUS_PROPERTIES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonDefaultConfigurationFilesSetup_whenRequestSpringVisibleProperties_thenEndpointDoesntRetrieveArchaiusProperties() {
|
||||
Map<String, String> initialResponse = this.exchangeAsMap(BASE_URL + VALUE_PROPERTIES_URL, new ParameterizedTypeReference<Map<String, String>>() {
|
||||
});
|
||||
|
||||
assertThat(initialResponse).containsAllEntriesOf(EXPECTED_VALUE_PROPERTIES);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
baeldung.archaius.test.properties.one=test-one
|
||||
baeldung.archaius.test.properties.two=test-two
|
||||
baeldung.archaius.test.properties.int=1
|
||||
Reference in New Issue
Block a user