From f9d6380b5950a1290b77823cea2dac80a32afde1 Mon Sep 17 00:00:00 2001 From: Olga MaciaszekSharma Date: Thu, 5 Oct 2023 13:32:13 +0200 Subject: [PATCH] Add Context Refresh Hikari sample Closes gh-39 --- STATUS.adoc | 5 ++ ci/smoke-tests.yml | 3 + cloud/context-refresh-hikari/build.gradle | 33 ++++++++++ cloud/context-refresh-hikari/dev.properties | 3 + .../context-refresh-hikari/docker-compose.yml | 26 ++++++++ cloud/context-refresh-hikari/init.sql | 1 + .../ContextRefreshHikariApplicationTests.java | 64 +++++++++++++++++++ .../src/appTest/resources/application.yml | 6 ++ .../ContextRefreshHikariApplication.java | 33 ++++++++++ .../controllers/ConfigUpdateController.java | 52 +++++++++++++++ .../DataSourceMetadataController.java | 24 +++++++ .../controllers/UserController.java | 36 +++++++++++ .../contextrefreshhikari/entities/User.java | 50 +++++++++++++++ .../repositories/UserRepository.java | 12 ++++ .../src/main/resources/application.yml | 23 +++++++ .../ContextRefreshHttpApplicationTests.java | 1 + .../controllers}/RefreshScopeController.java | 2 +- .../{ => controllers}/SimpleController.java | 2 +- .../ContextRefreshApplicationTests.java | 14 ++-- .../ConfigUpdateController.java | 4 +- .../controllers}/RefreshScopeController.java | 2 +- .../{ => controllers}/SimpleController.java | 2 +- 22 files changed, 385 insertions(+), 13 deletions(-) create mode 100644 cloud/context-refresh-hikari/build.gradle create mode 100644 cloud/context-refresh-hikari/dev.properties create mode 100644 cloud/context-refresh-hikari/docker-compose.yml create mode 100644 cloud/context-refresh-hikari/init.sql create mode 100644 cloud/context-refresh-hikari/src/appTest/java/com/example/contextrefreshhikari/ContextRefreshHikariApplicationTests.java create mode 100644 cloud/context-refresh-hikari/src/appTest/resources/application.yml create mode 100644 cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/ContextRefreshHikariApplication.java create mode 100644 cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/ConfigUpdateController.java create mode 100644 cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/DataSourceMetadataController.java create mode 100644 cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/UserController.java create mode 100644 cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/entities/User.java create mode 100644 cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/repositories/UserRepository.java create mode 100644 cloud/context-refresh-hikari/src/main/resources/application.yml rename cloud/{context-refresh/src/main/java/com/example/contextrefresh => context-refresh-http/src/main/java/com/example/contextrefreshhttp/controllers}/RefreshScopeController.java (90%) rename cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/{ => controllers}/SimpleController.java (91%) rename cloud/context-refresh/src/main/java/com/example/contextrefresh/{ => controllers}/ConfigUpdateController.java (92%) rename cloud/{context-refresh-http/src/main/java/com/example/contextrefreshhttp => context-refresh/src/main/java/com/example/contextrefresh/controllers}/RefreshScopeController.java (91%) rename cloud/context-refresh/src/main/java/com/example/contextrefresh/{ => controllers}/SimpleController.java (92%) diff --git a/STATUS.adoc b/STATUS.adoc index 2255bdf..d2168eb 100644 --- a/STATUS.adoc +++ b/STATUS.adoc @@ -37,6 +37,11 @@ h|test |image:https://ci.spring.io/api/v1/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-cr-app-test/badge[link=https://ci.spring.io/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-cr-app-test] |image:https://ci.spring.io/api/v1/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-test/badge[link=https://ci.spring.io/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-test] +|context-refresh-hikari +|image:https://ci.spring.io/api/v1/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-hikari-app-test/badge[link=https://ci.spring.io/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-hikari-app-test] +|image:https://ci.spring.io/api/v1/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-hikari-cr-app-test/badge[link=https://ci.spring.io/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-hikari-cr-app-test] +| + |context-refresh-http |image:https://ci.spring.io/api/v1/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-http-app-test/badge[link=https://ci.spring.io/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-http-app-test] |image:https://ci.spring.io/api/v1/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-http-cr-app-test/badge[link=https://ci.spring.io/teams/spring-checkpoint-restore-smoke-tests/pipelines/spring-checkpoint-restore-smoke-tests-3.2.x/jobs/context-refresh-http-cr-app-test] diff --git a/ci/smoke-tests.yml b/ci/smoke-tests.yml index 3160291..ecc8981 100644 --- a/ci/smoke-tests.yml +++ b/ci/smoke-tests.yml @@ -12,6 +12,9 @@ groups: - name: context-refresh app_test: true test: true + - name: context-refresh-hikari + app_test: true + test: false - name: context-refresh-http app_test: true test: true diff --git a/cloud/context-refresh-hikari/build.gradle b/cloud/context-refresh-hikari/build.gradle new file mode 100644 index 0000000..c8f8e12 --- /dev/null +++ b/cloud/context-refresh-hikari/build.gradle @@ -0,0 +1,33 @@ +plugins { + id "java" + id "org.springframework.boot" + id "org.springframework.cr.smoke-test" +} + +// Not using Spring Cloud bom due to existing issues with dependency management +ext { + set('springCloudStarterVersion', "4.1.0-SNAPSHOT") +} + +dependencies { + implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)) + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-data-jpa") + implementation("org.crac:crac:$cracVersion") + implementation(project(":cr-listener")) + implementation("org.springframework.cloud:spring-cloud-starter:${springCloudStarterVersion}") + runtimeOnly("org.postgresql:postgresql") + + + appTestImplementation(project(":cr-smoke-test-support")) + appTestImplementation(project(":cloud:context-refresh-hikari")) +} + +crSmokeTest { + webApplication = true +} + +// `@ApplicationTest`s need to only be executed during the `checkpointRestoreAppTest and not `appTest` +// (we're checking for a value that we want to have been modified in between these two phases). +appTest.enabled = false + diff --git a/cloud/context-refresh-hikari/dev.properties b/cloud/context-refresh-hikari/dev.properties new file mode 100644 index 0000000..30f4b55 --- /dev/null +++ b/cloud/context-refresh-hikari/dev.properties @@ -0,0 +1,3 @@ +spring.datasource.url: jdbc:postgresql://${DB1_HOST:localhost}:${DB1_PORT_5432:5432}/database1 +spring.datasource.username=user1 +spring.datasource.password=passwd1 diff --git a/cloud/context-refresh-hikari/docker-compose.yml b/cloud/context-refresh-hikari/docker-compose.yml new file mode 100644 index 0000000..71a5ffb --- /dev/null +++ b/cloud/context-refresh-hikari/docker-compose.yml @@ -0,0 +1,26 @@ +version: '2' + +services: + db1: + image: 'postgres:16.0-alpine' + container_name: db1 + volumes: + - ./init.sql:/docker-entrypoint-initdb.d/init.sql + environment: + - POSTGRES_USER=user1 + - POSTGRES_PASSWORD=passwd1 + - POSTGRES_DB=database1 + ports: + - "5432" + + db2: + image: 'postgres:16.0-alpine' + container_name: db2 + volumes: + - ./init.sql:/docker-entrypoint-initdb.d/init.sql + environment: + - POSTGRES_USER=user2 + - POSTGRES_PASSWORD=passwd2 + - POSTGRES_DB=database2 + ports: + - "5432" \ No newline at end of file diff --git a/cloud/context-refresh-hikari/init.sql b/cloud/context-refresh-hikari/init.sql new file mode 100644 index 0000000..8f41070 --- /dev/null +++ b/cloud/context-refresh-hikari/init.sql @@ -0,0 +1 @@ +create table if not exists users (id bigserial not null, email varchar(255), name varchar(255), primary key (id)); \ No newline at end of file diff --git a/cloud/context-refresh-hikari/src/appTest/java/com/example/contextrefreshhikari/ContextRefreshHikariApplicationTests.java b/cloud/context-refresh-hikari/src/appTest/java/com/example/contextrefreshhikari/ContextRefreshHikariApplicationTests.java new file mode 100644 index 0000000..ec98a1c --- /dev/null +++ b/cloud/context-refresh-hikari/src/appTest/java/com/example/contextrefreshhikari/ContextRefreshHikariApplicationTests.java @@ -0,0 +1,64 @@ +package com.example.contextrefreshhikari; + +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 com.example.contextrefreshhikari.entities.User; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; + +import org.springframework.cr.smoketest.support.assertj.AssertableOutput; +import org.springframework.cr.smoketest.support.junit.ApplicationTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.assertj.core.api.Assertions.assertThat; + +@SuppressWarnings({ "JUnitMalformedDeclaration" }) +@ApplicationTest +public class ContextRefreshHikariApplicationTests { + + // 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(); + } + + @Test + void connectionMetadata(WebTestClient webClient) { + webClient.get().uri("/metadata").exchange().expectStatus().isOk().expectBody().consumeWith(result -> { + String connectionString = new String(result.getResponseBodyContent()); + assertThat(connectionString).startsWith("jdbc:postgresql"); + assertThat(connectionString).endsWith("database2"); + }); + } + + @Test + void users(WebTestClient webTestClient, AssertableOutput output) { + // We've connected to a different db after restore, so it should not have any + // users + webTestClient.get().uri("/users/add").exchange().expectStatus().isOk(); + + webTestClient.get() + .uri("/users") + .header("ContentType", MediaType.APPLICATION_JSON_VALUE) + .exchange() + .expectStatus() + .isOk() + .expectBodyList(User.class) + .hasSize(2); + } + + private static void editExternalConfigurationProperties() throws IOException { + Files.writeString(Path.of("./dev.properties"), """ + spring.datasource.url: jdbc:postgresql://${DB1_HOST:localhost}:${DB1_PORT_5432:5432}/database1 + spring.datasource.username=user1 + spring.datasource.password=passwd1 + """, Charset.defaultCharset(), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); + } + +} diff --git a/cloud/context-refresh-hikari/src/appTest/resources/application.yml b/cloud/context-refresh-hikari/src/appTest/resources/application.yml new file mode 100644 index 0000000..e63f288 --- /dev/null +++ b/cloud/context-refresh-hikari/src/appTest/resources/application.yml @@ -0,0 +1,6 @@ +spring: + config: + import: file:././dev.properties + +config: + path: ././dev.properties \ No newline at end of file diff --git a/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/ContextRefreshHikariApplication.java b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/ContextRefreshHikariApplication.java new file mode 100644 index 0000000..e718eb8 --- /dev/null +++ b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/ContextRefreshHikariApplication.java @@ -0,0 +1,33 @@ +package com.example.contextrefreshhikari; + +import com.example.contextrefreshhikari.entities.User; +import com.example.contextrefreshhikari.repositories.UserRepository; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class ContextRefreshHikariApplication { + + private static final Log LOG = LogFactory.getLog(ContextRefreshHikariApplication.class); + + public static void main(String[] args) { + SpringApplication.run(ContextRefreshHikariApplication.class, args); + } + + @Bean + public CommandLineRunner run(UserRepository userRepository) { + return (String[] args) -> { + User user1 = new User("John", "john@domain.com"); + User user2 = new User("Julie", "julie@domain.com"); + userRepository.save(user1); + userRepository.save(user2); + userRepository.findAll().forEach(LOG::info); + }; + } + +} diff --git a/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/ConfigUpdateController.java b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/ConfigUpdateController.java new file mode 100644 index 0000000..9567f96 --- /dev/null +++ b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/ConfigUpdateController.java @@ -0,0 +1,52 @@ +package com.example.contextrefreshhikari.controllers; + +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 editExternalConfig() throws IOException { + if (LOG.isDebugEnabled()) { + LOG.debug("Updating file values"); + } + editExternalConfigurationProperties(""" + spring.datasource.url: jdbc:postgresql://${DB2_HOST:localhost}:${DB2_PORT_5432:5432}/database2 + spring.datasource.username=user2 + spring.datasource.password=passwd2 + """); + } + + @GetMapping("/reset") + void reset() throws IOException { + editExternalConfigurationProperties(""" + spring.datasource.url: jdbc:postgresql://${DB1_HOST:localhost}:${DB1_PORT_5432:5432}/database1 + spring.datasource.username=user1 + spring.datasource.password=passwd1 + """); + } + + private void editExternalConfigurationProperties(String newFileContent) throws IOException { + Files.writeString(Path.of(path), newFileContent, Charset.defaultCharset(), StandardOpenOption.WRITE, + StandardOpenOption.TRUNCATE_EXISTING); + } + +} diff --git a/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/DataSourceMetadataController.java b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/DataSourceMetadataController.java new file mode 100644 index 0000000..edf1181 --- /dev/null +++ b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/DataSourceMetadataController.java @@ -0,0 +1,24 @@ +package com.example.contextrefreshhikari.controllers; + +import java.sql.SQLException; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/metadata") +public class DataSourceMetadataController { + + @Autowired + DataSource dataSource; + + @GetMapping + public String getConnectionsUrl() throws SQLException { + return dataSource.getConnection().getMetaData().getURL(); + } + +} diff --git a/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/UserController.java b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/UserController.java new file mode 100644 index 0000000..cb3cc72 --- /dev/null +++ b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/controllers/UserController.java @@ -0,0 +1,36 @@ +package com.example.contextrefreshhikari.controllers; + +import java.util.List; + +import com.example.contextrefreshhikari.entities.User; +import com.example.contextrefreshhikari.repositories.UserRepository; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/users") +public class UserController { + + private final UserRepository userRepository; + + public UserController(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) + List getUsers() { + return userRepository.findAll(); + } + + @GetMapping("/add") + void addUsers() { + User user1 = new User("Steve", "steve@domain.com"); + User user2 = new User("Sarah", "sarah@domain.com"); + userRepository.save(user1); + userRepository.save(user2); + } + +} diff --git a/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/entities/User.java b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/entities/User.java new file mode 100644 index 0000000..b0bd1ce --- /dev/null +++ b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/entities/User.java @@ -0,0 +1,50 @@ +package com.example.contextrefreshhikari.entities; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +@Entity +@Table(name = "users") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private String name; + + private String email; + + public User(String name, String email) { + this.name = name; + this.email = email; + } + + public User() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public String toString() { + return "User{" + "id=" + id + ", name='" + name + '\'' + ", email='" + email + '\'' + '}'; + } + +} diff --git a/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/repositories/UserRepository.java b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/repositories/UserRepository.java new file mode 100644 index 0000000..170df79 --- /dev/null +++ b/cloud/context-refresh-hikari/src/main/java/com/example/contextrefreshhikari/repositories/UserRepository.java @@ -0,0 +1,12 @@ +package com.example.contextrefreshhikari.repositories; + +import com.example.contextrefreshhikari.entities.User; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserRepository extends JpaRepository { + +} \ No newline at end of file diff --git a/cloud/context-refresh-hikari/src/main/resources/application.yml b/cloud/context-refresh-hikari/src/main/resources/application.yml new file mode 100644 index 0000000..5bf55ad --- /dev/null +++ b/cloud/context-refresh-hikari/src/main/resources/application.yml @@ -0,0 +1,23 @@ +spring: + datasource: + hikari: + allow-pool-suspension: true + driver-class-name: org.postgresql.Driver + cloud: + refresh: + never-refreshable: + extra-refreshable: + - javax.sql.DataSource + - com.zaxxer.hikari.HikariDataSource + application: + name: context-refresh-hikari + config: + import: file:./cloud/context-refresh-hikari/dev.properties + jpa: + hibernate: + ddl-auto: create + show-sql: true + properties: + hibernate.id.new_generator_mappings: false +config: + path: ./cloud/context-refresh-hikari/dev.properties \ No newline at end of file diff --git a/cloud/context-refresh-http/src/appTest/java/com/example/contextrefreshhttp/ContextRefreshHttpApplicationTests.java b/cloud/context-refresh-http/src/appTest/java/com/example/contextrefreshhttp/ContextRefreshHttpApplicationTests.java index 9145193..5dc1c83 100644 --- a/cloud/context-refresh-http/src/appTest/java/com/example/contextrefreshhttp/ContextRefreshHttpApplicationTests.java +++ b/cloud/context-refresh-http/src/appTest/java/com/example/contextrefreshhttp/ContextRefreshHttpApplicationTests.java @@ -14,6 +14,7 @@ import org.springframework.test.web.reactive.server.WebTestClient; import static org.assertj.core.api.Assertions.assertThat; +@SuppressWarnings({ "JUnitMalformedDeclaration", "DataFlowIssue" }) @ApplicationTest public class ContextRefreshHttpApplicationTests { diff --git a/cloud/context-refresh/src/main/java/com/example/contextrefresh/RefreshScopeController.java b/cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/controllers/RefreshScopeController.java similarity index 90% rename from cloud/context-refresh/src/main/java/com/example/contextrefresh/RefreshScopeController.java rename to cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/controllers/RefreshScopeController.java index cedbd91..2658b57 100644 --- a/cloud/context-refresh/src/main/java/com/example/contextrefresh/RefreshScopeController.java +++ b/cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/controllers/RefreshScopeController.java @@ -1,4 +1,4 @@ -package com.example.contextrefresh; +package com.example.contextrefreshhttp.controllers; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; diff --git a/cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/SimpleController.java b/cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/controllers/SimpleController.java similarity index 91% rename from cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/SimpleController.java rename to cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/controllers/SimpleController.java index d0e12e5..7d731db 100644 --- a/cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/SimpleController.java +++ b/cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/controllers/SimpleController.java @@ -1,4 +1,4 @@ -package com.example.contextrefreshhttp; +package com.example.contextrefreshhttp.controllers; import com.example.contextrefreshhttp.config.SimpleConfigurationProperties; diff --git a/cloud/context-refresh/src/appTest/java/com/example/contextrefresh/ContextRefreshApplicationTests.java b/cloud/context-refresh/src/appTest/java/com/example/contextrefresh/ContextRefreshApplicationTests.java index e489c0f..dd85343 100644 --- a/cloud/context-refresh/src/appTest/java/com/example/contextrefresh/ContextRefreshApplicationTests.java +++ b/cloud/context-refresh/src/appTest/java/com/example/contextrefresh/ContextRefreshApplicationTests.java @@ -14,6 +14,7 @@ import org.springframework.test.web.reactive.server.WebTestClient; import static org.assertj.core.api.Assertions.assertThat; +@SuppressWarnings({ "JUnitMalformedDeclaration", "DataFlowIssue" }) @ApplicationTest public class ContextRefreshApplicationTests { @@ -21,10 +22,7 @@ public class ContextRefreshApplicationTests { // since the app updates the property file after properties set @AfterAll static void cleanUp() throws IOException { - editExternalConfigurationProperties(""" - simple.test=testVal - test=propVal - """); + editExternalConfigurationProperties(); } @Test @@ -49,9 +47,11 @@ public class ContextRefreshApplicationTests { .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); + private static void editExternalConfigurationProperties() throws IOException { + Files.writeString(Path.of("./dev.properties"), """ + simple.test=testVal + test=propVal + """, Charset.defaultCharset(), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); } } diff --git a/cloud/context-refresh/src/main/java/com/example/contextrefresh/ConfigUpdateController.java b/cloud/context-refresh/src/main/java/com/example/contextrefresh/controllers/ConfigUpdateController.java similarity index 92% rename from cloud/context-refresh/src/main/java/com/example/contextrefresh/ConfigUpdateController.java rename to cloud/context-refresh/src/main/java/com/example/contextrefresh/controllers/ConfigUpdateController.java index 65371de..a798094 100644 --- a/cloud/context-refresh/src/main/java/com/example/contextrefresh/ConfigUpdateController.java +++ b/cloud/context-refresh/src/main/java/com/example/contextrefresh/controllers/ConfigUpdateController.java @@ -1,4 +1,4 @@ -package com.example.contextrefresh; +package com.example.contextrefresh.controllers; import java.io.IOException; import java.nio.charset.Charset; @@ -24,7 +24,7 @@ public class ConfigUpdateController { private String path; @EventListener(ApplicationStartedEvent.class) - public void doSomethingAfterStartup() throws IOException { + public void editExternalConfig() throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("Updating file values"); } diff --git a/cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/RefreshScopeController.java b/cloud/context-refresh/src/main/java/com/example/contextrefresh/controllers/RefreshScopeController.java similarity index 91% rename from cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/RefreshScopeController.java rename to cloud/context-refresh/src/main/java/com/example/contextrefresh/controllers/RefreshScopeController.java index 82c4200..ae29a5d 100644 --- a/cloud/context-refresh-http/src/main/java/com/example/contextrefreshhttp/RefreshScopeController.java +++ b/cloud/context-refresh/src/main/java/com/example/contextrefresh/controllers/RefreshScopeController.java @@ -1,4 +1,4 @@ -package com.example.contextrefreshhttp; +package com.example.contextrefresh.controllers; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; diff --git a/cloud/context-refresh/src/main/java/com/example/contextrefresh/SimpleController.java b/cloud/context-refresh/src/main/java/com/example/contextrefresh/controllers/SimpleController.java similarity index 92% rename from cloud/context-refresh/src/main/java/com/example/contextrefresh/SimpleController.java rename to cloud/context-refresh/src/main/java/com/example/contextrefresh/controllers/SimpleController.java index a1e9eea..9a5b6ce 100644 --- a/cloud/context-refresh/src/main/java/com/example/contextrefresh/SimpleController.java +++ b/cloud/context-refresh/src/main/java/com/example/contextrefresh/controllers/SimpleController.java @@ -1,4 +1,4 @@ -package com.example.contextrefresh; +package com.example.contextrefresh.controllers; import com.example.contextrefresh.config.SimpleConfigurationProperties;