|
| 1 | +package com.baeldung.spring.cloud.archaius.additionalsources; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 13 | +import org.springframework.context.ConfigurableApplicationContext; |
| 14 | +import org.springframework.core.ParameterizedTypeReference; |
| 15 | +import org.springframework.http.HttpMethod; |
| 16 | +import org.springframework.test.context.junit4.SpringRunner; |
| 17 | + |
| 18 | +@RunWith(SpringRunner.class) |
| 19 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 20 | +public class ArchaiusAdditionalSourcesLiveTest { |
| 21 | + |
| 22 | + private static final String BASE_URL = "http://localhost:8082"; |
| 23 | + |
| 24 | + private static final String DYNAMIC_PROPERTIES_URL = "/properties-from-dynamic"; |
| 25 | + private static final Map<String, String> EXPECTED_ARCHAIUS_PROPERTIES = createExpectedArchaiusProperties(); |
| 26 | + |
| 27 | + private static Map<String, String> createExpectedArchaiusProperties() { |
| 28 | + Map<String, String> map = new HashMap<>(); |
| 29 | + map.put("baeldung.archaius.properties.one", "one FROM:other-config.properties"); |
| 30 | + map.put("baeldung.archaius.properties.two", "two FROM:application.properties"); |
| 31 | + map.put("baeldung.archaius.properties.three", "three FROM:config.properties"); |
| 32 | + map.put("baeldung.archaius.properties.four", "four FROM:other-config.properties"); |
| 33 | + return map; |
| 34 | + } |
| 35 | + |
| 36 | + @Autowired |
| 37 | + ConfigurableApplicationContext context; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private TestRestTemplate template; |
| 41 | + |
| 42 | + private <T> Map<T, T> exchangeAsMap(String uri, ParameterizedTypeReference<Map<T, T>> responseType) { |
| 43 | + return template.exchange(uri, HttpMethod.GET, null, responseType) |
| 44 | + .getBody(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenNonDefaultConfigurationFilesSetup_whenRequestProperties_thenEndpointRetrievesValuesInFiles() { |
| 49 | + Map<String, String> initialResponse = this.exchangeAsMap(BASE_URL + DYNAMIC_PROPERTIES_URL, new ParameterizedTypeReference<Map<String, String>>() { |
| 50 | + }); |
| 51 | + |
| 52 | + assertThat(initialResponse).containsAllEntriesOf(EXPECTED_ARCHAIUS_PROPERTIES); |
| 53 | + } |
| 54 | +} |
0 commit comments