Skip to content

Commit

Permalink
fix: Maintenance API (#316)
Browse files Browse the repository at this point in the history
* fix get maintenance summary when no maintenance is scheduled

* fix get maintenance
  • Loading branch information
gioelemella authored Jul 30, 2024
1 parent 2bf37eb commit 2e8183d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 64 deletions.
20 changes: 1 addition & 19 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -20974,4 +20956,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ public ResponseEntity<StationMaintenanceResource> 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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface ExtendedStationMaintenanceRepository extends StationMaintenanceRepository {
Expand All @@ -27,10 +26,4 @@ List<StationMaintenance> 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<StationMaintenance> findByIdAndBrokerCode(Long maintenanceId, String brokerCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
*
Expand Down Expand Up @@ -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";
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e8183d

Please sign in to comment.