Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gioelemella committed Oct 21, 2024
1 parent 926fee1 commit d2d042c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.smallrye.mutiny.Uni;
import it.gov.pagopa.gpd.payments.pull.models.ErrorResponse;
import it.gov.pagopa.gpd.payments.pull.models.PaymentNotice;
import it.gov.pagopa.gpd.payments.pull.models.enums.AppErrorCodeEnum;
Expand All @@ -21,8 +20,8 @@
import static jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@QuarkusTest
class PaymentNoticesTest {
Expand All @@ -35,8 +34,8 @@ class PaymentNoticesTest {

@Test
void getPaymentNoticesOnValidTaxCodeShouldReturnData() throws JsonProcessingException {
doReturn(Uni.createFrom().item(Collections.singletonList(PaymentNotice.builder().build())))
.when(paymentNoticesService).getPaymentNotices(FISCAL_CODE, null, 50, 0);
when(paymentNoticesService.getPaymentNotices(FISCAL_CODE, null, 50, 0))
.thenReturn(Collections.singletonList(PaymentNotice.builder().build()));
String responseString =
given()
.header("x-tax-code", FISCAL_CODE)
Expand All @@ -58,8 +57,8 @@ void getPaymentNoticesOnValidTaxCodeShouldReturnData() throws JsonProcessingExce

@Test
void getPaymentNoticesOnValidTaxCodeAndDateShouldReturnData() throws JsonProcessingException {
doReturn(Uni.createFrom().item(Collections.singletonList(PaymentNotice.builder().build())))
.when(paymentNoticesService).getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0);
when(paymentNoticesService.getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0))
.thenReturn(Collections.singletonList(PaymentNotice.builder().build()));
String responseString =
given()
.header("x-tax-code", FISCAL_CODE)
Expand Down Expand Up @@ -104,9 +103,8 @@ void getPaymentNoticesOnValidTaxCodeAndDateShouldReturnData() throws JsonProcess

@Test
void getPaymentNoticesOnServiceErrorShouldReturnIntServerError() throws JsonProcessingException {
doReturn(Uni.createFrom().item(() -> {
throw new RuntimeException();
})).when(paymentNoticesService).getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0);
when(paymentNoticesService.getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0))
.thenThrow(RuntimeException.class);
String responseString =
given()
.header("x-tax-code", FISCAL_CODE)
Expand Down Expand Up @@ -150,7 +148,6 @@ void getPaymentNoticesOnMissingTaxCodeShouldReturnBadRequest() throws JsonProces
assertNotNull(response.getTitle());
}


@Test
void getPaymentNoticesOnValidTaxCodeAndInvalidDateShouldBadRequest() throws JsonProcessingException {
String responseString =
Expand All @@ -174,5 +171,4 @@ void getPaymentNoticesOnValidTaxCodeAndInvalidDateShouldBadRequest() throws Json
assertNotNull(response.getDetail());
assertNotNull(response.getTitle());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.smallrye.mutiny.CompositeException;
import io.smallrye.mutiny.Uni;
import it.gov.pagopa.gpd.payments.pull.entity.PaymentOption;
import it.gov.pagopa.gpd.payments.pull.entity.PaymentPosition;
import it.gov.pagopa.gpd.payments.pull.exception.PaymentNoticeException;
Expand All @@ -27,8 +26,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@QuarkusTest
class PaymentNoticesServiceImplTest {
Expand All @@ -41,13 +40,14 @@ class PaymentNoticesServiceImplTest {

@Test
void getPaymentNoticesShouldReturnOK() {
doReturn(Uni.createFrom().item(Arrays.asList(createPaymentPosition("", false),
createPaymentPosition("ACA_", false),
createPaymentPosition("PARTIAL_", true))))
.when(paymentPositionRepository).findPaymentPositionsByTaxCodeAndDueDate
(FISCAL_CODE, DUE_DATE, 50, 0);
when(paymentPositionRepository.findPaymentPositionsByTaxCodeAndDueDate(FISCAL_CODE, DUE_DATE, 50, 0))
.thenReturn(Arrays.asList(createPaymentPosition("", false),
createPaymentPosition("ACA_", false),
createPaymentPosition("PARTIAL_", true)));

List<PaymentNotice> response = assertDoesNotThrow(() ->
paymentNoticesService.getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0));
paymentNoticesService.getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0));

assertNotNull(response);
assertEquals(2, response.size());
assertEquals("iupd", response.get(0).getIupd());
Expand All @@ -60,44 +60,45 @@ void getPaymentNoticesShouldReturnOK() {

@Test
void getPaymentNoticesShouldReturnExceptionOnRepositoryError() {
doReturn(Uni.createFrom().item(() -> {
throw new RuntimeException();
})).when(paymentPositionRepository).findPaymentPositionsByTaxCodeAndDueDate
(FISCAL_CODE, DUE_DATE, 50, 0);
when(paymentPositionRepository.findPaymentPositionsByTaxCodeAndDueDate(FISCAL_CODE, DUE_DATE, 50, 0))
.thenThrow(RuntimeException.class);

CompositeException paymentNoticeException =
assertThrows(CompositeException.class, () ->
paymentNoticesService.getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0));
paymentNoticesService.getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0));

