From a511af6ffc16fde0f211884745ed3ae887527dc5 Mon Sep 17 00:00:00 2001 From: Pavel Vetokhin Date: Thu, 5 Oct 2023 21:35:41 +0300 Subject: [PATCH] Decompose properties files --- .../java/smecalculus/bezmen/construction/App.java | 2 ++ .../foo/src/main/resources/application.properties | 0 apps/foo/src/main/resources/application.yaml | 6 ------ apps/foo/src/main/resources/reference.conf | 0 .../bezmen/configuration/MessagingCfgMapper.java | 6 +++--- .../bezmen/construction/MessagingConfigBeans.java | 2 ++ .../src/main/resources/messaging.properties | 2 ++ libs/messaging/src/main/resources/messaging.yaml | 7 ------- .../construction/MessagingConfigBeansIT.java | 6 +++--- .../MessagingConfigBeansLightbendConfigIT.java | 6 ++++++ .../MessagingConfigBeansSpringConfigIT.java | 6 ++++++ .../bezmen/configuration/StorageCfgMapper.java | 2 +- .../bezmen/construction/StorageConfigBeans.java | 2 ++ .../storage/src/main/resources/storage.properties | 8 ++++++++ libs/storage/src/main/resources/storage.yaml | 15 --------------- .../bezmen/construction/StorageConfigBeansIT.java | 6 +++--- .../StorageConfigBeansLightbendConfigIT.java | 6 ++++++ .../StorageConfigBeansSpringConfigIT.java | 6 ++++++ .../construction/StorageConfigBeansTest.java | 14 ++++++-------- solutions/basis/compose.yml | 2 +- 20 files changed, 57 insertions(+), 47 deletions(-) create mode 100644 apps/foo/src/main/resources/application.properties delete mode 100644 apps/foo/src/main/resources/application.yaml create mode 100644 apps/foo/src/main/resources/reference.conf create mode 100644 libs/messaging/src/main/resources/messaging.properties delete mode 100644 libs/messaging/src/main/resources/messaging.yaml create mode 100644 libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansLightbendConfigIT.java create mode 100644 libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansSpringConfigIT.java create mode 100644 libs/storage/src/main/resources/storage.properties delete mode 100644 libs/storage/src/main/resources/storage.yaml create mode 100644 libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansLightbendConfigIT.java create mode 100644 libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansSpringConfigIT.java diff --git a/apps/foo/src/main/java/smecalculus/bezmen/construction/App.java b/apps/foo/src/main/java/smecalculus/bezmen/construction/App.java index f6250dc9..3a08cfff 100644 --- a/apps/foo/src/main/java/smecalculus/bezmen/construction/App.java +++ b/apps/foo/src/main/java/smecalculus/bezmen/construction/App.java @@ -8,6 +8,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; import smecalculus.bezmen.core.SepulkaConverter; import smecalculus.bezmen.core.SepulkaConverterImpl; import smecalculus.bezmen.core.SepulkaService; @@ -27,6 +28,7 @@ import smecalculus.bezmen.validation.EdgeValidator; @Import({ConfigBeans.class, ValidationBeans.class, MessagingBeans.class, StorageBeans.class}) +@PropertySource("classpath:application.properties") @Configuration(proxyBeanMethods = false) public class App { diff --git a/apps/foo/src/main/resources/application.properties b/apps/foo/src/main/resources/application.properties new file mode 100644 index 00000000..e69de29b diff --git a/apps/foo/src/main/resources/application.yaml b/apps/foo/src/main/resources/application.yaml deleted file mode 100644 index dc5a7746..00000000 --- a/apps/foo/src/main/resources/application.yaml +++ /dev/null @@ -1,6 +0,0 @@ ---- -spring: - config: - import: - - classpath:messaging.yaml - - classpath:storage.yaml diff --git a/apps/foo/src/main/resources/reference.conf b/apps/foo/src/main/resources/reference.conf new file mode 100644 index 00000000..e69de29b diff --git a/libs/messaging/src/main/java/smecalculus/bezmen/configuration/MessagingCfgMapper.java b/libs/messaging/src/main/java/smecalculus/bezmen/configuration/MessagingCfgMapper.java index f04a1c62..2cd35b05 100644 --- a/libs/messaging/src/main/java/smecalculus/bezmen/configuration/MessagingCfgMapper.java +++ b/libs/messaging/src/main/java/smecalculus/bezmen/configuration/MessagingCfgMapper.java @@ -8,13 +8,13 @@ public interface MessagingCfgMapper { @Mapping(source = "protocol", target = "protocolProps") @Mapping(source = "mapping", target = "mappingProps") - MessagingProps toDomain(MessagingPropsCfg messagingPropsCfg); + MessagingProps toDomain(MessagingPropsCfg propsCfg); @Mapping(source = "modes", target = "protocolModes") - MessagingProtocolProps toDomain(MessagingProtocolPropsCfg protocolPropsCfg); + MessagingProtocolProps toDomain(MessagingProtocolPropsCfg propsCfg); @Mapping(source = "modes", target = "mappingModes") - MessageMappingProps toDomain(MessageMappingPropsCfg mappingPropsCfg); + MessageMappingProps toDomain(MessageMappingPropsCfg propsCfg); default MessagingProtocolMode toProtocolMode(String value) { return MessagingProtocolMode.valueOf(value.toUpperCase()); diff --git a/libs/messaging/src/main/java/smecalculus/bezmen/construction/MessagingConfigBeans.java b/libs/messaging/src/main/java/smecalculus/bezmen/construction/MessagingConfigBeans.java index 35265465..cd190ca5 100644 --- a/libs/messaging/src/main/java/smecalculus/bezmen/construction/MessagingConfigBeans.java +++ b/libs/messaging/src/main/java/smecalculus/bezmen/construction/MessagingConfigBeans.java @@ -4,6 +4,7 @@ import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; import smecalculus.bezmen.configuration.ConfigKeeper; import smecalculus.bezmen.configuration.MessagingCfgMapper; import smecalculus.bezmen.configuration.MessagingCfgMapperImpl; @@ -11,6 +12,7 @@ import smecalculus.bezmen.configuration.MessagingPropsCfg; import smecalculus.bezmen.validation.EdgeValidator; +@PropertySource("classpath:messaging.properties") @Configuration(proxyBeanMethods = false) public class MessagingConfigBeans { diff --git a/libs/messaging/src/main/resources/messaging.properties b/libs/messaging/src/main/resources/messaging.properties new file mode 100644 index 00000000..4cec1140 --- /dev/null +++ b/libs/messaging/src/main/resources/messaging.properties @@ -0,0 +1,2 @@ +bezmen.messaging.protocol.modes[0]=http +bezmen.messaging.mapping.modes[0]=spring_mvc diff --git a/libs/messaging/src/main/resources/messaging.yaml b/libs/messaging/src/main/resources/messaging.yaml deleted file mode 100644 index aa960877..00000000 --- a/libs/messaging/src/main/resources/messaging.yaml +++ /dev/null @@ -1,7 +0,0 @@ ---- -bezmen: - messaging: - protocol: - modes: [http] - mapping: - modes: [spring_mvc] diff --git a/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansIT.java b/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansIT.java index 5a929f5d..19dbbb29 100644 --- a/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansIT.java +++ b/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansIT.java @@ -11,15 +11,15 @@ import smecalculus.bezmen.configuration.MessagingProps; @ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {ConfigBeans.class, ValidationBeans.class, MessagingConfigBeans.class}) -class MessagingConfigBeansIT { +@ContextConfiguration(classes = {MessagingConfigBeans.class, ConfigBeans.class, ValidationBeans.class}) +abstract class MessagingConfigBeansIT { @Test void defaultConfigShouldBeBackwardCompatible(@Autowired MessagingProps actualProps) { // given MessagingProps expectedProps = messagingProps().build(); // when - // construction + // default construction // then assertThat(actualProps).isEqualTo(expectedProps); } diff --git a/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansLightbendConfigIT.java b/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansLightbendConfigIT.java new file mode 100644 index 00000000..5e8b69f1 --- /dev/null +++ b/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansLightbendConfigIT.java @@ -0,0 +1,6 @@ +package smecalculus.bezmen.construction; + +import org.springframework.test.context.TestPropertySource; + +@TestPropertySource(properties = {"bezmen.config.mapping.mode=lightbend_config"}) +class MessagingConfigBeansLightbendConfigIT extends MessagingConfigBeansIT {} diff --git a/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansSpringConfigIT.java b/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansSpringConfigIT.java new file mode 100644 index 00000000..668b3e3f --- /dev/null +++ b/libs/messaging/src/test/java/smecalculus/bezmen/construction/MessagingConfigBeansSpringConfigIT.java @@ -0,0 +1,6 @@ +package smecalculus.bezmen.construction; + +import org.springframework.test.context.TestPropertySource; + +@TestPropertySource(properties = {"bezmen.config.mapping.mode=spring_config"}) +class MessagingConfigBeansSpringConfigIT extends MessagingConfigBeansIT {} diff --git a/libs/storage/src/main/java/smecalculus/bezmen/configuration/StorageCfgMapper.java b/libs/storage/src/main/java/smecalculus/bezmen/configuration/StorageCfgMapper.java index 7c6c1e77..02ca7a8b 100644 --- a/libs/storage/src/main/java/smecalculus/bezmen/configuration/StorageCfgMapper.java +++ b/libs/storage/src/main/java/smecalculus/bezmen/configuration/StorageCfgMapper.java @@ -8,7 +8,7 @@ public interface StorageCfgMapper { @Mapping(source = "protocol", target = "protocolProps") @Mapping(source = "mapping", target = "mappingProps") - StorageProps toDomain(StoragePropsCfg storagePropsCfg); + StorageProps toDomain(StoragePropsCfg propsCfg); @Mapping(source = "mode", target = "protocolMode") @Mapping(source = "h2", target = "h2Props") diff --git a/libs/storage/src/main/java/smecalculus/bezmen/construction/StorageConfigBeans.java b/libs/storage/src/main/java/smecalculus/bezmen/construction/StorageConfigBeans.java index 6958fe7b..061cf4d7 100644 --- a/libs/storage/src/main/java/smecalculus/bezmen/construction/StorageConfigBeans.java +++ b/libs/storage/src/main/java/smecalculus/bezmen/construction/StorageConfigBeans.java @@ -4,6 +4,7 @@ import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; import smecalculus.bezmen.configuration.ConfigKeeper; import smecalculus.bezmen.configuration.StorageCfgMapper; import smecalculus.bezmen.configuration.StorageCfgMapperImpl; @@ -11,6 +12,7 @@ import smecalculus.bezmen.configuration.StoragePropsCfg; import smecalculus.bezmen.validation.EdgeValidator; +@PropertySource("classpath:storage.properties") @Configuration(proxyBeanMethods = false) public class StorageConfigBeans { diff --git a/libs/storage/src/main/resources/storage.properties b/libs/storage/src/main/resources/storage.properties new file mode 100644 index 00000000..b3056b72 --- /dev/null +++ b/libs/storage/src/main/resources/storage.properties @@ -0,0 +1,8 @@ +bezmen.storage.protocol.mode=h2 +bezmen.storage.protocol.h2.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 +bezmen.storage.protocol.h2.username=sa +bezmen.storage.protocol.h2.password=sa +bezmen.storage.protocol.postgres.url=jdbc:postgresql://localhost:5432/bezmen +bezmen.storage.protocol.postgres.username=bezmen +bezmen.storage.protocol.postgres.password=bezmen +bezmen.storage.mapping.mode=spring_data diff --git a/libs/storage/src/main/resources/storage.yaml b/libs/storage/src/main/resources/storage.yaml deleted file mode 100644 index 0c2d3567..00000000 --- a/libs/storage/src/main/resources/storage.yaml +++ /dev/null @@ -1,15 +0,0 @@ ---- -bezmen: - storage: - protocol: - mode: h2 - h2: - url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 - username: sa - password: sa - postgres: - url: jdbc:postgresql://localhost:5432/bezmen - username: bezmen - password: bezmen - mapping: - mode: spring_data diff --git a/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansIT.java b/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansIT.java index f0381447..62de9f65 100644 --- a/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansIT.java +++ b/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansIT.java @@ -11,15 +11,15 @@ import smecalculus.bezmen.configuration.StorageProps; @ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {ConfigBeans.class, ValidationBeans.class, StorageConfigBeans.class}) -class StorageConfigBeansIT { +@ContextConfiguration(classes = {StorageConfigBeans.class, ConfigBeans.class, ValidationBeans.class}) +abstract class StorageConfigBeansIT { @Test void defaultConfigShouldBeBackwardCompatible(@Autowired StorageProps actualProps) { // given StorageProps expectedProps = storageProps().build(); // when - // construction + // default construction // then assertThat(actualProps).isEqualTo(expectedProps); } diff --git a/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansLightbendConfigIT.java b/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansLightbendConfigIT.java new file mode 100644 index 00000000..4631e191 --- /dev/null +++ b/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansLightbendConfigIT.java @@ -0,0 +1,6 @@ +package smecalculus.bezmen.construction; + +import org.springframework.test.context.TestPropertySource; + +@TestPropertySource(properties = {"bezmen.config.mapping.mode=lightbend_config"}) +class StorageConfigBeansLightbendConfigIT extends StorageConfigBeansIT {} diff --git a/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansSpringConfigIT.java b/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansSpringConfigIT.java new file mode 100644 index 00000000..3fe90f67 --- /dev/null +++ b/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansSpringConfigIT.java @@ -0,0 +1,6 @@ +package smecalculus.bezmen.construction; + +import org.springframework.test.context.TestPropertySource; + +@TestPropertySource(properties = {"bezmen.config.mapping.mode=spring_config"}) +class StorageConfigBeansSpringConfigIT extends StorageConfigBeansIT {} diff --git a/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansTest.java b/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansTest.java index b01f3867..79bf441f 100644 --- a/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansTest.java +++ b/libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansTest.java @@ -15,28 +15,26 @@ class StorageConfigBeansTest { - private StorageConfigBeans configBeans; + private final StorageConfigBeans config = new StorageConfigBeans(); + private final StorageCfgMapper mapper = new StorageCfgMapperImpl(); private EdgeValidator validatorMock; private ConfigKeeper keeperMock; - private StorageCfgMapper mapper; @BeforeEach void setUp() { - configBeans = new StorageConfigBeans(); validatorMock = mock(EdgeValidator.class); keeperMock = mock(ConfigKeeper.class); - mapper = new StorageCfgMapperImpl(); } @Test void shouldValidateConf() { // given - StoragePropsCfg expectedStorageProps = storagePropsCfg(); + StoragePropsCfg expectedProps = storagePropsCfg(); // and - when(keeperMock.read("bezmen.storage", StoragePropsCfg.class)).thenReturn(expectedStorageProps); + when(keeperMock.read("bezmen.storage", StoragePropsCfg.class)).thenReturn(expectedProps); // when - configBeans.storageProps(keeperMock, validatorMock, mapper); + config.storageProps(keeperMock, validatorMock, mapper); // then - verify(validatorMock).validate(expectedStorageProps); + verify(validatorMock).validate(expectedProps); } } diff --git a/solutions/basis/compose.yml b/solutions/basis/compose.yml index 2237356f..e3695f7c 100644 --- a/solutions/basis/compose.yml +++ b/solutions/basis/compose.yml @@ -88,7 +88,7 @@ services: target: /home/bezmen/${CONFIG_FILE_NAME} command: > --bezmen.config.mapping.mode=${CONFIG_MAPPING_MODE} - --spring.config.location=classpath:application.yaml,optional:file:${CONFIG_FILE_NAME} + --spring.config.location=optional:file:${CONFIG_FILE_NAME} depends_on: schema-owner: condition: service_completed_successfully