AccountServiceDefaultTest.java 6.38 KB
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());
        }
    }
}