From 431fa3a3fab5d791a3317e3e5a7638ee359dc8e4 Mon Sep 17 00:00:00 2001 From: svariant Date: Thu, 17 Oct 2024 12:51:34 +0200 Subject: [PATCH] [PPANTT-142] fix: Duplicate code --- .../StationMaintenanceController.java | 8 ++- .../service/StationMaintenanceService.java | 14 +---- .../impl/StationMaintenanceServiceImpl.java | 57 ++++--------------- .../StationMaintenanceControllerTest.java | 2 +- .../StationMaintenanceServiceImplTest.java | 34 +++++------ 5 files changed, 37 insertions(+), 78 deletions(-) diff --git a/src/main/java/it/pagopa/selfcare/pagopa/controller/StationMaintenanceController.java b/src/main/java/it/pagopa/selfcare/pagopa/controller/StationMaintenanceController.java index b62cd4a..a8be6d6 100644 --- a/src/main/java/it/pagopa/selfcare/pagopa/controller/StationMaintenanceController.java +++ b/src/main/java/it/pagopa/selfcare/pagopa/controller/StationMaintenanceController.java @@ -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 ); } diff --git a/src/main/java/it/pagopa/selfcare/pagopa/service/StationMaintenanceService.java b/src/main/java/it/pagopa/selfcare/pagopa/service/StationMaintenanceService.java index 5a26659..0e64d9c 100644 --- a/src/main/java/it/pagopa/selfcare/pagopa/service/StationMaintenanceService.java +++ b/src/main/java/it/pagopa/selfcare/pagopa/service/StationMaintenanceService.java @@ -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 @@ -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 * diff --git a/src/main/java/it/pagopa/selfcare/pagopa/service/impl/StationMaintenanceServiceImpl.java b/src/main/java/it/pagopa/selfcare/pagopa/service/impl/StationMaintenanceServiceImpl.java index 4b22226..042eb95 100644 --- a/src/main/java/it/pagopa/selfcare/pagopa/service/impl/StationMaintenanceServiceImpl.java +++ b/src/main/java/it/pagopa/selfcare/pagopa/service/impl/StationMaintenanceServiceImpl.java @@ -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( @@ -116,6 +82,7 @@ public StationMaintenanceListResource getAllStationsMaintenances( ); } + /** * @inheritDoc */ diff --git a/src/test/java/it/pagopa/selfcare/pagopa/controller/StationMaintenanceControllerTest.java b/src/test/java/it/pagopa/selfcare/pagopa/controller/StationMaintenanceControllerTest.java index 736a69e..9b242d6 100644 --- a/src/test/java/it/pagopa/selfcare/pagopa/controller/StationMaintenanceControllerTest.java +++ b/src/test/java/it/pagopa/selfcare/pagopa/controller/StationMaintenanceControllerTest.java @@ -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)) diff --git a/src/test/java/it/pagopa/selfcare/pagopa/service/impl/StationMaintenanceServiceImplTest.java b/src/test/java/it/pagopa/selfcare/pagopa/service/impl/StationMaintenanceServiceImplTest.java index 787236a..21d1d11 100644 --- a/src/test/java/it/pagopa/selfcare/pagopa/service/impl/StationMaintenanceServiceImplTest.java +++ b/src/test/java/it/pagopa/selfcare/pagopa/service/impl/StationMaintenanceServiceImplTest.java @@ -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); @@ -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); @@ -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() { @@ -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); @@ -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); @@ -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); @@ -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); @@ -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);