/******************************************************************************* * Copyright 2019 Graz University of Technology * MOA ZS has been developed in a cooperation between EGIZ * and Graz University of Technology. * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * https://joinup.ec.europa.eu/news/understanding-eupl-v12 * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. *******************************************************************************/ package at.gv.egiz.moazs; import at.gv.egiz.moazs.preprocess.MzsDeliveryRequestValidator; import at.gv.egiz.moazs.preprocess.*; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import java.util.Map; import static at.gv.egiz.moazs.MoaZSException.moaZSException; import static at.gv.egiz.moazs.preprocess.ConfigProfileGenerator.DEFAULT_CONFIG_KEY; 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.*; /** * @author Christof Rabensteiner * */ @RunWith(MockitoJUnitRunner.class) public class ConfigProfileGeneratorTest { private static final String PREFIX = "prefix"; @Mock private ConfigUtil util; @Mock private MzsDeliveryRequestValidator validator; @Mock private SpringPropertiesFacade properties; @Test public void assembleDefaultProfile() { var propMap = Map.of( PREFIX + "." + DEFAULT_CONFIG_KEY + ".property-a", "value-a", PREFIX + "." + DEFAULT_CONFIG_KEY + ".property-b", "value-b", PREFIX + "." + DEFAULT_CONFIG_KEY + ".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_CONFIG_KEY); } @Test public void assembleEmptyDefault() { var propMap = Map.of(PREFIX + "." + DEFAULT_CONFIG_KEY, ""); 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_CONFIG_KEY + ".property-a", "value-a", PREFIX + "." + DEFAULT_CONFIG_KEY + ".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_CONFIG_KEY, "profile-1", "profile-2"); } @Test(expected = MoaZSException.class) public void cancelAtIncompleteDefaultProfile() { var propMap = Map.of(PREFIX + "." + DEFAULT_CONFIG_KEY + ".property-a", "value-a"); var generator = setupMocksAndBuilder(propMap).build(); doThrow(moaZSException("Not Complete.")).when(validator).isConfigProfileComplete(any()); generator.generate(); } @Test public void continueAtIncompleteDefaultWhenVerificationDisabled() { var propMap = Map.of(PREFIX + "." + DEFAULT_CONFIG_KEY + ".property-a", "value-a"); doThrow(moaZSException("Not Complete.")).when(validator).isConfigProfileComplete(any()); 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_CONFIG_KEY); } 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.convert(any())).thenReturn(configTypeBuilder().build()); return configProfileGeneratorBuilder() .withProperties(properties) .withConfigUtil(util) .withProfilePrefix(PREFIX) .withValidator(validator) .withVerifyCompletenessOfDefaultConfiguration(true); } }