Skip to content

Commit

Permalink
[PRDP-17] Reach unit tests coverage (#14)
Browse files Browse the repository at this point in the history
* [PRDP-17] Added unit tests for singleton instances and class constructors

* [PRDP-17] Fix error event data null and discarder info

* [PRDP-17] Fix function loop in case of wrong biz event structure

* [PRDP-17] Throw error in case of invalid biz event message - added unit test

* [PRDP-17] Throw receipt not found exception
  • Loading branch information
svariant authored Jun 22, 2023
1 parent f10c05f commit 69e8f83
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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.BizEventNotValidException;
import it.gov.pagopa.receipt.pdf.datastore.exception.ReceiptNotFoundException;
import it.gov.pagopa.receipt.pdf.datastore.model.PdfGeneration;
import it.gov.pagopa.receipt.pdf.datastore.model.PdfMetadata;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void processGenerateReceipt(
queueName = "pagopa-d-weu-receipts-queue-receipt-waiting-4-gen",
connection = "RECEIPT_QUEUE_CONN_STRING")
OutputBinding<String> requeueMessage,
final ExecutionContext context) {
final ExecutionContext context) throws BizEventNotValidException, ReceiptNotFoundException {

//Map queue message to BizEvent
BizEvent bizEvent = ObjectMapperUtils.mapString(message, BizEvent.class);
Expand All @@ -100,7 +101,7 @@ public void processGenerateReceipt(
try {
receipt = receiptCosmosClient.getReceiptDocument(bizEvent.getId());
} catch (ReceiptNotFoundException e) {
requeueMessage.setValue(message);
throw new ReceiptNotFoundException("Receipt not found with the following biz-event id: "+bizEvent.getId());
}

int discarder = 0;
Expand Down Expand Up @@ -132,9 +133,12 @@ public void processGenerateReceipt(
//Add receipt to items to be saved to CosmosDB
itemsToNotify.add(receipt);
} else {
discarder++;
requeueMessage.setValue(message);
}
} else {
discarder++;
}

//Discarder info
logMsg = String.format("itemsDone stat %s function - %d number of events in discarder ", context.getInvocationId(), discarder);
logger.info(logMsg);
Expand All @@ -151,7 +155,7 @@ public void processGenerateReceipt(
documentdb.setValue(itemsToNotify);
}
} else {
requeueMessage.setValue(message);
throw new BizEventNotValidException("The message coming from the queue is not a valid BizEvent message");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package it.gov.pagopa.receipt.pdf.datastore.exception;

/** Thrown in case the message triggering the function GenerateReceiptPdf is an invalid biz-event */
public class BizEventNotValidException extends Exception{

/**
* Constructs new exception with provided message and cause
*
* @param message Detail message
*/
public BizEventNotValidException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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.BizEventNotValidException;
import it.gov.pagopa.receipt.pdf.datastore.exception.ReceiptNotFoundException;
import it.gov.pagopa.receipt.pdf.datastore.model.response.BlobStorageResponse;
import it.gov.pagopa.receipt.pdf.datastore.model.response.PdfEngineResponse;
Expand Down Expand Up @@ -38,6 +39,9 @@ class GenerateReceiptPdfTest {

private final String BIZ_EVENT_MESSAGE_SAME_CF = "{\"id\":\"variant062-a330-4210-9c67-465b7d641aVS\",\"version\":\"2\",\"idPaymentManager\":null,\"complete\":\"false\",\"receiptId\":\"9a9bad2caf604b86a339476373c659b0\",\"missingInfo\":[\"idPaymentManager\",\"psp.pspPartitaIVA\",\"paymentInfo.primaryCiIncurredFee\",\"paymentInfo.idBundle\",\"paymentInfo.idCiBundle\",\"paymentInfo.metadata\"],\"debtorPosition\":{\"modelType\":\"2\",\"noticeNumber\":\"302119891614290410\",\"iuv\":\"02119891614290410\"},\"creditor\":{\"idPA\":\"66666666666\",\"idBrokerPA\":\"66666666666\",\"idStation\":\"66666666666_01\",\"companyName\":\"PA paolo\",\"officeName\":\"office PA\"},\"psp\":{\"idPsp\":\"60000000001\",\"idBrokerPsp\":\"60000000001\",\"idChannel\":\"60000000001_01\",\"psp\":\"PSP Paolo\",\"pspPartitaIVA\":null,\"pspFiscalCode\":\"CF60000000006\",\"channelDescription\":\"app\"},\"debtor\":{\"fullName\":\"John Doe\",\"entityUniqueIdentifierType\":\"F\",\"entityUniqueIdentifierValue\":\"JHNDOE00A01F205N\",\"streetName\":\"street\",\"civicNumber\":\"12\",\"postalCode\":\"89020\",\"city\":\"city\",\"stateProvinceRegion\":\"MI\",\"country\":\"IT\",\"eMail\":\"john.doe@test.it\"},\"payer\":{\"fullName\":\"John Doe\",\"entityUniqueIdentifierType\":\"F\",\"entityUniqueIdentifierValue\":\"JHNDOE00A01F205N\",\"streetName\":\"street\",\"civicNumber\":\"12\",\"postalCode\":\"89020\",\"city\":\"city\",\"stateProvinceRegion\":\"MI\",\"country\":\"IT\",\"eMail\":\"john.doe@test.it\"},\"paymentInfo\":{\"paymentDateTime\":\"2023-04-12T16:21:39.022486\",\"applicationDate\":\"2021-10-01\",\"transferDate\":\"2021-10-02\",\"dueDate\":\"2021-07-31\",\"paymentToken\":\"9a9bad2caf604b86a339476373c659b0\",\"amount\":\"7000\",\"fee\":\"200\",\"primaryCiIncurredFee\":null,\"idBundle\":null,\"idCiBundle\":null,\"totalNotice\":\"1\",\"paymentMethod\":\"creditCard\",\"touchpoint\":\"app\",\"remittanceInformation\":\"TARI 2021\",\"description\":\"TARI 2021\",\"metadata\":null},\"transferList\":[{\"idTransfer\":\"1\",\"fiscalCodePA\":\"77777777777\",\"companyName\":\"Pa Salvo\",\"amount\":\"7000\",\"transferCategory\":\"0101101IM\",\"remittanceInformation\":\"TARI Comune EC_TE\",\"metadata\":null,\"mbdattachment\":null,\"iban\":\"IT96R0123454321000000012345\"}],\"transactionDetails\":null,\"timestamp\":1686919660002,\"properties\":{},\"eventStatus\":\"DONE\",\"eventRetryEnrichmentCount\":0,\"eventTriggeredBySchedule\":false,\"eventErrorMessage\":null}";
private final String BIZ_EVENT_MESSAGE_DIFFERENT_CF = "{\"id\":\"variant061-a330-4210-9c67-465b7d641aVS\",\"version\":\"2\",\"idPaymentManager\":null,\"complete\":\"false\",\"receiptId\":\"9a9bad2caf604b86a339476373c659b0\",\"missingInfo\":[\"idPaymentManager\",\"psp.pspPartitaIVA\",\"paymentInfo.primaryCiIncurredFee\",\"paymentInfo.idBundle\",\"paymentInfo.idCiBundle\",\"paymentInfo.metadata\"],\"debtorPosition\":{\"modelType\":\"2\",\"noticeNumber\":\"302119891614290410\",\"iuv\":\"02119891614290410\"},\"creditor\":{\"idPA\":\"66666666666\",\"idBrokerPA\":\"66666666666\",\"idStation\":\"66666666666_01\",\"companyName\":\"PA paolo\",\"officeName\":\"office PA\"},\"psp\":{\"idPsp\":\"60000000001\",\"idBrokerPsp\":\"60000000001\",\"idChannel\":\"60000000001_01\",\"psp\":\"PSP Paolo\",\"pspPartitaIVA\":null,\"pspFiscalCode\":\"CF60000000006\",\"channelDescription\":\"app\"},\"debtor\":{\"fullName\":\"John Doe\",\"entityUniqueIdentifierType\":\"F\",\"entityUniqueIdentifierValue\":\"JHNDOE00A01F205N\",\"streetName\":\"street\",\"civicNumber\":\"12\",\"postalCode\":\"89020\",\"city\":\"city\",\"stateProvinceRegion\":\"MI\",\"country\":\"IT\",\"eMail\":\"john.doe@test.it\"},\"payer\":{\"fullName\":\"Collins Dinners\",\"entityUniqueIdentifierType\":\"F\",\"entityUniqueIdentifierValue\":\"COLDIN00A01F205N\",\"streetName\":\"street\",\"civicNumber\":\"12\",\"postalCode\":\"89020\",\"city\":\"city\",\"stateProvinceRegion\":\"MI\",\"country\":\"IT\",\"eMail\":\"john.doe@test.it\"},\"paymentInfo\":{\"paymentDateTime\":\"2023-04-12T16:21:39.022486\",\"applicationDate\":\"2021-10-01\",\"transferDate\":\"2021-10-02\",\"dueDate\":\"2021-07-31\",\"paymentToken\":\"9a9bad2caf604b86a339476373c659b0\",\"amount\":\"7000\",\"fee\":\"200\",\"primaryCiIncurredFee\":null,\"idBundle\":null,\"idCiBundle\":null,\"totalNotice\":\"1\",\"paymentMethod\":\"creditCard\",\"touchpoint\":\"app\",\"remittanceInformation\":\"TARI 2021\",\"description\":\"TARI 2021\",\"metadata\":null},\"transferList\":[{\"idTransfer\":\"1\",\"fiscalCodePA\":\"77777777777\",\"companyName\":\"Pa Salvo\",\"amount\":\"7000\",\"transferCategory\":\"0101101IM\",\"remittanceInformation\":\"TARI Comune EC_TE\",\"metadata\":null,\"mbdattachment\":null,\"iban\":\"IT96R0123454321000000012345\"}],\"transactionDetails\":null,\"timestamp\":1686919035121,\"properties\":{},\"eventStatus\":\"DONE\",\"eventRetryEnrichmentCount\":0,\"eventTriggeredBySchedule\":false,\"eventErrorMessage\":null}";

private final String BIZ_EVENT_INVALID_MESSAGE = "invalid message";

private final String VALID_PAYER_CF = "a valid payer fiscal code";
private final String VALID_DEBTOR_CF = "a valid debtor fiscal code";
private final String VALID_BLOB_URL = "a valid debtor blob url";
Expand Down Expand Up @@ -237,6 +241,17 @@ void runOkReceiptStatusRetrySameFiscalCode() throws ReceiptNotFoundException, IO
);
}

@Test
void runKoInvalidBizEventMessage(){
@SuppressWarnings("unchecked")
OutputBinding<List<Receipt>> documentdb = (OutputBinding<List<Receipt>>) spy(OutputBinding.class);

@SuppressWarnings("unchecked")
OutputBinding<String> requeueMessage = (OutputBinding<String>) spy(OutputBinding.class);

assertThrows(BizEventNotValidException.class, () -> function.processGenerateReceipt(BIZ_EVENT_INVALID_MESSAGE, documentdb, requeueMessage, context));
}

@Test
void runKoPdfEngine400() throws ReceiptNotFoundException {
Logger logger = Logger.getLogger("BizEventToReceipt-test-logger");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class PdfEngineClientImplTest {

@Test
void testSingleton(){
Assertions.assertDoesNotThrow(PdfEngineClientImpl::getInstance);
}

@Test
void runOk() throws IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.microsoft.azure.functions.HttpStatus;
import it.gov.pagopa.receipt.pdf.datastore.client.impl.ReceiptBlobClientImpl;
import it.gov.pagopa.receipt.pdf.datastore.model.response.BlobStorageResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -18,9 +19,21 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables;

class ReceiptBlobClientImplTest {

@Test
void testSingleton() throws Exception {
@SuppressWarnings("secrets:S6338")
String mockKey = "mockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeyMK==";
withEnvironmentVariables(
"BLOB_STORAGE_CONN_STRING", "DefaultEndpointsProtocol=https;AccountName=samplestorage;AccountKey="+mockKey+";EndpointSuffix=core.windows.net",
"BLOB_STORAGE_ACCOUNT_ENDPOINT", "https://samplestorage.blob.core.windows.net"
).execute(() -> Assertions.assertDoesNotThrow(ReceiptBlobClientImpl::getInstance)
);
}

@Test
void runOk() throws IOException {
BlobServiceClient mockServiceClient = mock(BlobServiceClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables;

class ReceiptCosmosClientImplTest {

@Test
void testSingletonConnectionError() throws Exception {
String mockKey = "mockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeyMK==";
withEnvironmentVariables(
"COSMOS_RECEIPT_KEY", mockKey,
"COSMOS_RECEIPT_SERVICE_ENDPOINT", ""
).execute(() -> Assertions.assertThrows(IllegalArgumentException.class, ReceiptCosmosClientImpl::getInstance)
);
}

@Test
void runOk() throws ReceiptNotFoundException {
String RECEIPT_ID = "a valid receipt id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.azure.storage.queue.QueueClient;
import com.azure.storage.queue.models.SendMessageResult;
import com.microsoft.azure.functions.HttpStatus;
import it.gov.pagopa.receipt.pdf.datastore.client.impl.ReceiptCosmosClientImpl;
import it.gov.pagopa.receipt.pdf.datastore.client.impl.ReceiptQueueClientImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -12,9 +13,21 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables;

class ReceiptQueueClientImplTest {

@Test
void testSingletonConnectionError() throws Exception {
@SuppressWarnings("secrets:S6338")
String mockKey = "mockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeymockKeyMK==";
withEnvironmentVariables(
"RECEIPT_QUEUE_CONN_STRING", "DefaultEndpointsProtocol=https;AccountName=samplequeue;AccountKey="+mockKey+";EndpointSuffix=core.windows.net",
"RECEIPT_QUEUE_TOPIC", "validTopic"
).execute(() -> Assertions.assertDoesNotThrow(ReceiptQueueClientImpl::getInstance)
);
}

@Test
void runOk() {
String MESSAGE_TEXT = "a valid message text";
Expand Down

0 comments on commit 69e8f83

Please sign in to comment.