Skip to content

Commit

Permalink
Update payment methods to Checkout Object (#1242)
Browse files Browse the repository at this point in the history
* Update payment methods to Checkout Object
  • Loading branch information
nashtech-huyphamphu authored Oct 29, 2024
1 parent df23875 commit 54c7bf2
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,4 +41,19 @@ public ResponseEntity<Long> updateCheckoutStatus(@Valid @RequestBody CheckoutSta
public ResponseEntity<CheckoutVm> 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<Void> updatePaymentMethod(@PathVariable final String id,
@Valid @RequestBody
final CheckoutPaymentMethodPutVm checkoutPaymentMethodPutVm) {
checkoutService.updateCheckoutPaymentMethod(id, checkoutPaymentMethodPutVm);
return ResponseEntity.ok().build();
}
}
2 changes: 1 addition & 1 deletion order/src/main/java/com/yas/order/model/Checkout.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 10 additions & 2 deletions order/src/main/java/com/yas/order/service/CheckoutService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.yas.order.viewmodel.checkout;

public record CheckoutPaymentMethodPutVm(String paymentMethodId) {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<CheckoutItemPostVm> getCheckoutItemPostVms() {
CheckoutItemPostVm item1 = new CheckoutItemPostVm(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");

Expand All @@ -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();
}
}

0 comments on commit 54c7bf2

Please sign in to comment.