Skip to content

Commit

Permalink
[PRDP-289] feat: Updated existing apis and tests for cart management
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-cialini committed Jan 5, 2024
1 parent edfe4c2 commit 3153483
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ public Receipt createCartReceipt(List<BizEvent> bizEventList) {
}

public static BigDecimal getAmount(BizEvent bizEvent) {
if (bizEvent.getTransactionDetails() != null && bizEvent.getTransactionDetails().getTransaction() != null) {
if (bizEvent.getTransactionDetails() != null && bizEvent.getTransactionDetails().getTransaction() != null
&& bizEvent.getTransactionDetails().getTransaction().getGrandTotal() != 0) {
return new BigDecimal(bizEvent.getTransactionDetails().getTransaction().getGrandTotal());
}
if (bizEvent.getPaymentInfo() != null && bizEvent.getPaymentInfo().getAmount() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ public static void tokenizeReceipt(BizEventToReceiptService service, List<BizEve
CartItem.builder()
.payeeName(bizEvent.getCreditor() != null ? bizEvent.getCreditor().getCompanyName() : null)
.subject(getItemSubject(bizEvent))
.build()); });
.build());
});

if (!amount.get().equals(BigDecimal.ZERO)) {
eventData.setAmount(amount.get().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ private BizEvent generateValidBizEvent(String totalNotice){

PaymentInfo paymentInfo = new PaymentInfo();
paymentInfo.setTotalNotice(totalNotice);
paymentInfo.setAmount("102.30");

item.setEventStatus(BizEventStatusType.DONE);
item.setId(EVENT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,51 @@ void requestOnValidBizEventShouldCreateRequest() {
assertEquals(1, captured.getEventData().getCart().size());
}

@Test
@SneakyThrows
void requestOnValidCartShouldCreateRequest() {
when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(DEBTOR_FISCAL_CODE))
.thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE);
when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(PAYER_FISCAL_CODE))
.thenReturn(TOKENIZED_PAYER_FISCAL_CODE);

Response<SendMessageResult> queueResponse = mock(Response.class);
when(queueResponse.getStatusCode()).thenReturn(HttpStatus.CREATED.value());
when(queueClientMock.sendMessageToQueue(anyString())).thenReturn(queueResponse);

when(requestMock.getQueryParameters()).thenReturn(Collections.singletonMap("isCart","true"));

FeedResponse feedResponseMock = mock(FeedResponse.class);
List<BizEvent> receiptList = Collections.singletonList(generateValidBizEvent("1"));
when(feedResponseMock.getResults()).thenReturn(receiptList);
doReturn(Collections.singletonList(feedResponseMock)).when(bizEventCosmosClientMock)
.getAllBizEventDocument(Mockito.eq("a valid id"), any(), any());

when(receiptCosmosServiceMock.getReceipt(EVENT_ID)).thenThrow(ReceiptNotFoundException.class);

doAnswer((Answer<HttpResponseMessage.Builder>) invocation -> {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}).when(requestMock).createResponseBuilder(any(HttpStatus.class));

// test execution
HttpResponseMessage response = assertDoesNotThrow(() -> sut.run(requestMock, EVENT_ID, documentdb, contextMock));

// test assertion
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatus());
assertNotNull(response.getBody());

verify(documentdb).setValue(receiptCaptor.capture());
Receipt captured = receiptCaptor.getValue();
assertEquals(ReceiptStatusType.INSERTED, captured.getStatus());
assertEquals(EVENT_ID, captured.getEventId());
assertEquals(TOKENIZED_PAYER_FISCAL_CODE, captured.getEventData().getPayerFiscalCode());
assertEquals(TOKENIZED_DEBTOR_FISCAL_CODE, captured.getEventData().getDebtorFiscalCode());
assertNotNull(captured.getEventData().getCart());
assertEquals(1, captured.getEventData().getCart().size());
}

@Test
@SneakyThrows
void requestOnValidBizEventTransactionDetailsShouldCreateRequest() {
Expand Down

0 comments on commit 3153483

Please sign in to comment.