Add Spring Cloud Refresh Context smoke tests

Closes gh-26
This commit is contained in:
Olga MaciaszekSharma
2023-09-14 18:29:13 +02:00
committed by Sébastien Deleuze
parent 888b9e0c02
commit dae8b34de9
25 changed files with 619 additions and 5 deletions

View File

@@ -0,0 +1,57 @@
package com.example.contextrefresh;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.springframework.cr.smoketest.support.junit.ApplicationTest;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.assertj.core.api.Assertions.assertThat;
@ApplicationTest
public class ContextRefreshApplicationTests {
// Bring the config file back to init values before next app start,
// since the app updates the property file after properties set
@AfterAll
static void cleanUp() throws IOException {
editExternalConfigurationProperties("""
simple.test=testVal
test=propVal
""");
}
@Test
void refreshScopeBean(WebTestClient webClient) {
webClient.get()
.uri("/refresh")
.exchange()
.expectStatus()
.isOk()
.expectBody()
.consumeWith(result -> assertThat(new String(result.getResponseBodyContent())).isEqualTo("propValNew"));
}
@Test
void configurationProperties(WebTestClient webClient) {
webClient.get()
.uri("/simple")
.exchange()
.expectStatus()
.isOk()
.expectBody()
.consumeWith(result -> assertThat(new String(result.getResponseBodyContent())).isEqualTo("testValNew"));
}
private static void editExternalConfigurationProperties(String newFileContent) throws IOException {
Files.writeString(Path.of("./dev.properties"), newFileContent, Charset.defaultCharset(),
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
}
}

View File

@@ -0,0 +1,50 @@
package com.example.contextrefresh;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigUpdateController {
private static final Log LOG = LogFactory.getLog(ConfigUpdateController.class);
@Value("${config.path}")
private String path;
@EventListener(ApplicationStartedEvent.class)
public void doSomethingAfterStartup() throws IOException {
if (LOG.isDebugEnabled()) {
LOG.debug("Updating file values");
}
editExternalConfigurationProperties("""
simple.test=testValNew
test=propValNew
""");
}
@GetMapping("/reset")
void reset() throws IOException {
editExternalConfigurationProperties("""
simple.test=testVal
test=propVal
""");
}
private void editExternalConfigurationProperties(String newFileContent) throws IOException {
Files.writeString(Path.of(path), newFileContent, Charset.defaultCharset(), StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
}
}

View File

@@ -0,0 +1,17 @@
package com.example.contextrefresh;
import com.example.contextrefresh.config.SimpleConfigurationProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties(SimpleConfigurationProperties.class)
public class ContextRefreshApplication {
public static void main(String[] args) {
SpringApplication.run(ContextRefreshApplication.class, args);
}
}

View File

@@ -0,0 +1,22 @@
package com.example.contextrefresh;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/refresh")
@RefreshScope
public class RefreshScopeController {
@Value("${test}")
private String test;
@GetMapping
String test() {
return test;
}
}

View File

@@ -0,0 +1,24 @@
package com.example.contextrefresh;
import com.example.contextrefresh.config.SimpleConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/simple")
public class SimpleController {
private final SimpleConfigurationProperties properties;
public SimpleController(SimpleConfigurationProperties properties) {
this.properties = properties;
}
@GetMapping
String test() {
return properties.getTest();
}
}

View File

@@ -0,0 +1,18 @@
package com.example.contextrefresh.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "simple")
public class SimpleConfigurationProperties {
private String test = "default";
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}

View File

@@ -0,0 +1,8 @@
spring:
application:
name: context-refresh
config:
import: file:./cloud/context-refresh/dev.properties
config:
path: ./cloud/context-refresh/dev.properties

View File

@@ -0,0 +1,37 @@
package com.example.contextrefresh;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ContextRefreshTests {
@Autowired
private TestRestTemplate restTemplate;
@AfterEach
void cleanUp() {
restTemplate.getForObject("/reset", Void.class);
}
// Verify for initial values only
@Test
void refreshScopeBean() {
String property = this.restTemplate.getForObject("/refresh", String.class);
assertThat(property).isEqualTo("propVal");
}
// Verify for initial values only
@Test
void configurationProperties() {
String property = this.restTemplate.getForObject("/simple", String.class);
assertThat(property).isEqualTo("testVal");
}
}

View File

@@ -0,0 +1,8 @@
spring:
application:
name: context-refresh
config:
import: file:././dev.properties
config:
path: ././dev.properties