Skip to content

Commit

Permalink
[PPANTT-142] fix: Duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
svariant committed Oct 17, 2024
1 parent e805ef4 commit 431fa3a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ public StationMaintenanceListResource getAllStationsMaintenances(
@Parameter(description = "Maintenances' state") @RequestParam(required = false, defaultValue = "SCHEDULED_AND_IN_PROGRESS") StationMaintenanceListState state,
@Parameter(description = "Maintenance's starting year") @RequestParam(required = false) Integer year
) {
return this.stationMaintenanceService.getAllStationsMaintenances(
return this.stationMaintenanceService.getStationMaintenances(
null,
null,
state,
year
year,
null,
null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public interface StationMaintenanceService {

/**
* Retrieves the list of station's maintenance of the specified broker that match the provided filters
* Retrieves the list of stations' maintenances that match the provided filters
*
* @param brokerCode broker's tax code
* @param stationCode station's code, used to filter out results
Expand All @@ -24,18 +24,6 @@ StationMaintenanceListResource getStationMaintenances(
Integer page
);

/**
* Retrieves the list of all stations' maintenance that match the provided filters
*
* @param state state of the maintenance (based on start and end date), used to filter out results
* @param year year of the maintenance, used to filter out results
* @return the filtered list of station's maintenance
*/
StationMaintenanceListResource getAllStationsMaintenances(
StationMaintenanceListState state,
Integer year
);

/**
* Retrieves broker related station maintenance summary for the provided year
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,51 +61,17 @@ public StationMaintenanceListResource getStationMaintenances(
startDateTimeAfter = startDateTimeAfter != null ? startDateTimeAfter.withYear(year) : getStartOfYear(year);
}

return this.apiConfigClient.getStationMaintenances(
brokerCode,
stationCode,
startDateTimeBefore,
startDateTimeAfter,
endDateTimeBefore,
endDateTimeAfter,
limit,
page
);
}

/**
* @inheritDoc
*/
@Override
public StationMaintenanceListResource getAllStationsMaintenances(
StationMaintenanceListState state,
Integer year
) {
OffsetDateTime startDateTimeBefore = null;
OffsetDateTime startDateTimeAfter = null;
OffsetDateTime endDateTimeBefore = null;
OffsetDateTime endDateTimeAfter = null;

if (state != null) {
if (state.equals(StationMaintenanceListState.FINISHED)) {
endDateTimeBefore = getDateToday();
}
if (state.equals(StationMaintenanceListState.SCHEDULED_AND_IN_PROGRESS)) {
endDateTimeAfter = getDateToday();
}
if (state.equals(StationMaintenanceListState.SCHEDULED)) {
startDateTimeAfter = getDateToday();
}
if (state.equals(StationMaintenanceListState.IN_PROGRESS)) {
startDateTimeBefore = getDateToday();
endDateTimeAfter = getDateToday();
}
}

if (year != null
) {
startDateTimeBefore = startDateTimeBefore != null ? startDateTimeBefore.withYear(year) : getEndOfYear(year);
startDateTimeAfter = startDateTimeAfter != null ? startDateTimeAfter.withYear(year) : getStartOfYear(year);
if(brokerCode != null){
return this.apiConfigClient.getStationMaintenances(
brokerCode,
stationCode,
startDateTimeBefore,
startDateTimeAfter,
endDateTimeBefore,
endDateTimeAfter,
limit,
page
);
}

return this.apiConfigClient.getAllStationsMaintenances(
Expand All @@ -116,6 +82,7 @@ public StationMaintenanceListResource getAllStationsMaintenances(
);
}


/**
* @inheritDoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void getAllStationsMaintenances() throws Exception {
StationMaintenanceListResource response = new StationMaintenanceListResource();
response.setMaintenanceList(Collections.singletonList(maintenanceResource));
response.setPageInfo(new PageInfo());
when(stationMaintenanceService.getAllStationsMaintenances(any(StationMaintenanceListState.class), anyInt())).thenReturn(response);
when(stationMaintenanceService.getStationMaintenances(eq(null), eq(null), any(StationMaintenanceListState.class), anyInt(), eq(null), eq(null))).thenReturn(response);

mvc.perform(get("/station-maintenances")
.param("state", String.valueOf(StationMaintenanceListState.SCHEDULED_AND_IN_PROGRESS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ void getAllStationsMaintenancesFINISHEDWithoutYearFilterSuccess() {

when(apiConfigClient.getAllStationsMaintenances(any(), any(), any(), any())).thenReturn(response);

StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getAllStationsMaintenances(
StationMaintenanceListState.FINISHED, null)
StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getStationMaintenances(
eq(null), eq(null), StationMaintenanceListState.FINISHED, null, eq(null), eq(null))
);

assertNotNull(result);
Expand All @@ -203,8 +203,8 @@ void getAllStationsMaintenancesFINISHEDWithYearFilterSuccess() {

when(apiConfigClient.getAllStationsMaintenances(any(), any(), any(), any())).thenReturn(response);

StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getAllStationsMaintenances(
StationMaintenanceListState.FINISHED, YEAR_FILTER)
StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getStationMaintenances(
eq(null), eq(null), StationMaintenanceListState.FINISHED, YEAR_FILTER, eq(null), eq(null))
);

assertNotNull(result);
Expand All @@ -221,14 +221,14 @@ void getAllStationsMaintenancesIN_PROGRESSWithoutYearFilterSuccess() {

when(apiConfigClient.getAllStationsMaintenances(any(), any(), any(), any())).thenReturn(response);

StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getAllStationsMaintenances(
StationMaintenanceListState.IN_PROGRESS, null)
StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getStationMaintenances(
eq(null), eq(null), StationMaintenanceListState.IN_PROGRESS, null, eq(null), eq(null))
);

assertNotNull(result);

verify(apiConfigClient).getAllStationsMaintenances(any(OffsetDateTime.class), eq(null), eq(null), any(OffsetDateTime.class));
}
}

@Test
void getAllStationsMaintenancesIN_PROGRESSWithYearFilterSuccess() {
Expand All @@ -239,8 +239,8 @@ void getAllStationsMaintenancesIN_PROGRESSWithYearFilterSuccess() {

when(apiConfigClient.getAllStationsMaintenances(any(), any(), any(), any())).thenReturn(response);

StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getAllStationsMaintenances(
StationMaintenanceListState.IN_PROGRESS, YEAR_FILTER)
StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getStationMaintenances(
eq(null), eq(null), StationMaintenanceListState.IN_PROGRESS, YEAR_FILTER, eq(null), eq(null))
);

assertNotNull(result);
Expand All @@ -257,8 +257,8 @@ void getAllStationsMaintenancesSCHEDULEDWithoutYearFilterSuccess() {

when(apiConfigClient.getAllStationsMaintenances(any(), any(), any(), any())).thenReturn(response);

StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getAllStationsMaintenances(
StationMaintenanceListState.SCHEDULED, null)
StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getStationMaintenances(
eq(null), eq(null), StationMaintenanceListState.SCHEDULED, null, eq(null), eq(null))
);

assertNotNull(result);
Expand All @@ -275,8 +275,8 @@ void getAllStationsMaintenancesSCHEDULEDWithYearFilterSuccess() {

when(apiConfigClient.getAllStationsMaintenances(any(), any(), any(), any())).thenReturn(response);

StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getAllStationsMaintenances(
StationMaintenanceListState.SCHEDULED, YEAR_FILTER)
StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getStationMaintenances(
eq(null), eq(null), StationMaintenanceListState.SCHEDULED, YEAR_FILTER, eq(null), eq(null))
);

assertNotNull(result);
Expand All @@ -293,8 +293,8 @@ void getAllStationsMaintenancesSCHEDULED_AND_IN_PROGRESSWithoutYearFilterSuccess

when(apiConfigClient.getAllStationsMaintenances(any(), any(), any(), any())).thenReturn(response);

StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getAllStationsMaintenances(
StationMaintenanceListState.SCHEDULED_AND_IN_PROGRESS, null)
StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getStationMaintenances(
eq(null), eq(null), StationMaintenanceListState.SCHEDULED_AND_IN_PROGRESS, null, eq(null), eq(null))
);

assertNotNull(result);
Expand All @@ -312,8 +312,8 @@ void getAllStationsMaintenancesSCHEDULED_AND_IN_PROGRESSWithYearFilterSuccess()

when(apiConfigClient.getAllStationsMaintenances(any(), any(), any(), any())).thenReturn(response);

StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getAllStationsMaintenances(
StationMaintenanceListState.SCHEDULED_AND_IN_PROGRESS, YEAR_FILTER)
StationMaintenanceListResource result = assertDoesNotThrow(() -> stationMaintenanceService.getStationMaintenances(
eq(null), eq(null), StationMaintenanceListState.SCHEDULED_AND_IN_PROGRESS, YEAR_FILTER, eq(null), eq(null))
);

assertNotNull(result);
Expand Down

0 comments on commit 431fa3a

Please sign in to comment.