Skip to content

Commit f201a60

Browse files
coverage
1 parent 2c2e837 commit f201a60

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

src/test/java/it/gov/pagopa/idpay/transactions/controller/ReportControllerImplTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package it.gov.pagopa.idpay.transactions.controller;
22

3+
import it.gov.pagopa.common.web.exception.ClientExceptionWithBody;
4+
import it.gov.pagopa.idpay.transactions.dto.PatchReportRequest;
35
import it.gov.pagopa.idpay.transactions.dto.ReportDTO;
46
import it.gov.pagopa.idpay.transactions.dto.ReportListDTO;
57
import it.gov.pagopa.idpay.transactions.dto.ReportRequest;
@@ -16,13 +18,16 @@
1618
import org.springframework.data.domain.PageImpl;
1719
import org.springframework.data.domain.PageRequest;
1820
import org.springframework.data.domain.Pageable;
21+
import org.springframework.http.HttpStatus;
1922
import org.springframework.test.context.bean.override.mockito.MockitoBean;
2023
import org.springframework.test.web.reactive.server.WebTestClient;
2124
import reactor.core.publisher.Mono;
2225

2326
import java.time.LocalDateTime;
2427
import java.util.List;
2528

29+
import static it.gov.pagopa.idpay.transactions.utils.ExceptionConstants.ExceptionCode.REPORT_NOT_FOUND;
30+
import static it.gov.pagopa.idpay.transactions.utils.ExceptionConstants.ExceptionMessage.ERROR_MESSAGE_REPORT_NOT_FOUND;
2631
import static org.junit.jupiter.api.Assertions.*;
2732
import static org.mockito.ArgumentMatchers.*;
2833
import static org.mockito.Mockito.*;
@@ -215,5 +220,71 @@ void generateReport_ServiceFails_InternalServerError() {
215220
.generateReport(eq(MERCHANT_ID), isNull(), eq(INITIATIVE_ID), eq(request));
216221
}
217222

223+
@Test
224+
void patchReport_Success() {
225+
PatchReportRequest request = PatchReportRequest.builder()
226+
.reportStatus(ReportStatus.GENERATED)
227+
.build();
228+
229+
Report report = Report.builder()
230+
.id("report123")
231+
.initiativeId(INITIATIVE_ID)
232+
.merchantId(MERCHANT_ID)
233+
.reportStatus(ReportStatus.GENERATED)
234+
.build();
235+
236+
ReportDTO reportDTO = ReportDTO.builder()
237+
.id(report.getId())
238+
.initiativeId(report.getInitiativeId())
239+
.merchantId(report.getMerchantId())
240+
.reportStatus(report.getReportStatus())
241+
.build();
242+
243+
when(reportService.patchReport(eq(INITIATIVE_ID), eq("report123"), any(PatchReportRequest.class)))
244+
.thenReturn(Mono.just(reportDTO));
245+
246+
webClient.patch()
247+
.uri("/idpay/merchant/portal/initiatives/{initiativeId}/reports/{reportId}",
248+
INITIATIVE_ID, "report123")
249+
.bodyValue(request)
250+
.exchange()
251+
.expectStatus().isOk()
252+
.expectBody(ReportDTO.class)
253+
.value(response -> {
254+
assertNotNull(response);
255+
assertEquals("report123", response.getId());
256+
assertEquals(ReportStatus.GENERATED, response.getReportStatus());
257+
});
258+
259+
verify(reportService, times(1))
260+
.patchReport(eq(INITIATIVE_ID), eq("report123"), any(PatchReportRequest.class));
261+
}
262+
263+
@Test
264+
void patchReport_NotFound() {
265+
PatchReportRequest request = PatchReportRequest.builder()
266+
.reportStatus(ReportStatus.GENERATED)
267+
.build();
268+
269+
when(reportService.patchReport(eq(INITIATIVE_ID), eq("missingReport"), any()))
270+
.thenReturn(Mono.error(new ClientExceptionWithBody(
271+
HttpStatus.NOT_FOUND,
272+
REPORT_NOT_FOUND,
273+
ERROR_MESSAGE_REPORT_NOT_FOUND.formatted("missingReport", INITIATIVE_ID)
274+
)));
275+
276+
webClient.patch()
277+
.uri("/idpay/merchant/portal/initiatives/{initiativeId}/reports/{reportId}",
278+
INITIATIVE_ID, "missingReport")
279+
.header("x-merchant-id", MERCHANT_ID)
280+
.bodyValue(request)
281+
.exchange()
282+
.expectStatus().isNotFound();
283+
284+
verify(reportService, times(1))
285+
.patchReport(eq(INITIATIVE_ID), eq("missingReport"), any());
286+
}
287+
288+
218289

219290
}

src/test/java/it/gov/pagopa/idpay/transactions/service/ReportServiceImplTest.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import it.gov.pagopa.common.web.exception.ClientExceptionWithBody;
44
import it.gov.pagopa.idpay.transactions.connector.rest.MerchantRestClient;
55
import it.gov.pagopa.idpay.transactions.connector.rest.dto.MerchantDetailDTO;
6+
import it.gov.pagopa.idpay.transactions.dto.PatchReportRequest;
67
import it.gov.pagopa.idpay.transactions.dto.ReportDTO;
78
import it.gov.pagopa.idpay.transactions.dto.ReportRequest;
89
import it.gov.pagopa.idpay.transactions.dto.mapper.ReportMapper;
@@ -26,6 +27,8 @@
2627

2728
import java.time.LocalDateTime;
2829

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;
2932
import static org.junit.jupiter.api.Assertions.*;
3033
import static org.mockito.ArgumentMatchers.*;
3134
import static org.mockito.Mockito.*;
@@ -372,5 +375,117 @@ void generateMerchantTransactionsReport_fileNameGeneratedCorrectly() {
372375
}
373376
}
374377

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+
}
375490

376491
}

0 commit comments

Comments
 (0)