diff --git a/aggregation-manager-job/src/main/java/de/ozgcloud/aggregation/AggregationManagerRunner.java b/aggregation-manager-job/src/main/java/de/ozgcloud/aggregation/AggregationManagerRunner.java index 3d5596e8748fbdd4c3cf9ebcc884faa7d40fa79b..f3b47301892eadbfae81fb8b84438284ceba478c 100644 --- a/aggregation-manager-job/src/main/java/de/ozgcloud/aggregation/AggregationManagerRunner.java +++ b/aggregation-manager-job/src/main/java/de/ozgcloud/aggregation/AggregationManagerRunner.java @@ -23,8 +23,9 @@ */ package de.ozgcloud.aggregation; -import java.util.Objects; +import java.util.Arrays; import java.util.UUID; +import java.util.stream.Stream; import org.apache.logging.log4j.ThreadContext; import org.springframework.beans.factory.annotation.Lookup; @@ -54,19 +55,25 @@ public abstract class AggregationManagerRunner implements CommandLineRunner { @Override public void run(String... args) throws TransformationException { - var identifier = transformationProperties.getIdentifier(); - var aggregationMappings = transformationProperties.getAggregationMappings(); - if (Objects.isNull(aggregationMappings) || aggregationMappings.isEmpty()) { - runWithDefaultTransformation(transformationService.load(identifier, null)); - } else { - aggregationMappings.stream() - .forEach(aggregationMapping -> runWithTransformation(transformationService.load(identifier, aggregationMapping), - aggregationMapping)); - } + getScopesWithoutConfiguredTransformations().forEach(this::runWithDefaultTransformation); + runWithConfiguredTransformations(); + } + + Stream<AggregationMapping.Scope> getScopesWithoutConfiguredTransformations() { + return Arrays.stream(AggregationMapping.Scope.values()).filter(this::hasNoConfiguredTransformationsWithScope); + } + + boolean hasNoConfiguredTransformationsWithScope(AggregationMapping.Scope scope) { + return transformationProperties.getAggregationMappings().stream().map(AggregationMapping::getScope).noneMatch(scope::equals); + } + + void runWithDefaultTransformation(AggregationMapping.Scope scope) throws TransformationException { + runWithTransformation(transformationService.load(transformationProperties.getIdentifier(), null), null); } - void runWithDefaultTransformation(Transformation transformation) { - runWithTransformation(transformation, null); + void runWithConfiguredTransformations() { + transformationProperties.getAggregationMappings().forEach(aggregationMapping -> + runWithTransformation(transformationService.load(transformationProperties.getIdentifier(), aggregationMapping), aggregationMapping)); } void runWithTransformation(Transformation transformation, AggregationMapping aggregationMapping) { diff --git a/aggregation-manager-job/src/main/java/de/ozgcloud/aggregation/TransformationProperties.java b/aggregation-manager-job/src/main/java/de/ozgcloud/aggregation/TransformationProperties.java index aadc1cac0a74c6ceec10de99559e7c6862b2a84c..25c70cafbd354f0f5c345c28654a0efa8662de22 100644 --- a/aggregation-manager-job/src/main/java/de/ozgcloud/aggregation/TransformationProperties.java +++ b/aggregation-manager-job/src/main/java/de/ozgcloud/aggregation/TransformationProperties.java @@ -23,15 +23,15 @@ */ package de.ozgcloud.aggregation; +import java.util.ArrayList; import java.util.List; -import jakarta.validation.Valid; - import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.validation.annotation.Validated; import de.ozgcloud.aggregation.transformation.AggregationMapping; +import jakarta.validation.Valid; import lombok.Getter; import lombok.Setter; import lombok.extern.log4j.Log4j2; @@ -49,7 +49,7 @@ public class TransformationProperties { * field mappings should be applied */ @Valid - private List<AggregationMapping> aggregationMappings; + private List<AggregationMapping> aggregationMappings = new ArrayList<>(); /* * mapping definition for the entry id diff --git a/aggregation-manager-job/src/test/java/de/ozgcloud/aggregation/AggregationManagerRunnerTest.java b/aggregation-manager-job/src/test/java/de/ozgcloud/aggregation/AggregationManagerRunnerTest.java index ef2a1f26bb2fdfbcdf47a5dcd7d2f6292f63b661..b4c365f6ef91a952d6ec8e608391df0cfebfd91f 100644 --- a/aggregation-manager-job/src/test/java/de/ozgcloud/aggregation/AggregationManagerRunnerTest.java +++ b/aggregation-manager-job/src/test/java/de/ozgcloud/aggregation/AggregationManagerRunnerTest.java @@ -27,13 +27,16 @@ import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; -import java.util.Collections; import java.util.List; +import java.util.stream.Stream; import org.apache.commons.lang3.RandomUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.AdditionalMatchers; import org.mockito.ArgumentMatcher; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -72,129 +75,171 @@ class AggregationManagerRunnerTest { @Nested class TestRun { - private final String identifier = LoremIpsum.getInstance().getWords(1); @BeforeEach - void mock() { - when(transformationProperties.getIdentifier()).thenReturn(identifier); - doNothing().when(runner).runWithTransformation(any(), any()); + void init() { + doNothing().when(runner).runWithDefaultTransformation(any()); + doNothing().when(runner).runWithConfiguredTransformations(); } @Test - void shouldGetAggregationMappings() { + void shouldGetScopesWithoutConfiguredTransformations() { runner.run(); - verify(transformationProperties).getAggregationMappings(); + verify(runner).getScopesWithoutConfiguredTransformations(); } - @Test - void shouldGetIdentifier() { + @ParameterizedTest + @EnumSource + void shouldRunWithDefaultTransformationForScopesWithoutConfiguredOnes(AggregationMapping.Scope scope) { + doReturn(Stream.of(scope)).when(runner).getScopesWithoutConfiguredTransformations(); + runner.run(); - verify(transformationProperties).getIdentifier(); + verify(runner).runWithDefaultTransformation(scope); } - @Nested - class TestOnAggregationMappingConfigured { - private final List<AggregationMapping> aggregationMappings = List.of(AggregationMappingTestFactory.create(), - AggregationMappingTestFactory.create()); - - @Mock - private Transformation firstTransformation; - @Mock - private Transformation secondTransformation; - - @BeforeEach - void mock() { - when(transformationProperties.getAggregationMappings()).thenReturn(aggregationMappings); - when(transformationService.load(any(), any())).thenReturn(firstTransformation, secondTransformation); - } + @Test + void shouldRunWithConfiguredTransformations() { + runner.run(); - @Test - void shouldLoadTransformationForEachAggregationMapping() { - runner.run(); + verify(runner).runWithConfiguredTransformations(); + } + } - aggregationMappings.forEach(mapping -> verify(transformationService).load(identifier, mapping)); - } + @Nested + class TestGetScopesWithoutConfiguredTransformations { - @Test - void shouldRunWithTransformationForEachTransformation() { - runner.run(); + @ParameterizedTest + @EnumSource + void shouldCheckForConfiguredTransformationsWithScope(AggregationMapping.Scope scope) { + runner.getScopesWithoutConfiguredTransformations().toList(); - verify(runner).runWithTransformation(firstTransformation, aggregationMappings.get(0)); - verify(runner).runWithTransformation(secondTransformation, aggregationMappings.get(1)); - } + verify(runner).hasNoConfiguredTransformationsWithScope(scope); } - @Nested - class TestOnAggregationMappingsNull { - @Mock - private Transformation transformation; + @ParameterizedTest + @EnumSource + void shouldReturnScopes(AggregationMapping.Scope scope) { + doReturn(true).when(runner).hasNoConfiguredTransformationsWithScope(scope); + doReturn(false).when(runner).hasNoConfiguredTransformationsWithScope(AdditionalMatchers.not(eq(scope))); - @BeforeEach - void mock() { - when(transformationProperties.getAggregationMappings()).thenReturn(null); - when(transformationService.load(any(), any())).thenReturn(transformation); - } + var scopes = runner.getScopesWithoutConfiguredTransformations(); - @Test - void shouldLoadTransformation() { - runner.run(); + assertThat(scopes).containsExactly(scope); + } + } - verify(transformationService).load(identifier, null); - } + @Nested + class TestHasNoConfiguredTransformationsWithScope { - @Test - void shouldCallRunWithDefaultTransformation() { - runner.run(); + @Test + void shouldGetAggregationMappings() { + runner.hasNoConfiguredTransformationsWithScope(AggregationMapping.Scope.INTERN); - verify(runner).runWithDefaultTransformation(transformation); - } + verify(transformationProperties).getAggregationMappings(); } - @Nested - class TestOnAggregationMappingsEmpty { - @Mock - private Transformation transformation; + @Test + void shouldReturnFalseIfThereAreConfiguredTransformations() { + when(transformationProperties.getAggregationMappings()).thenReturn(List.of(AggregationMappingTestFactory.create())); - @BeforeEach - void mock() { - when(transformationProperties.getAggregationMappings()).thenReturn(Collections.emptyList()); - when(transformationService.load(any(), any())).thenReturn(transformation); - } + var hasNoTransformations = runner.hasNoConfiguredTransformationsWithScope(AggregationMappingTestFactory.SCOPE); - @Test - void shouldLoadTransformation() { - runner.run(); + assertThat(hasNoTransformations).isFalse(); + } - verify(transformationService).load(identifier, null); - } + @Test + void shouldReturnTrueIfThereAreNoConfiguredTransformations() { + when(transformationProperties.getAggregationMappings()).thenReturn(List.of(AggregationMappingTestFactory.createBuilder().scope( + AggregationMapping.Scope.INTERN).build())); - @Test - void shouldCallRunWithDefaultTransformation() { - runner.run(); + var hasNoTransformations = runner.hasNoConfiguredTransformationsWithScope(AggregationMapping.Scope.EXTERN); - verify(runner).runWithDefaultTransformation(transformation); - } + assertThat(hasNoTransformations).isTrue(); } } @Nested class TestRunWithDefaultTransformation { + private final String identifier = LoremIpsum.getInstance().getWords(1); + @Mock private Transformation transformation; - @Test - void shouldCallRunWithTransformation() { + @BeforeEach + void init() { + when(transformationProperties.getIdentifier()).thenReturn(identifier); + when(transformationService.load(any(), any())).thenReturn(transformation); doNothing().when(runner).runWithTransformation(any(), any()); + } + + @Test + void shouldGetIdentifier() { + runner.runWithDefaultTransformation(AggregationMapping.Scope.INTERN); - runner.runWithDefaultTransformation(transformation); + verify(transformationProperties).getIdentifier(); + } + + @Test + void shouldLoadTransformation() { + runner.runWithDefaultTransformation(AggregationMapping.Scope.INTERN); + + verify(transformationService).load(identifier, null); + } + + @Test + void shouldRunWithTransformation() { + runner.runWithDefaultTransformation(AggregationMapping.Scope.INTERN); verify(runner).runWithTransformation(eq(transformation), isNull()); } } + @Nested + class TestRunWithConfiguredTransformations { + + private final String identifier = LoremIpsum.getInstance().getWords(1); + private final List<AggregationMapping> aggregationMappings = List.of(AggregationMappingTestFactory.create(), + AggregationMappingTestFactory.create()); + + @Mock + private Transformation firstTransformation; + @Mock + private Transformation secondTransformation; + + @BeforeEach + void mock() { + when(transformationProperties.getAggregationMappings()).thenReturn(aggregationMappings); + when(transformationProperties.getIdentifier()).thenReturn(identifier); + when(transformationService.load(any(), any())).thenReturn(firstTransformation, secondTransformation); + doNothing().when(runner).runWithTransformation(any(), any()); + } + + @Test + void shouldGetIdentifier() { + runner.runWithConfiguredTransformations(); + + verify(transformationProperties, times(2)).getIdentifier(); + } + + @Test + void shouldLoadTransformationForEachAggregationMapping() { + runner.runWithConfiguredTransformations(); + + aggregationMappings.forEach(mapping -> verify(transformationService).load(identifier, mapping)); + } + + @Test + void shouldRunWithTransformationForEachTransformation() { + runner.runWithConfiguredTransformations(); + + verify(runner).runWithTransformation(firstTransformation, aggregationMappings.get(0)); + verify(runner).runWithTransformation(secondTransformation, aggregationMappings.get(1)); + } + } + @Nested class TestRunWithTransformation { diff --git a/aggregation-manager-job/src/test/java/de/ozgcloud/aggregation/transformation/AggregationMappingTestFactory.java b/aggregation-manager-job/src/test/java/de/ozgcloud/aggregation/transformation/AggregationMappingTestFactory.java index 44a5c25ce128edf4625a5b95ab27d772bb621c46..8986c7903621e7d0b8fa5282c0a4b36111c6f26d 100644 --- a/aggregation-manager-job/src/test/java/de/ozgcloud/aggregation/transformation/AggregationMappingTestFactory.java +++ b/aggregation-manager-job/src/test/java/de/ozgcloud/aggregation/transformation/AggregationMappingTestFactory.java @@ -34,6 +34,7 @@ public class AggregationMappingTestFactory { public static final FormIdentifier FORM_IDENTIFIER = FormIdentifierTestFactory.create(); public static final FieldMapping MAPPING = FieldMappingTestFactory.create(); public static final String NAME = LoremIpsum.getInstance().getWords(1); + public static final AggregationMapping.Scope SCOPE = AggregationMapping.Scope.INTERN; public static AggregationMapping create() { return createBuilder().build(); @@ -43,6 +44,7 @@ public class AggregationMappingTestFactory { return AggregationMapping.builder() .formIdentifier(FORM_IDENTIFIER) .fieldMapping(MAPPING) - .name(NAME); + .name(NAME) + .scope(SCOPE); } }