Skip to content

Commit 9cf5c0c

Browse files
authored
Merge pull request #63 from UdL-EPS-SoftArch/customer
customer
2 parents 8faf75b + 75dcf7c commit 9cf5c0c

8 files changed

Lines changed: 124 additions & 10 deletions

File tree

src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
3131
.requestMatchers(HttpMethod.GET, "/businesses", "/businesses/**").permitAll()
3232
.requestMatchers(HttpMethod.GET, "/products", "/products/**").permitAll() // <-- NUEVO: Ver productos es público
3333
.requestMatchers(HttpMethod.POST, "/users").anonymous()
34-
.requestMatchers(HttpMethod.POST, "/customers").anonymous()
34+
.requestMatchers(HttpMethod.POST, "/customers").permitAll()
3535

3636
// Endpoints Bloqueados Específicamente
3737
.requestMatchers(HttpMethod.POST, "/users/*").denyAll()

src/main/java/cat/udl/eps/softarch/demo/domain/Customer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class Customer extends User {
2525
@NotEmpty
2626
private String phoneNumber;
2727

28+
2829
@Override
2930
@JsonValue(value = false)
3031
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@@ -40,4 +41,4 @@ public Collection<? extends GrantedAuthority> getAuthorities() {
4041
//TODO
4142
// @OneToOne
4243
// private Loyalty loyalty;
43-
}
44+
}

src/main/java/cat/udl/eps/softarch/demo/domain/Loyalty.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cat.udl.eps.softarch.demo.domain;
22

