Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/main/java/io/github/isagroup/services/parsing/AddOnParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ private static void setAddOnFeatures(String addOnName, Map<String, Object> addOn

for (String addOnFeatureName : addOnFeaturesMap.keySet()) {

Map<String, Object> addOnFeatureMap = (Map<String, Object>) addOnFeaturesMap.get(addOnFeatureName);
Object featureObj = addOnFeaturesMap.get(addOnFeatureName);
if (!(featureObj instanceof Map)) {
throw new PricingParsingException("The feature " + addOnFeatureName
+ " of the add-on " + addOnName + " is not a valid map. Maybe 'value' attribute is missing to set the value of the feature");
}
@SuppressWarnings("unchecked")
Map<String, Object> addOnFeatureMap = (Map<String, Object>) featureObj;

if (!globalFeaturesMap.containsKey(addOnFeatureName)) {
throw new FeatureNotFoundException(
Expand Down Expand Up @@ -232,9 +238,23 @@ private static void setAddOnUsageLimits(String addOnName, Map<String, Object> ad
Map<String, Object> addOnUsageLimitsMap = null;

if (areExtensions) {
addOnUsageLimitsMap = (Map<String, Object>) addOnMap.get("usageLimitsExtensions");
Object usageLimitsExtensionsObj = addOnMap.get("usageLimitsExtensions");
if (usageLimitsExtensionsObj instanceof Map<?, ?>) {
addOnUsageLimitsMap = (Map<String, Object>) usageLimitsExtensionsObj;
} else if (usageLimitsExtensionsObj != null) {
throw new PricingParsingException("The field \"usageLimitsExtensions\" should be a map. It is currently: "
+ usageLimitsExtensionsObj.getClass().getSimpleName() + ". "
+ "Maybe you forgot to add the 'value' attribute to the usage limit in the add-on definition.");
}
} else {
addOnUsageLimitsMap = (Map<String, Object>) addOnMap.get("usageLimits");
Object usageLimitsObj = addOnMap.get("usageLimits");
if (usageLimitsObj instanceof Map<?, ?>) {
addOnUsageLimitsMap = (Map<String, Object>) usageLimitsObj;
} else if (usageLimitsObj != null) {
throw new PricingParsingException("The field \"usageLimits\" should be a map. It is currently: "
+ usageLimitsObj.getClass().getSimpleName() + ". "
+ "Maybe you forgot to add the 'value' attribute to the usage limit in the add-on definition.");
}
}
Map<String, UsageLimit> globalUsageLimitsMap = pricingManager.getUsageLimits();
Map<String, UsageLimit> addOnUsageLimits = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,17 @@ private static Integration parseMapToIntegration(String featureName, Map<String,
}

if (integration.getIntegrationType().equals(IntegrationType.WEB_SAAS)) {
integration.setPricingUrls((List<String>) map.get("pricingUrls"));
if (map.get("pricingUrls") != null) {
if (!(map.get("pricingUrls") instanceof List) || ((List<String>) map.get("pricingUrls")).isEmpty()
|| ((List<String>) map.get("pricingUrls")).stream().anyMatch(url -> !url.matches("^(http|https)://.*"))) {
throw new PricingParsingException("The feature " + featureName
+ " is from type INTEGRATION with integrationType WEB_SAAS but does not have a valid pricingUrls list (each item must be a valid URL with the http or https protocol). Current value: " + map.get("pricingUrls")
+ ". To specify a list you must use dash (-) before each item. Remember, it is an optional field so you can remove it from the input.");
}
integration.setPricingUrls((List<String>) map.get("pricingUrls"));
} else {
integration.setPricingUrls(List.of());
}
}

return integration;
Expand Down Expand Up @@ -134,7 +144,7 @@ private static Automation parseMapToAutomation(String featureName, Map<String, O

try {
automation.setAutomationType(AutomationType.valueOf(automationType));
} catch (IllegalArgumentException e) {
} catch (NullPointerException | IllegalArgumentException e) {
throw new InvalidAutomationTypeException(
"The feature " + featureName + " does not have a supported automationType (" + Arrays.toString(AutomationType.values()) + "). Current value: "
+ automationType);
Expand All @@ -160,7 +170,11 @@ private static Guarantee parseMapToGuarantee(String featureName, Map<String, Obj

if (map.get("docUrl") != null && !(map.get("docUrl") instanceof String)) {
throw new PricingParsingException("\'docUrl\' must be a String but found a "
+ map.get("docUrl").getClass().getSimpleName() + " instead");
+ map.get("docUrl").getClass().getSimpleName() + " instead (feature affected: '" + featureName + "'). Remember, it is an optional field so you can remove it from the input.");
}

if (map.get("docUrl") != null && !((String) map.get("docUrl")).matches("^(http|https)://.*")) {
throw new PricingParsingException("The docUrl field (from feature '" + featureName + "') must be a valid URL with the http or https protocol. Received: " + map.get("docUrl") + ". Remember, it is an optional field so you can remove it from the input.");
}

guarantee.setDocURL((String) map.get("docUrl"));
Expand Down Expand Up @@ -279,7 +293,7 @@ private static void parsePaymentValue(Feature feature, String featureName, Map<S
for (String paymentType : allowedPaymentTypes) {
try {
PaymentType.valueOf(paymentType);
} catch (IllegalArgumentException e) {
} catch (NullPointerException | IllegalArgumentException e) {
throw new InvalidDefaultValueException("The feature " + featureName
+ " does not have a valid defaultValue consisting on a list of supported paymentType ("+Arrays.toString(PaymentType.values())+"). PaymentType that generates the issue: " + paymentType);
}
Expand Down
29 changes: 25 additions & 4 deletions src/main/java/io/github/isagroup/services/parsing/PlanParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ private static void setFeaturesToPlan(String planName, Map<String, Object> map,

for (String planFeatureName : planFeaturesMap.keySet()) {

Map<String, Object> planFeatureMap = (Map<String, Object>) planFeaturesMap.get(planFeatureName);
Object planFeatureObj = planFeaturesMap.get(planFeatureName);
if (!(planFeatureObj instanceof Map)) {
throw new PricingParsingException("The feature " + planFeatureName
+ " of the plan " + planName + " is not a valid map. Maybe 'value' attribute is missing to set the value of the feature");
}
@SuppressWarnings("unchecked")
Map<String, Object> planFeatureMap = (Map<String, Object>) planFeatureObj;

if (!plan.getFeatures().containsKey(planFeatureName)) {
throw new FeatureNotFoundException(
Expand Down Expand Up @@ -188,15 +194,30 @@ private static void setUsageLimitsToPlan(String planName, Map<String, Object> ma

for (String planUsageLimitName : planUsageLimitsMap.keySet()) {

Map<String, Object> planUsageLimitMap = (Map<String, Object>) planUsageLimitsMap.get(planUsageLimitName);
Object planUsageLimitObj = planUsageLimitsMap.get(planUsageLimitName);
if (!(planUsageLimitObj instanceof Map)) {
throw new PricingParsingException("The usageLimit " + planUsageLimitName
+ " of the plan " + planName + " is not a valid map. Maybe 'value' attribute is missing to set the value of the usageLimit");
}
@SuppressWarnings("unchecked")
Map<String, Object> planUsageLimitMap = (Map<String, Object>) planUsageLimitObj;

if (!plan.getUsageLimits().containsKey(planUsageLimitName)) {
throw new FeatureNotFoundException(
"The usageLimit " + planUsageLimitName + " is not defined in the global usageLimits");
} else {
UsageLimit usageLimit = plan.getUsageLimits().get(planUsageLimitName);

Object value = planUsageLimitMap.get("value");
Object value = null;
try{
value = planUsageLimitMap.get("value");
}
catch (NullPointerException e){
throw new InvalidDefaultValueException("The usageLimit " + planUsageLimitName
+ " does not have a valid value. Current valueType: "
+ usageLimit.getValueType().toString() + "; Current value in plan " + plan.getName() + " is null");
}

boolean isValueNull = (value == null);

if (isValueNull){
Expand Down Expand Up @@ -251,7 +272,7 @@ public static void parsePaymentValue(Feature feature, String featureName, Map<St
for (String type : allowedPaymentTypes) {
try {
PaymentType.valueOf(type);
} catch (IllegalArgumentException e) {
} catch (NullPointerException | IllegalArgumentException e) {
throw new InvalidDefaultValueException(
"Invalid payment type for feature \"" + featureName + "\": \"" + type + "\" is not a supported payment type. "
+ "Supported types are: " + Arrays.toString(PaymentType.values()) + ". "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ private static void setPlans(Map<String, Object> map, PricingManager pricingMana
}

private static void setAddOns(Map<String, Object> map, PricingManager pricingManager) {
Map<String, Object> addOnsMap = (Map<String, Object>) map.get("addOns");
Map<String, Object> addOnsMap = null;
if (map.get("addOns") instanceof Map) {
addOnsMap = (Map<String, Object>) map.get("addOns");
}

if (addOnsMap == null) {
return;
Expand All @@ -356,7 +359,7 @@ private static void setAddOns(Map<String, Object> map, PricingManager pricingMan
AddOn addOn = AddOnParser.parseMapToAddOn(addOnName, addOnMap, pricingManager);

pricingManager.getAddOns().put(addOnName, addOn);
} catch (ClassCastException e) {
} catch (ClassCastException | NullPointerException | IllegalArgumentException e) {
throw new PricingParsingException(
"An error has occurred while parsing the add-on " + addOnName + ". Error: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static UsageLimit parseMapToUsageLimit(String limitName, Map<String, Obje
default:
return null;
}
} catch (IllegalArgumentException e) {
} catch (NullPointerException | IllegalArgumentException e) {
throw new IllegalArgumentException("The usage limit " + limitName
+ " does not have a supported type (" + Arrays.toString(UsageLimitType.values()) + "). Current type value: " + (String) limitMap.get("type"));
}
Expand Down Expand Up @@ -96,7 +96,7 @@ private static void loadBasicAttributes(UsageLimit limit, String limitName, Map<
limit.setDescription((String) map.get("description"));
try {
limit.setValueType(ValueType.valueOf((String) map.get("valueType")));
} catch (IllegalArgumentException e) {
} catch (NullPointerException | IllegalArgumentException e) {
throw new InvalidValueTypeException("The feature " + limitName
+ " does not have a supported valueType (" + Arrays.toString(ValueType.values()) + "). Current valueType: " + (String) map.get("valueType"));
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/negative-parsing-tests.csv
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Throw an error if 'currency' is missing;parsing/negative/currency/currency-is-nu
Throw an error if a feature is not a map of attributes;parsing/negative/feature/feature-is-key-value.yml;Feature 'foo' must be a Map but found String instead
Throw an error if a feature is named 'null';parsing/negative/feature/feature-null-as-key.yml;A feature cannot have the name null
# feature.docUrl
Throw an error if feature 'docUrl' is not a string;parsing/negative/feature/docUrl/is-bool.yml;'docUrl' must be a String but found a Boolean instead
Throw an error if feature 'docUrl' is not a string;parsing/negative/feature/docUrl/is-bool.yml;'docUrl' must be a String but found a Boolean instead (feature affected: 'guarantee'). Remember, it is an optional field so you can remove it from the input.
# feature.type
Throw an error if feature 'type' is missing or null;parsing/negative/feature/type/null-type.yml;feature 'type' is mandatory
# feature.valueType
Expand Down