diff options
author | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-05-16 13:31:53 +0200 |
---|---|---|
committer | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-05-16 13:31:53 +0200 |
commit | 4501468f1ffe1a8141e0c8711cd3cf78260df0c9 (patch) | |
tree | b3a3f2fc25018fed85c8e5abdd942fa5007d5a4f | |
parent | f193a2165d9261c2bd3413339c954a8bba829a93 (diff) | |
download | moa-zs-4501468f1ffe1a8141e0c8711cd3cf78260df0c9.tar.gz moa-zs-4501468f1ffe1a8141e0c8711cd3cf78260df0c9.tar.bz2 moa-zs-4501468f1ffe1a8141e0c8711cd3cf78260df0c9.zip |
Refactor: Merge Converter, Validator, and Merger into Util
- Reason: All three classes opertate with the same data type, have
the same clients, and have the same reasons for change.
- Makes code in client more readable as it reduces number of
dependencies.
9 files changed, 131 insertions, 165 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/config/PreprocessConfig.java b/src/main/java/at/gv/egiz/moazs/config/PreprocessConfig.java index ce40691..d1295b1 100644 --- a/src/main/java/at/gv/egiz/moazs/config/PreprocessConfig.java +++ b/src/main/java/at/gv/egiz/moazs/config/PreprocessConfig.java @@ -27,15 +27,11 @@ public class PreprocessConfig { @Autowired public ConfigProfileGenerator configProfileGenerator( SpringPropertiesFacade properties, - MapToConfigConverter converter, - ConfigProfileValidator validator, - ConfigProfileMerger merger) { + ConfigUtil util) { return configProfileGeneratorBuilder() .withProperties(properties) - .withConverter(converter) - .withValidator(validator) - .withMerger(merger) + .withConfigUtil(util) .withVerifyCompletenessOfDefaultConfiguration(verifyCompletenessOfDefaultConfiguration) .build(); } 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 0b4bc19..6337ff2 100644 --- a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java +++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java @@ -28,9 +28,7 @@ public class ConfigProfileGenerator { "availability."; private final SpringPropertiesFacade properties; - private final MapToConfigConverter converter; - private final ConfigProfileValidator validator; - private final ConfigProfileMerger merger; + private final ConfigUtil util; private final boolean verifyCompletenessOfDefaultConfiguration; private final String profilePrefix; private final String defaultConfigKey; @@ -41,16 +39,12 @@ public class ConfigProfileGenerator { private ConfigProfileGenerator( SpringPropertiesFacade properties, - MapToConfigConverter converter, - ConfigProfileValidator validator, - ConfigProfileMerger merger, + ConfigUtil util, boolean verifyCompletenessOfDefaultConfiguration, String profilePrefix, String defaultConfigKey) { - this.merger = merger; - this.validator = validator; + this.util = util; this.properties = properties; - this.converter = converter; this.verifyCompletenessOfDefaultConfiguration = verifyCompletenessOfDefaultConfiguration; this.profilePrefix = profilePrefix; this.defaultConfigKey = defaultConfigKey; @@ -70,7 +64,7 @@ public class ConfigProfileGenerator { var defaultProfile = profiles.get(defaultConfigKey); - if (!validator.isComplete(defaultProfile)) { + if (!util.isComplete(defaultProfile)) { if (verifyCompletenessOfDefaultConfiguration) throw MoaZSException.moaZSException(PROFILE_NOT_COMPLETE_ERROR_MESSAGE); else { @@ -110,7 +104,7 @@ public class ConfigProfileGenerator { values.put(key, value); }); - return converter.convert(values); + return util.convert(values); } @@ -118,36 +112,23 @@ public class ConfigProfileGenerator { return profiles.entrySet().stream() .collect(toUnmodifiableMap( Entry::getKey, - e -> merger.merge(e.getValue(), defaultProfile))); + e -> util.merge(e.getValue(), defaultProfile))); } public static class ConfigProfileGeneratorBuilder { private SpringPropertiesFacade properties; - private MapToConfigConverter converter; - private ConfigProfileValidator validator; - private ConfigProfileMerger merger; + private ConfigUtil util; private boolean verifyCompletenessOfDefaultConfiguration = true; private String profilePrefix = "delivery-request-configuration-profiles"; private String defaultConfigKey = "default"; - public ConfigProfileGeneratorBuilder withProperties(SpringPropertiesFacade properties) { this.properties = properties; return this; } - public ConfigProfileGeneratorBuilder withConverter(MapToConfigConverter converter) { - this.converter = converter; - return this; - } - - public ConfigProfileGeneratorBuilder withValidator(ConfigProfileValidator validator) { - this.validator = validator; - return this; - } - - public ConfigProfileGeneratorBuilder withMerger(ConfigProfileMerger merger) { - this.merger = merger; + public ConfigProfileGeneratorBuilder withConfigUtil(ConfigUtil util) { + this.util = util; return this; } @@ -167,9 +148,9 @@ public class ConfigProfileGenerator { } public ConfigProfileGenerator build() { - if(properties == null || converter == null || validator == null || merger == null || profilePrefix == null || defaultConfigKey == null) + if(properties == null || util == null || profilePrefix == null || defaultConfigKey == null) throw new IllegalArgumentException("Cannot build ConfigProfileGenerator: One or more arguments are null."); - return new ConfigProfileGenerator(properties, converter, validator, merger, verifyCompletenessOfDefaultConfiguration, profilePrefix, defaultConfigKey); + return new ConfigProfileGenerator(properties, util, verifyCompletenessOfDefaultConfiguration, profilePrefix, defaultConfigKey); } } } diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java deleted file mode 100644 index c94723e..0000000 --- a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java +++ /dev/null @@ -1,46 +0,0 @@ -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) { - - if (fallback == null) { - return primary; - } - - 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/ConfigProfileValidator.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileValidator.java deleted file mode 100644 index 08d8aea..0000000 --- a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileValidator.java +++ /dev/null @@ -1,17 +0,0 @@ -package at.gv.egiz.moazs.preprocess; - -import at.gv.zustellung.app2mzs.xsd.ConfigType; -import org.springframework.stereotype.Component; - -@Component -public class ConfigProfileValidator { - - public boolean isComplete(ConfigType profile) { - //TODO: add check fo x509 certificate - return profile != null - && profile.isPerformQueryPersonRequest() != null - && profile.getServer() != null - && profile.getServer().getZUSEUrlID() != null; - } - -} diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java new file mode 100644 index 0000000..736fb0a --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java @@ -0,0 +1,95 @@ +package at.gv.egiz.moazs.preprocess; + +import at.gv.zustellung.app2mzs.xsd.ConfigType; +import at.gv.zustellung.app2mzs.xsd.ServerType; +import org.springframework.lang.Nullable; +import org.springframework.stereotype.Component; + +import javax.validation.constraints.Null; +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 ConfigUtil { + + private static final String TNVZ_REQUEST_KEY = "perform-query-person-request"; + private static final String MSG_URL_KEY = "msg.url"; + + /** + * Builds a ConvertType Object out of a Map on the Basis of it's Keys and Values. + * + * @param Map with keys and values. + * @return ConvertType Object + */ + public ConfigType convert(Map<String, String> values) { + var server = serverTypeBuilder() + .withZUSEUrlID(values.get(MSG_URL_KEY)) + .build(); + + Boolean performQueryPersonRequest = values.get(TNVZ_REQUEST_KEY) == null + ? null : Boolean.getBoolean(values.get(TNVZ_REQUEST_KEY)); + + return ConfigType.configTypeBuilder() + .withPerformQueryPersonRequest(performQueryPersonRequest) + .withServer(server) + .build(); + } + + /** + * Combines Properties of Two ConfigType Objects; {@code primary} overrides {@code fallback}. + * + * @param primary + * @param fallback + * @return Combined ConfigType + */ + public ConfigType merge(@Nullable 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) { + + if (fallback == null) { + return primary; + } + + var builder = serverTypeBuilder(fallback); + + if (primary.getX509() != null) builder.withX509 (primary.getX509() ); + if (primary.getZUSEUrlID() != null) builder.withZUSEUrlID(primary.getZUSEUrlID()); + + return builder.build(); + } + + /** + * + * + * @param profile + * @return + */ + public boolean isComplete(@Nullable ConfigType profile) { + //TODO: add check fo x509 certificate + return profile != null + && profile.isPerformQueryPersonRequest() != null + && profile.getServer() != null + && profile.getServer().getZUSEUrlID() != null; + } + +} 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 0b49347..f3a05d1 100644 --- a/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java +++ b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java @@ -13,9 +13,8 @@ import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.deliveryRequestTy @Component public class DeliveryRequestAugmenter { - private final ConfigProfileMerger merger; + private final ConfigUtil util; private final Map<String, ConfigType> configs; - private final ConfigProfileValidator validator; private static final String INCOMPLETE_CONFIG_ERROR_MESSAGE = "Could not find a profile for " + "the delivery request configuration, and the configuration attached to mzs:DeliveryRequest is incomplete."; @@ -24,11 +23,9 @@ public class DeliveryRequestAugmenter { "configuration is incomplete."; @Autowired - public DeliveryRequestAugmenter(Map<String, ConfigType> deliveryRequestConfigs, ConfigProfileMerger merger, - ConfigProfileValidator validator) { + public DeliveryRequestAugmenter(Map<String, ConfigType> deliveryRequestConfigs, ConfigUtil util) { this.configs = deliveryRequestConfigs; - this.merger = merger; - this.validator = validator; + this.util = util; } /** @@ -45,16 +42,16 @@ public class DeliveryRequestAugmenter { if (fallbackConfig == null) { - if (validator.isComplete(requestConfig)) + if (util.isComplete(requestConfig)) return request; else throw moaZSException(INCOMPLETE_CONFIG_ERROR_MESSAGE); } else { - var mergedConfig = merger.merge(requestConfig, fallbackConfig); + var mergedConfig = util.merge(requestConfig, fallbackConfig); - if (validator.isComplete(mergedConfig)) { + if (util.isComplete(mergedConfig)) { return deliveryRequestTypeBuilder(request) .withConfig(mergedConfig) .build(); diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/MapToConfigConverter.java b/src/main/java/at/gv/egiz/moazs/preprocess/MapToConfigConverter.java deleted file mode 100644 index ef7cff1..0000000 --- a/src/main/java/at/gv/egiz/moazs/preprocess/MapToConfigConverter.java +++ /dev/null @@ -1,29 +0,0 @@ -package at.gv.egiz.moazs.preprocess; - -import at.gv.zustellung.app2mzs.xsd.ConfigType; -import org.springframework.stereotype.Component; - -import java.util.Map; - -import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; - -@Component -public class MapToConfigConverter { - - private static final String TNVZ_REQUEST_KEY = "perform-query-person-request"; - private static final String MSG_URL_KEY = "msg.url"; - - public ConfigType convert(Map<String, String> values) { - var server = serverTypeBuilder() - .withZUSEUrlID(values.get(MSG_URL_KEY)) - .build(); - - Boolean performQueryPersonRequest = values.get(TNVZ_REQUEST_KEY) == null - ? null : Boolean.getBoolean(values.get(TNVZ_REQUEST_KEY)); - - return ConfigType.configTypeBuilder() - .withPerformQueryPersonRequest(performQueryPersonRequest) - .withServer(server) - .build(); - } -} diff --git a/src/test/java/at/gv/egiz/moazs/ConfigProfileGeneratorTest.java b/src/test/java/at/gv/egiz/moazs/ConfigProfileGeneratorTest.java index c0ff96c..95cccd1 100644 --- a/src/test/java/at/gv/egiz/moazs/ConfigProfileGeneratorTest.java +++ b/src/test/java/at/gv/egiz/moazs/ConfigProfileGeneratorTest.java @@ -1,7 +1,6 @@ package at.gv.egiz.moazs; import at.gv.egiz.moazs.preprocess.*; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -22,17 +21,11 @@ public class ConfigProfileGeneratorTest { private static final String DEFAULT = "default"; @Mock - private ConfigProfileMerger merger; - - @Mock - private ConfigProfileValidator validator; + private ConfigUtil util; @Mock private SpringPropertiesFacade properties; - @Mock - private MapToConfigConverter converter; - @Test public void assembleDefaultProfile() { var propMap = Map.of( @@ -43,7 +36,7 @@ public class ConfigProfileGeneratorTest { var profiles = generator.generate(); - verify(converter).convert(Map.of( + verify(util).convert(Map.of( "property-a", "value-a", "property-b", "value-b", "property-c", "value-c")); @@ -57,7 +50,7 @@ public class ConfigProfileGeneratorTest { var profiles = generator.generate(); - verifyZeroInteractions(converter); + verify(util, never()).convert(any()); assertThat(profiles.keySet()).isEmpty(); } @@ -68,7 +61,7 @@ public class ConfigProfileGeneratorTest { var profiles = generator.generate(); - verifyZeroInteractions(converter); + verify(util, never()).convert(any()); assertThat(profiles.keySet()).isEmpty(); } @@ -84,9 +77,9 @@ public class ConfigProfileGeneratorTest { var profiles = generator.generate(); - verify(converter).convert(Map.of("property-a", "value-a", "property-b", "value-b")); - verify(converter).convert(Map.of("property-c", "value-c")); - verify(converter).convert(Map.of("property-d", "value-d")); + 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, "profile-1", "profile-2"); } @@ -94,7 +87,7 @@ public class ConfigProfileGeneratorTest { public void cancelAtIncompleteDefaultProfile() { var propMap = Map.of(PREFIX + "." + DEFAULT + ".property-a", "value-a"); var generator = setupMocksAndBuilder(propMap).build(); - when(validator.isComplete(any())).thenReturn(false); + when(util.isComplete(any())).thenReturn(false); generator.generate(); } @@ -103,7 +96,7 @@ public class ConfigProfileGeneratorTest { public void continueAtIncompleteDefaultWhenVerificationDisabled() { var propMap = Map.of(PREFIX + "." + DEFAULT + ".property-a", "value-a"); - when(validator.isComplete(any())).thenReturn(false); + when(util.isComplete(any())).thenReturn(false); var generator = setupMocksAndBuilder(propMap) .withVerifyCompletenessOfDefaultConfiguration(false) @@ -111,22 +104,20 @@ public class ConfigProfileGeneratorTest { var profiles = generator.generate(); - verify(converter).convert(Map.of("property-a", "value-a")); + verify(util).convert(Map.of("property-a", "value-a")); assertThat(profiles.keySet()).containsExactlyInAnyOrder(DEFAULT); } private ConfigProfileGenerator.ConfigProfileGeneratorBuilder setupMocksAndBuilder(Map<String, String> propMap) { when(properties.getPropertyNames()).thenReturn(propMap.keySet().stream()); when(properties.getProperty(any())).thenAnswer(i -> propMap.get(i.getArgument(0))); - when(merger.merge(any(), any())).thenAnswer(i -> i.getArgument(0)); - when(validator.isComplete(any())).thenReturn(true); - when(converter.convert(any())).thenReturn(configTypeBuilder().build()); + when(util.merge(any(), any())).thenAnswer(i -> i.getArgument(0)); + when(util.isComplete(any())).thenReturn(true); + when(util.convert(any())).thenReturn(configTypeBuilder().build()); return configProfileGeneratorBuilder() .withProperties(properties) - .withConverter(converter) - .withValidator(validator) - .withMerger(merger) + .withConfigUtil(util) .withDefaultConfigKey(DEFAULT) .withProfilePrefix(PREFIX) .withVerifyCompletenessOfDefaultConfiguration(true); diff --git a/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java b/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java index 73313af..c389b61 100644 --- a/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java +++ b/src/test/java/at/gv/egiz/moazs/DeliveryRequestAugmenterTest.java @@ -1,7 +1,6 @@ package at.gv.egiz.moazs; -import at.gv.egiz.moazs.preprocess.ConfigProfileMerger; -import at.gv.egiz.moazs.preprocess.ConfigProfileValidator; +import at.gv.egiz.moazs.preprocess.ConfigUtil; import at.gv.egiz.moazs.preprocess.DeliveryRequestAugmenter; import at.gv.zustellung.app2mzs.xsd.ConfigType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; @@ -89,14 +88,13 @@ public class DeliveryRequestAugmenterTest { } private DeliveryRequestAugmenter createAugmenter(ConfigType fallback) { - return new DeliveryRequestAugmenter(Map.of("default", fallback), new ConfigProfileMerger(), new ConfigProfileValidator()); + return new DeliveryRequestAugmenter(Map.of("default", fallback), new ConfigUtil()); } private DeliveryRequestAugmenter createAugmenter(Map<String, ConfigType> profiles) { - return new DeliveryRequestAugmenter(profiles, new ConfigProfileMerger(), new ConfigProfileValidator()); + return new DeliveryRequestAugmenter(profiles, new ConfigUtil()); } - private ConfigType createConfig(String url, Boolean performTnvz) { return createConfig(url, performTnvz, null); } |