ReportsService { public PeriodDetails detailsForMonth(long yearMonth) { List<Transaction> transactions = new TransactionNormalizer().normalize(transactions(yearMonth)); Map<String, Long> amountByCategoryName = new HashMap<>(); for (Transaction transaction : transactions) { Category category = transaction.getCategory(); String categoryName = category == null ? "Uncategorized" : category.getName(); Long amount = amountByCategoryName.get(categoryName); if (amount == null) { amount = 0L; } amountByCategoryName.put(categoryName, amount.longValue() + transaction.getAmount()); } List<CategorySummary> categorySummaries = new ArrayList<>(); for (Entry<String, Long> entry : amountByCategoryName.entrySet()) { categorySummaries.add(new CategorySummary(entry.getKey(), entry.getValue())); } Collections.sort(categorySummaries, new CategorySummaryAmountDescendingComparator()); return new PeriodDetails(monthName(yearMonth), categorySummaries); } @Inject  ReportsService(TransactionsService transactionsService); IncomeVersusExpensesReport incomeVersusExpenses(); ExpensesByCategoryReport expensesByCategory(); PeriodTransactions transactionsForMonth(long yearMonth); PeriodDetails detailsForMonth(long yearMonth);  }
@Test public void detailsForMonthNoCategories() { PeriodDetails detailsForMonth = service.detailsForMonth(201502); assertEquals("February 2015", detailsForMonth.getName()); assertEquals(1, detailsForMonth.getCategories().size()); CategorySummary categorySummary = detailsForMonth.getCategories().get(0); assertEquals("Uncategorized", categorySummary.getName()); assertEquals(-12345L, categorySummary.getAmount()); }
@Test public void detailsForMonthWithCategories() { transactions = createTransactionsWithCategories(); doReturn(transactions).when(transactionsService).retrieve(); PeriodDetails detailsForMonth = service.detailsForMonth(201501); assertEquals("January 2015", detailsForMonth.getName()); assertEquals(2, detailsForMonth.getCategories().size()); CategorySummary categorySummary = detailsForMonth.getCategories().get(0); assertEquals("Fries", categorySummary.getName()); assertEquals(-1500L, categorySummary.getAmount()); categorySummary = detailsForMonth.getCategories().get(1); assertEquals("Burgers", categorySummary.getName()); assertEquals(102300L, categorySummary.getAmount()); }