diff --git a/order/src/main/java/com/yas/order/controller/CheckoutController.java b/order/src/main/java/com/yas/order/controller/CheckoutController.java index c96bd9df60..655638e5a1 100644 --- a/order/src/main/java/com/yas/order/controller/CheckoutController.java +++ b/order/src/main/java/com/yas/order/controller/CheckoutController.java @@ -1,9 +1,16 @@ package com.yas.order.controller; +import com.yas.commonlibrary.constants.ApiConstant; import com.yas.order.service.CheckoutService; +import com.yas.order.viewmodel.ErrorVm; +import com.yas.order.viewmodel.checkout.CheckoutPaymentMethodPutVm; import com.yas.order.viewmodel.checkout.CheckoutPostVm; import com.yas.order.viewmodel.checkout.CheckoutStatusPutVm; import com.yas.order.viewmodel.checkout.CheckoutVm; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -34,4 +41,19 @@ public ResponseEntity updateCheckoutStatus(@Valid @RequestBody CheckoutSta public ResponseEntity getOrderWithItemsById(@PathVariable String id) { return ResponseEntity.ok(checkoutService.getCheckoutPendingStateWithItemsById(id)); } + + @PutMapping("/storefront/checkouts/{id}/payment-method") + @ApiResponses(value = { + @ApiResponse(responseCode = ApiConstant.CODE_200, description = ApiConstant.OK, + content = @Content()), + @ApiResponse(responseCode = ApiConstant.CODE_404, description = ApiConstant.NOT_FOUND, + content = @Content(schema = @Schema(implementation = ErrorVm.class))), + @ApiResponse(responseCode = ApiConstant.CODE_400, description = ApiConstant.BAD_REQUEST, + content = @Content(schema = @Schema(implementation = ErrorVm.class)))}) + public ResponseEntity updatePaymentMethod(@PathVariable final String id, + @Valid @RequestBody + final CheckoutPaymentMethodPutVm checkoutPaymentMethodPutVm) { + checkoutService.updateCheckoutPaymentMethod(id, checkoutPaymentMethodPutVm); + return ResponseEntity.ok().build(); + } } diff --git a/order/src/main/java/com/yas/order/model/Checkout.java b/order/src/main/java/com/yas/order/model/Checkout.java index 9bd240350a..c2c555628f 100644 --- a/order/src/main/java/com/yas/order/model/Checkout.java +++ b/order/src/main/java/com/yas/order/model/Checkout.java @@ -48,7 +48,7 @@ public class Checkout extends AbstractAuditEntity { @SuppressWarnings("unused") private String shipmentMethodId; - @SuppressWarnings("unused") + @Column(name = "payment_method_id") private String paymentMethodId; @SuppressWarnings("unused") diff --git a/order/src/main/java/com/yas/order/service/CheckoutService.java b/order/src/main/java/com/yas/order/service/CheckoutService.java index d5fcc1897c..f8ab6b5169 100644 --- a/order/src/main/java/com/yas/order/service/CheckoutService.java +++ b/order/src/main/java/com/yas/order/service/CheckoutService.java @@ -14,6 +14,7 @@ import com.yas.order.utils.AuthenticationUtils; import com.yas.order.utils.Constants; import com.yas.order.viewmodel.checkout.CheckoutItemVm; +import com.yas.order.viewmodel.checkout.CheckoutPaymentMethodPutVm; import com.yas.order.viewmodel.checkout.CheckoutPostVm; import com.yas.order.viewmodel.checkout.CheckoutStatusPutVm; import com.yas.order.viewmodel.checkout.CheckoutVm; @@ -72,7 +73,7 @@ public CheckoutVm createCheckout(CheckoutPostVm checkoutPostVm) { public CheckoutVm getCheckoutPendingStateWithItemsById(String id) { Checkout checkout = checkoutRepository.findByIdAndCheckoutState(id, CheckoutState.PENDING).orElseThrow(() - -> new NotFoundException(CHECKOUT_NOT_FOUND, id)); + -> new NotFoundException(CHECKOUT_NOT_FOUND, id)); if (!checkout.getCreatedBy().equals(AuthenticationUtils.getCurrentUserId())) { throw new Forbidden(Constants.ErrorCode.FORBIDDEN); @@ -95,10 +96,17 @@ public CheckoutVm getCheckoutPendingStateWithItemsById(String id) { public Long updateCheckoutStatus(CheckoutStatusPutVm checkoutStatusPutVm) { Checkout checkout = checkoutRepository.findById(checkoutStatusPutVm.checkoutId()) - .orElseThrow(() -> new NotFoundException(CHECKOUT_NOT_FOUND, checkoutStatusPutVm.checkoutId())); + .orElseThrow(() -> new NotFoundException(CHECKOUT_NOT_FOUND, checkoutStatusPutVm.checkoutId())); checkout.setCheckoutState(CheckoutState.valueOf(checkoutStatusPutVm.checkoutStatus())); checkoutRepository.save(checkout); Order order = orderService.findOrderByCheckoutId(checkoutStatusPutVm.checkoutId()); return order.getId(); } + + public void updateCheckoutPaymentMethod(String id, CheckoutPaymentMethodPutVm checkoutPaymentMethodPutVm) { + Checkout checkout = checkoutRepository.findById(id) + .orElseThrow(() -> new NotFoundException(CHECKOUT_NOT_FOUND, id)); + checkout.setPaymentMethodId(checkoutPaymentMethodPutVm.paymentMethodId()); + checkoutRepository.save(checkout); + } } diff --git a/order/src/main/java/com/yas/order/viewmodel/checkout/CheckoutPaymentMethodPutVm.java b/order/src/main/java/com/yas/order/viewmodel/checkout/CheckoutPaymentMethodPutVm.java new file mode 100644 index 0000000000..8ff0cd0f0a --- /dev/null +++ b/order/src/main/java/com/yas/order/viewmodel/checkout/CheckoutPaymentMethodPutVm.java @@ -0,0 +1,4 @@ +package com.yas.order.viewmodel.checkout; + +public record CheckoutPaymentMethodPutVm(String paymentMethodId) { +} diff --git a/order/src/test/java/com/yas/order/controller/CheckoutControllerTest.java b/order/src/test/java/com/yas/order/controller/CheckoutControllerTest.java index 0fd13b5140..aea2ace062 100644 --- a/order/src/test/java/com/yas/order/controller/CheckoutControllerTest.java +++ b/order/src/test/java/com/yas/order/controller/CheckoutControllerTest.java @@ -1,7 +1,7 @@ package com.yas.order.controller; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; 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.request.MockMvcRequestBuilders.put; @@ -11,11 +11,8 @@ import com.fasterxml.jackson.databind.ObjectWriter; import com.yas.order.OrderApplication; import com.yas.order.service.CheckoutService; -import com.yas.order.viewmodel.checkout.CheckoutItemPostVm; -import com.yas.order.viewmodel.checkout.CheckoutItemVm; -import com.yas.order.viewmodel.checkout.CheckoutPostVm; -import com.yas.order.viewmodel.checkout.CheckoutStatusPutVm; -import com.yas.order.viewmodel.checkout.CheckoutVm; +import com.yas.order.viewmodel.checkout.*; + import java.math.BigDecimal; import java.util.HashSet; import java.util.List; @@ -102,6 +99,20 @@ void testGetOrderWithItemsById_whenRequestIsValid_thenReturnCheckoutVm() throws .andExpect(MockMvcResultMatchers.content().json(objectWriter.writeValueAsString(response))); } + @Test + void testUpdatePaymentMethod_whenRequestIsValid_thenReturnNoContent() throws Exception { + String id = "123"; + CheckoutPaymentMethodPutVm request = new CheckoutPaymentMethodPutVm("12hgds1"); + + doNothing().when(checkoutService).updateCheckoutPaymentMethod(id, request); + + mockMvc.perform(put("/storefront/checkouts/{id}/payment-method", id) + .contentType(MediaType.APPLICATION_JSON) + .content(objectWriter.writeValueAsString(request))) + .andExpect(status().isOk()); + + verify(checkoutService).updateCheckoutPaymentMethod(id, request); + } private static @NotNull List getCheckoutItemPostVms() { CheckoutItemPostVm item1 = new CheckoutItemPostVm( diff --git a/order/src/test/java/com/yas/order/service/CheckoutServiceTest.java b/order/src/test/java/com/yas/order/service/CheckoutServiceTest.java index 78e5fc94ee..cfa16d5f78 100644 --- a/order/src/test/java/com/yas/order/service/CheckoutServiceTest.java +++ b/order/src/test/java/com/yas/order/service/CheckoutServiceTest.java @@ -2,19 +2,23 @@ import static com.yas.order.utils.SecurityContextUtils.setSubjectUpSecurityContext; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.yas.commonlibrary.exception.Forbidden; +import com.yas.commonlibrary.exception.NotFoundException; import com.yas.order.mapper.CheckoutMapperImpl; import com.yas.order.model.Checkout; import com.yas.order.model.CheckoutItem; import com.yas.order.model.enumeration.CheckoutState; import com.yas.order.repository.CheckoutItemRepository; import com.yas.order.repository.CheckoutRepository; +import com.yas.order.viewmodel.checkout.CheckoutPaymentMethodPutVm; import com.yas.order.viewmodel.checkout.CheckoutPostVm; import java.util.List; import java.util.Optional; @@ -143,7 +147,7 @@ void testGetCheckoutPendingStateWithItemsById_whenNotEqualsCreateBy_throwForbidd .thenReturn(Optional.ofNullable(checkoutCreated)); setSubjectUpSecurityContext("test--by"); - Assertions.assertThrows(Forbidden.class, + assertThrows(Forbidden.class, () -> checkoutService.getCheckoutPendingStateWithItemsById("1"), "You don't have permission to access this page"); @@ -169,4 +173,54 @@ void testGetCheckoutPendingStateWithItemsById_whenNormalCase_returnCheckoutVmWit assertThat(res.checkoutItemVms()).isNull(); } + + @Test + void testUpdateCheckoutPaymentMethod_whenCheckoutExists_thenUpdatePaymentMethod() { + // Arrange + String id = "123"; + Checkout checkout = new Checkout(); + checkout.setId(id); + + CheckoutPaymentMethodPutVm request = new CheckoutPaymentMethodPutVm("new-payment-method-id"); + + when(checkoutRepository.findById(id)).thenReturn(Optional.of(checkout)); + + // Act + checkoutService.updateCheckoutPaymentMethod(id, request); + + // Assert + verify(checkoutRepository).save(checkout); + assertThat(checkout.getPaymentMethodId()).isEqualTo(request.paymentMethodId()); + } + + @Test + void testUpdateCheckoutPaymentMethod_whenCheckoutNotFound_thenThrowNotFoundException() { + // Arrange + String id = "invalid-id"; + CheckoutPaymentMethodPutVm request = new CheckoutPaymentMethodPutVm("new-payment-method-id"); + + when(checkoutRepository.findById(id)).thenReturn(Optional.empty()); + + // Act & Assert + assertThrows(NotFoundException.class, () -> checkoutService.updateCheckoutPaymentMethod(id, request)); + } + + @Test + void testUpdateCheckoutPaymentMethod_whenPaymentMethodIdIsNull_thenDoNotUpdate() { + // Arrange + String id = "123"; + Checkout checkout = new Checkout(); + checkout.setId(id); + + CheckoutPaymentMethodPutVm request = new CheckoutPaymentMethodPutVm(null); + + when(checkoutRepository.findById(id)).thenReturn(Optional.of(checkout)); + + // Act + checkoutService.updateCheckoutPaymentMethod(id, request); + + // Assert + verify(checkoutRepository).save(checkout); + assertThat(checkout.getPaymentMethodId()).isNull(); + } } \ No newline at end of file