Skip to content

Commit

Permalink
[PRDP-289] feat: Updated openapi.json and existing apis for managemen…
Browse files Browse the repository at this point in the history
…t of cart based receipts
  • Loading branch information
alessio-cialini committed Jan 5, 2024
1 parent 20fa16f commit f7f1b9d
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 8 deletions.
18 changes: 18 additions & 0 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,15 @@
"type": "string"
},
"required": true
},
{
"in": "query",
"name": "isCart",
"description": "Boolean to determine if the id refers to a cart",
"schema": {
"type": "string"
},
"required": false
}
],
"responses": {
Expand Down Expand Up @@ -1381,6 +1390,15 @@
"type": "string"
},
"required": true
},
{
"in": "query",
"name": "isCart",
"description": "Boolean to determine if the id refers to a cart",
"schema": {
"type": "string"
},
"required": false
}
],
"requestBody": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.ReceiptCosmosClientImpl;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptError;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptErrorStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson;
import org.slf4j.Logger;
Expand Down Expand Up @@ -68,8 +69,18 @@ public HttpResponseMessage run(
ReceiptError receiptError;

try {
receiptError = receiptCosmosClient.getReceiptError(eventId);
} catch (NoSuchElementException | ReceiptNotFoundException e) {

boolean isCart = Boolean.parseBoolean(request.getQueryParameters().getOrDefault(
"isCart", "false"));

if (isCart) {
receiptError = this.receiptCosmosClient.getReceiptError(
(String) receiptCosmosClient.getCartDocument(eventId).getCartPaymentId().toArray()[0]);
} else {
receiptError = receiptCosmosClient.getReceiptError(eventId);
}

} catch (NoSuchElementException | ReceiptNotFoundException | CartNotFoundException e) {
responseMsg = String.format("No receiptError has been found with bizEventId %s", eventId);
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import com.microsoft.azure.functions.annotation.CosmosDBOutput;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.event.BizEvent;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson;
import it.gov.pagopa.receipt.pdf.helpdesk.service.ReceiptCosmosService;
Expand Down Expand Up @@ -85,8 +87,18 @@ public HttpResponseMessage run(

Receipt receipt;
try {
receipt = this.receiptCosmosService.getReceipt(eventId);
} catch (ReceiptNotFoundException e) {

boolean isCart = Boolean.parseBoolean(request.getQueryParameters().getOrDefault(
"isCart", "false"));

if (isCart) {
receipt = this.receiptCosmosService.getReceipt(
(String) receiptCosmosService.getCart(eventId).getCartPaymentId().toArray()[0]);
} else {
receipt = this.receiptCosmosService.getReceipt(eventId);
}

} catch (ReceiptNotFoundException | CartNotFoundException e) {
String responseMsg = String.format("Unable to retrieve the receipt with eventId %s", eventId);
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,21 @@ public static Receipt getEvent(
bizEvent = bizEventCosmosClient.getBizEventDocument(eventId);
}

if (isBizEventInvalid(bizEvent, context, logger)) {
if (isCart) {
Integer intTotalNotice = Integer.parseInt(bizEvent.getPaymentInfo().getTotalNotice());
if (!intTotalNotice.equals(listCart.size())) {
return null;
}
for (BizEvent event : listCart) {
if (isBizEventInvalid(event, context, logger)) {
return null;
}
}
} else if (isBizEventInvalid(bizEvent, context, logger)) {
return null;
}


if (receipt == null) {
try {
receipt = receiptCosmosService.getReceipt(eventId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package it.gov.pagopa.receipt.pdf.helpdesk;

import com.azure.cosmos.models.FeedResponse;
import com.microsoft.azure.functions.*;
import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.event.BizEvent;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptError;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptErrorStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.model.ReceiptToReviewedRequest;
import it.gov.pagopa.receipt.pdf.helpdesk.util.HttpResponseMessageMock;
Expand All @@ -12,10 +17,11 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;

import java.util.Optional;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
Expand Down Expand Up @@ -63,6 +69,36 @@ void requestWithValidBizEventSaveReceiptErrorInReviewed() throws ReceiptNotFound
assertEquals(ReceiptErrorStatusType.REVIEWED, captured.getStatus());
}

@Test
void requestWithValidCartSaveReceiptErrorInReviewed() throws ReceiptNotFoundException, CartNotFoundException {
doAnswer((Answer<HttpResponseMessage.Builder>) invocation -> {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}).when(request).createResponseBuilder(any(HttpStatus.class));

ReceiptError receiptError = ReceiptError.builder()
.bizEventId(BIZ_EVENT_ID)
.status(ReceiptErrorStatusType.TO_REVIEW)
.build();
when(receiptCosmosClient.getReceiptError(BIZ_EVENT_ID)).thenReturn(receiptError);

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

when(receiptCosmosClient.getCartDocument(any())).thenReturn(generateCart());

function = spy(new ReceiptToReviewed(receiptCosmosClient));

// test execution
AtomicReference<HttpResponseMessage> responseMessage = new AtomicReference<>();
assertDoesNotThrow(() -> responseMessage.set(function.run(request, BIZ_EVENT_ID, documentdb,executionContextMock )));
assertEquals(HttpStatus.OK, responseMessage.get().getStatus());

verify(documentdb).setValue(receiptErrorCaptor.capture());
ReceiptError captured = receiptErrorCaptor.getValue();
assertEquals(BIZ_EVENT_ID, captured.getBizEventId());
assertEquals(ReceiptErrorStatusType.REVIEWED, captured.getStatus());
}

@Test
void requestWithValidBizEventIdButReceiptNotFound() throws ReceiptNotFoundException {
doAnswer((Answer<HttpResponseMessage.Builder>) invocation -> {
Expand Down Expand Up @@ -120,4 +156,14 @@ void requestWithoutEventIdReturnsBadRequest() {

verifyNoInteractions(documentdb);
}

private CartForReceipt generateCart() {
CartForReceipt cart = new CartForReceipt();
cart.setId("1");
cart.setStatus(CartStatusType.FAILED);
cart.setTotalNotice(1);
cart.setCartPaymentId(new HashSet<>(new ArrayList<>(
List.of(new String[]{"valid_biz_event_id"}))));
return cart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.OutputBinding;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReasonError;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson;
import it.gov.pagopa.receipt.pdf.helpdesk.service.ReceiptCosmosService;
Expand All @@ -24,8 +27,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;

import java.util.List;
import java.util.Optional;
import java.util.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -101,6 +103,39 @@ void recoverNotNotifiedReceiptSuccess() throws ReceiptNotFoundException {
assertNull(captured.getReasonErrPayer());
}

@Test
void recoverNotNotifiedCartReceiptSuccess() throws ReceiptNotFoundException, CartNotFoundException {
Receipt receipt = buildReceipt();
when(receiptCosmosServiceMock.getReceipt(EVENT_ID)).thenReturn(receipt);

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

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

when(receiptCosmosServiceMock.getCart(any())).thenReturn(generateCart());

// test execution
HttpResponseMessage response = sut.run(requestMock, EVENT_ID, documentReceipts, executionContextMock);

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

verify(documentReceipts).setValue(receiptCaptor.capture());

assertEquals(1, receiptCaptor.getValue().size());
Receipt captured = receiptCaptor.getValue().get(0);
assertEquals(ReceiptStatusType.GENERATED, captured.getStatus());
assertEquals(EVENT_ID, captured.getEventId());
assertEquals(0, captured.getNotificationNumRetry());
assertNull(captured.getReasonErr());
assertNull(captured.getReasonErrPayer());
}

@Test
void recoverReceiptFailForMissingEventId() {
doAnswer((Answer<HttpResponseMessage.Builder>) invocation -> {
Expand Down Expand Up @@ -217,4 +252,14 @@ private Receipt buildReceipt() {
.notifiedAt(0)
.build();
}

private CartForReceipt generateCart() {
CartForReceipt cart = new CartForReceipt();
cart.setId("1");
cart.setStatus(CartStatusType.FAILED);
cart.setTotalNotice(1);
cart.setCartPaymentId(new HashSet<>(new ArrayList<>(
List.of(new String[]{"eventId"}))));
return cart;
}
}

0 comments on commit f7f1b9d

Please sign in to comment.