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 @@
*/
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) {
......
......@@ -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
......
......@@ -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,126 +75,168 @@ 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();
}
@ParameterizedTest
@EnumSource
void shouldRunWithDefaultTransformationForScopesWithoutConfiguredOnes(AggregationMapping.Scope scope) {
doReturn(Stream.of(scope)).when(runner).getScopesWithoutConfiguredTransformations();
runner.run();
verify(runner).runWithDefaultTransformation(scope);
}
@Test
void shouldGetIdentifier() {
void shouldRunWithConfiguredTransformations() {
runner.run();
verify(transformationProperties).getIdentifier();
verify(runner).runWithConfiguredTransformations();
}
}
@Nested
class TestOnAggregationMappingConfigured {
private final List<AggregationMapping> aggregationMappings = List.of(AggregationMappingTestFactory.create(),
AggregationMappingTestFactory.create());
class TestGetScopesWithoutConfiguredTransformations {
@Mock
private Transformation firstTransformation;
@Mock
private Transformation secondTransformation;
@ParameterizedTest
@EnumSource
void shouldCheckForConfiguredTransformationsWithScope(AggregationMapping.Scope scope) {
runner.getScopesWithoutConfiguredTransformations().toList();
@BeforeEach
void mock() {
when(transformationProperties.getAggregationMappings()).thenReturn(aggregationMappings);
when(transformationService.load(any(), any())).thenReturn(firstTransformation, secondTransformation);
verify(runner).hasNoConfiguredTransformationsWithScope(scope);
}
@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
void shouldLoadTransformationForEachAggregationMapping() {
runner.run();
void shouldGetAggregationMappings() {
runner.hasNoConfiguredTransformationsWithScope(AggregationMapping.Scope.INTERN);
aggregationMappings.forEach(mapping -> verify(transformationService).load(identifier, mapping));
verify(transformationProperties).getAggregationMappings();
}
@Test
void shouldRunWithTransformationForEachTransformation() {
runner.run();
void shouldReturnFalseIfThereAreConfiguredTransformations() {
when(transformationProperties.getAggregationMappings()).thenReturn(List.of(AggregationMappingTestFactory.create()));
verify(runner).runWithTransformation(firstTransformation, aggregationMappings.get(0));
verify(runner).runWithTransformation(secondTransformation, aggregationMappings.get(1));
var hasNoTransformations = runner.hasNoConfiguredTransformationsWithScope(AggregationMappingTestFactory.SCOPE);
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
class TestOnAggregationMappingsNull {
class TestRunWithDefaultTransformation {
private final String identifier = LoremIpsum.getInstance().getWords(1);
@Mock
private Transformation transformation;
@BeforeEach
void mock() {
when(transformationProperties.getAggregationMappings()).thenReturn(null);
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);
verify(transformationProperties).getIdentifier();
}
@Test
void shouldLoadTransformation() {
runner.run();
runner.runWithDefaultTransformation(AggregationMapping.Scope.INTERN);
verify(transformationService).load(identifier, null);
}
@Test
void shouldCallRunWithDefaultTransformation() {
runner.run();
void shouldRunWithTransformation() {
runner.runWithDefaultTransformation(AggregationMapping.Scope.INTERN);
verify(runner).runWithDefaultTransformation(transformation);
verify(runner).runWithTransformation(eq(transformation), isNull());
}
}
@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
private Transformation transformation;
private Transformation firstTransformation;
@Mock
private Transformation secondTransformation;
@BeforeEach
void mock() {
when(transformationProperties.getAggregationMappings()).thenReturn(Collections.emptyList());
when(transformationService.load(any(), any())).thenReturn(transformation);
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 shouldLoadTransformation() {
runner.run();
void shouldGetIdentifier() {
runner.runWithConfiguredTransformations();
verify(transformationService).load(identifier, null);
verify(transformationProperties, times(2)).getIdentifier();
}
@Test
void shouldCallRunWithDefaultTransformation() {
runner.run();
void shouldLoadTransformationForEachAggregationMapping() {
runner.runWithConfiguredTransformations();
verify(runner).runWithDefaultTransformation(transformation);
}
}
aggregationMappings.forEach(mapping -> verify(transformationService).load(identifier, mapping));
}
@Nested
class TestRunWithDefaultTransformation {
@Mock
private Transformation transformation;
@Test
void shouldCallRunWithTransformation() {
doNothing().when(runner).runWithTransformation(any(), any());
runner.runWithDefaultTransformation(transformation);
void shouldRunWithTransformationForEachTransformation() {
runner.runWithConfiguredTransformations();
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 {
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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment