Skip to content

Commit

Permalink
Decompose properties files
Browse files Browse the repository at this point in the history
  • Loading branch information
pavetok committed Oct 5, 2023
1 parent 28956cd commit a511af6
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down
Empty file.
6 changes: 0 additions & 6 deletions apps/foo/src/main/resources/application.yaml

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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;
import smecalculus.bezmen.configuration.MessagingProps;
import smecalculus.bezmen.configuration.MessagingPropsCfg;
import smecalculus.bezmen.validation.EdgeValidator;

@PropertySource("classpath:messaging.properties")
@Configuration(proxyBeanMethods = false)
public class MessagingConfigBeans {

Expand Down
2 changes: 2 additions & 0 deletions libs/messaging/src/main/resources/messaging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bezmen.messaging.protocol.modes[0]=http
bezmen.messaging.mapping.modes[0]=spring_mvc
7 changes: 0 additions & 7 deletions libs/messaging/src/main/resources/messaging.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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;
import smecalculus.bezmen.configuration.StorageProps;
import smecalculus.bezmen.configuration.StoragePropsCfg;
import smecalculus.bezmen.validation.EdgeValidator;

@PropertySource("classpath:storage.properties")
@Configuration(proxyBeanMethods = false)
public class StorageConfigBeans {

Expand Down
8 changes: 8 additions & 0 deletions libs/storage/src/main/resources/storage.properties
Original file line number Diff line number Diff line change
@@ -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
15 changes: 0 additions & 15 deletions libs/storage/src/main/resources/storage.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package smecalculus.bezmen.construction;

Check failure on line 1 in libs/storage/src/test/java/smecalculus/bezmen/construction/StorageConfigBeansSpringConfigIT.java

View workflow job for this annotation

GitHub Actions / Integration Test Report

StorageConfigBeansSpringConfigIT.defaultConfigShouldBeBackwardCompatible(StorageProps)

Failed to load ApplicationContext for [MergedContextConfiguration@59262a90 testClass = smecalculus.bezmen.construction.StorageConfigBeansSpringConfigIT, locations = [], classes = [smecalculus.bezmen.construction.StorageConfigBeans, smecalculus.bezmen.construction.ConfigBeans, smecalculus.bezmen.construction.ValidationBeans], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["bezmen.config.mapping.mode=spring_config"], contextCustomizers = [], contextLoader = org.springframework.test.context.support.DelegatingSmartContextLoader, parent = null]
Raw output
java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@59262a90 testClass = smecalculus.bezmen.construction.StorageConfigBeansSpringConfigIT, locations = [], classes = [smecalculus.bezmen.construction.StorageConfigBeans, smecalculus.bezmen.construction.ConfigBeans, smecalculus.bezmen.construction.ValidationBeans], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["bezmen.config.mapping.mode=spring_config"], contextCustomizers = [], contextLoader = org.springframework.test.context.support.DelegatingSmartContextLoader, parent = null]
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:143)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:127)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:141)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:97)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:241)
	at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:310)
	at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735)
	at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734)
	at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
	at java.base/java.util.Optional.orElseGet(Optional.java:364)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storageProps' defined in smecalculus.bezmen.construction.StorageConfigBeans: Failed to instantiate [smecalculus.bezmen.configuration.StorageProps]: Factory method 'storageProps' threw exception with message: org/springframework/boot/context/properties/bind/Binder
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:654)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:642)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:942)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:221)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:110)
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:212)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:187)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:119)
	... 17 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [smecalculus.bezmen.configuration.StorageProps]: Factory method 'storageProps' threw exception with message: org/springframework/boot/context/properties/bind/Binder
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650)
	... 34 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/bind/Binder
	at smecalculus.bezmen.configuration.ConfigKeeperSpringConfig.read(ConfigKeeperSpringConfig.java:10)
	at smecalculus.bezmen.construction.StorageConfigBeans.storageProps(StorageConfigBeans.java:28)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139)
	... 35 more
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.bind.Binder
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 39 more

import org.springframework.test.context.TestPropertySource;

@TestPropertySource(properties = {"bezmen.config.mapping.mode=spring_config"})
class StorageConfigBeansSpringConfigIT extends StorageConfigBeansIT {}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion solutions/basis/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a511af6

Please sign in to comment.