Skip to content

Commit

Permalink
[PRDP-252] Updated unit test ReceiptToReviewed after refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
svariant committed Dec 1, 2023
1 parent 5b8e4a1 commit f05dbfd
Showing 1 changed file with 20 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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<List<ReceiptError>> receiptErrorCaptor;
private ArgumentCaptor<ReceiptError> receiptErrorCaptor;
@Mock
HttpRequestMessage<Optional<String>> request;
@SuppressWarnings("unchecked")
OutputBinding<ReceiptError> documentdb = (OutputBinding<ReceiptError>) spy(OutputBinding.class);

@Test
void requestWithValidBizEventSaveReceiptErrorInReviewed() throws ReceiptNotFoundException {
ReceiptToReviewedRequest receiptToReviewedRequest = new ReceiptToReviewedRequest();
receiptToReviewedRequest.setEventId(BIZ_EVENT_ID);
HttpRequestMessage<Optional<ReceiptToReviewedRequest>> request = mock(HttpRequestMessage.class);
when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest));

doAnswer((Answer<HttpResponseMessage.Builder>) invocation -> {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
Expand All @@ -59,155 +50,40 @@ void requestWithValidBizEventSaveReceiptErrorInReviewed() throws ReceiptNotFound
.build();
when(receiptCosmosClient.getReceiptError(BIZ_EVENT_ID)).thenReturn(receiptError);


function = new ReceiptToReviewed(receiptCosmosClient);

@SuppressWarnings("unchecked")
OutputBinding<List<ReceiptError>> documentdb = (OutputBinding<List<ReceiptError>>) spy(OutputBinding.class);
function = spy(new ReceiptToReviewed(receiptCosmosClient));

// test execution
AtomicReference<HttpResponseMessage> 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<Optional<ReceiptToReviewedRequest>> request = mock(HttpRequestMessage.class);
when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest));

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


FeedResponse<ReceiptError> 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<ReceiptError> listReceipt = List.of(receiptError1, receiptError2);
when(feedResponse.getResults()).thenReturn(listReceipt);
Iterable<FeedResponse<ReceiptError>> feedResponseIterator = List.of(feedResponse);

when(receiptCosmosClient.getToReviewReceiptsError(any(), anyInt())).thenReturn(feedResponseIterator);

function = new ReceiptToReviewed(receiptCosmosClient);

@SuppressWarnings("unchecked")
OutputBinding<List<ReceiptError>> documentdb = (OutputBinding<List<ReceiptError>>) spy(OutputBinding.class);

// test execution
AtomicReference<HttpResponseMessage> 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<Optional<ReceiptToReviewedRequest>> request = mock(HttpRequestMessage.class);
when(request.getBody()).thenReturn(Optional.of(new ReceiptToReviewedRequest()));

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


FeedResponse<ReceiptError> 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<ReceiptError> listReceipt = List.of(receiptError1, receiptError2);
when(feedResponse.getResults()).thenReturn(listReceipt);
Iterable<FeedResponse<ReceiptError>> feedResponseIterator = List.of(feedResponse);

when(receiptCosmosClient.getToReviewReceiptsError(any(), anyInt())).thenReturn(feedResponseIterator);

function = new ReceiptToReviewed(receiptCosmosClient);

@SuppressWarnings("unchecked")
OutputBinding<List<ReceiptError>> documentdb = (OutputBinding<List<ReceiptError>>) spy(OutputBinding.class);

// test execution
AtomicReference<HttpResponseMessage> 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<Optional<ReceiptToReviewedRequest>> request = mock(HttpRequestMessage.class);
when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest));

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

when(receiptCosmosClient.getReceiptError(BIZ_EVENT_ID)).thenThrow(ReceiptNotFoundException.class);

function = new ReceiptToReviewed(receiptCosmosClient);

@SuppressWarnings("unchecked")
OutputBinding<List<ReceiptError>> documentdb = (OutputBinding<List<ReceiptError>>) spy(OutputBinding.class);
function = spy(new ReceiptToReviewed(receiptCosmosClient));

// test execution
AtomicReference<HttpResponseMessage> 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<Optional<ReceiptToReviewedRequest>> request = mock(HttpRequestMessage.class);
when(request.getBody()).thenReturn(Optional.of(receiptToReviewedRequest));

void requestWithValidBizEventIdButReceiptWrongStatusReturnsInternalServerError() throws ReceiptNotFoundException {
doAnswer((Answer<HttpResponseMessage.Builder>) invocation -> {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
Expand All @@ -218,47 +94,29 @@ void requestWithValidBizEventIdButReceiptWrongStatus() throws ReceiptNotFoundExc
.status(ReceiptErrorStatusType.REQUEUED)
.build());

function = new ReceiptToReviewed(receiptCosmosClient);

@SuppressWarnings("unchecked")
OutputBinding<List<ReceiptError>> documentdb = (OutputBinding<List<ReceiptError>>) spy(OutputBinding.class);
function = spy(new ReceiptToReviewed(receiptCosmosClient));

// test execution
AtomicReference<HttpResponseMessage> 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<Optional<ReceiptToReviewedRequest>> request = mock(HttpRequestMessage.class);
when(request.getBody()).thenReturn(Optional.of(new ReceiptToReviewedRequest()));

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


FeedResponse<ReceiptError> feedResponse = mock(FeedResponse.class);

List<ReceiptError> listReceipt = new ArrayList<>();
when(feedResponse.getResults()).thenReturn(listReceipt);
Iterable<FeedResponse<ReceiptError>> feedResponseIterator = List.of(feedResponse);

when(receiptCosmosClient.getToReviewReceiptsError(any(), anyInt())).thenReturn(feedResponseIterator);

function = new ReceiptToReviewed(receiptCosmosClient);

@SuppressWarnings("unchecked")
OutputBinding<List<ReceiptError>> documentdb = (OutputBinding<List<ReceiptError>>) spy(OutputBinding.class);
function = spy(new ReceiptToReviewed(receiptCosmosClient));

// test execution
AtomicReference<HttpResponseMessage> 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);
}
Expand Down

0 comments on commit f05dbfd

Please sign in to comment.