Skip to content
Snippets Groups Projects
Commit 1863b99a authored by Krzysztof Witukiewicz's avatar Krzysztof Witukiewicz
Browse files

OZG-7811 OZG-8099 runWithDefaultTransformation pro scope

parent 39eece3a
Branches
Tags
1 merge request!23Ozg 7811 vorgaenge zur landesebene schicken
...@@ -23,8 +23,9 @@ ...@@ -23,8 +23,9 @@
*/ */
package de.ozgcloud.aggregation; package de.ozgcloud.aggregation;
import java.util.Objects; import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Stream;
import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.ThreadContext;
import org.springframework.beans.factory.annotation.Lookup; import org.springframework.beans.factory.annotation.Lookup;
...@@ -54,19 +55,25 @@ public abstract class AggregationManagerRunner implements CommandLineRunner { ...@@ -54,19 +55,25 @@ public abstract class AggregationManagerRunner implements CommandLineRunner {
@Override @Override
public void run(String... args) throws TransformationException { public void run(String... args) throws TransformationException {
var identifier = transformationProperties.getIdentifier(); getScopesWithoutConfiguredTransformations().forEach(this::runWithDefaultTransformation);
var aggregationMappings = transformationProperties.getAggregationMappings(); runWithConfiguredTransformations();
if (Objects.isNull(aggregationMappings) || aggregationMappings.isEmpty()) {
runWithDefaultTransformation(transformationService.load(identifier, null));
} else {
aggregationMappings.stream()
.forEach(aggregationMapping -> runWithTransformation(transformationService.load(identifier, aggregationMapping),
aggregationMapping));
} }
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) { void runWithConfiguredTransformations() {
runWithTransformation(transformation, null); transformationProperties.getAggregationMappings().forEach(aggregationMapping ->
runWithTransformation(transformationService.load(transformationProperties.getIdentifier(), aggregationMapping), aggregationMapping));
} }
void runWithTransformation(Transformation transformation, AggregationMapping aggregationMapping) { void runWithTransformation(Transformation transformation, AggregationMapping aggregationMapping) {
......
...@@ -23,15 +23,15 @@ ...@@ -23,15 +23,15 @@
*/ */
package de.ozgcloud.aggregation; package de.ozgcloud.aggregation;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import jakarta.validation.Valid;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import de.ozgcloud.aggregation.transformation.AggregationMapping; import de.ozgcloud.aggregation.transformation.AggregationMapping;
import jakarta.validation.Valid;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
...@@ -49,7 +49,7 @@ public class TransformationProperties { ...@@ -49,7 +49,7 @@ public class TransformationProperties {
* field mappings should be applied * field mappings should be applied
*/ */
@Valid @Valid
private List<AggregationMapping> aggregationMappings; private List<AggregationMapping> aggregationMappings = new ArrayList<>();
/* /*
* mapping definition for the entry id * mapping definition for the entry id
......
...@@ -27,13 +27,16 @@ import static org.assertj.core.api.Assertions.*; ...@@ -27,13 +27,16 @@ import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Stream;
import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; 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.ArgumentMatcher;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
...@@ -72,126 +75,168 @@ class AggregationManagerRunnerTest { ...@@ -72,126 +75,168 @@ class AggregationManagerRunnerTest {
@Nested @Nested
class TestRun { class TestRun {
private final String identifier = LoremIpsum.getInstance().getWords(1);
@BeforeEach @BeforeEach
void mock() { void init() {
when(transformationProperties.getIdentifier()).thenReturn(identifier); doNothing().when(runner).runWithDefaultTransformation(any());
doNothing().when(runner).runWithTransformation(any(), any()); doNothing().when(runner).runWithConfiguredTransformations();
} }
@Test @Test
void shouldGetAggregationMappings() { void shouldGetScopesWithoutConfiguredTransformations() {
runner.run(); runner.run();
verify(transformationProperties).getAggregationMappings(); verify(runner).getScopesWithoutConfiguredTransformations();
}
@ParameterizedTest
@EnumSource
void shouldRunWithDefaultTransformationForScopesWithoutConfiguredOnes(AggregationMapping.Scope scope) {
doReturn(Stream.of(scope)).when(runner).getScopesWithoutConfiguredTransformations();
runner.run();
verify(runner).runWithDefaultTransformation(scope);
} }
@Test @Test
void shouldGetIdentifier() { void shouldRunWithConfiguredTransformations() {
runner.run(); runner.run();
verify(transformationProperties).getIdentifier(); verify(runner).runWithConfiguredTransformations();
}
} }
@Nested @Nested
class TestOnAggregationMappingConfigured { class TestGetScopesWithoutConfiguredTransformations {
private final List<AggregationMapping> aggregationMappings = List.of(AggregationMappingTestFactory.create(),
AggregationMappingTestFactory.create());
@Mock @ParameterizedTest
private Transformation firstTransformation; @EnumSource
@Mock void shouldCheckForConfiguredTransformationsWithScope(AggregationMapping.Scope scope) {
private Transformation secondTransformation; runner.getScopesWithoutConfiguredTransformations().toList();
@BeforeEach verify(runner).hasNoConfiguredTransformationsWithScope(scope);
void mock() {
when(transformationProperties.getAggregationMappings()).thenReturn(aggregationMappings);
when(transformationService.load(any(), any())).thenReturn(firstTransformation, secondTransformation);
} }
@ParameterizedTest
@EnumSource
void shouldReturnScopes(AggregationMapping.Scope scope) {
doReturn(true).when(runner).hasNoConfiguredTransformationsWithScope(scope);
doReturn(false).when(runner).hasNoConfiguredTransformationsWithScope(AdditionalMatchers.not(eq(scope)));
var scopes = runner.getScopesWithoutConfiguredTransformations();
assertThat(scopes).containsExactly(scope);
}
}
@Nested
class TestHasNoConfiguredTransformationsWithScope {
@Test @Test
void shouldLoadTransformationForEachAggregationMapping() { void shouldGetAggregationMappings() {
runner.run(); runner.hasNoConfiguredTransformationsWithScope(AggregationMapping.Scope.INTERN);
aggregationMappings.forEach(mapping -> verify(transformationService).load(identifier, mapping)); verify(transformationProperties).getAggregationMappings();
} }
@Test @Test
void shouldRunWithTransformationForEachTransformation() { void shouldReturnFalseIfThereAreConfiguredTransformations() {
runner.run(); when(transformationProperties.getAggregationMappings()).thenReturn(List.of(AggregationMappingTestFactory.create()));
verify(runner).runWithTransformation(firstTransformation, aggregationMappings.get(0)); var hasNoTransformations = runner.hasNoConfiguredTransformationsWithScope(AggregationMappingTestFactory.SCOPE);
verify(runner).runWithTransformation(secondTransformation, aggregationMappings.get(1));
assertThat(hasNoTransformations).isFalse();
}
@Test
void shouldReturnTrueIfThereAreNoConfiguredTransformations() {
when(transformationProperties.getAggregationMappings()).thenReturn(List.of(AggregationMappingTestFactory.createBuilder().scope(
AggregationMapping.Scope.INTERN).build()));
var hasNoTransformations = runner.hasNoConfiguredTransformationsWithScope(AggregationMapping.Scope.EXTERN);
assertThat(hasNoTransformations).isTrue();
} }
} }
@Nested @Nested
class TestOnAggregationMappingsNull { class TestRunWithDefaultTransformation {
private final String identifier = LoremIpsum.getInstance().getWords(1);
@Mock @Mock
private Transformation transformation; private Transformation transformation;
@BeforeEach @BeforeEach
void mock() { void init() {
when(transformationProperties.getAggregationMappings()).thenReturn(null); when(transformationProperties.getIdentifier()).thenReturn(identifier);
when(transformationService.load(any(), any())).thenReturn(transformation); when(transformationService.load(any(), any())).thenReturn(transformation);
doNothing().when(runner).runWithTransformation(any(), any());
}
@Test
void shouldGetIdentifier() {
runner.runWithDefaultTransformation(AggregationMapping.Scope.INTERN);
verify(transformationProperties).getIdentifier();
} }
@Test @Test
void shouldLoadTransformation() { void shouldLoadTransformation() {
runner.run(); runner.runWithDefaultTransformation(AggregationMapping.Scope.INTERN);
verify(transformationService).load(identifier, null); verify(transformationService).load(identifier, null);
} }
@Test @Test
void shouldCallRunWithDefaultTransformation() { void shouldRunWithTransformation() {
runner.run(); runner.runWithDefaultTransformation(AggregationMapping.Scope.INTERN);
verify(runner).runWithDefaultTransformation(transformation); verify(runner).runWithTransformation(eq(transformation), isNull());
} }
} }
@Nested @Nested
class TestOnAggregationMappingsEmpty { class TestRunWithConfiguredTransformations {
private final String identifier = LoremIpsum.getInstance().getWords(1);
private final List<AggregationMapping> aggregationMappings = List.of(AggregationMappingTestFactory.create(),
AggregationMappingTestFactory.create());
@Mock @Mock
private Transformation transformation; private Transformation firstTransformation;
@Mock
private Transformation secondTransformation;
@BeforeEach @BeforeEach
void mock() { void mock() {
when(transformationProperties.getAggregationMappings()).thenReturn(Collections.emptyList()); when(transformationProperties.getAggregationMappings()).thenReturn(aggregationMappings);
when(transformationService.load(any(), any())).thenReturn(transformation); when(transformationProperties.getIdentifier()).thenReturn(identifier);
when(transformationService.load(any(), any())).thenReturn(firstTransformation, secondTransformation);
doNothing().when(runner).runWithTransformation(any(), any());
} }
@Test @Test
void shouldLoadTransformation() { void shouldGetIdentifier() {
runner.run(); runner.runWithConfiguredTransformations();
verify(transformationService).load(identifier, null); verify(transformationProperties, times(2)).getIdentifier();
} }
@Test @Test
void shouldCallRunWithDefaultTransformation() { void shouldLoadTransformationForEachAggregationMapping() {
runner.run(); runner.runWithConfiguredTransformations();
verify(runner).runWithDefaultTransformation(transformation); aggregationMappings.forEach(mapping -> verify(transformationService).load(identifier, mapping));
}
}
} }
@Nested
class TestRunWithDefaultTransformation {
@Mock
private Transformation transformation;
@Test @Test
void shouldCallRunWithTransformation() { void shouldRunWithTransformationForEachTransformation() {
doNothing().when(runner).runWithTransformation(any(), any()); runner.runWithConfiguredTransformations();
runner.runWithDefaultTransformation(transformation);
verify(runner).runWithTransformation(eq(transformation), isNull()); verify(runner).runWithTransformation(firstTransformation, aggregationMappings.get(0));
verify(runner).runWithTransformation(secondTransformation, aggregationMappings.get(1));
} }
} }
......
...@@ -34,6 +34,7 @@ public class AggregationMappingTestFactory { ...@@ -34,6 +34,7 @@ public class AggregationMappingTestFactory {
public static final FormIdentifier FORM_IDENTIFIER = FormIdentifierTestFactory.create(); public static final FormIdentifier FORM_IDENTIFIER = FormIdentifierTestFactory.create();
public static final FieldMapping MAPPING = FieldMappingTestFactory.create(); public static final FieldMapping MAPPING = FieldMappingTestFactory.create();
public static final String NAME = LoremIpsum.getInstance().getWords(1); public static final String NAME = LoremIpsum.getInstance().getWords(1);
public static final AggregationMapping.Scope SCOPE = AggregationMapping.Scope.INTERN;
public static AggregationMapping create() { public static AggregationMapping create() {
return createBuilder().build(); return createBuilder().build();
...@@ -43,6 +44,7 @@ public class AggregationMappingTestFactory { ...@@ -43,6 +44,7 @@ public class AggregationMappingTestFactory {
return AggregationMapping.builder() return AggregationMapping.builder()
.formIdentifier(FORM_IDENTIFIER) .formIdentifier(FORM_IDENTIFIER)
.fieldMapping(MAPPING) .fieldMapping(MAPPING)
.name(NAME); .name(NAME)
.scope(SCOPE);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment