AccountServiceDefaultTest.java
6.38 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.legrog.web.account;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.legrog.entities.*;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Classe testing AccountServiceDefault.
*/
public class AccountServiceDefaultTest {
AccountServiceDefault accountServiceDefault;
AccountServiceDefault accountServiceDefaultSpy;
@Captor
ArgumentCaptor<List <Account>> accountsArgumentCaptor;
@BeforeEach
public void setUp(@Mock AccountRepository accountRepository,
@Mock AccountSearchRepository accountSearchRepository) throws Exception {
accountServiceDefault = Mockito.spy(new AccountServiceDefault(accountRepository, accountSearchRepository)
{
public List<IndexedAccount> convertAccountsIntoIndexedAccounts(List<Account> accounts) {
return new ArrayList<>();
}
}
);
}
@Nested
@DisplayName("search method")
class SearchTests {
@DisplayName("When called, should delegate search to AccountSearchRepository")
@Test
public void testDelegateSearchToAccountSearchRepository(@Mock AccountSearchRepository accountSearchRepository) throws SearchingException {
accountServiceDefault.search("3");
Mockito.verify(accountSearchRepository).search("3");
}
@DisplayName("When getting IndexedAccounts from search, should convertIndexedAccountsIntoAccounts them")
@Test
public void testConvertReturnedIndexedAccounts(@Mock AccountSearchRepository accountSearchRepository) throws SearchingException {
List<IndexedAccount> indexedAccounts = new ArrayList<>();
when(accountSearchRepository.search("4")).thenReturn(indexedAccounts);
accountServiceDefault.search("4");
verify(accountServiceDefault, times(1)).convertIndexedAccountsIntoAccounts(indexedAccounts);
}
@DisplayName("When called, should return the Accounts it gets from convertIndexedAccountsIntoAccounts")
@Test
public void testReturnFromConvert(@Mock AccountSearchRepository accountSearchRepository) throws SearchingException {
List<Account> accounts = new ArrayList<>();
List<IndexedAccount> indexedAccounts = new ArrayList<>();
Mockito.doReturn(accounts).when(accountServiceDefault).convertIndexedAccountsIntoAccounts(indexedAccounts);
when(accountSearchRepository.search("5")).thenReturn(indexedAccounts);
assertThat(accountServiceDefault.search("5")).isEqualTo(accounts);
}
}
@Nested
@DisplayName("convertIndexedAccountsIntoAccounts method")
class ConvertTests {
@DisplayName("When called, should return the Accounts it gets from findByUserIdIn")
@Test
public void testReturnFromFind(@Mock AccountRepository accountRepository) {
List<Account> accounts = new ArrayList<>();
List<IndexedAccount> indexedAccounts = new ArrayList<>();
when(accountRepository.findByUserIdIn(Mockito.any())).thenReturn(accounts);
assertThat(accountServiceDefault.convertIndexedAccountsIntoAccounts(indexedAccounts)).isEqualTo(accounts);
}
}
@Nested
@DisplayName("reindexAllAccounts method")
class ReindexAllAccountsTests {
@DisplayName("When called, should follow the call sequence")
@Test
public void testCorrectCalls(@Mock AccountRepository accountRepository, @Mock AccountSearchRepository accountSearchRepository) throws IndexingException {
accountServiceDefault.reindexAllAccounts();
Mockito.verify(accountRepository).findByPresentationIsNotNull();
verify(accountServiceDefault, times(1)).convertAccountsIntoIndexedAccounts(Mockito.any());
Mockito.verify(accountSearchRepository).reindex(Mockito.any());
}
@DisplayName("When called, should send accounts it gets from findByPresentationIsNotNull to convertAccountsIntoIndexedAccounts")
@Test
public void testTransmitsAccounts(@Mock AccountRepository accountRepository, @Mock AccountSearchRepository accountSearchRepository) throws IndexingException {
List<Account> accounts = new ArrayList<>();
Account account = new Account();
accounts.add(account);
when(accountRepository.findByPresentationIsNotNull()).thenReturn(accounts);
accountServiceDefault.reindexAllAccounts();
verify(accountServiceDefault, times(1)).convertAccountsIntoIndexedAccounts(accountsArgumentCaptor.capture());
assertThat(accountsArgumentCaptor.getValue()).isEqualTo(accounts);
}
@DisplayName("When called, should send indexedAccounts it gets from convertAccountsIntoIndexedAccounts to reindex")
@Test
public void testTransmitIndexedAccounts(@Mock AccountSearchRepository accountSearchRepository) throws IndexingException {
List<IndexedAccount> indexedAccounts = new ArrayList<>();
indexedAccounts.add(new IndexedAccount());
when(accountServiceDefaultSpy.convertAccountsIntoIndexedAccounts(Mockito.any())).thenReturn(indexedAccounts);
accountServiceDefaultSpy.reindexAllAccounts();
verify(accountSearchRepository).reindex(indexedAccounts);
}
@DisplayName("When called, should return the number of indexedAccounts it's sent to indexing")
@Test
public void testReturnCorrectNumber() throws IndexingException {
List<IndexedAccount> indexedAccounts = new ArrayList<>();
indexedAccounts.add(new IndexedAccount());
when(accountServiceDefaultSpy.convertAccountsIntoIndexedAccounts(Mockito.any())).thenReturn(indexedAccounts);
assertThat(accountServiceDefaultSpy.reindexAllAccounts()).isEqualTo(indexedAccounts.size());
}
}
}