|
3 | 3 | import it.gov.pagopa.common.web.exception.ClientExceptionWithBody; |
4 | 4 | import it.gov.pagopa.idpay.transactions.connector.rest.MerchantRestClient; |
5 | 5 | import it.gov.pagopa.idpay.transactions.connector.rest.dto.MerchantDetailDTO; |
| 6 | +import it.gov.pagopa.idpay.transactions.dto.PatchReportRequest; |
6 | 7 | import it.gov.pagopa.idpay.transactions.dto.ReportDTO; |
7 | 8 | import it.gov.pagopa.idpay.transactions.dto.ReportRequest; |
8 | 9 | import it.gov.pagopa.idpay.transactions.dto.mapper.ReportMapper; |
|
26 | 27 |
|
27 | 28 | import java.time.LocalDateTime; |
28 | 29 |
|
| 30 | +import static it.gov.pagopa.idpay.transactions.utils.ExceptionConstants.ExceptionCode.REPORT_NOT_FOUND; |
| 31 | +import static it.gov.pagopa.idpay.transactions.utils.ExceptionConstants.ExceptionMessage.ERROR_MESSAGE_REPORT_NOT_FOUND; |
29 | 32 | import static org.junit.jupiter.api.Assertions.*; |
30 | 33 | import static org.mockito.ArgumentMatchers.*; |
31 | 34 | import static org.mockito.Mockito.*; |
@@ -372,5 +375,117 @@ void generateMerchantTransactionsReport_fileNameGeneratedCorrectly() { |
372 | 375 | } |
373 | 376 | } |
374 | 377 |
|
| 378 | + @Test |
| 379 | + void patchReport_success_updatesStatus() { |
| 380 | + PatchReportRequest request = PatchReportRequest.builder() |
| 381 | + .reportStatus(ReportStatus.GENERATED) |
| 382 | + .build(); |
| 383 | + |
| 384 | + Report existing = Report.builder() |
| 385 | + .id("R1") |
| 386 | + .initiativeId(INITIATIVE_ID) |
| 387 | + .reportStatus(ReportStatus.INSERTED) |
| 388 | + .build(); |
| 389 | + |
| 390 | + Report updated = Report.builder() |
| 391 | + .id("R1") |
| 392 | + .initiativeId(INITIATIVE_ID) |
| 393 | + .reportStatus(ReportStatus.GENERATED) |
| 394 | + .build(); |
| 395 | + |
| 396 | + ReportDTO updatedDTO = ReportDTO.builder() |
| 397 | + .id("R1") |
| 398 | + .initiativeId(INITIATIVE_ID) |
| 399 | + .reportStatus(ReportStatus.GENERATED) |
| 400 | + .build(); |
| 401 | + |
| 402 | + when(reportRepository.findByIdAndInitiativeId("R1", INITIATIVE_ID)) |
| 403 | + .thenReturn(Mono.just(existing)); |
| 404 | + |
| 405 | + when(reportRepository.save(any(Report.class))) |
| 406 | + .thenReturn(Mono.just(updated)); |
| 407 | + |
| 408 | + when(reportMapper.toDTO(updated)).thenReturn(updatedDTO); |
| 409 | + |
| 410 | + StepVerifier.create(service.patchReport(INITIATIVE_ID, "R1", request)) |
| 411 | + .assertNext(dto -> { |
| 412 | + assertEquals("R1", dto.getId()); |
| 413 | + assertEquals(ReportStatus.GENERATED, dto.getReportStatus()); |
| 414 | + }) |
| 415 | + .verifyComplete(); |
| 416 | + |
| 417 | + verify(reportRepository).findByIdAndInitiativeId("R1", INITIATIVE_ID); |
| 418 | + verify(reportRepository).save(any(Report.class)); |
| 419 | + verify(reportMapper).toDTO(updated); |
| 420 | + } |
| 421 | + @Test |
| 422 | + void patchReport_success_noStatusUpdateWhenNull() { |
| 423 | + PatchReportRequest request = PatchReportRequest.builder() |
| 424 | + .reportStatus(null) |
| 425 | + .build(); |
| 426 | + |
| 427 | + Report existing = Report.builder() |
| 428 | + .id("R2") |
| 429 | + .initiativeId(INITIATIVE_ID) |
| 430 | + .reportStatus(ReportStatus.INSERTED) |
| 431 | + .build(); |
| 432 | + |
| 433 | + Report saved = Report.builder() |
| 434 | + .id("R2") |
| 435 | + .initiativeId(INITIATIVE_ID) |
| 436 | + .reportStatus(ReportStatus.INSERTED) |
| 437 | + .build(); |
| 438 | + |
| 439 | + ReportDTO savedDTO = ReportDTO.builder() |
| 440 | + .id("R2") |
| 441 | + .initiativeId(INITIATIVE_ID) |
| 442 | + .reportStatus(ReportStatus.INSERTED) |
| 443 | + .build(); |
| 444 | + |
| 445 | + when(reportRepository.findByIdAndInitiativeId("R2", INITIATIVE_ID)) |
| 446 | + .thenReturn(Mono.just(existing)); |
| 447 | + |
| 448 | + when(reportRepository.save(any(Report.class))) |
| 449 | + .thenReturn(Mono.just(saved)); |
| 450 | + |
| 451 | + when(reportMapper.toDTO(saved)).thenReturn(savedDTO); |
| 452 | + |
| 453 | + StepVerifier.create(service.patchReport(INITIATIVE_ID, "R2", request)) |
| 454 | + .assertNext(dto -> { |
| 455 | + assertEquals("R2", dto.getId()); |
| 456 | + assertEquals(ReportStatus.INSERTED, dto.getReportStatus()); |
| 457 | + }) |
| 458 | + .verifyComplete(); |
| 459 | + |
| 460 | + verify(reportRepository).findByIdAndInitiativeId("R2", INITIATIVE_ID); |
| 461 | + verify(reportRepository).save(any(Report.class)); |
| 462 | + verify(reportMapper).toDTO(saved); |
| 463 | + } |
| 464 | + @Test |
| 465 | + void patchReport_notFound_throwsException() { |
| 466 | + PatchReportRequest request = PatchReportRequest.builder() |
| 467 | + .reportStatus(ReportStatus.GENERATED) |
| 468 | + .build(); |
| 469 | + |
| 470 | + when(reportRepository.findByIdAndInitiativeId("missing", INITIATIVE_ID)) |
| 471 | + .thenReturn(Mono.empty()); |
| 472 | + |
| 473 | + StepVerifier.create(service.patchReport(INITIATIVE_ID, "missing", request)) |
| 474 | + .expectErrorSatisfies(error -> { |
| 475 | + assertInstanceOf(ClientExceptionWithBody.class, error); |
| 476 | + ClientExceptionWithBody ex = (ClientExceptionWithBody) error; |
| 477 | + assertEquals(404, ex.getHttpStatus().value()); |
| 478 | + assertEquals(REPORT_NOT_FOUND, ex.getCode()); |
| 479 | + assertEquals( |
| 480 | + ERROR_MESSAGE_REPORT_NOT_FOUND.formatted("missing", INITIATIVE_ID), |
| 481 | + ex.getMessage() |
| 482 | + ); |
| 483 | + }) |
| 484 | + .verify(); |
| 485 | + |
| 486 | + verify(reportRepository).findByIdAndInitiativeId("missing", INITIATIVE_ID); |
| 487 | + verify(reportRepository, never()).save(any()); |
| 488 | + verify(reportMapper, never()).toDTO(any()); |
| 489 | + } |
375 | 490 |
|
376 | 491 | } |
0 commit comments