diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/at/gv/egiz/moazs/ConfigProfileGeneratorTest.java | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/src/test/java/at/gv/egiz/moazs/ConfigProfileGeneratorTest.java b/src/test/java/at/gv/egiz/moazs/ConfigProfileGeneratorTest.java new file mode 100644 index 0000000..c0ff96c --- /dev/null +++ b/src/test/java/at/gv/egiz/moazs/ConfigProfileGeneratorTest.java @@ -0,0 +1,135 @@ +package at.gv.egiz.moazs; + +import at.gv.egiz.moazs.preprocess.*; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.Map; + +import static at.gv.egiz.moazs.preprocess.ConfigProfileGenerator.configProfileGeneratorBuilder; +import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; +import static org.mockito.ArgumentMatchers.any; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class ConfigProfileGeneratorTest { + + private static final String PREFIX = "prefix"; + private static final String DEFAULT = "default"; + + @Mock + private ConfigProfileMerger merger; + + @Mock + private ConfigProfileValidator validator; + + @Mock + private SpringPropertiesFacade properties; + + @Mock + private MapToConfigConverter converter; + + @Test + public void assembleDefaultProfile() { + var propMap = Map.of( + PREFIX + "." + DEFAULT + ".property-a", "value-a", + PREFIX + "." + DEFAULT + ".property-b", "value-b", + PREFIX + "." + DEFAULT + ".property-c", "value-c"); + var generator = setupMocksAndBuilder(propMap).build(); + + var profiles = generator.generate(); + + verify(converter).convert(Map.of( + "property-a", "value-a", + "property-b", "value-b", + "property-c", "value-c")); + assertThat(profiles.keySet()).containsExactlyInAnyOrder(DEFAULT); + } + + @Test + public void assembleEmptyDefault() { + var propMap = Map.of(PREFIX + "." + DEFAULT, ""); + var generator = setupMocksAndBuilder(propMap).build(); + + var profiles = generator.generate(); + + verifyZeroInteractions(converter); + assertThat(profiles.keySet()).isEmpty(); + } + + @Test + public void assembleNoProfiles() { + var propMap = Map.of("unrelatedpropertyname", "unrelatedvalue"); + var generator = setupMocksAndBuilder(propMap).build(); + + var profiles = generator.generate(); + + verifyZeroInteractions(converter); + assertThat(profiles.keySet()).isEmpty(); + } + + @Test + public void assembleMultipleProfiles() { + var propMap = Map.of( + PREFIX + "." + DEFAULT+ ".property-a", "value-a", + PREFIX + "." + DEFAULT+ ".property-b", "value-b", + PREFIX + "." + "profile-1.property-c", "value-c", + PREFIX + "." + "profile-2.property-d", "value-d"); + + var generator = setupMocksAndBuilder(propMap).build(); + + var profiles = generator.generate(); + + verify(converter).convert(Map.of("property-a", "value-a", "property-b", "value-b")); + verify(converter).convert(Map.of("property-c", "value-c")); + verify(converter).convert(Map.of("property-d", "value-d")); + assertThat(profiles.keySet()).containsExactlyInAnyOrder(DEFAULT, "profile-1", "profile-2"); + } + + @Test(expected = MoaZSException.class) + public void cancelAtIncompleteDefaultProfile() { + var propMap = Map.of(PREFIX + "." + DEFAULT + ".property-a", "value-a"); + var generator = setupMocksAndBuilder(propMap).build(); + when(validator.isComplete(any())).thenReturn(false); + + generator.generate(); + } + + @Test + public void continueAtIncompleteDefaultWhenVerificationDisabled() { + var propMap = Map.of(PREFIX + "." + DEFAULT + ".property-a", "value-a"); + + when(validator.isComplete(any())).thenReturn(false); + + var generator = setupMocksAndBuilder(propMap) + .withVerifyCompletenessOfDefaultConfiguration(false) + .build(); + + var profiles = generator.generate(); + + verify(converter).convert(Map.of("property-a", "value-a")); + assertThat(profiles.keySet()).containsExactlyInAnyOrder(DEFAULT); + } + + private ConfigProfileGenerator.ConfigProfileGeneratorBuilder setupMocksAndBuilder(Map<String, String> propMap) { + when(properties.getPropertyNames()).thenReturn(propMap.keySet().stream()); + when(properties.getProperty(any())).thenAnswer(i -> propMap.get(i.getArgument(0))); + when(merger.merge(any(), any())).thenAnswer(i -> i.getArgument(0)); + when(validator.isComplete(any())).thenReturn(true); + when(converter.convert(any())).thenReturn(configTypeBuilder().build()); + + return configProfileGeneratorBuilder() + .withProperties(properties) + .withConverter(converter) + .withValidator(validator) + .withMerger(merger) + .withDefaultConfigKey(DEFAULT) + .withProfilePrefix(PREFIX) + .withVerifyCompletenessOfDefaultConfiguration(true); + } + +} |