Novo repositorio do projeto
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package com.agendaestudantil.servico;
|
||||
|
||||
import com.agendaestudantil.dto.RequisicaoCadastroDTO;
|
||||
import com.agendaestudantil.dto.RespostaEstudanteDTO;
|
||||
import com.agendaestudantil.dto.RequisicaoLoginDTO;
|
||||
import com.agendaestudantil.dto.RespostaLoginDTO;
|
||||
import com.agendaestudantil.entidade.Estudante;
|
||||
import com.agendaestudantil.excecao.ExcecaoNegocio;
|
||||
import com.agendaestudantil.repositorio.EstudanteRepositorio;
|
||||
import com.agendaestudantil.utilitario.UtilJwt;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class EstudanteServicoTest {
|
||||
|
||||
@Mock
|
||||
private EstudanteRepositorio estudanteRepositorio;
|
||||
|
||||
@Mock
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Mock
|
||||
private UtilJwt utilJwt;
|
||||
|
||||
@InjectMocks
|
||||
private EstudanteServico estudanteServico;
|
||||
|
||||
private RequisicaoCadastroDTO requisicaoCadastro;
|
||||
private RequisicaoLoginDTO requisicaoLogin;
|
||||
private Estudante estudante;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
requisicaoCadastro = new RequisicaoCadastroDTO("Teste", "teste@teste.com", "123456", "Curso", 1);
|
||||
requisicaoLogin = new RequisicaoLoginDTO("teste@teste.com", "123456");
|
||||
|
||||
estudante = new Estudante();
|
||||
estudante.setId("1");
|
||||
estudante.setNome("Teste");
|
||||
estudante.setEmail("teste@teste.com");
|
||||
estudante.setSenha("encodedPassword");
|
||||
estudante.setCurso("Curso");
|
||||
estudante.setPeriodo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveCadastrarEstudanteComSucesso() {
|
||||
when(estudanteRepositorio.findByEmail(anyString())).thenReturn(Optional.empty());
|
||||
when(passwordEncoder.encode(anyString())).thenReturn("encodedPassword");
|
||||
when(estudanteRepositorio.save(any(Estudante.class))).thenReturn(estudante);
|
||||
|
||||
RespostaEstudanteDTO resposta = estudanteServico.cadastrar(requisicaoCadastro);
|
||||
|
||||
assertNotNull(resposta);
|
||||
assertEquals("Teste", resposta.nome());
|
||||
verify(estudanteRepositorio, times(1)).save(any(Estudante.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveLancarExcecaoAoCadastrarEmailExistente() {
|
||||
when(estudanteRepositorio.findByEmail(anyString())).thenReturn(Optional.of(estudante));
|
||||
|
||||
assertThrows(ExcecaoNegocio.class, () -> estudanteServico.cadastrar(requisicaoCadastro));
|
||||
verify(estudanteRepositorio, never()).save(any(Estudante.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveRealizarLoginComSucesso() {
|
||||
when(estudanteRepositorio.findByEmail(anyString())).thenReturn(Optional.of(estudante));
|
||||
when(passwordEncoder.matches(anyString(), anyString())).thenReturn(true);
|
||||
when(utilJwt.generateToken(anyString())).thenReturn("token123");
|
||||
|
||||
RespostaLoginDTO resposta = estudanteServico.login(requisicaoLogin);
|
||||
|
||||
assertNotNull(resposta);
|
||||
assertEquals("token123", resposta.token());
|
||||
assertEquals("Teste", resposta.estudante().nome());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveRejeitarSenhaInvalida() {
|
||||
when(estudanteRepositorio.findByEmail(anyString())).thenReturn(Optional.of(estudante));
|
||||
when(passwordEncoder.matches(anyString(), anyString())).thenReturn(false);
|
||||
|
||||
assertThrows(ResponseStatusException.class, () -> estudanteServico.login(requisicaoLogin));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveRejeitarEmailNaoEncontrado() {
|
||||
when(estudanteRepositorio.findByEmail(anyString())).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(ResponseStatusException.class, () -> estudanteServico.login(requisicaoLogin));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user