Skip to content
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 @@ -30,8 +30,9 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
.requestMatchers(HttpMethod.GET, "/identity").authenticated()
.requestMatchers(HttpMethod.POST, "/users").anonymous()
.requestMatchers(HttpMethod.POST, "/users/*").denyAll()
.requestMatchers(HttpMethod.POST, "/*").authenticated()
.requestMatchers(HttpMethod.POST, "/*/*").authenticated()
.requestMatchers(HttpMethod.POST, "/*").authenticated() // ← Protege POST /products

.requestMatchers(HttpMethod.POST, "/*/*").authenticated()
.requestMatchers(HttpMethod.PUT, "/*/*").authenticated()
.requestMatchers(HttpMethod.PATCH, "/*/*").authenticated()
.requestMatchers(HttpMethod.DELETE, "/*/*").authenticated()
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cat/udl/eps/softarch/demo/domain/Product.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cat.udl.eps.softarch.demo.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import lombok.*;
Expand All @@ -25,6 +26,7 @@ public class Product extends UriEntity<Long>{
@Column(unique = true, nullable = false)
private String name;

@org.hibernate.validator.constraints.Length(max = 100)
@Column(length = 100)
private String description;

Expand All @@ -43,6 +45,7 @@ public class Product extends UriEntity<Long>{
@PositiveOrZero
private BigDecimal tax;

@JsonProperty("available")
private boolean isAvailable;

private String promotions;
Expand All @@ -68,7 +71,8 @@ public class Product extends UriEntity<Long>{

@PositiveOrZero
private Integer pointsCost; // Points needed to redeem this product


@JsonProperty("partOfLoyaltyProgram")
private boolean isPartOfLoyaltyProgram;


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Product;
import cat.udl.eps.softarch.demo.repository.ProductRepository;
import org.springframework.http.MediaType;

import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import io.cucumber.java.Before;
import io.cucumber.java.en.And;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MvcResult;

import java.math.BigDecimal;
import java.util.Map;


public class CreateProductStepDefs {

@Autowired
private StepDefs stepDefs;

public static Product currentProduct;

@Autowired
private ProductRepository productRepository;

@Before
public void setUp(){
//Mirar si es bona practica ficar un step de el RegisterStepDefs
}

/**
* Converteix un Map<String, String> en un Product, mapejant només els camps presents.
*/
private Product fromMap(Map<String, String> data) {
Product p = new Product();

// ⭐ Valores por defecto para campos con validaciones
p.setStock(0);
p.setKcal(0);
p.setCarbs(0);
p.setProteins(0);
p.setFats(0);
p.setAvailable(true); // Disponible por defecto
p.setPartOfLoyaltyProgram(false);

// Sobrescribir con los datos del test
data.forEach((key, value) -> {
if (value == null || value.isBlank()) return;
switch (key) {
case "name" -> p.setName(value);
case "price" -> p.setPrice(new BigDecimal(value));
case "stock" -> p.setStock(Integer.parseInt(value));
case "barcode" -> p.setBarcode(value);
case "rating" -> p.setRating(Double.parseDouble(value));
case "tax" -> p.setTax(new BigDecimal(value));
case "kcal" -> p.setKcal(Integer.parseInt(value));
case "carbs" -> p.setCarbs(Integer.parseInt(value));
case "proteins" -> p.setProteins(Integer.parseInt(value));
case "fats" -> p.setFats(Integer.parseInt(value));
case "pointsGiven" -> p.setPointsGiven(Integer.parseInt(value));
case "pointsCost" -> p.setPointsCost(Integer.parseInt(value));
case "isAvailable" -> p.setAvailable(Boolean.parseBoolean(value));
case "isPartOfLoyaltyProgram" -> p.setPartOfLoyaltyProgram(Boolean.parseBoolean(value));
case "brand" -> p.setBrand(value);
case "description" -> p.setDescription(value);
case "discount" -> p.setDiscount(value);
case "size" -> p.setSize(value);
case "promotions" -> p.setPromotions(value);
default -> System.out.println("⚠️ Campo desconocido: " + key);
}
});

return p;
}

@When("^I register a new product with the following details:$")
public void iRegisterANewProductWithDetails(io.cucumber.datatable.DataTable dataTable) throws Exception {
// Convertir la tabla correctamente (primera fila = headers, segunda = valores)
Map<String, String> productData = dataTable.asMap(String.class, String.class);

// 🔍 DEBUG: Ver qué recibe de Cucumber
System.out.println("📋 Datos recibidos de Cucumber:");
productData.forEach((k, v) -> System.out.println(" " + k + " = " + v));

currentProduct = fromMap(productData);

// 🔍 DEBUG
String jsonBody = stepDefs.mapper.writeValueAsString(currentProduct);
System.out.println("📤 JSON enviado: " + jsonBody);

var requestBuilder = post("/products")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonBody)
.accept(MediaType.APPLICATION_JSON);

if (AuthenticationStepDefs.currentUsername != null) {
requestBuilder = requestBuilder.with(AuthenticationStepDefs.authenticate());
}

stepDefs.result = stepDefs.mockMvc.perform(requestBuilder).andDo(print());

// 🔍 DEBUG
MvcResult mvcresult = stepDefs.result.andReturn();
String responseBody = mvcresult.getResponse().getContentAsString();
System.out.println("📥 Respuesta: " + responseBody);
}

@And("^The product with name \"([^\"]*)\" is registered$")
public void theProductWithNameIsRegistered(String productName) throws Exception {

if (productRepository.findByName(productName).isEmpty()) {
Product product = new Product();
product.setName(productName);
productRepository.save(product);
}

}

@And("^The product with name \"([^\"]*)\" is not registered$")
public void theProductWithNameIsNotRegistered(String productName) throws Exception {
stepDefs.result = stepDefs.mockMvc.perform(
get("/products/search/findByName")
.param("name", productName)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.products", hasSize(0))); // Solo 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Product;
import cat.udl.eps.softarch.demo.repository.ProductRepository;
import io.cucumber.java.en.When;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

@Transactional
public class DeleteProductStepDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
private ProductRepository productRepository;

public static Product currentProduct;

@When("^I delete the product with id \"([^\"]*)\"$")
public void iRequestTheProductWithId(String id) throws Exception {

var requestBuilder = delete("/products/" + id)
.accept(MediaType.APPLICATION_JSON);

if (AuthenticationStepDefs.currentUsername != null) {
requestBuilder = requestBuilder.with(AuthenticationStepDefs.authenticate());
}

stepDefs.result = stepDefs.mockMvc.perform(requestBuilder).andDo(print());

}


}
77 changes: 0 additions & 77 deletions src/test/java/cat/udl/eps/softarch/demo/steps/ProductStepDefs.java

This file was deleted.

Loading