From f193a2165d9261c2bd3413339c954a8bba829a93 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Thu, 16 May 2019 10:01:43 +0200 Subject: 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. --- .../gv/egiz/moazs/ConfigProfileGeneratorTest.java | 135 +++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/test/java/at/gv/egiz/moazs/ConfigProfileGeneratorTest.java (limited to 'src/test') 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 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); + } + +} -- cgit v1.2.3