Skip to content

Commit

Permalink
Change the logic for TaxRates update actions. (#753)
Browse files Browse the repository at this point in the history
* Change the logic for TaxRates update actions.
  • Loading branch information
praveenkumarct authored Jul 8, 2021
1 parent 8a54d89 commit 5041772
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,10 @@ private static List<UpdateAction<TaxCategory>> buildRemoveOrReplaceTaxRateUpdate
oldTaxRate ->
newTaxRatesDrafts.stream()
.filter(
taxRateDraft ->
Objects.equals(oldTaxRate.getCountry(), taxRateDraft.getCountry()))
.filter(
taxRateDraft ->
oldTaxRate.getState() == null
|| (oldTaxRate.getState() != null
&& taxRateDraft.getState() == null)
|| Objects.equals(oldTaxRate.getState(), taxRateDraft.getState()))
newTaxRateDraft ->
Objects.equals(oldTaxRate.getCountry(), newTaxRateDraft.getCountry())
&& Objects.equals(
oldTaxRate.getState(), newTaxRateDraft.getState()))
.findFirst()
.map(
matchedTaxRateDraft -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,7 @@ void buildTaxRatesUpdateActions_WithMoreSubRates_ShouldReturnOnlyReplaceAction()
when(taxCategory.getName()).thenReturn("name");
when(taxCategory.getDescription()).thenReturn("desc");

final TaxRate taxRate1 = mock(TaxRate.class);
when(taxRate1.getName()).thenReturn("11% US");
when(taxRate1.getState()).thenReturn("state");
when(taxRate1.getId()).thenReturn("taxRate-1");
when(taxRate1.getAmount()).thenReturn(0.11);
when(taxRate1.getCountry()).thenReturn(CountryCode.US);
when(taxRate1.isIncludedInPrice()).thenReturn(false);

final SubRate oldSubRate1 = SubRate.of("subRate-1", 0.11);

when(taxRate1.getSubRates()).thenReturn(singletonList(oldSubRate1));
final TaxRate taxRate1 = buildTaxRate("state");
when(taxCategory.getTaxRates()).thenReturn(singletonList(taxRate1));

final SubRate subRate1 = SubRate.of("subRate-1", 0.06);
Expand Down Expand Up @@ -330,17 +320,7 @@ void buildTaxRatesUpdateActions_WithSameTaxRateAndSubRates_ShouldNotBuildAnyActi
when(taxCategory.getName()).thenReturn("name");
when(taxCategory.getDescription()).thenReturn("desc");

final TaxRate taxRate1 = mock(TaxRate.class);
when(taxRate1.getName()).thenReturn("11% US");
when(taxRate1.getState()).thenReturn("state");
when(taxRate1.getId()).thenReturn("taxRate-1");
when(taxRate1.getAmount()).thenReturn(0.11);
when(taxRate1.getCountry()).thenReturn(CountryCode.US);
when(taxRate1.isIncludedInPrice()).thenReturn(false);

final SubRate oldSubRate1 = SubRate.of("subRate-1", 0.11);

when(taxRate1.getSubRates()).thenReturn(singletonList(oldSubRate1));
final TaxRate taxRate1 = buildTaxRate("state");
when(taxCategory.getTaxRates()).thenReturn(singletonList(taxRate1));

final SubRate subRate1 = SubRate.of("subRate-1", 0.11);
Expand Down Expand Up @@ -370,17 +350,7 @@ void buildTaxRatesUpdateActions_WithNullOldState_ShouldReturnOnlyReplaceAction()
when(taxCategory.getName()).thenReturn("name");
when(taxCategory.getDescription()).thenReturn("desc");

final TaxRate taxRate1 = mock(TaxRate.class);
when(taxRate1.getName()).thenReturn("11% US");
when(taxRate1.getState()).thenReturn(null);
when(taxRate1.getId()).thenReturn("taxRate-1");
when(taxRate1.getAmount()).thenReturn(0.11);
when(taxRate1.getCountry()).thenReturn(CountryCode.US);
when(taxRate1.isIncludedInPrice()).thenReturn(false);

final SubRate oldSubRate1 = SubRate.of("subRate-1", 0.11);

when(taxRate1.getSubRates()).thenReturn(singletonList(oldSubRate1));
final TaxRate taxRate1 = buildTaxRate(null);
when(taxCategory.getTaxRates()).thenReturn(singletonList(taxRate1));

final SubRate subRate1 = SubRate.of("subRate-1", 0.06);
Expand All @@ -400,28 +370,19 @@ void buildTaxRatesUpdateActions_WithNullOldState_ShouldReturnOnlyReplaceAction()
final List<UpdateAction<TaxCategory>> result =
buildTaxRateUpdateActions(taxCategory, taxCategoryDraft);

assertThat(result).isEqualTo(singletonList(ReplaceTaxRate.of("taxRate-1", taxRateDraft)));
assertThat(result)
.isEqualTo(asList(RemoveTaxRate.of("taxRate-1"), AddTaxRate.of(taxRateDraft)));
}

