From 2b8a7df25878f546ace25373f44baeb026cf6d2b Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Tue, 20 Aug 2019 11:13:28 +0200 Subject: Add Custom Http Headers to HTTP Clients - Change app2mzs schema: Add Custom Headers to ClientType Element. - Set custom headers in ClientFactory. - Parse custom headers from spring environment. - Merge custom headers from two different profiles. - Add example to application.yaml. - Test conversion of custom headers from spring profile - Test merging and overriding custom headers. --- src/test/java/at/gv/egiz/moazs/ConfigUtilTest.java | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/test/java/at/gv/egiz/moazs/ConfigUtilTest.java (limited to 'src/test/java/at/gv/egiz/moazs') diff --git a/src/test/java/at/gv/egiz/moazs/ConfigUtilTest.java b/src/test/java/at/gv/egiz/moazs/ConfigUtilTest.java new file mode 100644 index 0000000..d7ac0a1 --- /dev/null +++ b/src/test/java/at/gv/egiz/moazs/ConfigUtilTest.java @@ -0,0 +1,80 @@ +package at.gv.egiz.moazs; + +import at.gv.egiz.moazs.preprocess.ConfigUtil; +import at.gv.zustellung.app2mzs.xsd.ConfigType; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +import static at.gv.zustellung.app2mzs.xsd.ClientType.clientTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; +import static org.assertj.core.api.Assertions.assertThat; + +public class ConfigUtilTest { + + private ConfigUtil configUtil; + + @Before + public void setup() { + configUtil = new ConfigUtil(); + } + + @Test + public void convertTNVZClientsCustomHttpHeaders() { + var prefix = ConfigUtil.TNVZ_CLIENT_KEY + "." + ConfigUtil.CUSTOM_HTTP_HEADERS_KEY + "."; + var map = Map.of( + prefix + "X-PVP-VERSION", "2.1", + prefix + "X-PVP-BINDING", "http", + prefix + "X-PVP-PARTICIPANT_ID", "AT:L9:MA2412" + ); + + ConfigType config = configUtil.convert(map); + var customHeaderMap = configUtil.convertHeadersToMap(config.getTNVZClient().getCustomHTTPHeader()); + + assertThat(customHeaderMap).containsEntry("X-PVP-VERSION", List.of("2.1")); + assertThat(customHeaderMap).containsEntry("X-PVP-BINDING", List.of("http")); + assertThat(customHeaderMap).containsEntry("X-PVP-PARTICIPANT_ID", List.of("AT:L9:MA2412")); + assertThat(customHeaderMap).hasSize(3); + } + + @Test + public void overrideCustomHeaders() { + + var fallbackConfig = createConfig(Map.of( + "X-header-1", "fallback-value-a", + "X-header-2", "fallback-value-b" + )); + + var primaryConfig = createConfig(Map.of( + "X-header-2", "primary-value-c", + "X-header-3", "primary-value-d" + )); + + ConfigType mergedConfig = configUtil.merge(primaryConfig, fallbackConfig); + var mergedHeaderMap = configUtil.convertHeadersToMap(mergedConfig.getTNVZClient().getCustomHTTPHeader()); + + assertThat(mergedHeaderMap).containsEntry("X-header-1", List.of("fallback-value-a")); + assertThat(mergedHeaderMap).containsEntry("X-header-2", List.of("primary-value-c")); + assertThat(mergedHeaderMap).containsEntry("X-header-3", List.of("primary-value-d")); + assertThat(mergedHeaderMap).hasSize(3); + + } + + private ConfigType createConfig(Map headerMap) { + + var headers = configUtil.convertMapToHeaders(headerMap); + + var client = clientTypeBuilder() + .withCustomHTTPHeader(headers) + .build(); + + return configTypeBuilder() + .withTNVZClient(client) + .build(); + } + + + +} -- cgit v1.2.3