Skip to content

Commit

Permalink
Use var keyword and add validation props
Browse files Browse the repository at this point in the history
  • Loading branch information
pavetok committed Oct 14, 2023
1 parent 6a12f46 commit 67e58b7
Show file tree
Hide file tree
Showing 30 changed files with 153 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Sepulka getById(@NonNull UUID id) {

@Override
public Sepulka save(@NonNull Sepulka sepulka) {
SepulkaEdge sepulkaEdge = stateMapper.toEdge(sepulka);
var sepulkaEdge = stateMapper.toEdge(sepulka);
sqlMapper.insert(sepulkaEdge);
return sepulka;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public Sepulka getById(@NonNull UUID id) {

@Override
public Sepulka save(@NonNull Sepulka sepulka) {
SepulkaEdge newSepulkaEdge = mapper.toEdge(sepulka);
SepulkaEdge savedSepulkaEdge = repository.save(newSepulkaEdge);
var newSepulkaEdge = mapper.toEdge(sepulka);
var savedSepulkaEdge = repository.save(newSepulkaEdge);
return mapper.toDomain(savedSepulkaEdge);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public class SepulkaDaoBeans {

@Bean
public DataSource dataSource(StorageProps storageProps) {
List<String> common = List.of("DB_CLOSE_DELAY=-1");
List<String> specific =
var common = List.of("DB_CLOSE_DELAY=-1");
var specific =
switch (storageProps.protocolProps().protocolMode()) {
case H2 -> List.of("MODE=STRICT");
case POSTGRES -> List.of("MODE=PostgreSQL", "DATABASE_TO_LOWER=TRUE", "DEFAULT_NULL_ORDERING=HIGH");
};
String nameWithSettings = Stream.of(List.of(DB), common, specific)
var nameWithSettings = Stream.of(List.of(DB), common, specific)
.flatMap(Collection::stream)
.collect(joining(";"));
return new EmbeddedDatabaseBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import smecalculus.bezmen.construction.SepulkaDaoBeans;
import smecalculus.bezmen.core.Sepulka;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SepulkaDaoBeans.class)
Expand All @@ -23,11 +22,11 @@ abstract class SepulkaDaoIT {
@Test
void shouldSaveOneSepulka() {
// given
Sepulka expectedSepulka = sepulka();
var expectedSepulka = sepulka();
// when
Sepulka actualSepulka1 = sepulkaDao.save(expectedSepulka);
var actualSepulka1 = sepulkaDao.save(expectedSepulka);
// and
Sepulka actualSepulka2 = sepulkaDao.getById(expectedSepulka.id());
var actualSepulka2 = sepulkaDao.getById(expectedSepulka.id());
// then
assertThat(actualSepulka1).isEqualTo(expectedSepulka);
// and
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package smecalculus.bezmen.configuration;

public enum ValidationMode {
HIBERNATE_VALIDATOR
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package smecalculus.bezmen.configuration;

import lombok.Builder;
import lombok.NonNull;

@Builder
public record ValidationProps(@NonNull ValidationMode validationMode) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package smecalculus.bezmen.configuration;

import lombok.Data;

@Data
public class ValidationPropsEdge {
private String mode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package smecalculus.bezmen.construction;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Conditional;
import smecalculus.bezmen.configuration.ValidationMode;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Conditional(ValidationModeCondition.class)
public @interface ConditionalOnValidationMode {
ValidationMode value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN;

import java.util.Map;
import lombok.NonNull;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;
Expand All @@ -12,16 +12,15 @@ class ConfigMappingModeCondition implements ConfigurationCondition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes =
metadata.getAnnotationAttributes(ConditionalOnConfigMappingMode.class.getName());
ConfigMappingMode expectedMode = (ConfigMappingMode) attributes.get("value");
var attributes = metadata.getAnnotationAttributes(ConditionalOnConfigMappingMode.class.getName());
var expectedMode = (ConfigMappingMode) attributes.get("value");
String actualMode = context.getEnvironment()
.getProperty("bezmen.config.mapping.mode", ConfigMappingMode.LIGHTBEND_CONFIG.name());
return expectedMode.name().equalsIgnoreCase(actualMode);
}

@Override
public ConfigurationPhase getConfigurationPhase() {
public @NonNull ConfigurationPhase getConfigurationPhase() {
return REGISTER_BEAN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN;

import java.util.Map;
import lombok.NonNull;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;
Expand All @@ -14,14 +15,14 @@ class ConfigProtocolModeCondition implements ConfigurationCondition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes =
metadata.getAnnotationAttributes(ConditionalOnConfigProtocolMode.class.getName());
ConfigProtocolMode expectedMode = (ConfigProtocolMode) attributes.get("value");
var expectedMode = (ConfigProtocolMode) attributes.get("value");
String actualMode = context.getEnvironment()
.getProperty("bezmen.config.protocol.mode", ConfigProtocolMode.FILE_SYSTEM.name());
return expectedMode.name().equalsIgnoreCase(actualMode);
}

@Override
public ConfigurationPhase getConfigurationPhase() {
public @NonNull ConfigurationPhase getConfigurationPhase() {
return REGISTER_BEAN;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
package smecalculus.bezmen.construction;

import static java.util.Objects.requireNonNull;
import static smecalculus.bezmen.configuration.ValidationMode.HIBERNATE_VALIDATOR;

import jakarta.validation.Validation;
import jakarta.validation.ValidatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import smecalculus.bezmen.configuration.PropsKeeper;
import smecalculus.bezmen.configuration.ValidationProps;
import smecalculus.bezmen.configuration.ValidationPropsEdge;
import smecalculus.bezmen.validation.EdgeValidator;
import smecalculus.bezmen.validation.EdgeValidatorHibernateValidator;
import smecalculus.bezmen.validation.ValidationPropsMapper;

@Configuration(proxyBeanMethods = false)
public class ValidationBeans {

private static final Logger LOG = LoggerFactory.getLogger(ValidationBeans.class);

@Bean
ValidationProps validationProps(PropsKeeper keeper, ValidationPropsMapper mapper) {
var propsEdge = keeper.read("bezmen.validation", ValidationPropsEdge.class);
requireNonNull(propsEdge.getMode(), "validation mode must not be null");
LOG.info("Read {}", propsEdge);
return mapper.toDomain(propsEdge);
}

@Bean
@ConditionalOnValidationMode(HIBERNATE_VALIDATOR)
EdgeValidator edgeValidatorHibernateValidator() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
return new EdgeValidatorHibernateValidator(factory.getValidator());
try (var factory = Validation.buildDefaultValidatorFactory()) {
return new EdgeValidatorHibernateValidator(factory.getValidator());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package smecalculus.bezmen.construction;

import static org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN;

import lombok.NonNull;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;
import smecalculus.bezmen.configuration.ValidationMode;
import smecalculus.bezmen.configuration.ValidationProps;

class ValidationModeCondition implements ConfigurationCondition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
var attributes = metadata.getAnnotationAttributes(ConditionalOnValidationMode.class.getName());
var mode = (ValidationMode) attributes.get("value");
var props = context.getBeanFactory().getBean(ValidationProps.class);
return mode == props.validationMode();
}

@Override
public @NonNull ConfigurationPhase getConfigurationPhase() {
return REGISTER_BEAN;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package smecalculus.bezmen.validation;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validator;
import java.util.Set;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

Expand All @@ -15,7 +13,7 @@ public class EdgeValidatorHibernateValidator implements EdgeValidator {

@Override
public <T> void validate(T object, Class<?>... groups) {
Set<ConstraintViolation<T>> violations = validator.validate(object, groups);
var violations = validator.validate(object, groups);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package smecalculus.bezmen.validation;

import org.mapstruct.Mapping;
import smecalculus.bezmen.configuration.ValidationMode;
import smecalculus.bezmen.configuration.ValidationProps;
import smecalculus.bezmen.configuration.ValidationPropsEdge;

public interface ValidationPropsMapper {
@Mapping(source = "mode", target = "validationMode")
ValidationProps toDomain(ValidationPropsEdge propsEdge);

default ValidationMode toValidationMode(String value) {
return ValidationMode.valueOf(value.toUpperCase());
}
}
5 changes: 5 additions & 0 deletions libs/essentials/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bezmen {
validation {
mode = hibernate_validator
}
}
1 change: 1 addition & 0 deletions libs/essentials/src/main/resources/validation.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bezmen.validation.mode=hibernate_validator
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import static org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN;

import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import lombok.NonNull;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;
Expand All @@ -15,16 +14,15 @@ class MessageMappingModeCondition implements ConfigurationCondition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes =
metadata.getAnnotationAttributes(ConditionalOnMessageMappingModes.class.getName());
MessageMappingMode[] expectedModes = (MessageMappingMode[]) attributes.get("value");
MessagingProps messagingProps = context.getBeanFactory().getBean(MessagingProps.class);
Set<MessageMappingMode> actualModes = messagingProps.mappingProps().mappingModes();
var attributes = metadata.getAnnotationAttributes(ConditionalOnMessageMappingModes.class.getName());
var expectedModes = (MessageMappingMode[]) attributes.get("value");
var props = context.getBeanFactory().getBean(MessagingProps.class);
var actualModes = props.mappingProps().mappingModes();
return Stream.of(expectedModes).anyMatch(actualModes::contains);
}

@Override
public ConfigurationPhase getConfigurationPhase() {
public @NonNull ConfigurationPhase getConfigurationPhase() {
return REGISTER_BEAN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ MessagingPropsMapper messagingPropsMapper() {

@Bean
MessagingProps messagingProps(PropsKeeper keeper, EdgeValidator validator, MessagingPropsMapper mapper) {
MessagingPropsEdge propsEdge = keeper.read("bezmen.messaging", MessagingPropsEdge.class);
var propsEdge = keeper.read("bezmen.messaging", MessagingPropsEdge.class);
validator.validate(propsEdge);
LOG.info("Read {}", propsEdge);
return mapper.toDomain(propsEdge);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import static org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN;

import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import lombok.NonNull;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;
Expand All @@ -15,16 +14,15 @@ class MessagingProtocolModeCondition implements ConfigurationCondition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes =
metadata.getAnnotationAttributes(ConditionalOnMessagingProtocolModes.class.getName());
MessagingProtocolMode[] expectedModes = (MessagingProtocolMode[]) attributes.get("value");
MessagingProps messagingProps = context.getBeanFactory().getBean(MessagingProps.class);
Set<MessagingProtocolMode> actualModes = messagingProps.protocolProps().protocolModes();
var attributes = metadata.getAnnotationAttributes(ConditionalOnMessagingProtocolModes.class.getName());
var expectedModes = (MessagingProtocolMode[]) attributes.get("value");
var props = context.getBeanFactory().getBean(MessagingProps.class);
var actualModes = props.protocolProps().protocolModes();
return Stream.of(expectedModes).anyMatch(actualModes::contains);
}

@Override
public ConfigurationPhase getConfigurationPhase() {
public @NonNull ConfigurationPhase getConfigurationPhase() {
return REGISTER_BEAN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class MessagingConfigBeansIT {
@Test
void defaultConfigShouldBeBackwardCompatible(@Autowired MessagingProps actualProps) {
// given
MessagingProps expectedProps = messagingProps().build();
var expectedProps = messagingProps().build();
// when
// default construction
// then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package smecalculus.bezmen.configuration;

import lombok.Builder;
import lombok.NonNull;

@Builder
public record StateMappingProps(StateMappingMode mappingMode) {}
public record StateMappingProps(@NonNull StateMappingMode mappingMode) {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class StateMappingPropsEg {
public static class Pojos {
public static StateMappingPropsEdge stateMappingPropsEdge() {
StateMappingPropsEdge propsEdge = new StateMappingPropsEdge();
var propsEdge = new StateMappingPropsEdge();
propsEdge.setMode(SPRING_DATA.name());
return propsEdge;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class StoragePropsEg {
public static class Pojos {
public static StoragePropsEdge storagePropsEdge() {
StoragePropsEdge propsEdge = new StoragePropsEdge();
var propsEdge = new StoragePropsEdge();
propsEdge.setProtocol(storageProtocolPropsEdge());
propsEdge.setMapping(stateMappingPropsEdge());
return propsEdge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class StorageProtocolPropsEg {
public static class Pojos {
public static StorageProtocolPropsEdge storageProtocolPropsEdge() {
StorageProtocolPropsEdge propsEdge = new StorageProtocolPropsEdge();
var propsEdge = new StorageProtocolPropsEdge();
propsEdge.setMode(StorageProtocolMode.H2.name());
return propsEdge;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MappingMyBatisBeans {

@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
var factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean.getObject();
}
Expand Down
Loading

0 comments on commit 67e58b7

Please sign in to comment.