@Test
void buildTaxRatesUpdateActions_WithRemovedState_ShouldReturnOnlyReplaceAction() {
void buildTaxRatesUpdateActions_WithRemovedState_ShouldReturnRemoveAndAddActions() {

TaxCategory taxCategory = mock(TaxCategory.class);
when(taxCategory.getKey()).thenReturn("tax-category-key");
when(taxCategory.getName()).thenReturn("name");
when(taxCategory.getDescription()).thenReturn("desc");

final TaxRate taxRate1 = mock(TaxRate.class);
when(taxRate1.getName()).thenReturn("11% US");
when(taxRate1.getState()).thenReturn("state");
when(taxRate1.getId()).thenReturn("taxRate-1");
when(taxRate1.getAmount()).thenReturn(0.11);
when(taxRate1.getCountry()).thenReturn(CountryCode.US);
when(taxRate1.isIncludedInPrice()).thenReturn(false);

final SubRate oldSubRate1 = SubRate.of("subRate-1", 0.11);

when(taxRate1.getSubRates()).thenReturn(singletonList(oldSubRate1));
final TaxRate taxRate1 = buildTaxRate("state");
when(taxCategory.getTaxRates()).thenReturn(singletonList(taxRate1));

final SubRate subRate1 = SubRate.of("subRate-1", 0.06);
Expand All @@ -441,28 +402,45 @@ void buildTaxRatesUpdateActions_WithRemovedState_ShouldReturnOnlyReplaceAction()
final List<UpdateAction<TaxCategory>> result =
buildTaxRateUpdateActions(taxCategory, taxCategoryDraft);

assertThat(result).isEqualTo(singletonList(ReplaceTaxRate.of("taxRate-1", taxRateDraft)));
assertThat(result)
.isEqualTo(asList(RemoveTaxRate.of("taxRate-1"), AddTaxRate.of(taxRateDraft)));
}

@Test
void buildTaxRatesUpdateActions_WithRemovedCountryCode_ShouldReturnOnlyRemoveAction() {
void WhenTaxRatesWithAndWithoutState_WithRemovedTaxRate_ShouldBuildAndReturnRemoveAction() {

TaxCategory taxCategory = mock(TaxCategory.class);
when(taxCategory.getKey()).thenReturn("tax-category-key");
when(taxCategory.getName()).thenReturn("name");
when(taxCategory.getDescription()).thenReturn("desc");

final TaxRate taxRate1 = mock(TaxRate.class);
when(taxRate1.getName()).thenReturn("11% US");
when(taxRate1.getState()).thenReturn("state");
when(taxRate1.getId()).thenReturn("taxRate-1");
when(taxRate1.getAmount()).thenReturn(0.11);
when(taxRate1.getCountry()).thenReturn(CountryCode.US);
when(taxRate1.isIncludedInPrice()).thenReturn(false);
final TaxRate taxRate1 = buildTaxRate("state");
final TaxRate taxRate2 = buildTaxRate(null);
when(taxCategory.getTaxRates()).thenReturn(asList(taxRate1, taxRate2));

TaxRateDraft taxRateDraft = TaxRateDraftBuilder.of(taxRate2).build();

// TaxCategoryDraft with only one taxRate
TaxCategoryDraft taxCategoryDraft =
TaxCategoryDraftBuilder.of("name", singletonList(taxRateDraft), "desc")
.key("tax-category-key")
.build();

final List<UpdateAction<TaxCategory>> result =
buildTaxRateUpdateActions(taxCategory, taxCategoryDraft);

assertThat(result).isEqualTo(asList(RemoveTaxRate.of("taxRate-1")));
}

@Test
void buildTaxRatesUpdateActions_WithRemovedCountryCode_ShouldReturnOnlyRemoveAction() {

final SubRate oldSubRate1 = SubRate.of("subRate-1", 0.11);
TaxCategory taxCategory = mock(TaxCategory.class);
when(taxCategory.getKey()).thenReturn("tax-category-key");
when(taxCategory.getName()).thenReturn("name");
when(taxCategory.getDescription()).thenReturn("desc");

when(taxRate1.getSubRates()).thenReturn(singletonList(oldSubRate1));
final TaxRate taxRate1 = buildTaxRate("state");
when(taxCategory.getTaxRates()).thenReturn(singletonList(taxRate1));

final SubRate subRate1 = SubRate.of("subRate-1", 0.06);
Expand Down Expand Up @@ -493,17 +471,7 @@ void buildTaxRatesUpdateActions_WithDuplicatedTaxRateDrafts_ShouldReturnOnlyFirs
when(taxCategory.getName()).thenReturn("name");
when(taxCategory.getDescription()).thenReturn("desc");

final TaxRate taxRate1 = mock(TaxRate.class);
when(taxRate1.getName()).thenReturn("11% US");
when(taxRate1.getState()).thenReturn("state");
when(taxRate1.getId()).thenReturn("taxRate-1");
when(taxRate1.getAmount()).thenReturn(0.11);
when(taxRate1.getCountry()).thenReturn(CountryCode.US);
when(taxRate1.isIncludedInPrice()).thenReturn(false);

final SubRate oldSubRate1 = SubRate.of("subRate-1", 0.11);

when(taxRate1.getSubRates()).thenReturn(singletonList(oldSubRate1));
final TaxRate taxRate1 = buildTaxRate("state");
when(taxCategory.getTaxRates()).thenReturn(singletonList(taxRate1));

final SubRate subRate1 = SubRate.of("subRate-1", 0.06);
Expand Down Expand Up @@ -589,4 +557,19 @@ void buildTaxRatesUpdateActions_WithDuplicatedTaxRateDrafts_ShouldReturnOnlyFirs
assertThat(result)
.isEqualTo(asList(RemoveTaxRate.of("taxRate-4"), AddTaxRate.of(newTaxRateDraft)));
}

private TaxRate buildTaxRate(String state) {
final TaxRate taxRate = mock(TaxRate.class);
when(taxRate.getName()).thenReturn("11% US");
when(taxRate.getState()).thenReturn(state);
when(taxRate.getId()).thenReturn("taxRate-1");
when(taxRate.getAmount()).thenReturn(0.11);
when(taxRate.getCountry()).thenReturn(CountryCode.US);
when(taxRate.isIncludedInPrice()).thenReturn(false);

final SubRate oldSubRate = SubRate.of("subRate-1", 0.11);

when(taxRate.getSubRates()).thenReturn(singletonList(oldSubRate));
return taxRate;
}
}

0 comments on commit 5041772

Please sign in to comment.