|
1 | 1 | package de.codecentric.java8examples.streaming; |
2 | 2 |
|
3 | 3 | import de.codecentric.java8examples.Invoice; |
| 4 | +import de.codecentric.java8examples.InvoiceItem; |
4 | 5 | import de.codecentric.java8examples.Person; |
5 | 6 | import de.codecentric.java8examples.TestData; |
6 | 7 | import org.junit.Test; |
7 | 8 |
|
8 | 9 | import java.math.BigDecimal; |
9 | | -import java.util.Arrays; |
10 | | -import java.util.IntSummaryStatistics; |
11 | | -import java.util.List; |
12 | | -import java.util.Map; |
| 10 | +import java.util.*; |
13 | 11 |
|
14 | 12 | import static junit.framework.Assert.assertNotNull; |
15 | 13 | import static org.hamcrest.Matchers.*; |
@@ -109,7 +107,24 @@ public void testExpensesByRecipient() throws Exception { |
109 | 107 |
|
110 | 108 | @Test |
111 | 109 | public void testCountByProduct() throws Exception { |
| 110 | + Map<String, Integer> expected = new HashMap<>(); |
| 111 | + for (Invoice invoice: invoices) { |
| 112 | + for (InvoiceItem item: invoice.getItems()) { |
| 113 | + String product = item.getProduct(); |
| 114 | + if (expected.get(product) == null) { |
| 115 | + expected.put(product, Integer.valueOf(0)); |
| 116 | + } |
| 117 | + expected.put( |
| 118 | + product, |
| 119 | + expected.get(product) + item.getQuantity()); |
| 120 | + } |
| 121 | + } |
112 | 122 |
|
| 123 | + Map<String, Integer> actual = CollectingAndReducing.countByProduct(invoices); |
| 124 | + assertThat(actual.keySet(), hasSize(expected.size())); |
| 125 | + for (Map.Entry entry: expected.entrySet()) { |
| 126 | + assertThat(actual, hasEntry(entry.getKey(), entry.getValue())); |
| 127 | + } |
113 | 128 | } |
114 | 129 |
|
115 | 130 | @Test |
|
0 commit comments