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