-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateLoyaltyStepDefs.java
More file actions
155 lines (132 loc) · 7.02 KB
/
Copy pathCreateLoyaltyStepDefs.java
File metadata and controls
155 lines (132 loc) · 7.02 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package cat.udl.eps.softarch.demo.steps;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
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 cat.udl.eps.softarch.demo.domain.Loyalty;
import cat.udl.eps.softarch.demo.domain.Customer;
import cat.udl.eps.softarch.demo.domain.Business;
import cat.udl.eps.softarch.demo.repository.LoyaltyRepository;
import cat.udl.eps.softarch.demo.repository.CustomerRepository;
import cat.udl.eps.softarch.demo.repository.BusinessRepository;
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.http.MediaType;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.List;
public class CreateLoyaltyStepDefs {
private final StepDefs stepDefs;
private final LoyaltyRepository loyaltyRepository;
private final CustomerRepository customerRepository;
private final BusinessRepository businessRepository;
public CreateLoyaltyStepDefs(StepDefs stepDefs, LoyaltyRepository loyaltyRepository, CustomerRepository customerRepository, BusinessRepository businessRepository) {
this.stepDefs = stepDefs;
this.loyaltyRepository = loyaltyRepository;
this.customerRepository = customerRepository;
this.businessRepository = businessRepository;
}
@Given("There is no loyalty with id {long}")
public void thereIsNoLoyaltyWithId(Long id) {
assertFalse(loyaltyRepository.existsById(id),
"Loyalty with id \"" + id + "\" shouldn't exist");
}
@Given("There is a registered customer with username {string} and password {string} and email {string}")
public void thereIsARegisteredCustomerWithUsernameAndPasswordAndEmail(
String username, String password, String email) {
if (!customerRepository.existsById(username)) {
Customer customer = new Customer();
customer.setId(username);
customer.setEmail(email);
customer.setPhoneNumber("123456789");
customer.setName(username);
customer.setPassword(password);
customer.encodePassword();
customerRepository.save(customer);
}
}
@Given("There is a registered business with id {string} and name {string} and address {string}")
public void thereIsARegisteredBusinessWithIdAndNameAndAddress(String id, String name, String address) {
if (!businessRepository.existsById(id)) {
Business business = new Business();
business.setId(id);
business.setName(name);
business.setAddress(address);
business.setEmail("business" + id + "@example.com");
business.setPassword("password");
business.encodePassword();
businessRepository.save(business);
}
}
@Given("There is a loyalty for customer {string} and business {string} with {int} points")
public void thereIsALoyaltyForCustomerAndBusinessWithPoints(
String customerUsername, String businessId, Integer points) {
Customer customer = customerRepository.findById(customerUsername).orElseThrow();
Business business = businessRepository.findById(businessId).orElseThrow();
List<Loyalty> existing = loyaltyRepository.findByCustomerAndBusiness(customer, business);
if (existing.isEmpty()) {
Loyalty loyalty = new Loyalty();
loyalty.setCustomer(customer);
loyalty.setBusiness(business);
loyalty.setStartDate(ZonedDateTime.now());
loyalty.setAccumulatedPoints(points);
loyaltyRepository.save(loyalty);
}
}
@When("I create a loyalty for customer {string} and business {string} with {int} points")
public void iCreateALoyaltyForCustomerAndBusinessWithPoints(
String customerUsername, String businessId, Integer points) throws Exception {
Customer customer = customerRepository.findByName(customerUsername).getFirst();
Business business = businessRepository.findById(businessId).orElseThrow();
Loyalty loyalty = new Loyalty();
loyalty.setAccumulatedPoints(points);
loyalty.setCustomer(customer);
loyalty.setBusiness(business);
loyalty.setStartDate(ZonedDateTime.now());
stepDefs.result = stepDefs.mockMvc.perform(
post("/loyalties")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(loyalty))
.characterEncoding(StandardCharsets.UTF_8)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}
@When("I retrieve the loyalty with id {long}")
public void iRetrieveTheLoyaltyWithId(Long id) throws Exception {
stepDefs.result = stepDefs.mockMvc.perform(
get("/loyalties/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}
@And("It has been created a loyalty for customer {string} and business {string} with {int} points")
public void itHasBeenCreatedALoyaltyForCustomerAndBusinessWithPoints(
String customerUsername, String businessId, Integer points) throws Exception {
Customer customer = customerRepository.findById(customerUsername).orElseThrow();
Business business = businessRepository.findById(businessId).orElseThrow();
List<Loyalty> loyalties = loyaltyRepository.findByCustomerAndBusiness(customer, business);
assertFalse(loyalties.isEmpty(), "Loyalty should exist");
Loyalty loyalty = loyalties.getFirst();
Long createdLoyaltyId = loyalty.getId();
stepDefs.result = stepDefs.mockMvc.perform(
get("/loyalties/{id}", loyalty.getId())
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print())
.andExpect(jsonPath("$.accumulatedPoints", is(points)));
}
@And("The loyalty has {int} accumulated points")
public void theLoyaltyHasAccumulatedPoints(Integer points) throws Exception {
stepDefs.result.andExpect(jsonPath("$.accumulatedPoints", is(points)));
}
@And("The loyalty does not exist with id {long}")
public void theLoyaltyDoesNotExistWithId(Long id) {
assertFalse(loyaltyRepository.existsById(id),
"Loyalty with id \"" + id + "\" should not exist");
}
}