AccountServiceDefaultTest.java 3.21 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.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;

    @BeforeEach
    public void setUp(@Mock AccountRepository accountRepository,
                      @Mock AccountSearchRepository accountSearchRepository) throws Exception {
        accountServiceDefault = Mockito.spy(new AccountServiceDefault(accountRepository, accountSearchRepository));
    }

    @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);
        }

    }
}