3+
import com.fasterxml.jackson.annotation.JsonIdentityReference;
34
import jakarta.persistence.*;
45
import jakarta.validation.constraints.NotNull;
56
import jakarta.validation.constraints.PositiveOrZero;
@@ -25,10 +26,12 @@ public class Loyalty extends UriEntity<Long> {
2526

2627
@NotNull
2728
@ManyToOne
29+
@JsonIdentityReference(alwaysAsId = true)
2830
private Customer customer;
2931

3032
@NotNull
3133
@ManyToOne
34+
@JsonIdentityReference(alwaysAsId = true)
3235
private Business business;
3336

3437
@NotNull

src/main/java/cat/udl/eps/softarch/demo/handler/CustomerEventHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public CustomerEventHandler(CustomerRepository customerRepository) {
3333
@HandleBeforeCreate
3434
public void handleCustomerPreCreate(Customer customer) {
3535
logger.info("Before creating: {}", customer.toString());
36+
// Codificar password ANTES de crear
37+
if (customer.getPassword() != null && !customer.getPassword().isEmpty()) {
38+
customer.encodePassword();
39+
}
3640
}
3741

3842
@HandleBeforeSave

src/test/java/cat/udl/eps/softarch/demo/steps/CreateLoyaltyStepDefs.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ public void thereIsALoyaltyForCustomerAndBusinessWithPoints(
9292
@When("I create a loyalty for customer {string} and business {string} with {int} points")
9393
public void iCreateALoyaltyForCustomerAndBusinessWithPoints(
9494
String customerUsername, String businessId, Integer points) throws Exception {
95-
Customer customer = customerRepository.findById(customerUsername).orElseThrow();
95+
Customer customer = customerRepository.findByName(customerUsername).getFirst();
9696
Business business = businessRepository.findById(businessId).orElseThrow();
9797

98-
JSONObject loyalty = new JSONObject();
99-
loyalty.put("startDate", ZonedDateTime.now().toString());
100-
loyalty.put("accumulatedPoints", points);
101-
loyalty.put("customer", "/customers/" + customerUsername);
102-
loyalty.put("business", "/businesses/" + businessId);
98+
Loyalty loyalty = new Loyalty();
99+
loyalty.setAccumulatedPoints(points);
100+
loyalty.setCustomer(customer);
101+
loyalty.setBusiness(business);
102+
loyalty.setStartDate(ZonedDateTime.now());
103103

104104
stepDefs.result = stepDefs.mockMvc.perform(
105105
post("/loyalties")
106106
.contentType(MediaType.APPLICATION_JSON)
107-
.content(loyalty.toString())
107+
.content(stepDefs.mapper.writeValueAsString(loyalty))
108108
.characterEncoding(StandardCharsets.UTF_8)
109109
.accept(MediaType.APPLICATION_JSON)
110110
.with(AuthenticationStepDefs.authenticate()))
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cat.udl.eps.softarch.demo.steps;
2+
3+
import cat.udl.eps.softarch.demo.domain.Business;
4+
import cat.udl.eps.softarch.demo.domain.Customer;
5+
import cat.udl.eps.softarch.demo.domain.Loyalty;
6+
import cat.udl.eps.softarch.demo.repository.BusinessRepository;
7+
import cat.udl.eps.softarch.demo.repository.CustomerRepository;
8+
import cat.udl.eps.softarch.demo.repository.LoyaltyRepository;
9+
import io.cucumber.java.en.And;
10+
import io.cucumber.java.en.Given;
11+
import io.cucumber.java.en.When;
12+
import org.json.JSONObject;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.http.MediaType;
15+
16+
import java.nio.charset.StandardCharsets;
17+
import java.util.List;
18+
19+
import static org.hamcrest.Matchers.is;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
23+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
26+
27+
public class UpdateLoyaltyStepDefs {
28+
29+
@Autowired
30+
private StepDefs stepDefs;
31+
32+
@Autowired
33+
private LoyaltyRepository loyaltyRepository;
34+
35+
@Autowired
36+
private CustomerRepository customerRepository;
37+
38+
@Autowired
39+
private BusinessRepository businessRepository;
40+
41+
private Long loyaltyId;
42+
43+
@When("I update the loyalty for customer {string} and business {string} to have {int} points")
44+
public void iUpdateTheLoyaltyForCustomerAndBusinessToHavePoints(
45+
String customerUsername, String businessUsername, Integer newPoints) throws Exception {
46+
47+
Customer customer = customerRepository.findById(customerUsername).orElseThrow();
48+
Business business = businessRepository.findById(businessUsername).orElseThrow();
49+
50+
List<Loyalty> loyalties = loyaltyRepository.findByCustomerAndBusiness(customer, business);
51+
assertFalse(loyalties.isEmpty(), "Loyalty should exist before updating");
52+
Loyalty loyalty = loyalties.getFirst();
53+
loyaltyId = loyalty.getId();
54+
55+
JSONObject loyaltyUpdate = new JSONObject();
56+
loyaltyUpdate.put("accumulatedPoints", newPoints);
57+
58+
stepDefs.result = stepDefs.mockMvc.perform(
59+
patch("/loyalties/{id}", loyaltyId)
60+
.contentType(MediaType.APPLICATION_JSON)
61+
.content(loyaltyUpdate.toString())
62+
.characterEncoding(StandardCharsets.UTF_8)
63+
.accept(MediaType.APPLICATION_JSON)
64+
.with(AuthenticationStepDefs.authenticate()))
65+
.andDo(print());
66+
}
67+
68+
@When("I update the loyalty with id {long} to have {int} points")
69+
public void iUpdateTheLoyaltyWithIdToHavePoints(Long id, Integer newPoints) throws Exception {
70+
JSONObject loyaltyUpdate = new JSONObject();
71+
loyaltyUpdate.put("accumulatedPoints", newPoints);
72+
73+
stepDefs.result = stepDefs.mockMvc.perform(
74+
patch("/loyalties/{id}", id)
75+
.contentType(MediaType.APPLICATION_JSON)
76+
.content(loyaltyUpdate.toString())
77+
.characterEncoding(StandardCharsets.UTF_8)
78+
.accept(MediaType.APPLICATION_JSON)
79+
.with(AuthenticationStepDefs.authenticate()))
80+
.andDo(print());
81+
}
82+
}

src/test/resources/features/RegisterCustomer.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Feature: Register Customer
1414
Given There is a registered customer with username "customer1" and password "password" and email "customer1@example.com" and phoneNumber "123456789"
1515
And I'm not logged in
1616
When I register a new customer with username "customer1" and password "newpass" and email "customer2@example.com" and phoneNumber "987654321"
17-
Then The response code is 400
17+
Then The response code is 409
1818

1919
Scenario: Register customer without username
2020
Given I'm not logged in
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Feature: Update Loyalty
2+
In order to manage loyalty program progress
3+
As a business
4+
I want to update existing loyalty records
5+
6+
Background:
7+
Given There is a registered customer with username "customer1" and password "password" and email "customer1@example.com"
8+
And There is a registered business with id "testbusiness" and name "Test Business" and address "123 Main St"
9+
And There is a loyalty for customer "customer1" and business "testbusiness" with 50 points
10+
And I login as "customer1" with password "password"
11+
12+
Scenario: Update loyalty points successfully
13+
When I update the loyalty for customer "customer1" and business "testbusiness" to have 120 points
14+
Then The response code is 200
15+
And The loyalty has 120 accumulated points
16+
17+
Scenario: Update loyalty points to a negative value fails
18+
When I update the loyalty for customer "customer1" and business "testbusiness" to have -30 points
19+
Then The response code is 400
20+
21+
Scenario: Update non-existent loyalty fails
22+
Given There is no loyalty with id 999
23+
When I update the loyalty with id 999 to have 200 points
24+
Then The response code is 404

0 commit comments

Comments
 (0)