List<Throwable> causes = paymentNoticeException.getCauses();
assertEquals(AppErrorCodeEnum.PPL_800, ((PaymentNoticeException) causes.get(causes.size()-1)).getErrorCode());
assertEquals(AppErrorCodeEnum.PPL_800, ((PaymentNoticeException) causes.get(causes.size() - 1)).getErrorCode());
}

@Test
void getPaymentNoticesShouldReturnExceptionOnMappingError() {
PaymentPosition paymentPosition = createPaymentPosition("", true);
paymentPosition.setPaymentOption(null);
doReturn(Uni.createFrom().item(Collections.singletonList(paymentPosition)))
.when(paymentPositionRepository).findPaymentPositionsByTaxCodeAndDueDate
(FISCAL_CODE, DUE_DATE, 50, 0);
when(paymentPositionRepository.findPaymentPositionsByTaxCodeAndDueDate(FISCAL_CODE, DUE_DATE, 50, 0))
.thenReturn(Collections.singletonList(paymentPosition));

CompositeException paymentNoticeException =
assertThrows(CompositeException.class, () ->
paymentNoticesService.getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0));
paymentNoticesService.getPaymentNotices(FISCAL_CODE, DUE_DATE, 50, 0));

List<Throwable> causes = paymentNoticeException.getCauses();
assertEquals(AppErrorCodeEnum.PPL_800, ((PaymentNoticeException) causes.get(causes.size()-1)).getErrorCode());
assertEquals(AppErrorCodeEnum.PPL_800, ((PaymentNoticeException) causes.get(causes.size() - 1)).getErrorCode());
}

PaymentPosition createPaymentPosition(String prefix, Boolean isPartialPayment) {
private PaymentPosition createPaymentPosition(String prefix, Boolean isPartialPayment) {
PaymentPosition paymentPosition = PaymentPosition.builder()
.iupd(prefix+"iupd")
.iupd(prefix + "iupd")
.status(DebtPositionStatus.VALID)
.type(Type.F)
.build();

List<PaymentOption> paymentOption = new ArrayList<>(
List.of(new PaymentOption[]{PaymentOption.builder()
.amount(100)
.dueDate(LocalDateTime.now())
.isPartialPayment(isPartialPayment)
.build()}));
.amount(100)
.dueDate(LocalDateTime.now())
.isPartialPayment(isPartialPayment)
.build()}));

if (isPartialPayment) {
paymentOption.add(PaymentOption.builder()
Expand All @@ -111,5 +112,4 @@ PaymentPosition createPaymentPosition(String prefix, Boolean isPartialPayment) {

return paymentPosition;
}

}

0 comments on commit d2d042c

Please sign in to comment.