From fcbe5503e00f317c2b9b908b9ec08d30620c4240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Gonz=C3=A1lez=20Marcos?= Date: Wed, 15 Jan 2025 10:19:31 +0100 Subject: [PATCH] feat: refactor contrib tutorial --- .../java/io/github/isagroup/models/Plan.java | 2 ++ .../isagroup/services/parsing/PlanParser.java | 9 ++++++++ .../parsing/PricingManagerParser.java | 8 +++++++ .../services/updaters/V11ToV20Updater.java | 9 +------- .../services/updaters/V20ToV21Updater.java | 9 +------- .../services/updaters/V21ToV22Updater.java | 22 +++++++++++++++++++ .../isagroup/services/updaters/Version.java | 16 +++++++++----- .../services/updaters/VersionUpdater.java | 11 +++++++++- .../services/updaters/YamlUpdater.java | 3 ++- .../parsing/positive/PlanParserTest.java | 15 +++++++++++++ src/test/resources/negative-parsing-tests.csv | 3 +++ .../plan/highlight/multiple-highlight.yml | 21 ++++++++++++++++++ .../plan/highlight/number-highlight.yml | 16 ++++++++++++++ .../positive/plan/highlight/highlight.yml | 21 ++++++++++++++++++ .../parsing/positive/plan/highlight/issue.yml | 19 ++++++++++++++++ .../positive/plan/highlight/no-highlight.yml | 15 +++++++++++++ 16 files changed, 176 insertions(+), 23 deletions(-) create mode 100644 src/main/java/io/github/isagroup/services/updaters/V21ToV22Updater.java create mode 100644 src/test/resources/parsing/negative/plan/highlight/multiple-highlight.yml create mode 100644 src/test/resources/parsing/negative/plan/highlight/number-highlight.yml create mode 100644 src/test/resources/parsing/positive/plan/highlight/highlight.yml create mode 100644 src/test/resources/parsing/positive/plan/highlight/issue.yml create mode 100644 src/test/resources/parsing/positive/plan/highlight/no-highlight.yml diff --git a/src/main/java/io/github/isagroup/models/Plan.java b/src/main/java/io/github/isagroup/models/Plan.java index 22084ea..1f718c2 100644 --- a/src/main/java/io/github/isagroup/models/Plan.java +++ b/src/main/java/io/github/isagroup/models/Plan.java @@ -20,6 +20,7 @@ public class Plan { private Object price; private String unit; private Boolean isPrivate; + private Boolean highlight; private Map features; private Map usageLimits; @@ -41,6 +42,7 @@ public Map serializePlan() { attributes.put("price", price); attributes.put("unit", unit); attributes.put("private", isPrivate); + attributes.put("highlight", highlight); Map features = serializeFeatures().orElse(null); Map usageLimits = serializeUsageLimits().orElse(null); diff --git a/src/main/java/io/github/isagroup/services/parsing/PlanParser.java b/src/main/java/io/github/isagroup/services/parsing/PlanParser.java index b878fcf..56dd986 100644 --- a/src/main/java/io/github/isagroup/services/parsing/PlanParser.java +++ b/src/main/java/io/github/isagroup/services/parsing/PlanParser.java @@ -61,6 +61,15 @@ public static Plan parseMapToPlan(String planName, Map map, Pric plan.setPrice(map.get("price")); } + // ---------- highlight ---------- + if (map.get("highlight") == null) { + map.put("highlight", false); + } else if (!(map.get("highlight") instanceof Boolean)) { + throw new PricingParsingException("plan " + planName + " received a " + + map.get("highlight").getClass().getSimpleName() + " but was expecting a bool"); + } + plan.setHighlight((Boolean) map.get("highlight")); + plan.setUnit((String) map.get("unit")); diff --git a/src/main/java/io/github/isagroup/services/parsing/PricingManagerParser.java b/src/main/java/io/github/isagroup/services/parsing/PricingManagerParser.java index c7b66b1..355f31d 100644 --- a/src/main/java/io/github/isagroup/services/parsing/PricingManagerParser.java +++ b/src/main/java/io/github/isagroup/services/parsing/PricingManagerParser.java @@ -338,9 +338,17 @@ private static void setPlans(Map map, PricingManager pricingMana } } + if (plansHaveMultipleHighlight(plans)) { + throw new PricingParsingException("Multiple highlighted plans are not allowed"); + } + pricingManager.setPlans(plans); } + private static boolean plansHaveMultipleHighlight(Map plans) { + return plans.entrySet().stream().filter(plan -> plan.getValue().getHighlight()).count() > 1; + } + private static void setAddOns(Map map, PricingManager pricingManager) { Map addOnsMap = (Map) map.get("addOns"); diff --git a/src/main/java/io/github/isagroup/services/updaters/V11ToV20Updater.java b/src/main/java/io/github/isagroup/services/updaters/V11ToV20Updater.java index 2a1e950..f24619e 100644 --- a/src/main/java/io/github/isagroup/services/updaters/V11ToV20Updater.java +++ b/src/main/java/io/github/isagroup/services/updaters/V11ToV20Updater.java @@ -16,14 +16,7 @@ public V11ToV20Updater(Updater updater) { @Override public void update(Map configFile) throws UpdateException { - try { - if (Version.version(configFile.get("version")).compare(this.getSource()) < 0) { - super.update(configFile); - - } - } catch (VersionException e) { - throw new UpdateException(e.getMessage(), configFile); - } + super.update(configFile); updateContainersWithOnlyOnePriceField(configFile); removeHasAnnualPaymentField(configFile); diff --git a/src/main/java/io/github/isagroup/services/updaters/V20ToV21Updater.java b/src/main/java/io/github/isagroup/services/updaters/V20ToV21Updater.java index 3d7caf7..1b2f3ea 100644 --- a/src/main/java/io/github/isagroup/services/updaters/V20ToV21Updater.java +++ b/src/main/java/io/github/isagroup/services/updaters/V20ToV21Updater.java @@ -14,14 +14,7 @@ public V20ToV21Updater(Updater updater) { @Override public void update(Map configFile) throws UpdateException { - try { - if (Version.version(configFile.get("version")).compare(this.getSource()) < 0) { - super.update(configFile); - - } - } catch (VersionException e) { - throw new UpdateException(e.getMessage(), configFile); - } + super.update(configFile); refactorPricingVersion(configFile); } diff --git a/src/main/java/io/github/isagroup/services/updaters/V21ToV22Updater.java b/src/main/java/io/github/isagroup/services/updaters/V21ToV22Updater.java new file mode 100644 index 0000000..f540fbf --- /dev/null +++ b/src/main/java/io/github/isagroup/services/updaters/V21ToV22Updater.java @@ -0,0 +1,22 @@ +package io.github.isagroup.services.updaters; + +import java.util.Map; + +import io.github.isagroup.exceptions.UpdateException; +import io.github.isagroup.exceptions.VersionException; + +public class V21ToV22Updater extends VersionUpdater { + + public V21ToV22Updater(Updater updater) { + super(Version.V2_1, updater); + } + + @Override + public void update(Map configFile) throws UpdateException { + + super.update(configFile); + + configFile.put("syntaxVersion", "2.1"); + } + +} diff --git a/src/main/java/io/github/isagroup/services/updaters/Version.java b/src/main/java/io/github/isagroup/services/updaters/Version.java index 207e5bb..15917ef 100644 --- a/src/main/java/io/github/isagroup/services/updaters/Version.java +++ b/src/main/java/io/github/isagroup/services/updaters/Version.java @@ -6,12 +6,13 @@ import java.util.regex.Pattern; public enum Version { - V1_0(1, 0), V1_1(1, 1), V2_0(2, 0), V2_1(2, 1); + V1_0(1, 0), V1_1(1, 1), V2_0(2, 0), V2_1(2, 1), + V2_2(2, 2); private final int major; private final int minor; - public static final Version LATEST = V2_1; + public static final Version LATEST = V2_2; Version(int major, int minor) { if (!isValid(major, minor)) { @@ -96,9 +97,10 @@ public static Version version(int major, int minor) { } else if (major == 2) { if (minor == 0) { return V2_0; - } - if (minor == 1) { + } else if (minor == 1) { return V2_1; + } else if (minor == 2) { + return V2_2; } } @@ -115,7 +117,7 @@ public int getMinor() { public static boolean isValid(int major, int minor) { boolean oneDotVersions = major == 1 && (minor == 0 || minor == 1); - boolean twoDotVersions = major == 2 && (minor == 0 || minor == 1); + boolean twoDotVersions = major == 2 && (minor == 0 || minor == 1 || minor == 2); return oneDotVersions || twoDotVersions; } @@ -138,6 +140,10 @@ public int compare(Version version) { return 0; } + public boolean lessThan(Version version) { + return this.compare(version) < 0; + } + @Override public String toString() { return this.major + "." + this.minor; diff --git a/src/main/java/io/github/isagroup/services/updaters/VersionUpdater.java b/src/main/java/io/github/isagroup/services/updaters/VersionUpdater.java index 9144d38..1a6c825 100644 --- a/src/main/java/io/github/isagroup/services/updaters/VersionUpdater.java +++ b/src/main/java/io/github/isagroup/services/updaters/VersionUpdater.java @@ -21,10 +21,19 @@ public void update(Map configFile) throws UpdateException { return; } - this.versionUpdater.update(configFile); + Object version = configFile.getOrDefault("syntaxVersion", configFile.get("version")); + + if (isSpecOutdated(version)) { + this.versionUpdater.update(configFile); + } } public Version getSource() { return source; } + + private boolean isSpecOutdated(Object version) { + return Version.version(version).lessThan(this.getSource()); + + } } diff --git a/src/main/java/io/github/isagroup/services/updaters/YamlUpdater.java b/src/main/java/io/github/isagroup/services/updaters/YamlUpdater.java index 664cf3f..d57a08f 100644 --- a/src/main/java/io/github/isagroup/services/updaters/YamlUpdater.java +++ b/src/main/java/io/github/isagroup/services/updaters/YamlUpdater.java @@ -15,6 +15,7 @@ public class YamlUpdater { updaters.put(Version.V1_0, new V10ToV11Updater(null)); updaters.put(Version.V1_1, new V11ToV20Updater(updaters.get(Version.V1_0))); updaters.put(Version.V2_0, new V20ToV21Updater(updaters.get(Version.V1_1))); + updaters.put(Version.V2_1, new V21ToV22Updater(updaters.get(Version.V2_0))); } public static void update(Map configFile) throws UpdateException { @@ -40,7 +41,7 @@ public static void update(Map configFile) throws UpdateException return; } - updaters.get(Version.V2_0).update(configFile); + updaters.get(Version.V2_1).update(configFile); }else{ throw new PricingParsingException("The syntax version field of the pricing must be a string or a double. Please ensure that the version field is present and correctly formatted"); } diff --git a/src/test/java/io/github/isagroup/parsing/positive/PlanParserTest.java b/src/test/java/io/github/isagroup/parsing/positive/PlanParserTest.java index 77b8bbb..e4582fe 100644 --- a/src/test/java/io/github/isagroup/parsing/positive/PlanParserTest.java +++ b/src/test/java/io/github/isagroup/parsing/positive/PlanParserTest.java @@ -98,4 +98,19 @@ void givenNullPrivateShouldParse() { } + @Test + @DisplayName(value = "When plan 'highlight' is not provided by default is false") + void givenNullHighlightShouldParse() { + String planName = "BASIC"; + PricingManager pricingManager = YamlUtils.retrieveManagerFromYaml(TEST_CASES + "highlight/no-highlight.yml"); + assertFalse(pricingManager.getPlans().get(planName).getHighlight()); + } + + @Test + @DisplayName(value = "One plan should be highlighted") + void givenHighlightShouldParse() { + String planName = "PRO"; + PricingManager pricingManager = YamlUtils.retrieveManagerFromYaml(TEST_CASES + "highlight/highlight.yml"); + assertTrue(pricingManager.getPlans().get(planName).getHighlight()); + } } diff --git a/src/test/resources/negative-parsing-tests.csv b/src/test/resources/negative-parsing-tests.csv index f2225d0..a24e617 100644 --- a/src/test/resources/negative-parsing-tests.csv +++ b/src/test/resources/negative-parsing-tests.csv @@ -49,6 +49,9 @@ Throw an error if 'features' is not a map;parsing/negative/features/features-is- Throw an error if plan 'private' is not a boolean;parsing/negative/plan/private/boolean-string-private.yml;The field "private" should be a boolean Throw an error if plan 'private' is not a boolean;parsing/negative/plan/private/number-private.yml;The field "private" should be a boolean Throw an error if plan 'private' is not a boolean;parsing/negative/plan/private/string-private.yml;The field "private" should be a boolean +# plan.highlight +Throw an error if plan 'highlight' is not a boolean;parsing/negative/plan/highlight/number-highlight.yml;plan BASIC received a Integer but was expecting a bool +Throw an error if plans has multiple 'highlight' set;parsing/negative/plan/highlight/multiple-highlight.yml;Multiple highlighted plans are not allowed # plans Throw an error if 'plans' is not a map;parsing/negative/plans/plans-is-boolean.yml;The plans are not defined correctly. It should be a map of plans and their options Throw an error if 'plans' is not a map;parsing/negative/plans/plans-is-float.yml;The plans are not defined correctly. It should be a map of plans and their options diff --git a/src/test/resources/parsing/negative/plan/highlight/multiple-highlight.yml b/src/test/resources/parsing/negative/plan/highlight/multiple-highlight.yml new file mode 100644 index 0000000..0093fa7 --- /dev/null +++ b/src/test/resources/parsing/negative/plan/highlight/multiple-highlight.yml @@ -0,0 +1,21 @@ +saasName: Multiple highlight +syntaxVersion: "2.2" +createdAt: 2024-01-09 +currency: EUR +features: + feature1: + description: Feature 1 description + valueType: BOOLEAN + defaultValue: true + type: DOMAIN +plans: + BASIC: + highlight: true + description: Basic plan + price: 0.0 + unit: user/month + PRO: + highlight: true + description: Pro plan + price: 10.0 + unit: user/month diff --git a/src/test/resources/parsing/negative/plan/highlight/number-highlight.yml b/src/test/resources/parsing/negative/plan/highlight/number-highlight.yml new file mode 100644 index 0000000..e4d991f --- /dev/null +++ b/src/test/resources/parsing/negative/plan/highlight/number-highlight.yml @@ -0,0 +1,16 @@ +saasName: Test +syntaxVersion: "2.2" +createdAt: 2024-01-09 +currency: EUR +features: + feature1: + description: Feature 1 description + valueType: BOOLEAN + defaultValue: true + type: DOMAIN +plans: + BASIC: + description: Basic plan + price: 0.0 + highlight: 3 + unit: user/month diff --git a/src/test/resources/parsing/positive/plan/highlight/highlight.yml b/src/test/resources/parsing/positive/plan/highlight/highlight.yml new file mode 100644 index 0000000..5a4a07b --- /dev/null +++ b/src/test/resources/parsing/positive/plan/highlight/highlight.yml @@ -0,0 +1,21 @@ +saasName: Test +syntaxVersion: "2.2" +createdAt: 2024-01-09 +currency: EUR +features: + feature1: + description: Feature 1 description + valueType: BOOLEAN + defaultValue: true + type: DOMAIN +plans: + BASIC: + highlight: false + description: Basic plan + price: 0.0 + unit: user/month + PRO: + highlight: true + description: Pro plan + price: 10.0 + unit: user/month diff --git a/src/test/resources/parsing/positive/plan/highlight/issue.yml b/src/test/resources/parsing/positive/plan/highlight/issue.yml new file mode 100644 index 0000000..898bea2 --- /dev/null +++ b/src/test/resources/parsing/positive/plan/highlight/issue.yml @@ -0,0 +1,19 @@ +saasName: Test +syntaxVersion: "2.1" +createdAt: 2024-01-09 +currency: EUR +features: + feature1: + description: Feature 1 description + valueType: BOOLEAN + defaultValue: true + type: DOMAIN +plans: + BASIC: + description: Basic plan + price: 0.0 + unit: user/month + PRO: + description: Pro plan + price: 10.0 + unit: user/month diff --git a/src/test/resources/parsing/positive/plan/highlight/no-highlight.yml b/src/test/resources/parsing/positive/plan/highlight/no-highlight.yml new file mode 100644 index 0000000..126292c --- /dev/null +++ b/src/test/resources/parsing/positive/plan/highlight/no-highlight.yml @@ -0,0 +1,15 @@ +saasName: Test +syntaxVersion: "2.2" +createdAt: 2024-01-09 +currency: EUR +features: + feature1: + description: Feature 1 description + valueType: BOOLEAN + defaultValue: true + type: DOMAIN +plans: + BASIC: + description: Basic plan + price: 0.0 + unit: user/month