Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jordisilvazup/4abd706ab95132d0c053617f837c9a12 to your computer and use it in GitHub Desktop.
Save jordisilvazup/4abd706ab95132d0c053617f837c9a12 to your computer and use it in GitHub Desktop.
package br.com.zup.edu.marketplace.controller;
import br.com.zup.edu.marketplace.model.Produto;
import br.com.zup.edu.marketplace.repository.ProdutoRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import static br.com.zup.edu.marketplace.model.StatusProduto.*;
import static java.nio.charset.StandardCharsets.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@ActiveProfiles("test")
@AutoConfigureMockMvc
class InativarProdutosPresentesControllerTest {
@Autowired
private ProdutoRepository repository;
@Autowired
private ObjectMapper mapper;
@Autowired
private MockMvc mockMvc;
private static final LocalDateTime HOJE = LocalDateTime.now();
private static final LocalDateTime ONTEM = HOJE.minusDays(1);
private static final LocalDateTime AMANHA = HOJE.plusDays(1);
@BeforeEach
void setUp() {
repository.deleteAll();
}
@Test
@DisplayName("deve inativar os produtos pendentes a mais de 24 horas do sistema")
void test() throws Exception {
String descricao = "Videogame 2 controle";
Produto play2 = new Produto("PlayStation 2", descricao, new BigDecimal("250"), PENDENTE, AMANHA);
Produto play3 = new Produto("PlayStation 3", descricao, new BigDecimal("540"), ATIVO, HOJE);
Produto play4 = new Produto("PlayStation 4", descricao, new BigDecimal("2540"), PENDENTE, ONTEM);
Produto xbox = new Produto("xBox One", descricao, new BigDecimal("2240"), PENDENTE, HOJE);
Produto play5 = new Produto("PlayStation 4", descricao, new BigDecimal("2540"), CADASTRADO, HOJE);
List<Produto> videogames = List.of(play2, play3, play4, play5, xbox);
repository.saveAll(videogames);
MockHttpServletRequestBuilder request = post("/produtos/inativar")
.contentType(MediaType.APPLICATION_JSON);
String payload = mockMvc.perform(request)
.andExpect(
status().isOk()
)
.andReturn()
.getResponse()
.getContentAsString(UTF_8);
TypeFactory typeFactory = mapper.getTypeFactory();
List<Long> idDosProdutosAtualizados = mapper.readValue(
payload,
typeFactory.constructCollectionType(List.class, Long.class)
);
List<Long> idsEsperados = List.of(play4.getId(), xbox.getId());
assertEquals(idsEsperados, idDosProdutosAtualizados);
List<Produto> produtosAtualizados = repository.findAllById(idDosProdutosAtualizados);
produtosAtualizados.forEach(produto -> {
assertEquals(INATIVO, produto.getStatus());
});
assertEquals(play2.getStatus(), repository.findById(play2.getId()).get().getStatus());
assertEquals(play3.getStatus(), repository.findById(play3.getId()).get().getStatus());
assertEquals(play5.getStatus(), repository.findById(play5.getId()).get().getStatus());
}
@Test
@DisplayName("nao ha produtos para serem inativados no sistema")
void test1() throws Exception {
String descricao = "Videogame 2 controle";
Produto play2 = new Produto("PlayStation 2", descricao, new BigDecimal("250"), PENDENTE, AMANHA);
Produto play3 = new Produto("PlayStation 3", descricao, new BigDecimal("540"), ATIVO, HOJE);
List<Produto> videogames = List.of(play2, play3);
repository.saveAll(videogames);
MockHttpServletRequestBuilder request = post("/produtos/inativar")
.contentType(MediaType.APPLICATION_JSON);
String payload = mockMvc.perform(request)
.andExpect(
status().isOk()
)
.andReturn()
.getResponse()
.getContentAsString(UTF_8);
TypeFactory typeFactory = mapper.getTypeFactory();
List<Long> idDosProdutosAtualizados = mapper.readValue(
payload,
typeFactory.constructCollectionType(List.class, Long.class)
);
assertTrue(idDosProdutosAtualizados.isEmpty(), "Nao deve haver produtos atualizados");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment