Skip to content

Commit

Permalink
#3237: add test case; refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerd Gühne (Marfir) committed Apr 2, 2023
1 parent f245267 commit 1464fb7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ public void rateOfReturnCalculationTest()
assertEquals(0.1, dividends.getRateOfReturnPerYear(), 0.0);
}

@Test
public void testCalculateYieldOnCost_emptyPortfolioShouldNotCauseExceptions()
{
Account myWealthyAccount = new Account("myWealthyAccount");
myWealthyAccount.setCurrencyCode("USD");
Security apple = new Security("Apple Corp", "USD");

List<CalculationLineItem> transactions = new ArrayList<>();

@SuppressWarnings("unused")
CostCalculation cost = Calculation.perform(CostCalculation.class, converter, apple, transactions);
DividendCalculation dividends = Calculation.perform(DividendCalculation.class, converter, apple, transactions);

assertEquals(0.0, dividends.getYieldOnCost(), 0.0);
}

@Test
public void testCalculateYieldOnCost_noDividendsShouldNotCauseExceptions()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,23 +291,12 @@ public void visit(CurrencyConverter converter, CalculationLineItem.DividendPayme
{
double fifoCosts = fifoCostAmount * Values.AmountFraction.factor();

double diviSum = 0.0;
if (years > 1)
{
LocalDate startTimeframe = now.minusDays(365);
for (Payment p : payments)
{
if (p.date.isAfter(startTimeframe))
{
diviSum = diviSum + (p.amount.getAmount() * Values.AmountFraction.factor());
}
}
}
else
{
diviSum = sum.getAmount() * Values.AmountFraction.factor();
diviSum = diviSum / years;
}
LocalDate firstDividendPaymentAccepted = now.minusYears(1);
double diviSum = years > 1 ? payments.stream() //
.filter(p -> p.date.isAfter(firstDividendPaymentAccepted)) //
.mapToDouble(p -> p.amount.getAmount() * Values.AmountFraction.factor()) //
.sum() //
: sum.getAmount() * Values.AmountFraction.factor() / years;

return diviSum * 100.0 / fifoCosts;
}
Expand Down

0 comments on commit 1464fb7

Please sign in to comment.