From a48034324fecdb842438a0d41e5c4cd8b4cce3cb Mon Sep 17 00:00:00 2001 From: Pietro Tota Date: Thu, 10 Aug 2023 10:33:09 +0200 Subject: [PATCH] fix: add tests for coverage --- .../InvalidSearchCriteriaException.kt | 2 +- .../oracle/TransactionDataProviderTest.kt | 33 +++++++++++++++++++ .../exceptionhandler/ExceptionHandlerTest.kt | 21 +++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/it/pagopa/ecommerce/helpdesk/dataproviders/oracle/TransactionDataProviderTest.kt diff --git a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/exceptions/InvalidSearchCriteriaException.kt b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/exceptions/InvalidSearchCriteriaException.kt index 4c91f61..2b7165a 100644 --- a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/exceptions/InvalidSearchCriteriaException.kt +++ b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/exceptions/InvalidSearchCriteriaException.kt @@ -4,7 +4,7 @@ import it.pagopa.generated.ecommerce.helpdesk.model.ProductDto import org.springframework.http.HttpStatus class InvalidSearchCriteriaException(searchCriteriaType: String, productDto: ProductDto) : - ApiError("Invalid search criteria with type: $searchCriteriaType for product :$productDto") { + ApiError("Invalid search criteria with type: $searchCriteriaType for product: $productDto") { override fun toRestException() = RestApiException( diff --git a/src/test/kotlin/it/pagopa/ecommerce/helpdesk/dataproviders/oracle/TransactionDataProviderTest.kt b/src/test/kotlin/it/pagopa/ecommerce/helpdesk/dataproviders/oracle/TransactionDataProviderTest.kt new file mode 100644 index 0000000..c9899bd --- /dev/null +++ b/src/test/kotlin/it/pagopa/ecommerce/helpdesk/dataproviders/oracle/TransactionDataProviderTest.kt @@ -0,0 +1,33 @@ +package it.pagopa.ecommerce.helpdesk.dataproviders.oracle + +import it.pagopa.ecommerce.helpdesk.dataproviders.TransactionDataProvider +import it.pagopa.generated.ecommerce.helpdesk.model.* +import java.util.stream.Stream +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource + +class TransactionDataProviderTest { + + companion object { + @JvmStatic + private fun searchCriteriaMappingTestSource() = + Stream.of( + Arguments.of(SearchTransactionRequestPaymentTokenDto::class.java, "PAYMENT_TOKEN"), + Arguments.of(SearchTransactionRequestRptIdDto::class.java, "RPT_ID"), + Arguments.of( + SearchTransactionRequestTransactionIdDto::class.java, + "TRANSACTION_ID" + ), + Arguments.of(SearchTransactionRequestEmailDto::class.java, "USER_EMAIL"), + Arguments.of(SearchTransactionRequestFiscalCodeDto::class.java, "USER_FISCAL_CODE"), + ) + } + + @ParameterizedTest + @MethodSource("searchCriteriaMappingTestSource") + fun shouldMapSearchCriteriaType(clazz: Class<*>, searchType: String) { + assertEquals(searchType, TransactionDataProvider.SearchTypeMapping.getSearchType(clazz)) + } +} diff --git a/src/test/kotlin/it/pagopa/ecommerce/helpdesk/exceptionhandler/ExceptionHandlerTest.kt b/src/test/kotlin/it/pagopa/ecommerce/helpdesk/exceptionhandler/ExceptionHandlerTest.kt index 15a555c..264ef46 100644 --- a/src/test/kotlin/it/pagopa/ecommerce/helpdesk/exceptionhandler/ExceptionHandlerTest.kt +++ b/src/test/kotlin/it/pagopa/ecommerce/helpdesk/exceptionhandler/ExceptionHandlerTest.kt @@ -1,8 +1,10 @@ package it.pagopa.ecommerce.helpdesk.exceptionhandler import it.pagopa.ecommerce.helpdesk.HelpdeskTestUtils +import it.pagopa.ecommerce.helpdesk.exceptions.InvalidSearchCriteriaException import it.pagopa.ecommerce.helpdesk.exceptions.NoResultFoundException import it.pagopa.ecommerce.helpdesk.exceptions.RestApiException +import it.pagopa.generated.ecommerce.helpdesk.model.ProductDto import jakarta.xml.bind.ValidationException import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test @@ -34,7 +36,7 @@ class ExceptionHandlerTest { } @Test - fun `Should handle ApiError`() { + fun `Should handle NoResultFoundException`() { val searchCriteria = "searchCriteria" val exception = NoResultFoundException(searchCriteria) val response = exceptionHandler.handleException(exception) @@ -78,4 +80,21 @@ class ExceptionHandlerTest { ) assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.statusCode) } + + @Test + fun `Should handle InvalidSearchCriteriaException`() { + val searchCriteria = "searchCriteria" + val exception = InvalidSearchCriteriaException(searchCriteria, ProductDto.PM) + val response = exceptionHandler.handleException(exception) + assertEquals( + HelpdeskTestUtils.buildProblemJson( + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR, + title = "Invalid search criteria", + description = + "Invalid search criteria with type: $searchCriteria for product: ${ProductDto.PM}" + ), + response.body + ) + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.statusCode) + } }