diff options
author | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-05-15 09:16:04 +0200 |
---|---|---|
committer | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-05-15 09:16:04 +0200 |
commit | 87f4be0b8834e6090137f2db2154bf50bcfcf646 (patch) | |
tree | 31a8e0183f94f231adba381a655b54a3b48039f2 | |
parent | e70ec39488c0688d8144fa529d58d60ae9564fbd (diff) | |
download | moa-zs-87f4be0b8834e6090137f2db2154bf50bcfcf646.tar.gz moa-zs-87f4be0b8834e6090137f2db2154bf50bcfcf646.tar.bz2 moa-zs-87f4be0b8834e6090137f2db2154bf50bcfcf646.zip |
Integrate Config Profiles + Refactoring
Config Profile Integration:
- Merge generated Configs from ConfigProfileGenerator with the default
Config; Reason: Merging profile Configs with default Config asap
ensures that profile Configs are "as complete as possible" while
allowing profile Configs to "on-demand"-override values from the
default Config.
- Adapt Augmenter to work with Config profiles: Select a Config on the
basis of on msg:DeliveryRequest/Config/ProfileId, fall back to 'default'
Config.
- Schema change: Make ProfileID optional.
Refactoring:
- Move Config-merging code from Augmenter into dedicated
'ConfigProfileMerger' class.
- ConfigProfileGenerator: Clarify stream pipeline and comply with
coding standard w.r.t. constant members.
- Add Augmenter test cases for profile selection.
5 files changed, 177 insertions, 97 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java index cba776d..a0cf766 100644 --- a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java +++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java @@ -13,22 +13,28 @@ 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.*; import static java.util.stream.Collectors.toMap; @Component public class ConfigProfileGenerator { - @Autowired - private Environment env; + private static final String PROFILE_PREFIX = "delivery-request-configuration-profiles."; + private static final String TNVZ_REQUEST_KEY = "perform-query-person-request"; + private static final String MSG_URL_KEY = "msg.url"; - private final String profilePrefix = "delivery-request-configuration-profiles."; - private final String tnvzRequestKey = "perform-query-person-request"; - private final String msgUrlKey = "msg.url"; + private final Environment env; + private final ConfigProfileMerger merger; + + @Autowired + public ConfigProfileGenerator(Environment env, ConfigProfileMerger merger) { + this.env = env; + this.merger = merger; + } public Map<String, ConfigType> generate() { MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources(); @@ -37,25 +43,29 @@ public class ConfigProfileGenerator { .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() + .filter(this::isConfigurationProfileProperty) + .map(this::removePrefix) + .collect(groupingBy(this::keepPrefix, mapping(this::removePrefix, toSet()))); + + Map<String, ConfigType> profiles = groupedKeys.entrySet().stream() .collect(toMap(Entry::getKey, this::createConfig)); + + var defaultProfile = profiles.get("default"); + + return profiles.entrySet().stream() + .collect(toUnmodifiableMap(Entry::getKey, e -> merger.merge(e.getValue(), defaultProfile))); + } + + private boolean isConfigurationProfileProperty(String propName) { + return propName.startsWith(PROFILE_PREFIX); } private String keepPrefix(String name) { - return name.substring(0, name.indexOf(".")); + return name.substring(0, name.indexOf('.')); } private String removePrefix(String name) { - return name.substring(name.indexOf(".") + 1); + return name.substring(name.indexOf('.') + 1); } private ConfigType createConfig(Entry<String, Set<String>> entry) { @@ -66,17 +76,17 @@ public class ConfigProfileGenerator { entry.getValue().stream() .forEach(key -> { - var assembledKey = profilePrefix + "." + profile + "." + key; + var assembledKey = PROFILE_PREFIX + '.' + profile + '.' + key; var value = env.getProperty(assembledKey); values.put(key, value); }); var server = serverTypeBuilder() - .withZUSEUrlID(values.get(msgUrlKey)) + .withZUSEUrlID(values.get(MSG_URL_KEY)) .build(); - Boolean performQueryPersonRequest = values.get(tnvzRequestKey) == null - ? null : Boolean.getBoolean(values.get(tnvzRequestKey)); + Boolean performQueryPersonRequest = values.get(TNVZ_REQUEST_KEY) == null + ? null : Boolean.getBoolean(values.get(TNVZ_REQUEST_KEY)); return configTypeBuilder() .withPerformQueryPersonRequest(performQueryPersonRequest) diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java new file mode 100644 index 0000000..f25ec12 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java @@ -0,0 +1,42 @@ +package at.gv.egiz.moazs.preprocess; + +import at.gv.zustellung.app2mzs.xsd.ConfigType; +import at.gv.zustellung.app2mzs.xsd.ServerType; +import org.springframework.stereotype.Component; + +import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; + +@Component +public class ConfigProfileMerger { + + public 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/preprocess/DeliveryRequestAugmenter.java b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java index 623af38..7211f6d 100644 --- a/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java +++ b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java @@ -3,23 +3,23 @@ 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; +import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; @Component public class DeliveryRequestAugmenter { - private final ConfigType defaultMzsConfig; + private final ConfigProfileMerger merger; + private final Map<String, ConfigType> configs; @Autowired - public DeliveryRequestAugmenter(Map<String, ConfigType> deliveryRequestConfigs) { - this.defaultMzsConfig = deliveryRequestConfigs.get("default"); + public DeliveryRequestAugmenter(Map<String, ConfigType> deliveryRequestConfigs, ConfigProfileMerger merger) { + this.configs = deliveryRequestConfigs; + this.merger = merger; } /** @@ -30,40 +30,26 @@ public class DeliveryRequestAugmenter { */ public DeliveryRequestType augment(DeliveryRequestType request) { - var config = merge(request.getConfig(), defaultMzsConfig); + var requestConfig = request.getConfig(); + + var fallbackConfig = determineProfileFromProfileId(requestConfig); + + var config = merger.merge(requestConfig, fallbackConfig); 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()); - } + private ConfigType determineProfileFromProfileId(ConfigType requestConfig) { + if (requestConfig == null || requestConfig.getProfileID() == null) { + return configs.get("default"); } - return builder.build(); + var id = requestConfig.getProfileID(); + return coalesce(configs.get(id), configs.get("default")).get(); } - 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/mzs/app2mzs.xsd b/src/main/resources/mzs/app2mzs.xsd index 91e52c8..74a63db 100644 --- a/src/main/resources/mzs/app2mzs.xsd +++ b/src/main/resources/mzs/app2mzs.xsd @@ -80,7 +80,7 @@ <xs:element name="Config" type="ConfigType"/> <xs:complexType name="ConfigType"> <xs:sequence> - <xs:element name="ProfileID" type="xs:token"></xs:element> + <xs:element name="ProfileID" type="xs:token" minOccurs="0"></xs:element> <xs:element ref="Server" minOccurs="0"></xs:element> <xs:element name="PerformQueryPersonRequest" type="xs:boolean" minOccurs="0" /> </xs:sequence> diff --git a/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java b/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java index a555bb9..a79efaf 100644 --- a/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java +++ b/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java @@ -1,10 +1,13 @@ package at.gv.egiz.moazs; +import at.gv.egiz.moazs.preprocess.ConfigProfileMerger; 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; +import java.util.Map; + import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; @@ -14,52 +17,90 @@ public class DeliveryRequestAugmenterTest { private final String msgUrl = "http://mzs"; -// @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); -// } + @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 overrideTvnzQueryWithPrimary() { + 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), new ConfigProfileMerger()); + } + + private DeliveryRequestAugmenter createAugmenter(Map<String, ConfigType> profiles) { + return new DeliveryRequestAugmenter(profiles, new ConfigProfileMerger()); + } + private ConfigType createConfig(String url, Boolean performTnvz) { + return createConfig(url, performTnvz, null); + } + + private ConfigType createConfig(String url, Boolean performTnvz, String profileId) { var server = serverTypeBuilder() .withZUSEUrlID(url) @@ -68,6 +109,7 @@ public class DeliveryRequestAugmenterTest { return configTypeBuilder() .withServer(server) .withPerformQueryPersonRequest(performTnvz) + .withProfileID(profileId) .build(); } |