|
| 1 | +package cat.udl.eps.softarch.demo.steps; |
| 2 | + |
| 3 | +import cat.udl.eps.softarch.demo.domain.Product; |
| 4 | +import cat.udl.eps.softarch.demo.repository.ProductRepository; |
| 5 | +import org.springframework.http.MediaType; |
| 6 | + |
| 7 | +import static org.hamcrest.Matchers.hasSize; |
| 8 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 9 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 12 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 13 | +import io.cucumber.java.Before; |
| 14 | +import io.cucumber.java.en.And; |
| 15 | +import io.cucumber.java.en.When; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.test.web.servlet.MvcResult; |
| 18 | + |
| 19 | +import java.math.BigDecimal; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | + |
| 23 | +public class CreateProductStepDefs { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private StepDefs stepDefs; |
| 27 | + |
| 28 | + public static Product currentProduct; |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private ProductRepository productRepository; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp(){ |
| 35 | + //Mirar si es bona practica ficar un step de el RegisterStepDefs |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Converteix un Map<String, String> en un Product, mapejant només els camps presents. |
| 40 | + */ |
| 41 | + private Product fromMap(Map<String, String> data) { |
| 42 | + Product p = new Product(); |
| 43 | + |
| 44 | + // ⭐ Valores por defecto para campos con validaciones |
| 45 | + p.setStock(0); |
| 46 | + p.setKcal(0); |
| 47 | + p.setCarbs(0); |
| 48 | + p.setProteins(0); |
| 49 | + p.setFats(0); |
| 50 | + p.setAvailable(true); // Disponible por defecto |
| 51 | + p.setPartOfLoyaltyProgram(false); |
| 52 | + |
| 53 | + // Sobrescribir con los datos del test |
| 54 | + data.forEach((key, value) -> { |
| 55 | + if (value == null || value.isBlank()) return; |
| 56 | + switch (key) { |
| 57 | + case "name" -> p.setName(value); |
| 58 | + case "price" -> p.setPrice(new BigDecimal(value)); |
| 59 | + case "stock" -> p.setStock(Integer.parseInt(value)); |
| 60 | + case "barcode" -> p.setBarcode(value); |
| 61 | + case "rating" -> p.setRating(Double.parseDouble(value)); |
| 62 | + case "tax" -> p.setTax(new BigDecimal(value)); |
| 63 | + case "kcal" -> p.setKcal(Integer.parseInt(value)); |
| 64 | + case "carbs" -> p.setCarbs(Integer.parseInt(value)); |
| 65 | + case "proteins" -> p.setProteins(Integer.parseInt(value)); |
| 66 | + case "fats" -> p.setFats(Integer.parseInt(value)); |
| 67 | + case "pointsGiven" -> p.setPointsGiven(Integer.parseInt(value)); |
| 68 | + case "pointsCost" -> p.setPointsCost(Integer.parseInt(value)); |
| 69 | + case "isAvailable" -> p.setAvailable(Boolean.parseBoolean(value)); |
| 70 | + case "isPartOfLoyaltyProgram" -> p.setPartOfLoyaltyProgram(Boolean.parseBoolean(value)); |
| 71 | + case "brand" -> p.setBrand(value); |
| 72 | + case "description" -> p.setDescription(value); |
| 73 | + case "discount" -> p.setDiscount(value); |
| 74 | + case "size" -> p.setSize(value); |
| 75 | + case "promotions" -> p.setPromotions(value); |
| 76 | + default -> System.out.println("⚠️ Campo desconocido: " + key); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + return p; |
| 81 | + } |
| 82 | + |
| 83 | + @When("^I register a new product with the following details:$") |
| 84 | + public void iRegisterANewProductWithDetails(io.cucumber.datatable.DataTable dataTable) throws Exception { |
| 85 | + // Convertir la tabla correctamente (primera fila = headers, segunda = valores) |
| 86 | + Map<String, String> productData = dataTable.asMap(String.class, String.class); |
| 87 | + |
| 88 | + // 🔍 DEBUG: Ver qué recibe de Cucumber |
| 89 | + System.out.println("📋 Datos recibidos de Cucumber:"); |
| 90 | + productData.forEach((k, v) -> System.out.println(" " + k + " = " + v)); |
| 91 | + |
| 92 | + currentProduct = fromMap(productData); |
| 93 | + |
| 94 | + // 🔍 DEBUG |
| 95 | + String jsonBody = stepDefs.mapper.writeValueAsString(currentProduct); |
| 96 | + System.out.println("📤 JSON enviado: " + jsonBody); |
| 97 | + |
| 98 | + var requestBuilder = post("/products") |
| 99 | + .contentType(MediaType.APPLICATION_JSON) |
| 100 | + .content(jsonBody) |
| 101 | + .accept(MediaType.APPLICATION_JSON); |
| 102 | + |
| 103 | + if (AuthenticationStepDefs.currentUsername != null) { |
| 104 | + requestBuilder = requestBuilder.with(AuthenticationStepDefs.authenticate()); |
| 105 | + } |
| 106 | + |
| 107 | + stepDefs.result = stepDefs.mockMvc.perform(requestBuilder).andDo(print()); |
| 108 | + |
| 109 | + // 🔍 DEBUG |
| 110 | + MvcResult mvcresult = stepDefs.result.andReturn(); |
| 111 | + String responseBody = mvcresult.getResponse().getContentAsString(); |
| 112 | + System.out.println("📥 Respuesta: " + responseBody); |
| 113 | + } |
| 114 | + |
| 115 | + @And("^The product with name \"([^\"]*)\" is registered$") |
| 116 | + public void theProductWithNameIsRegistered(String productName) throws Exception { |
| 117 | + |
| 118 | + if (productRepository.findByName(productName).isEmpty()) { |
| 119 | + Product product = new Product(); |
| 120 | + product.setName(productName); |
| 121 | + productRepository.save(product); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + @And("^The product with name \"([^\"]*)\" is not registered$") |
| 127 | + public void theProductWithNameIsNotRegistered(String productName) throws Exception { |
| 128 | + stepDefs.result = stepDefs.mockMvc.perform( |
| 129 | + get("/products/search/findByName") |
| 130 | + .param("name", productName) |
| 131 | + .accept(MediaType.APPLICATION_JSON) |
| 132 | + .with(AuthenticationStepDefs.authenticate())) |
| 133 | + .andExpect(status().isOk()) |
| 134 | + .andExpect(jsonPath("$._embedded.products", hasSize(0))); // Solo 1 |
| 135 | + } |
| 136 | +} |
0 commit comments