diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java index 5d97c08..f51e66c 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java @@ -1,10 +1,6 @@ package it.gov.pagopa.receipt.pdf.helpdesk; -import com.azure.cosmos.models.FeedResponse; -import com.microsoft.azure.functions.HttpRequestMessage; -import com.microsoft.azure.functions.HttpResponseMessage; -import com.microsoft.azure.functions.HttpStatus; -import com.microsoft.azure.functions.OutputBinding; +import com.microsoft.azure.functions.*; import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptCosmosClient; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptError; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptErrorStatusType; @@ -19,8 +15,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.stubbing.Answer; -import java.util.ArrayList; -import java.util.List; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; @@ -32,22 +26,19 @@ class ReceiptToReviewedTest { private static final String BIZ_EVENT_ID = "valid_biz_event_id"; - + private final ExecutionContext executionContextMock = mock(ExecutionContext.class); private ReceiptToReviewed function; - @Mock private ReceiptCosmosClient receiptCosmosClient; - @Captor - private ArgumentCaptor> receiptErrorCaptor; + private ArgumentCaptor receiptErrorCaptor; + @Mock + HttpRequestMessage> request; + @SuppressWarnings("unchecked") + OutputBinding documentdb = (OutputBinding) spy(OutputBinding.class); @Test void requestWithValidBizEventSaveReceiptErrorInReviewed() throws ReceiptNotFoundException { - ReceiptToReviewedRequest receiptToReviewedRequest = new ReceiptToReviewedRequest(); - receiptToReviewedRequest.setEventId(BIZ_EVENT_ID); - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest)); - doAnswer((Answer) invocation -> { HttpStatus status = (HttpStatus) invocation.getArguments()[0]; return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); @@ -59,128 +50,21 @@ void requestWithValidBizEventSaveReceiptErrorInReviewed() throws ReceiptNotFound .build(); when(receiptCosmosClient.getReceiptError(BIZ_EVENT_ID)).thenReturn(receiptError); - - function = new ReceiptToReviewed(receiptCosmosClient); - - @SuppressWarnings("unchecked") - OutputBinding> documentdb = (OutputBinding>) spy(OutputBinding.class); + function = spy(new ReceiptToReviewed(receiptCosmosClient)); // test execution AtomicReference responseMessage = new AtomicReference<>(); - assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb))); + 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().get(0); + ReceiptError captured = receiptErrorCaptor.getValue(); assertEquals(BIZ_EVENT_ID, captured.getBizEventId()); assertEquals(ReceiptErrorStatusType.REVIEWED, captured.getStatus()); } - @Test - void requestWithoutBizEventIdSaveMultipleReceiptErrorInReviewed() { - ReceiptToReviewedRequest receiptToReviewedRequest = new ReceiptToReviewedRequest(); - receiptToReviewedRequest.setEventId(null); - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest)); - - doAnswer((Answer) invocation -> { - HttpStatus status = (HttpStatus) invocation.getArguments()[0]; - return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); - }).when(request).createResponseBuilder(any(HttpStatus.class)); - - - FeedResponse feedResponse = mock(FeedResponse.class); - - ReceiptError receiptError1 = ReceiptError.builder() - .bizEventId("1") - .status(ReceiptErrorStatusType.TO_REVIEW) - .build(); - ReceiptError receiptError2 = ReceiptError.builder() - .bizEventId("2") - .status(ReceiptErrorStatusType.TO_REVIEW) - .build(); - List listReceipt = List.of(receiptError1, receiptError2); - when(feedResponse.getResults()).thenReturn(listReceipt); - Iterable> feedResponseIterator = List.of(feedResponse); - - when(receiptCosmosClient.getToReviewReceiptsError(any(), anyInt())).thenReturn(feedResponseIterator); - - function = new ReceiptToReviewed(receiptCosmosClient); - - @SuppressWarnings("unchecked") - OutputBinding> documentdb = (OutputBinding>) spy(OutputBinding.class); - - // test execution - AtomicReference responseMessage = new AtomicReference<>(); - assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb))); - assertEquals(HttpStatus.OK, responseMessage.get().getStatus()); - - verify(documentdb).setValue(receiptErrorCaptor.capture()); - - ReceiptError captured1 = receiptErrorCaptor.getValue().get(0); - assertEquals("1", captured1.getBizEventId()); - assertEquals(ReceiptErrorStatusType.REVIEWED, captured1.getStatus()); - - ReceiptError captured2 = receiptErrorCaptor.getValue().get(1); - assertEquals("2", captured2.getBizEventId()); - assertEquals(ReceiptErrorStatusType.REVIEWED, captured2.getStatus()); - } - - @Test - void requestWithoutRequestBodySaveMultipleReceiptErrorInReviewed() { - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(new ReceiptToReviewedRequest())); - - doAnswer((Answer) invocation -> { - HttpStatus status = (HttpStatus) invocation.getArguments()[0]; - return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); - }).when(request).createResponseBuilder(any(HttpStatus.class)); - - - FeedResponse feedResponse = mock(FeedResponse.class); - - ReceiptError receiptError1 = ReceiptError.builder() - .bizEventId("1") - .status(ReceiptErrorStatusType.TO_REVIEW) - .build(); - ReceiptError receiptError2 = ReceiptError.builder() - .bizEventId("2") - .status(ReceiptErrorStatusType.TO_REVIEW) - .build(); - List listReceipt = List.of(receiptError1, receiptError2); - when(feedResponse.getResults()).thenReturn(listReceipt); - Iterable> feedResponseIterator = List.of(feedResponse); - - when(receiptCosmosClient.getToReviewReceiptsError(any(), anyInt())).thenReturn(feedResponseIterator); - - function = new ReceiptToReviewed(receiptCosmosClient); - - @SuppressWarnings("unchecked") - OutputBinding> documentdb = (OutputBinding>) spy(OutputBinding.class); - - // test execution - AtomicReference responseMessage = new AtomicReference<>(); - assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb))); - assertEquals(HttpStatus.OK, responseMessage.get().getStatus()); - - verify(documentdb).setValue(receiptErrorCaptor.capture()); - - ReceiptError captured1 = receiptErrorCaptor.getValue().get(0); - assertEquals("1", captured1.getBizEventId()); - assertEquals(ReceiptErrorStatusType.REVIEWED, captured1.getStatus()); - - ReceiptError captured2 = receiptErrorCaptor.getValue().get(1); - assertEquals("2", captured2.getBizEventId()); - assertEquals(ReceiptErrorStatusType.REVIEWED, captured2.getStatus()); - } - @Test void requestWithValidBizEventIdButReceiptNotFound() throws ReceiptNotFoundException { - ReceiptToReviewedRequest receiptToReviewedRequest = new ReceiptToReviewedRequest(); - receiptToReviewedRequest.setEventId(BIZ_EVENT_ID); - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest)); - doAnswer((Answer) invocation -> { HttpStatus status = (HttpStatus) invocation.getArguments()[0]; return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); @@ -188,26 +72,18 @@ void requestWithValidBizEventIdButReceiptNotFound() throws ReceiptNotFoundExcept when(receiptCosmosClient.getReceiptError(BIZ_EVENT_ID)).thenThrow(ReceiptNotFoundException.class); - function = new ReceiptToReviewed(receiptCosmosClient); - - @SuppressWarnings("unchecked") - OutputBinding> documentdb = (OutputBinding>) spy(OutputBinding.class); + function = spy(new ReceiptToReviewed(receiptCosmosClient)); // test execution AtomicReference responseMessage = new AtomicReference<>(); - assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb))); + assertDoesNotThrow(() -> responseMessage.set(function.run(request, BIZ_EVENT_ID, documentdb, executionContextMock))); assertEquals(HttpStatus.NOT_FOUND, responseMessage.get().getStatus()); verifyNoInteractions(documentdb); } @Test - void requestWithValidBizEventIdButReceiptWrongStatus() throws ReceiptNotFoundException { - ReceiptToReviewedRequest receiptToReviewedRequest = new ReceiptToReviewedRequest(); - receiptToReviewedRequest.setEventId(BIZ_EVENT_ID); - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest)); - + void requestWithValidBizEventIdButReceiptWrongStatusReturnsInternalServerError() throws ReceiptNotFoundException { doAnswer((Answer) invocation -> { HttpStatus status = (HttpStatus) invocation.getArguments()[0]; return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); @@ -218,47 +94,29 @@ void requestWithValidBizEventIdButReceiptWrongStatus() throws ReceiptNotFoundExc .status(ReceiptErrorStatusType.REQUEUED) .build()); - function = new ReceiptToReviewed(receiptCosmosClient); - - @SuppressWarnings("unchecked") - OutputBinding> documentdb = (OutputBinding>) spy(OutputBinding.class); + function = spy(new ReceiptToReviewed(receiptCosmosClient)); // test execution AtomicReference responseMessage = new AtomicReference<>(); - assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb))); - assertEquals(HttpStatus.BAD_REQUEST, responseMessage.get().getStatus()); + assertDoesNotThrow(() -> responseMessage.set(function.run(request, BIZ_EVENT_ID, documentdb, executionContextMock))); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseMessage.get().getStatus()); verifyNoInteractions(documentdb); } @Test - void requestWithoutRequestBodyDoesNotFindAnyReceiptError() { - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(new ReceiptToReviewedRequest())); - + void requestWithoutEventIdReturnsBadRequest() { doAnswer((Answer) invocation -> { HttpStatus status = (HttpStatus) invocation.getArguments()[0]; return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); }).when(request).createResponseBuilder(any(HttpStatus.class)); - - FeedResponse feedResponse = mock(FeedResponse.class); - - List listReceipt = new ArrayList<>(); - when(feedResponse.getResults()).thenReturn(listReceipt); - Iterable> feedResponseIterator = List.of(feedResponse); - - when(receiptCosmosClient.getToReviewReceiptsError(any(), anyInt())).thenReturn(feedResponseIterator); - - function = new ReceiptToReviewed(receiptCosmosClient); - - @SuppressWarnings("unchecked") - OutputBinding> documentdb = (OutputBinding>) spy(OutputBinding.class); + function = spy(new ReceiptToReviewed(receiptCosmosClient)); // test execution AtomicReference responseMessage = new AtomicReference<>(); - assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb))); - assertEquals(HttpStatus.OK, responseMessage.get().getStatus()); + assertDoesNotThrow(() -> responseMessage.set(function.run(request, null, documentdb, executionContextMock))); + assertEquals(HttpStatus.BAD_REQUEST, responseMessage.get().getStatus()); verifyNoInteractions(documentdb); }