Refactor: Pacotes em Português, Documentação da API e limpeza de arquivos
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package com.agendaestudantil.servico;
|
||||
|
||||
import com.agendaestudantil.dto.RequisicaoTarefaDTO;
|
||||
import com.agendaestudantil.dto.RespostaTarefaDTO;
|
||||
import com.agendaestudantil.entidade.Tarefa;
|
||||
import com.agendaestudantil.excecao.ExcecaoRecursoNaoEncontrado;
|
||||
import com.agendaestudantil.repositorio.TarefaRepositorio;
|
||||
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.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
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 TarefaServicoTest {
|
||||
|
||||
@Mock
|
||||
private TarefaRepositorio tarefaRepositorio;
|
||||
|
||||
@Mock
|
||||
private SecurityContext securityContext;
|
||||
|
||||
@Mock
|
||||
private Authentication authentication;
|
||||
|
||||
@InjectMocks
|
||||
private TarefaServico tarefaServico;
|
||||
|
||||
private RequisicaoTarefaDTO requisicaoTarefa;
|
||||
private Tarefa tarefa;
|
||||
private final String estudanteId = "estudante123";
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
requisicaoTarefa = new RequisicaoTarefaDTO(
|
||||
"Estudar Spring",
|
||||
"Aprender Spring Boot 3",
|
||||
Tarefa.Prioridade.ALTA,
|
||||
Tarefa.StatusTarefa.PENDENTE,
|
||||
LocalDate.now(),
|
||||
null,
|
||||
estudanteId);
|
||||
|
||||
tarefa = new Tarefa();
|
||||
tarefa.setId("tarefa1");
|
||||
tarefa.setTitulo("Estudar Spring");
|
||||
tarefa.setDescricao("Aprender Spring Boot 3");
|
||||
tarefa.setPrioridade(Tarefa.Prioridade.ALTA);
|
||||
tarefa.setStatus(Tarefa.StatusTarefa.PENDENTE);
|
||||
tarefa.setDataEntrega(LocalDate.now());
|
||||
tarefa.setEstudanteId(estudanteId);
|
||||
}
|
||||
|
||||
private void mockAuthentication(String user) {
|
||||
when(securityContext.getAuthentication()).thenReturn(authentication);
|
||||
when(authentication.getName()).thenReturn(user);
|
||||
SecurityContextHolder.setContext(securityContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveCriarTarefaComSucesso() {
|
||||
mockAuthentication(estudanteId);
|
||||
when(tarefaRepositorio.save(any(Tarefa.class))).thenReturn(tarefa);
|
||||
|
||||
RespostaTarefaDTO resposta = tarefaServico.criarTarefa(requisicaoTarefa);
|
||||
|
||||
assertNotNull(resposta);
|
||||
assertEquals("Estudar Spring", resposta.titulo());
|
||||
verify(tarefaRepositorio, times(1)).save(any(Tarefa.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveLancarExcecaoAoCriarTarefaParaOutroUsuario() {
|
||||
mockAuthentication("outroEstudante");
|
||||
|
||||
assertThrows(ResponseStatusException.class, () -> tarefaServico.criarTarefa(requisicaoTarefa));
|
||||
verify(tarefaRepositorio, never()).save(any(Tarefa.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveListarTarefasPorEstudanteComSucesso() {
|
||||
mockAuthentication(estudanteId);
|
||||
when(tarefaRepositorio.findByEstudanteId(estudanteId)).thenReturn(List.of(tarefa));
|
||||
|
||||
List<RespostaTarefaDTO> tarefas = tarefaServico.listarTarefasPorEstudante(estudanteId);
|
||||
|
||||
assertFalse(tarefas.isEmpty());
|
||||
assertEquals(1, tarefas.size());
|
||||
assertEquals("Estudar Spring", tarefas.get(0).titulo());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveMudarStatusTarefaParaConcluidoComSucesso() {
|
||||
mockAuthentication(estudanteId);
|
||||
when(tarefaRepositorio.findById(anyString())).thenReturn(Optional.of(tarefa));
|
||||
when(tarefaRepositorio.save(any(Tarefa.class))).thenReturn(tarefa);
|
||||
|
||||
RespostaTarefaDTO resposta = tarefaServico.marcarConcluida("tarefa1");
|
||||
|
||||
assertNotNull(resposta);
|
||||
verify(tarefaRepositorio, times(1)).save(any(Tarefa.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deveLancarExcecaoQuandoTarefaNaoEncontrada() {
|
||||
when(tarefaRepositorio.findById(anyString())).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(ExcecaoRecursoNaoEncontrado.class, () -> tarefaServico.buscarTarefaPorId("tarefaNaoExistente"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user