Skip to content

Commit

Permalink
Only set SplitChargeRules when cost category has splitChargeRules
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyuhere committed Jun 21, 2024
1 parent ce1c6f4 commit 96f4a9f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ public static List<CostCategorySplitChargeRule> costCategorySplitChargeRulesFrom
/**
* Parser list of {@link CostCategorySplitChargeRule} into JSON array string.
*/
public static String costCategorySplitChargeRulesToJson(List<CostCategorySplitChargeRule> rules) {
if (rules == null) {
public static String costCategorySplitChargeRulesToJson(CostCategory costCategory) {
if (!costCategory.hasSplitChargeRules()) {
return null;
}
try {
return objectWriter.writeValueAsString(rules);
return objectWriter.writeValueAsString(costCategory.splitChargeRules());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
model.setEffectiveStart(costCategory.effectiveStart());
model.setRuleVersion(costCategory.ruleVersionAsString());
model.setRules(CostCategoryParser.costCategoryRulesToJson(costCategory.rules()));
model.setSplitChargeRules(CostCategoryParser.costCategorySplitChargeRulesToJson(costCategory.splitChargeRules()));
model.setSplitChargeRules(CostCategoryParser.costCategorySplitChargeRulesToJson(costCategory));
model.setDefaultValue(costCategory.defaultValue());

return ProgressEvent.<ResourceModel, CallbackContext>builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package software.amazon.ce.costcategory

import software.amazon.awssdk.services.costexplorer.model.CostCategory
import software.amazon.awssdk.services.costexplorer.model.CostCategoryRule
import software.amazon.awssdk.services.costexplorer.model.CostCategorySplitChargeRule
import software.amazon.cloudformation.exceptions.CfnInvalidRequestException
import spock.lang.Specification
import spock.lang.Unroll

import static software.amazon.ce.costcategory.Fixtures.*
import static software.amazon.ce.costcategory.Fixtures.JSON_SPLIT_CHARGE_RULE_EVEN
import static software.amazon.ce.costcategory.Fixtures.JSON_SPLIT_CHARGE_RULE_FIXED
import static software.amazon.ce.costcategory.Fixtures.JSON_SPLIT_CHARGE_RULE_PROPORTIONAL
import static software.amazon.ce.costcategory.Fixtures.SPLIT_CHARGE_RULE_EVEN
import static software.amazon.ce.costcategory.Fixtures.SPLIT_CHARGE_RULE_FIXED
import static software.amazon.ce.costcategory.Fixtures.SPLIT_CHARGE_RULE_PROPORTIONAL

class CostCategoryParserTest extends Specification {

Expand Down Expand Up @@ -85,7 +80,8 @@ class CostCategoryParserTest extends Specification {
@Unroll
def "Test: costCategorySplitChargeRulesToJson for #rule -> #expectedJson"(String expectedJson, CostCategorySplitChargeRule rule) {
when:
def jsonArray = CostCategoryParser.costCategorySplitChargeRulesToJson([rule])
def costCategory = CostCategory.builder().splitChargeRules([rule]).build()
def jsonArray = CostCategoryParser.costCategorySplitChargeRulesToJson(costCategory)

then:
jsonArray == "[ ${expectedJson} ]"
Expand All @@ -97,9 +93,10 @@ class CostCategoryParserTest extends Specification {
JSON_SPLIT_CHARGE_RULE_FIXED | SPLIT_CHARGE_RULE_FIXED
}

def "Test: costCategorySplitChargeRulesToJson when input is null"() {
def "Test: costCategorySplitChargeRulesToJson when cost category has empty split charge rules"() {
when:
def jsonArray = CostCategoryParser.costCategorySplitChargeRulesToJson(null)
def costCategory = CostCategory.builder().build()
def jsonArray = CostCategoryParser.costCategorySplitChargeRulesToJson(costCategory)

then:
jsonArray == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ReadHandlerTest extends HandlerSpecification {
.effectiveStart(COST_CATEGORY_EFFECTIVE_START)
.ruleVersion(RULE_VERSION)
.rules([ RULE_DIMENSION ])
.splitChargeRules([ SPLIT_CHARGE_RULE_EVEN ])
.defaultValue(COST_CATEGORY_DEFAULT_VALUE)
.build()
).build()
Expand All @@ -42,6 +43,7 @@ class ReadHandlerTest extends HandlerSpecification {
model.effectiveStart == COST_CATEGORY_EFFECTIVE_START
model.ruleVersion == RULE_VERSION
model.rules == "[ ${JSON_RULE_DIMENSION} ]"
model.splitChargeRules == "[ ${JSON_SPLIT_CHARGE_RULE_EVEN} ]"
model.defaultValue == COST_CATEGORY_DEFAULT_VALUE

event.resourceModel == model
Expand Down

0 comments on commit 96f4a9f

Please sign in to comment.