Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit ca4e08c

Browse files
committed
Refactor test implementation
1 parent 83439ab commit ca4e08c

File tree

2 files changed

+40
-67
lines changed

2 files changed

+40
-67
lines changed

account-analytics/src/main/java/com.bobocode/AccountAnalytics.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Collection;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.Optional;
910

1011
/**
1112
* Implement methods using Stream API
@@ -21,7 +22,7 @@ private AccountAnalytics(Collection<Account> accounts) {
2122
this.accounts = accounts;
2223
}
2324

24-
public Account getRichestPerson() {
25+
public Optional<Account> findRichestPerson() {
2526
throw new UnsupportedOperationException("It's your job to make it work!"); // todo
2627
}
2728

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package com.bobocode;
22

3-
import com.bobocode.data.Accounts;
43
import com.bobocode.model.Account;
54
import com.bobocode.model.Sex;
65
import org.junit.Before;
76
import org.junit.Test;
87
import org.junit.runner.RunWith;
98
import org.junit.runners.JUnit4;
109

10+
import java.math.BigDecimal;
11+
import java.time.LocalDate;
12+
import java.time.LocalDateTime;
1113
import java.time.Month;
12-
import java.util.ArrayList;
13-
import java.util.HashMap;
14-
import java.util.List;
15-
import java.util.Map;
14+
import java.util.*;
1615

1716
import static org.junit.Assert.assertEquals;
1817

@@ -28,96 +27,69 @@ public class AccountAnalyticsTest {
2827

2928
@Before
3029
public void setUp() {
31-
accounts = Accounts.getAccountList(10);
30+
accounts = Arrays.asList(
31+
new Account(1L, "Justin", "Butler", "justin.butler@gmail.com",
32+
LocalDate.parse("2003-04-17"), Sex.MALE, LocalDateTime.now(), BigDecimal.valueOf(172966)),
33+
new Account(1L, "Olivia", "Cardenas", "cardenas@mail.com",
34+
LocalDate.parse("1930-01-19"), Sex.FEMALE, LocalDateTime.now(), BigDecimal.valueOf(38029)),
35+
new Account(1L, "Nolan", "Donovan", "nolandonovan@gmail.com",
36+
LocalDate.parse("1925-04-19"), Sex.MALE, LocalDateTime.now(), BigDecimal.valueOf(13889)),
37+
new Account(1L, "Lucas", "Lynn", "lucas.lynn@yahoo.com",
38+
LocalDate.parse("1987-05-25"), Sex.MALE, LocalDateTime.now(), BigDecimal.valueOf(16980))
39+
);
3240
analytics = AccountAnalytics.of(accounts);
3341
}
3442

3543
@Test
36-
public void testGetRichestPerson() {
37-
Account richestPerson = analytics.getRichestPerson();
44+
public void testFindRichestPerson() {
45+
Optional<Account> expectedPerson = Optional.of(accounts.get(0));
46+
Optional<Account> actualRichestPerson = analytics.findRichestPerson();
3847

39-
assertEquals(getRichestPerson(), richestPerson);
40-
}
41-
42-
private Account getRichestPerson() {
43-
Account richestPerson = accounts.get(0);
44-
for (Account account : accounts) {
45-
if (account.getBalance().compareTo(richestPerson.getBalance()) > 0) {
46-
richestPerson = account;
47-
}
48-
}
49-
50-
return richestPerson;
48+
assertEquals(expectedPerson, actualRichestPerson);
5149
}
5250

5351
@Test
5452
public void testSeparateMaleAccounts() {
53+
Map<Boolean, List<Account>> expectedAccountMap = getExpectedMaleMap();
5554
Map<Boolean, List<Account>> maleToAccountsMap = analytics.partitionMaleAccounts();
5655

57-
assertEquals(partitionMaleAccounts(), maleToAccountsMap);
56+
assertEquals(expectedAccountMap, maleToAccountsMap);
5857
}
5958

60-
private Map<Boolean, List<Account>> partitionMaleAccounts() {
61-
Map<Boolean, List<Account>> maleToAccountsMap = initializePartitionMap();
62-
63-
for (Account account : accounts) {
64-
if (account.getSex().equals(Sex.MALE)) {
65-
maleToAccountsMap.get(true).add(account);
66-
} else {
67-
maleToAccountsMap.get(false).add(account);
68-
}
69-
}
70-
71-
return maleToAccountsMap;
72-
}
73-
74-
private Map<Boolean, List<Account>> initializePartitionMap() {
75-
Map<Boolean, List<Account>> partitionMap = new HashMap<>();
76-
partitionMap.put(true, new ArrayList<>());
77-
partitionMap.put(false, new ArrayList<>());
78-
return partitionMap;
59+
private Map<Boolean, List<Account>> getExpectedMaleMap() {
60+
Map<Boolean, List<Account>> expectedMap = new HashMap<>(2);
61+
expectedMap.put(Boolean.TRUE, Arrays.asList(accounts.get(0), accounts.get(2), accounts.get(3)));
62+
expectedMap.put(Boolean.FALSE, Arrays.asList(accounts.get(1)));
63+
return expectedMap;
7964
}
8065

8166
@Test
8267
public void testFindAccountsByBirthdayMonth() {
68+
List<Account> expectedList = getExpectedList();
8369
List<Account> aprilAccounts = analytics.findAccountsByBirthdayMonth(Month.APRIL);
8470

85-
assertEquals(findAccountsByBirthdayMonth(Month.APRIL), aprilAccounts);
71+
assertEquals(expectedList, aprilAccounts);
8672
}
8773

88-
private List<Account> findAccountsByBirthdayMonth(Month month) {
89-
List<Account> accountList = new ArrayList<>();
90-
for (Account account : accounts) {
91-
if (account.getBirthday().getMonth().equals(month)) {
92-
accountList.add(account);
93-
}
94-
}
95-
96-
return accountList;
74+
private List<Account> getExpectedList() {
75+
return Arrays.asList(accounts.get(0), accounts.get(2));
9776
}
9877

9978
@Test
10079
public void testGroupAccountsByEmailDomain() {
80+
Map<String, List<Account>> expectedEmailMap = getExpectedEmailMap();
10181
Map<String, List<Account>> emailDomainToAccountsMap = analytics.groupAccountsByEmailDomain();
10282

103-
assertEquals(groupAccountsByEmailDomain(), emailDomainToAccountsMap);
83+
assertEquals(expectedEmailMap, emailDomainToAccountsMap);
10484
}
10585

106-
private Map<String, List<Account>> groupAccountsByEmailDomain() {
107-
Map<String, List<Account>> emailToAccountsMao = new HashMap<>();
108-
109-
for (Account account : accounts) {
110-
String emailDomain = account.getEmail().split("@")[1];
111-
if (emailToAccountsMao.containsKey(emailDomain)) {
112-
emailToAccountsMao.get(emailDomain).add(account);
113-
} else {
114-
List<Account> accountList = new ArrayList<>();
115-
accountList.add(account);
116-
emailToAccountsMao.put(emailDomain, accountList);
117-
}
118-
}
119-
120-
return emailToAccountsMao;
86+
private Map<String, List<Account>> getExpectedEmailMap() {
87+
Map<String, List<Account>> expectedEmailMap = new HashMap<>();
88+
expectedEmailMap.put("gmail.com", Arrays.asList(accounts.get(0), accounts.get(2)));
89+
expectedEmailMap.put("mail.com", Arrays.asList(accounts.get(1)));
90+
expectedEmailMap.put("yahoo.com", Arrays.asList(accounts.get(3)));
91+
92+
return expectedEmailMap;
12193
}
12294
}
12395

0 commit comments

Comments
 (0)