/******************************************************************************* * 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.ConfigUtil; import at.gv.egiz.moazs.preprocess.DeliveryRequestAugmenter; import at.gv.egiz.moazs.scheme.Marshaller; import at.gv.zustellung.app2mzs.xsd.ConfigType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; 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.zustellung.app2mzs.xsd.ClientType.clientTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; /** * @author Christof Rabensteiner * */ @RunWith(MockitoJUnitRunner.class) public class DeliveryRequestAugmenterTest { private final String msgUrl = "http://mzs"; @Mock private ConfigUtil configUtil; private Marshaller mzsMarshaller = new Marshaller(true); @Before public void setupMock() { when(configUtil.merge(any(), any())).thenCallRealMethod(); } @Test public void augmentPrimaryWithoutConfig() { var fallback = createConfig(msgUrl, false); var augmenter = createAugmenter(fallback); var actual = augmenter.augment(createRequest(null)).getConfig(); assertThat(actual).isEqualToComparingFieldByFieldRecursively(fallback); } @Test public void augmentPrimaryWithEmptyConfig() { var fallback = createConfig(msgUrl, false); var augmenter = createAugmenter(fallback); var primary = createConfig(null, null); var actual = augmenter.augment(createRequest(primary)).getConfig(); assertThat(actual).isEqualToComparingFieldByFieldRecursively(fallback); } @Test public void overrideUrlWithPrimary() { var fallback = createConfig("http://fallback.url", false); var primary = createConfig("http://primary.url", null); var augmenter = createAugmenter(fallback); var actual = augmenter.augment(createRequest(primary)).getConfig(); var expected = createConfig("http://primary.url", false); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test public void overrideTnvzQueryWithPrimary() { var fallback = createConfig(msgUrl, false); var primary = createConfig(null, true); var augmenter = createAugmenter(fallback); var actual = augmenter.augment(createRequest(primary)).getConfig(); var expected = createConfig(msgUrl, true); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test public void selectConfigByProfileId() { var defaultConfig = createConfig("http://default.url" , false); var selectableConfig = createConfig("http://selected.url", false); var augmenter = createAugmenter(Map.of("default", defaultConfig, "selectable-profile-id", selectableConfig)); var requestConfig = createConfig(null, null, "selectable-profile-id"); var actual = augmenter.augment(createRequest(requestConfig)).getConfig(); assertThat(actual).isEqualToComparingFieldByFieldRecursively(selectableConfig); } @Test public void selectConfigByProfileIdAndOverrideMsgUrl() { var defaultConfig = createConfig("http://default.url" , false); var selectableConfig = createConfig("http://selected.url", true); var augmenter = createAugmenter(Map.of("default", defaultConfig, "selectable-profile-id", selectableConfig)); var requestConfig = createConfig("http://override.url", null, "selectable-profile-id"); var actual = augmenter.augment(createRequest(requestConfig)).getConfig(); var expected = createConfig("http://override.url", true); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } private DeliveryRequestAugmenter createAugmenter(ConfigType fallback) { return new DeliveryRequestAugmenter(Map.of("default", fallback), configUtil, mzsMarshaller); } private DeliveryRequestAugmenter createAugmenter(Map profiles) { return new DeliveryRequestAugmenter(profiles, configUtil, mzsMarshaller); } private ConfigType createConfig(String url, Boolean performTnvz) { return createConfig(url, performTnvz, null); } private ConfigType createConfig(String url, Boolean performTnvz, String profileId) { var msgClient = clientTypeBuilder() .withURL(url) .build(); return configTypeBuilder() .withMSGClient(msgClient) .withPerformQueryPersonRequest(performTnvz) .withProfileID(profileId) .build(); } private DeliveryRequestType createRequest(ConfigType config) { return deliveryRequestTypeBuilder() .withConfig(config) .build(); } }