11package com .bobocode ;
22
3- import com .bobocode .data .Accounts ;
43import com .bobocode .model .Account ;
54import com .bobocode .model .Sex ;
65import org .junit .Before ;
76import org .junit .Test ;
87import org .junit .runner .RunWith ;
98import org .junit .runners .JUnit4 ;
109
10+ import java .math .BigDecimal ;
11+ import java .time .LocalDate ;
12+ import java .time .LocalDateTime ;
1113import 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
1716import 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