package at.gv.egiz.moazs; import at.gv.egiz.moazs.preprocess.*; 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 ConfigUtil util; @Mock private SpringPropertiesFacade properties; @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(util).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(); verify(util, never()).convert(any()); assertThat(profiles.keySet()).isEmpty(); } @Test public void assembleNoProfiles() { var propMap = Map.of("unrelatedpropertyname", "unrelatedvalue"); var generator = setupMocksAndBuilder(propMap).build(); var profiles = generator.generate(); verify(util, never()).convert(any()); 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(util).convert(Map.of("property-a", "value-a", "property-b", "value-b")); verify(util).convert(Map.of("property-c", "value-c")); verify(util).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(util.isComplete(any())).thenReturn(false); generator.generate(); } @Test public void continueAtIncompleteDefaultWhenVerificationDisabled() { var propMap = Map.of(PREFIX + "." + DEFAULT + ".property-a", "value-a"); when(util.isComplete(any())).thenReturn(false); var generator = setupMocksAndBuilder(propMap) .withVerifyCompletenessOfDefaultConfiguration(false) .build(); var profiles = generator.generate(); verify(util).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(util.merge(any(), any())).thenAnswer(i -> i.getArgument(0)); when(util.isComplete(any())).thenReturn(true); when(util.convert(any())).thenReturn(configTypeBuilder().build()); return configProfileGeneratorBuilder() .withProperties(properties) .withConfigUtil(util) .withDefaultConfigKey(DEFAULT) .withProfilePrefix(PREFIX) .withVerifyCompletenessOfDefaultConfiguration(true); } }