From 2e8183ddea8a818637a0acc7dafb27a9d59d2dc3 Mon Sep 17 00:00:00 2001 From: gioelemella <128155546+gioelemella@users.noreply.github.com> Date: Tue, 30 Jul 2024 16:48:38 +0200 Subject: [PATCH] fix: Maintenance API (#316) * fix get maintenance summary when no maintenance is scheduled * fix get maintenance --- openapi/openapi.json | 20 +------- .../StationMaintenanceController.java | 2 - .../apiconfig/core/exception/AppError.java | 1 - .../ExtendedStationMaintenanceRepository.java | 7 --- .../service/StationMaintenanceService.java | 51 +++++++++---------- .../StationMaintenanceServiceTest.java | 17 ++++--- 6 files changed, 34 insertions(+), 64 deletions(-) diff --git a/openapi/openapi.json b/openapi/openapi.json index 2a3f08f79..723a6c51a 100644 --- a/openapi/openapi.json +++ b/openapi/openapi.json @@ -1540,24 +1540,6 @@ } } }, - "404": { - "description": "Not Found", - "headers": { - "X-Request-Id": { - "description": "This header identifies the call", - "schema": { - "type": "string" - } - } - }, - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ProblemJson" - } - } - } - }, "429": { "description": "Too many requests", "headers": { @@ -20974,4 +20956,4 @@ } } } -} +} \ No newline at end of file diff --git a/src/main/java/it/gov/pagopa/apiconfig/core/controller/StationMaintenanceController.java b/src/main/java/it/gov/pagopa/apiconfig/core/controller/StationMaintenanceController.java index 2dc096c70..7d224eb00 100644 --- a/src/main/java/it/gov/pagopa/apiconfig/core/controller/StationMaintenanceController.java +++ b/src/main/java/it/gov/pagopa/apiconfig/core/controller/StationMaintenanceController.java @@ -179,8 +179,6 @@ public ResponseEntity getStationMaintenance( content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ProblemJson.class))), @ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(schema = @Schema())), @ApiResponse(responseCode = "403", description = "Forbidden", content = @Content(schema = @Schema())), - @ApiResponse(responseCode = "404", description = "Not Found", - content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ProblemJson.class))), @ApiResponse(responseCode = "429", description = "Too many requests", content = @Content(schema = @Schema())), @ApiResponse(responseCode = "500", description = "Service unavailable", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ProblemJson.class))) diff --git a/src/main/java/it/gov/pagopa/apiconfig/core/exception/AppError.java b/src/main/java/it/gov/pagopa/apiconfig/core/exception/AppError.java index 4bddfcd0d..249bc7a30 100644 --- a/src/main/java/it/gov/pagopa/apiconfig/core/exception/AppError.java +++ b/src/main/java/it/gov/pagopa/apiconfig/core/exception/AppError.java @@ -248,7 +248,6 @@ public enum AppError { MAINTENANCE_START_DATE_TIME_NOT_VALID(HttpStatus.BAD_REQUEST, "Invalid station maintenance start date time", "A maintenance must start after 72h from the current date time"), MAINTENANCE_DATE_TIME_INTERVAL_NOT_VALID(HttpStatus.BAD_REQUEST, "Invalid station maintenance date time interval", "The provided maintenance interval is not valid: %s"), MAINTENANCE_DATE_TIME_INTERVAL_HAS_OVERLAPPING(HttpStatus.CONFLICT, "Conflict on station maintenance date time interval", "There is an overlapping maintenance for the provided date time interval"), - MAINTENANCE_SUMMARY_NOT_FOUND(HttpStatus.NOT_FOUND, "Maintenance summary not found", "The maintenance summary for the provided broker %s and year %s was not found"), MAINTENANCE_NOT_FOUND(HttpStatus.NOT_FOUND, "Maintenance not found", "The maintenance with the provided id %s was not found"), diff --git a/src/main/java/it/gov/pagopa/apiconfig/core/repository/ExtendedStationMaintenanceRepository.java b/src/main/java/it/gov/pagopa/apiconfig/core/repository/ExtendedStationMaintenanceRepository.java index dbeb69909..38fa40c9c 100644 --- a/src/main/java/it/gov/pagopa/apiconfig/core/repository/ExtendedStationMaintenanceRepository.java +++ b/src/main/java/it/gov/pagopa/apiconfig/core/repository/ExtendedStationMaintenanceRepository.java @@ -8,7 +8,6 @@ import java.time.OffsetDateTime; import java.util.List; -import java.util.Optional; @Repository public interface ExtendedStationMaintenanceRepository extends StationMaintenanceRepository { @@ -27,10 +26,4 @@ List findOverlappingMaintenance( @Param("startDateTime") OffsetDateTime startDateTime, @Param("endDateTime") OffsetDateTime endDateTime ); - - @Query(value = "SELECT m FROM StationMaintenance m JOIN Stazioni s ON m.fkStation = s.objId JOIN " + - "IntermediariPa ipa ON s.fkIntermediarioPa = ipa.objId WHERE ipa.idIntermediarioPa = :brokerCode " + - "AND s.objId = :maintenanceId" - ) - Optional findByIdAndBrokerCode(Long maintenanceId, String brokerCode); } diff --git a/src/main/java/it/gov/pagopa/apiconfig/core/service/StationMaintenanceService.java b/src/main/java/it/gov/pagopa/apiconfig/core/service/StationMaintenanceService.java index a62e84362..0406e8e77 100644 --- a/src/main/java/it/gov/pagopa/apiconfig/core/service/StationMaintenanceService.java +++ b/src/main/java/it/gov/pagopa/apiconfig/core/service/StationMaintenanceService.java @@ -212,7 +212,7 @@ public MaintenanceHoursSummaryResource getBrokerMaintenancesSummary(String broke .maintenanceYear(maintenanceYear) .brokerCode(brokerCode) .build() - ).orElseThrow(() -> new AppException(AppError.MAINTENANCE_SUMMARY_NOT_FOUND, brokerCode, maintenanceYear)); + ).orElse(buildEmptySummary()); Double usedHours = maintenanceSummary.getUsedHours(); Double scheduledHours = maintenanceSummary.getScheduledHours(); @@ -233,6 +233,23 @@ public MaintenanceHoursSummaryResource getBrokerMaintenancesSummary(String broke .build(); } + /** + * Recovers a station maintenance, given its brokerCode and maintenanceId. + * If the provided brokerCode doesn't match the one related to the persisted one for the given maintenance, + * it will throw the maintenance not found exception + * + * @param brokerCode brokerCode to be used as filter in the maintenance recovery + * @param maintenanceId station maintenance id to be used for the detail recovery + * @return station maintenance data, provided in an instance of StationMaintenanceResource + * @throws AppException thrown when a maintenance, given the input data, has not been found + */ + public StationMaintenanceResource getStationMaintenance(String brokerCode, Long maintenanceId) { + StationMaintenance stationMaintenance = this.stationMaintenanceRepository.findById(maintenanceId) + .orElseThrow(() -> new AppException(AppError.MAINTENANCE_NOT_FOUND, maintenanceId)); + + return this.mapper.map(stationMaintenance, StationMaintenanceResource.class); + } + /** * Delete the maintenance with the provided id * @@ -389,32 +406,6 @@ private boolean isNotRoundedTo15Minutes(OffsetDateTime dateTime) { return dateTime.getMinute() % 15 != 0; } - /** - * Recovers a station maintenance, given its brokerCode and maintenanceId. - * If the the provided brokerCode doesnt match the one related to the persisted one for the given maintenance, - * it will throw the maintenance not found exception - * @param brokerCode brokerCode to be used as filter in the maintenance recovery - * @param maintenanceId station maintentance id to be used for the detail recovery - * @return station maintenance data, provided in an instance of StationMaintenanceResource - * @throws AppException thrown when a maintenance, given the input data, has not been found - */ - public StationMaintenanceResource getStationMaintenance(String brokerCode, Long maintenanceId) { - StationMaintenance stationMaintenance = this.stationMaintenanceRepository.findByIdAndBrokerCode( - maintenanceId, brokerCode) - .orElseThrow(() -> new AppException(AppError.MAINTENANCE_NOT_FOUND, maintenanceId)); - - return StationMaintenanceResource.builder() - .maintenanceId(stationMaintenance.getObjId()) - .brokerCode(brokerCode) - .startDateTime(stationMaintenance.getStartDateTime()) - .endDateTime(stationMaintenance.getEndDateTime()) - .standIn(stationMaintenance.getStandIn()) - .stationCode(stationMaintenance.getStation().getIdStazione()) - .build(); - - } - - private String transformHoursToStringFormat(Double hours) { if (hours == 0) { return "0"; @@ -435,4 +426,10 @@ private String transformHoursToStringFormat(Double hours) { return String.valueOf(intValue); } + private StationMaintenanceSummaryView buildEmptySummary() { + return StationMaintenanceSummaryView.builder() + .usedHours(0.0) + .scheduledHours(0.0) + .build(); + } } diff --git a/src/test/java/it/gov/pagopa/apiconfig/core/service/StationMaintenanceServiceTest.java b/src/test/java/it/gov/pagopa/apiconfig/core/service/StationMaintenanceServiceTest.java index e9c669c0e..bba5f243d 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/core/service/StationMaintenanceServiceTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/core/service/StationMaintenanceServiceTest.java @@ -606,7 +606,7 @@ void updateStationMaintenanceFailInProgressMaintenanceHasOverlappingMaintenance( @Test void getStationMaintenanceSuccessWhenValidData() { StationMaintenance oldMaintenance = buildMaintenanceParametrized(OffsetDateTime.now().minusHours(1)); - when(stationMaintenanceRepository.findByIdAndBrokerCode(anyLong(),any())) + when(stationMaintenanceRepository.findById(anyLong())) .thenReturn(Optional.of(oldMaintenance)); StationMaintenanceResource result = assertDoesNotThrow( () -> sut.getStationMaintenance("test", 0L)); @@ -616,11 +616,9 @@ void getStationMaintenanceSuccessWhenValidData() { @Test void getStationMaintenanceExceptionWhenMissingData() { - when(stationMaintenanceRepository.findByIdAndBrokerCode(anyLong(),any())) - .thenReturn(Optional.empty()); + when(stationMaintenanceRepository.findById(anyLong())).thenReturn(Optional.empty()); Assert.assertThrows(AppException.class, () -> sut.getStationMaintenance("test", 0L)); - } @Test @@ -709,12 +707,15 @@ void getBrokerMaintenancesSummaryFailNotFound() { .build()) ).thenReturn(Optional.empty()); - AppException e = assertThrows(AppException.class, () -> + MaintenanceHoursSummaryResource result = assertDoesNotThrow(() -> sut.getBrokerMaintenancesSummary(BROKER_CODE, "2024")); - assertNotNull(e); - assertEquals(AppError.MAINTENANCE_SUMMARY_NOT_FOUND.httpStatus, e.getHttpStatus()); - assertEquals(AppError.MAINTENANCE_SUMMARY_NOT_FOUND.title, e.getTitle()); + assertNotNull(result); + assertEquals("0", result.getUsedHours()); + assertEquals("0", result.getScheduledHours()); + assertEquals("36", result.getRemainingHours()); + assertEquals("0", result.getExtraHours()); + assertEquals("36", result.getAnnualHoursLimit()); } @Test