Skip to content

Commit

Permalink
[PRDP-183] Updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svariant committed Oct 31, 2023
1 parent 13159f5 commit 50229a3
Showing 1 changed file with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.azure.core.http.rest.Response;
import com.azure.storage.queue.models.SendMessageResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.OutputBinding;
Expand All @@ -11,6 +12,8 @@
import it.gov.pagopa.receipt.pdf.datastore.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.datastore.entity.receipt.enumeration.ReasonErrorCode;
import it.gov.pagopa.receipt.pdf.datastore.entity.receipt.enumeration.ReceiptStatusType;
import it.gov.pagopa.receipt.pdf.datastore.exception.PDVTokenizerException;
import it.gov.pagopa.receipt.pdf.datastore.service.PDVTokenizerService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -34,13 +37,17 @@ class BizEventToReceiptTest {

private final String PAYER_FISCAL_CODE = "a valid payer CF";
private final String DEBTOR_FISCAL_CODE = "a valid debtor CF";
private final String TOKENIZED_DEBTOR_FISCAL_CODE = "tokenizedDebtorFiscalCode";
private final String TOKENIZED_PAYER_FISCAL_CODE = "tokenizedPayerFiscalCode";
private final String EVENT_ID = "a valid id";

@Spy
private BizEventToReceipt function;

@Mock
private ExecutionContext context;
@Mock
private PDVTokenizerService pdvTokenizerServiceMock;

@Captor
private ArgumentCaptor<List<Receipt>> receiptCaptor;
Expand All @@ -54,7 +61,11 @@ public void teardown() throws Exception {
}

@Test
void runOk() {
void runOk() throws PDVTokenizerException, JsonProcessingException {
when(pdvTokenizerServiceMock.generateTokenForFiscalCode(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE);
when(pdvTokenizerServiceMock.generateTokenForFiscalCode(PAYER_FISCAL_CODE)).thenReturn(TOKENIZED_PAYER_FISCAL_CODE);
function = new BizEventToReceipt(pdvTokenizerServiceMock);

ReceiptQueueClientImpl serviceMock = mock(ReceiptQueueClientImpl.class);
Response<SendMessageResult> response = mock(Response.class);
when(response.getStatusCode()).thenReturn(HttpStatus.CREATED.value());
Expand All @@ -75,14 +86,18 @@ void runOk() {
Receipt captured = receiptCaptor.getValue().get(0);
assertEquals(ReceiptStatusType.INSERTED, captured.getStatus());
assertEquals(EVENT_ID, captured.getEventId());
assertEquals(PAYER_FISCAL_CODE, captured.getEventData().getPayerFiscalCode());
assertEquals(DEBTOR_FISCAL_CODE, captured.getEventData().getDebtorFiscalCode());
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
void runOkTotalNoticeNull() {
void runOkTotalNoticeNull() throws PDVTokenizerException, JsonProcessingException {
when(pdvTokenizerServiceMock.generateTokenForFiscalCode(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE);
when(pdvTokenizerServiceMock.generateTokenForFiscalCode(PAYER_FISCAL_CODE)).thenReturn(TOKENIZED_PAYER_FISCAL_CODE);
function = new BizEventToReceipt(pdvTokenizerServiceMock);

ReceiptQueueClientImpl serviceMock = mock(ReceiptQueueClientImpl.class);
Response<SendMessageResult> response = mock(Response.class);
when(response.getStatusCode()).thenReturn(HttpStatus.CREATED.value());
Expand All @@ -103,8 +118,8 @@ void runOkTotalNoticeNull() {
Receipt captured = receiptCaptor.getValue().get(0);
assertEquals(ReceiptStatusType.INSERTED, captured.getStatus());
assertEquals(EVENT_ID, captured.getEventId());
assertEquals(PAYER_FISCAL_CODE, captured.getEventData().getPayerFiscalCode());
assertEquals(DEBTOR_FISCAL_CODE, captured.getEventData().getDebtorFiscalCode());
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());
}
Expand Down Expand Up @@ -180,7 +195,11 @@ void runDiscardedWithCartEventWithInvalidTotalNotice() {
}

@Test
void errorAddingMessageToQueue() {
void errorAddingMessageToQueue() throws PDVTokenizerException, JsonProcessingException {
when(pdvTokenizerServiceMock.generateTokenForFiscalCode(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE);
when(pdvTokenizerServiceMock.generateTokenForFiscalCode(PAYER_FISCAL_CODE)).thenReturn(TOKENIZED_PAYER_FISCAL_CODE);
function = new BizEventToReceipt(pdvTokenizerServiceMock);

ReceiptQueueClientImpl serviceMock = mock(ReceiptQueueClientImpl.class);
Response<SendMessageResult> response = mock(Response.class);
when(response.getStatusCode()).thenReturn(400);
Expand All @@ -202,14 +221,18 @@ void errorAddingMessageToQueue() {
assertEquals(ReceiptStatusType.NOT_QUEUE_SENT, captured.getStatus());
assertEquals(ReasonErrorCode.ERROR_QUEUE.getCode(), captured.getReasonErr().getCode());
assertEquals(EVENT_ID, captured.getEventId());
assertEquals(PAYER_FISCAL_CODE, captured.getEventData().getPayerFiscalCode());
assertEquals(DEBTOR_FISCAL_CODE, captured.getEventData().getDebtorFiscalCode());
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
void errorAddingMessageToQueueThrowException() throws Exception {
when(pdvTokenizerServiceMock.generateTokenForFiscalCode(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE);
when(pdvTokenizerServiceMock.generateTokenForFiscalCode(PAYER_FISCAL_CODE)).thenReturn(TOKENIZED_PAYER_FISCAL_CODE);
function = new BizEventToReceipt(pdvTokenizerServiceMock);

ReceiptQueueClientImpl serviceMock = mock(ReceiptQueueClientImpl.class);

BizEventToReceiptTest.setMock(serviceMock);
Expand All @@ -231,8 +254,8 @@ void errorAddingMessageToQueueThrowException() throws Exception {
assertEquals(ReceiptStatusType.NOT_QUEUE_SENT, captured.getStatus());
assertEquals(ReasonErrorCode.ERROR_QUEUE.getCode(), captured.getReasonErr().getCode());
assertEquals(EVENT_ID, captured.getEventId());
assertEquals(PAYER_FISCAL_CODE, captured.getEventData().getPayerFiscalCode());
assertEquals(DEBTOR_FISCAL_CODE, captured.getEventData().getDebtorFiscalCode());
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());
}
Expand Down

0 comments on commit 50229a3

Please sign in to comment.