diff options
author | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-05-16 10:01:43 +0200 |
---|---|---|
committer | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-05-16 10:01:43 +0200 |
commit | f193a2165d9261c2bd3413339c954a8bba829a93 (patch) | |
tree | 79595af29ba8c74f1abf79b213367e9d9b515078 /src/test | |
parent | 5e6304ee003285793c0992a81e424969a2a6af88 (diff) | |
download | moa-zs-f193a2165d9261c2bd3413339c954a8bba829a93.tar.gz moa-zs-f193a2165d9261c2bd3413339c954a8bba829a93.tar.bz2 moa-zs-f193a2165d9261c2bd3413339c954a8bba829a93.zip |
Refactor and Test ConfigProfileGenerator
Refactor
- Add Builder to ConfigProfileGenerator. Reason: Constructor had too
many arguments.
- Move Conversion from Map to Config from ConfigProfileGenerator into
dedicated 'ConvertMapToConfig' Class; Reason: I expect additional
configuration properties and I don't want those changes to affect
the ConfigProfileGenerator (or it's test cases)
- Move Access to Spring's env into facade to simplify ConfigProfileGenerator.
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); + } + +} |