|
| 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 | +} |
0 commit comments