Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tests in UsuarioMedicamentoService and controller #70

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public MedicamentoRequest(Long medicamentoId, String dataInicial, String frequen
this.quantidadeInicialEstoque = quantidadeInicialEstoque;
}

public MedicamentoRequest() {
}

public Long getMedicamentoId() {
return this.medicamentoId;
}
Expand All @@ -38,6 +41,10 @@ public Integer getQuantidadeInicialEstoque() {
return this.quantidadeInicialEstoque;
}

public void setMedicamentoId(Long medicamentoId) {
this.medicamentoId = medicamentoId;
}

public void setDataInicial(String dataInicial) {
this.dataInicial = dataInicial;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class UsuarioEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Long id;

@Column(unique = true)
private String nome;
Expand All @@ -43,7 +43,7 @@ public UsuarioEntity(String nome, String email, String senha) {
this.senha = senha;
}

public Integer getId() {
public Long getId() {
return id;
}

Expand Down Expand Up @@ -71,6 +71,10 @@ public LocalDate getDataNascimento() {
return dataNascimento;
}

public void setId(Long id) {
this.id = id;
}

public void setNome(String nome) {
this.nome = nome;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public Integer getQuantidadeInicialEstoque() {
return quantidadeInicialEstoque;
}

public void setId(Long id) {
this.id = id;
}

public void setUsuario(UsuarioEntity usuario) {
this.usuario = usuario;
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ spring.application.name=remedius
spring.datasource.url=jdbc:postgresql://0.0.0.0:5432/remedius_db
spring.datasource.username=remedius
spring.datasource.password=remedius123

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

package com.remedius.remedius.controller;
import com.remedius.remedius.DTOs.MedicamentoRequest;
import com.remedius.remedius.entities.UsuarioMedicamentoEntity;
import com.remedius.remedius.service.UsuarioMedicamentoService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.Arrays;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;





public class UsuarioMedicamentoControllerTest {

private MockMvc mockMvc;

@Mock
private UsuarioMedicamentoService usuarioMedicamentoService;

@InjectMocks
private UsuarioMedicamentoController usuarioMedicamentoController;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(usuarioMedicamentoController).build();
}

@Test
public void testGetUserMedications() throws Exception {
List<UsuarioMedicamentoEntity> medicacoes = Arrays.asList(new UsuarioMedicamentoEntity(), new UsuarioMedicamentoEntity());
when(usuarioMedicamentoService.buscarMedicamentosDoUsuario(anyInt())).thenReturn(medicacoes);

mockMvc.perform(get("/usuarios-medicamentos/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(medicacoes.size()));
}

@Test
public void testAddMedicationToUser() throws Exception {
UsuarioMedicamentoEntity relacao = new UsuarioMedicamentoEntity();
when(usuarioMedicamentoService.adicionarMedicamentoAoUsuario(anyInt(), any(MedicamentoRequest.class))).thenReturn(relacao);

mockMvc.perform(post("/usuarios-medicamentos/1")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"medicamentoId\":1,\"dataInicial\":\"2023-01-01\",\"frequencia\":\"diaria\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(relacao.getId()));
}

@Test
public void testUpdateMedication() throws Exception {
UsuarioMedicamentoEntity relacao = new UsuarioMedicamentoEntity();
when(usuarioMedicamentoService.atualizarMedicamentoDoUsuario(anyInt(), any(MedicamentoRequest.class))).thenReturn(relacao);

mockMvc.perform(put("/usuarios-medicamentos/1")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"medicamentoId\":1,\"dataInicial\":\"2023-01-01\",\"frequencia\":\"diaria\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(relacao.getId()));
}

@Test
public void testRemoveMedicationToUser() throws Exception {
mockMvc.perform(delete("/usuarios-medicamentos/1"))
.andExpect(status().isNoContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.remedius.remedius.service;
import com.remedius.remedius.DTOs.MedicamentoRequest;
import com.remedius.remedius.entities.*;
import com.remedius.remedius.repository.UsuarioMedicamentoRepository;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;






@ExtendWith(MockitoExtension.class)
public class UsuarioMedicamentoServiceTest {

@InjectMocks
private UsuarioMedicamentoService usuarioMedicamentoService;

@Mock
private UsuarioMedicamentoRepository usuarioMedicamentoRepository;

@Mock
private UsuarioService usuarioService;

@Mock
private MedicamentoService medicamentoService;

private UsuarioEntity usuario;
private MedicamentoEntity medicamento;
private UsuarioMedicamentoEntity usuarioMedicamento;
private MedicamentoRequest medicamentoRequest;

@BeforeEach
public void setUp() {
usuario = new UsuarioEntity();
usuario.setId(1L);

medicamento = new MedicamentoEntity();
medicamento.setId(1L);

usuarioMedicamento = new UsuarioMedicamentoEntity();
usuarioMedicamento.setId(1L);
usuarioMedicamento.setUsuario(usuario);
usuarioMedicamento.setMedicamento(medicamento);

medicamentoRequest = new MedicamentoRequest();
medicamentoRequest.setMedicamentoId(1L);
medicamentoRequest.setDataInicial("2023-01-01");
medicamentoRequest.setFrequencia("daily");
medicamentoRequest.setDosagem(1.0);
medicamentoRequest.setQuantidadeInicialEstoque(10);
}

@Test
public void testAdicionarMedicamentoAoUsuario() {
when(usuarioService.getUsuarioById(1)).thenReturn(usuario);
when(medicamentoService.getMedicamentoById(1L)).thenReturn(medicamento);
when(usuarioMedicamentoRepository.save(any(UsuarioMedicamentoEntity.class))).thenReturn(usuarioMedicamento);

UsuarioMedicamentoEntity result = usuarioMedicamentoService.adicionarMedicamentoAoUsuario(1, medicamentoRequest);

// printar o result
System.out.println(result);

assertNotNull(result);
assertEquals(usuario, result.getUsuario());
assertEquals(medicamento, result.getMedicamento());
verify(usuarioMedicamentoRepository, times(1)).save(any(UsuarioMedicamentoEntity.class));
}

@Test
public void testRemoverMedicamentoDoUsuario() {
doNothing().when(usuarioMedicamentoRepository).deleteById(1);

usuarioMedicamentoService.removerMedicamentoDoUsuario(1);

verify(usuarioMedicamentoRepository, times(1)).deleteById(1);
}

@Test
public void testBuscarMedicamentosDoUsuario() {
List<UsuarioMedicamentoEntity> lista = new ArrayList<>();
lista.add(usuarioMedicamento);

when(usuarioMedicamentoRepository.findByUsuarioId(1)).thenReturn(lista);

List<UsuarioMedicamentoEntity> result = usuarioMedicamentoService.buscarMedicamentosDoUsuario(1);

assertNotNull(result);
assertEquals(1, result.size());
assertEquals(usuarioMedicamento, result.get(0));
verify(usuarioMedicamentoRepository, times(1)).findByUsuarioId(1);
}

@Test
public void testAtualizarMedicamentoDoUsuario() {
when(usuarioMedicamentoRepository.findById(1)).thenReturn(Optional.of(usuarioMedicamento));
when(usuarioMedicamentoRepository.save(any(UsuarioMedicamentoEntity.class))).thenReturn(usuarioMedicamento);

UsuarioMedicamentoEntity result = usuarioMedicamentoService.atualizarMedicamentoDoUsuario(1, medicamentoRequest);

assertNotNull(result);
assertEquals("2023-01-01", result.getDataInicial());
assertEquals("daily", result.getFrequencia());
assertEquals(1.0, result.getDosagem());
assertEquals(10, result.getQuantidadeInicialEstoque());
verify(usuarioMedicamentoRepository, times(1)).save(any(UsuarioMedicamentoEntity.class));
}
}