forked from Amitabh-DevOps/Springboot-BankApp
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAccountTest.java
More file actions
119 lines (97 loc) · 4.17 KB
/
Copy pathAccountTest.java
File metadata and controls
119 lines (97 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.example.bankapp.model;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class AccountTest {
private String username;
private String password;
private BigDecimal balance;
private List<Transaction> transactions;
private Collection<? extends GrantedAuthority> authorities;
@BeforeEach
void setUp() {
username = "alice";
password = "secret";
balance = new BigDecimal("100.50");
transactions = List.of(
new Transaction(new BigDecimal("25.00"), "DEPOSIT", LocalDateTime.now(), null));
authorities = List.of(new SimpleGrantedAuthority("USER"));
}
@Test
void allArgsConstructorSetsEveryField() {
Account account = new Account(username, password, balance, transactions, authorities);
assertThat(account.getUsername()).isEqualTo(username);
assertThat(account.getPassword()).isEqualTo(password);
assertThat(account.getBalance()).isEqualByComparingTo(new BigDecimal("100.50"));
assertThat(account.getTransactions()).isSameAs(transactions);
assertThat(account.getAuthorities()).isSameAs(authorities);
}
@Test
void getAuthoritiesReturnsSuppliedAuthorities() {
Account account = new Account(username, password, balance, transactions, authorities);
assertThat(account.getAuthorities())
.hasSize(1)
.first()
.extracting(GrantedAuthority::getAuthority)
.isEqualTo("USER");
}
@Test
void noArgsConstructorLeavesFieldsNull() {
Account account = new Account();
assertThat(account.getId()).isNull();
assertThat(account.getUsername()).isNull();
assertThat(account.getPassword()).isNull();
assertThat(account.getBalance()).isNull();
assertThat(account.getTransactions()).isNull();
assertThat(account.getAuthorities()).isNull();
}
@Test
void settersUpdateEveryField() {
Account account = new Account();
account.setId(42L);
account.setUsername(username);
account.setPassword(password);
account.setBalance(balance);
account.setTransactions(transactions);
account.setAuthorities(authorities);
assertThat(account.getId()).isEqualTo(42L);
assertThat(account.getUsername()).isEqualTo(username);
assertThat(account.getPassword()).isEqualTo(password);
assertThat(account.getBalance()).isEqualByComparingTo(new BigDecimal("100.50"));
assertThat(account.getTransactions()).isSameAs(transactions);
assertThat(account.getAuthorities()).isSameAs(authorities);
}
@Test
void getAuthoritiesReflectsSetAuthorities() {
Account account = new Account();
assertThat(account.getAuthorities()).isNull();
Collection<? extends GrantedAuthority> updated =
List.of(new SimpleGrantedAuthority("ADMIN"), new SimpleGrantedAuthority("USER"));
account.setAuthorities(updated);
assertThat(account.getAuthorities()).isSameAs(updated);
assertThat(account.getAuthorities())
.extracting(GrantedAuthority::getAuthority)
.containsExactly("ADMIN", "USER");
}
@Test
void getBalanceUsesExactBigDecimalValue() {
Account account = new Account();
account.setBalance(new BigDecimal("100.50"));
assertThat(account.getBalance()).isEqualByComparingTo(new BigDecimal("100.50"));
assertThat(account.getBalance()).isEqualTo(new BigDecimal("100.50"));
}
@Test
void inheritedUserDetailsFlagsReturnInterfaceDefaults() {
Account account = new Account();
assertThat(account.isAccountNonExpired()).isTrue();
assertThat(account.isAccountNonLocked()).isTrue();
assertThat(account.isCredentialsNonExpired()).isTrue();
assertThat(account.isEnabled()).isTrue();
}
}