From e70ec39488c0688d8144fa529d58d60ae9564fbd Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Wed, 15 May 2019 06:41:15 +0200 Subject: WIP: Generate Map of Config Profiles from Spring Environment Augmenter - Replace default Config Profile with map of Config Profiles. Now, Augmenter can choose a config profile at runtime and augment it. - Move Augmenter to sub package. WIP - Implement ConfigProfileGenerator, which retrieves Config profiles from Spring Environment and returns an easy-to-use map with profiles that is keyed with profile id's. - Replace application.properties with application.yaml. - Remove Augmenter test cases: They need to be adapted. --- src/main/java/at/gv/egiz/moazs/App2MzsService.java | 1 + .../at/gv/egiz/moazs/DeliveryRequestAugmenter.java | 68 ----------------- .../config/DeliveryRequestAugmenterConfig.java | 25 ++---- .../moazs/preprocess/ConfigProfileGenerator.java | 87 +++++++++++++++++++++ .../moazs/preprocess/DeliveryRequestAugmenter.java | 69 +++++++++++++++++ src/main/resources/application.properties | 5 +- src/main/resources/application.yaml | 28 +++++++ src/main/resources/mzs/app2mzs.xsd | 1 + .../egiz/moazs/DeliveryRequestAugmenterTest.java | 89 +++++++++++----------- 9 files changed, 239 insertions(+), 134 deletions(-) delete mode 100644 src/main/java/at/gv/egiz/moazs/DeliveryRequestAugmenter.java create mode 100644 src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java create mode 100644 src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java create mode 100644 src/main/resources/application.yaml (limited to 'src') diff --git a/src/main/java/at/gv/egiz/moazs/App2MzsService.java b/src/main/java/at/gv/egiz/moazs/App2MzsService.java index 622f1dc..19eb856 100644 --- a/src/main/java/at/gv/egiz/moazs/App2MzsService.java +++ b/src/main/java/at/gv/egiz/moazs/App2MzsService.java @@ -1,6 +1,7 @@ package at.gv.egiz.moazs; import at.gv.egiz.moazs.pipeline.DeliveryPipeline; +import at.gv.egiz.moazs.preprocess.DeliveryRequestAugmenter; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.zustellung.app2mzs.xsd.App2MzsPortType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; diff --git a/src/main/java/at/gv/egiz/moazs/DeliveryRequestAugmenter.java b/src/main/java/at/gv/egiz/moazs/DeliveryRequestAugmenter.java deleted file mode 100644 index 96197d0..0000000 --- a/src/main/java/at/gv/egiz/moazs/DeliveryRequestAugmenter.java +++ /dev/null @@ -1,68 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.zustellung.app2mzs.xsd.ConfigType; -import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; -import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.DeliveryRequestTypeBuilder; -import at.gv.zustellung.app2mzs.xsd.ServerType; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; -import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; -import static at.gv.zustellung.app2mzs.xsd.ServerType.*; - -@Component -public class DeliveryRequestAugmenter { - - private final ConfigType defaultMzsConfig; - - @Autowired - public DeliveryRequestAugmenter(ConfigType defaultMzsConfig) { - this.defaultMzsConfig = defaultMzsConfig; - } - - /** - * Augments an incoming {@code request} by filling gaps with config- or default parameters. - * - * @param request - * @return augmented request - */ - public DeliveryRequestType augment(DeliveryRequestType request) { - - var config = merge(request.getConfig(), defaultMzsConfig); - - return new DeliveryRequestTypeBuilder(request) - .withConfig(config) - .build(); - } - - private ConfigType merge(ConfigType primary, ConfigType fallback) { - - var builder = configTypeBuilder(fallback); - - if(primary != null) { - - if(primary.getServer() != null) { - builder.withServer(merge(primary.getServer(), fallback.getServer())); - } - - if(primary.isPerformQueryPersonRequest() != null) { - builder.withPerformQueryPersonRequest(primary.isPerformQueryPersonRequest()); - } - - } - - return builder.build(); - - } - - private ServerType merge(ServerType primary, ServerType fallback) { - - var builder = serverTypeBuilder(fallback); - - if (primary.getX509() != null) builder.withX509 (primary.getX509() ); - if (primary.getZUSEUrlID() != null) builder.withZUSEUrlID(primary.getZUSEUrlID()); - - return builder.build(); - } -} diff --git a/src/main/java/at/gv/egiz/moazs/config/DeliveryRequestAugmenterConfig.java b/src/main/java/at/gv/egiz/moazs/config/DeliveryRequestAugmenterConfig.java index 93615c4..d5b52c5 100644 --- a/src/main/java/at/gv/egiz/moazs/config/DeliveryRequestAugmenterConfig.java +++ b/src/main/java/at/gv/egiz/moazs/config/DeliveryRequestAugmenterConfig.java @@ -1,31 +1,20 @@ package at.gv.egiz.moazs.config; +import at.gv.egiz.moazs.preprocess.ConfigProfileGenerator; import at.gv.zustellung.app2mzs.xsd.ConfigType; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; -import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; +import java.util.Map; @Configuration public class DeliveryRequestAugmenterConfig { - - @Value("${msg.default.url}") - private String zuseUrlID; - @Bean - public ConfigType defaultMzsConfig() { - - var server = serverTypeBuilder() - .withZUSEUrlID(zuseUrlID) - .withX509(null) //todo load cert! - .build(); - - return configTypeBuilder() - .withPerformQueryPersonRequest(false) - .withServer(server) - .build(); + @Autowired + Map deliveryRequestConfigs(ConfigProfileGenerator generator) { + return generator.generate(); } + } diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java new file mode 100644 index 0000000..cba776d --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java @@ -0,0 +1,87 @@ +package at.gv.egiz.moazs.preprocess; + +import at.gv.zustellung.app2mzs.xsd.ConfigType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.AbstractEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; +import static java.util.stream.Collectors.toMap; + +@Component +public class ConfigProfileGenerator { + + @Autowired + private Environment env; + + private final String profilePrefix = "delivery-request-configuration-profiles."; + private final String tnvzRequestKey = "perform-query-person-request"; + private final String msgUrlKey = "msg.url"; + + public Map generate() { + MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources(); + + Map> groupedKeys = StreamSupport.stream(propSrcs.spliterator(), false) + .filter(ps -> ps instanceof EnumerablePropertySource) + .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()) + .flatMap(Arrays::stream) + .filter(propName -> propName.startsWith(profilePrefix)) + .map(name -> name.substring(name.indexOf(".") + 1)) + .collect( + Collectors.groupingBy( + this::keepPrefix, + Collectors.mapping( + this::removePrefix, + Collectors.toSet()))); + + return groupedKeys.entrySet().stream() + .collect(toMap(Entry::getKey, this::createConfig)); + } + + private String keepPrefix(String name) { + return name.substring(0, name.indexOf(".")); + } + + private String removePrefix(String name) { + return name.substring(name.indexOf(".") + 1); + } + + private ConfigType createConfig(Entry> entry) { + + var profile = entry.getKey(); + + var values = new HashMap(); + + entry.getValue().stream() + .forEach(key -> { + var assembledKey = profilePrefix + "." + profile + "." + key; + var value = env.getProperty(assembledKey); + values.put(key, value); + }); + + var server = serverTypeBuilder() + .withZUSEUrlID(values.get(msgUrlKey)) + .build(); + + Boolean performQueryPersonRequest = values.get(tnvzRequestKey) == null + ? null : Boolean.getBoolean(values.get(tnvzRequestKey)); + + return configTypeBuilder() + .withPerformQueryPersonRequest(performQueryPersonRequest) + .withServer(server) + .build(); + } + +} diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java new file mode 100644 index 0000000..623af38 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java @@ -0,0 +1,69 @@ +package at.gv.egiz.moazs.preprocess; + +import at.gv.zustellung.app2mzs.xsd.ConfigType; +import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; +import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.DeliveryRequestTypeBuilder; +import at.gv.zustellung.app2mzs.xsd.ServerType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Map; + +import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; + +@Component +public class DeliveryRequestAugmenter { + + private final ConfigType defaultMzsConfig; + + @Autowired + public DeliveryRequestAugmenter(Map deliveryRequestConfigs) { + this.defaultMzsConfig = deliveryRequestConfigs.get("default"); + } + + /** + * Augments an incoming {@code request} by filling gaps with config- or default parameters. + * + * @param request + * @return augmented request + */ + public DeliveryRequestType augment(DeliveryRequestType request) { + + var config = merge(request.getConfig(), defaultMzsConfig); + + return new DeliveryRequestTypeBuilder(request) + .withConfig(config) + .build(); + } + + private ConfigType merge(ConfigType primary, ConfigType fallback) { + + var builder = configTypeBuilder(fallback); + + if(primary != null) { + + if(primary.getServer() != null) { + builder.withServer(merge(primary.getServer(), fallback.getServer())); + } + + if(primary.isPerformQueryPersonRequest() != null) { + builder.withPerformQueryPersonRequest(primary.isPerformQueryPersonRequest()); + } + + } + + return builder.build(); + + } + + private ServerType merge(ServerType primary, ServerType fallback) { + + var builder = serverTypeBuilder(fallback); + + if (primary.getX509() != null) builder.withX509 (primary.getX509() ); + if (primary.getZUSEUrlID() != null) builder.withZUSEUrlID(primary.getZUSEUrlID()); + + return builder.build(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7bd66e2..8b13789 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1 @@ -spring.redis.host=172.17.0.2 -spring.redis.port=6379 -## activate cluster mode -# spring.profiles.active=cluster + diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..98987b8 --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,28 @@ +spring: + redis: + host: 172.17.0.2 + port: 6379 + +## activate cluster mode +# profiles: +# active: cluster + +# Configure parameters here or in DeliveryRequest/Config. +# Choose a profile in DeliveryRequest/Config/ProfileId. +# If a parameter is missing, moa zs falls back to default +# Order: DeliveryRequest/Config > [chosen-profile] > default +delivery-request-configuration-profiles: + default: + msg: + url: http://localhost:8090/ + x509: todo! + perform-query-person-request: false + + app-profile-1: + msg: + url: https://msg-url1.com + perform-query-person-request: true + + app-profile-2: + msg: + url: https://msg-url2.com \ No newline at end of file diff --git a/src/main/resources/mzs/app2mzs.xsd b/src/main/resources/mzs/app2mzs.xsd index e97e9b6..91e52c8 100644 --- a/src/main/resources/mzs/app2mzs.xsd +++ b/src/main/resources/mzs/app2mzs.xsd @@ -80,6 +80,7 @@ + diff --git a/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java b/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java index a98da74..a555bb9 100644 --- a/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java +++ b/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java @@ -1,5 +1,6 @@ package at.gv.egiz.moazs; +import at.gv.egiz.moazs.preprocess.DeliveryRequestAugmenter; import at.gv.zustellung.app2mzs.xsd.ConfigType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import org.junit.Test; @@ -13,50 +14,50 @@ public class DeliveryRequestAugmenterTest { private final String msgUrl = "http://mzs"; - @Test - public void augmentPrimaryWithoutConfig() { - var fallback = createConfig(msgUrl, false); - var augmenter = new DeliveryRequestAugmenter(fallback); - - var actual = augmenter.augment(createRequest(null)).getConfig(); - - assertThat(actual).isEqualToComparingFieldByFieldRecursively(fallback); - } - - @Test - public void augmentPrimaryWithEmptyConfig() { - var fallback = createConfig(msgUrl, false); - var augmenter = new DeliveryRequestAugmenter(fallback); - var primary = createConfig(null, null); - - var actual = augmenter.augment(createRequest(primary)).getConfig(); - - assertThat(actual).isEqualToComparingFieldByFieldRecursively(fallback); - } - - @Test - public void overrideFallbacksUrlWithPrimary() { - var fallback = createConfig("http://fallback.url", false); - var primary = createConfig("http://primary.url", null); - var augmenter = new DeliveryRequestAugmenter(fallback); - - var actual = augmenter.augment(createRequest(primary)).getConfig(); - - var expected = createConfig("http://primary.url", false); - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); - } - - @Test - public void overrideFallbacksTvnzQueryWithPrimary() { - var fallback = createConfig(msgUrl, false); - var primary = createConfig(null, true); - var augmenter = new DeliveryRequestAugmenter(fallback); - - var actual = augmenter.augment(createRequest(primary)).getConfig(); - - var expected = createConfig(msgUrl, true); - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); - } +// @Test +// public void augmentPrimaryWithoutConfig() { +// var fallback = createConfig(msgUrl, false); +// var augmenter = new DeliveryRequestAugmenter(fallback, mzsMarshaller, deliveryRequestConfigs); +// +// var actual = augmenter.augment(createRequest(null)).getConfig(); +// +// assertThat(actual).isEqualToComparingFieldByFieldRecursively(fallback); +// } +// +// @Test +// public void augmentPrimaryWithEmptyConfig() { +// var fallback = createConfig(msgUrl, false); +// var augmenter = new DeliveryRequestAugmenter(fallback, mzsMarshaller, deliveryRequestConfigs); +// var primary = createConfig(null, null); +// +// var actual = augmenter.augment(createRequest(primary)).getConfig(); +// +// assertThat(actual).isEqualToComparingFieldByFieldRecursively(fallback); +// } +// +// @Test +// public void overrideFallbacksUrlWithPrimary() { +// var fallback = createConfig("http://fallback.url", false); +// var primary = createConfig("http://primary.url", null); +// var augmenter = new DeliveryRequestAugmenter(fallback, mzsMarshaller, deliveryRequestConfigs); +// +// var actual = augmenter.augment(createRequest(primary)).getConfig(); +// +// var expected = createConfig("http://primary.url", false); +// assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); +// } +// +// @Test +// public void overrideFallbacksTvnzQueryWithPrimary() { +// var fallback = createConfig(msgUrl, false); +// var primary = createConfig(null, true); +// var augmenter = new DeliveryRequestAugmenter(fallback, mzsMarshaller, deliveryRequestConfigs); +// +// var actual = augmenter.augment(createRequest(primary)).getConfig(); +// +// var expected = createConfig(msgUrl, true); +// assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); +// } private ConfigType createConfig(String url, Boolean performTnvz) { -- cgit v1.2.3