From 4023cfe2372079fcb7f4825606aec7e8d480362e Mon Sep 17 00:00:00 2001 From: yulongcai Date: Tue, 30 Jan 2024 16:46:19 +0800 Subject: [PATCH 1/7] ADM-794:[backend]fix: add test for generate report --- .../service/report/ReportServiceTest.java | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java index 332be16bcd..68d4d29e60 100644 --- a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java @@ -1,40 +1,39 @@ package heartbeat.service.report; import heartbeat.controller.report.dto.request.GenerateReportRequest; -import heartbeat.controller.report.dto.request.ReportType; import heartbeat.controller.report.dto.request.MetricType; +import heartbeat.controller.report.dto.request.ReportType; import heartbeat.controller.report.dto.response.MetricsDataCompleted; +import heartbeat.exception.NotFoundException; import heartbeat.handler.AsyncMetricsDataHandler; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.invocation.InvocationOnMock; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import org.mockito.stubbing.Answer; import org.springframework.core.io.InputStreamResource; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.InputStreamReader; import java.io.InputStream; +import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.List; import java.util.stream.Collectors; +import static heartbeat.service.report.scheduler.DeleteExpireCSVScheduler.EXPORT_CSV_VALIDITY_TIME; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; - -import heartbeat.exception.NotFoundException; - -import static heartbeat.service.report.scheduler.DeleteExpireCSVScheduler.EXPORT_CSV_VALIDITY_TIME; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.verify; +import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.STRICT_STUBS) @@ -104,4 +103,23 @@ void ShouldInitializeMetricsDataCompletedInHandlerWithPreOneWhenPreOneExisted() verify(generateReporterService, never()).generateDoraReport(request); } + @Test + void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() throws InterruptedException { + GenerateReportRequest request = GenerateReportRequest.builder().csvTimeStamp("csvTimeStamp").metrics(List.of("velocity", "lead time for changes")).build(); + when(asyncMetricsDataHandler.getMetricsDataCompleted(any())).thenReturn(MetricsDataCompleted.builder().pipelineMetricsCompleted(true).build()); + doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request); + reportService.generateReportByType(request, MetricType.BOARD); + Thread.sleep(100); + verify(asyncMetricsDataHandler).putMetricsDataCompleted( + argThat("csvTimeStamp"::equals), + argThat(metricsDataCompleted -> { + Assertions.assertFalse(metricsDataCompleted.boardMetricsCompleted()); + Assertions.assertTrue(metricsDataCompleted.pipelineMetricsCompleted()); + Assertions.assertFalse(metricsDataCompleted.sourceControlMetricsCompleted()); + return true; + }) + ); + verify(generateReporterService).generateBoardReport(request); + verify(generateReporterService, never()).generateDoraReport(request); + } } From 7316696dc2e5a04b015d21490162d041ef838147 Mon Sep 17 00:00:00 2001 From: yulongcai Date: Wed, 31 Jan 2024 11:11:01 +0800 Subject: [PATCH 2/7] ADM-794:[backend]fix: repair test for pipeline --- .../service/report/PipelineServiceTest.java | 12 ++++++++---- .../service/report/ReportServiceTest.java | 17 ++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/backend/src/test/java/heartbeat/service/report/PipelineServiceTest.java b/backend/src/test/java/heartbeat/service/report/PipelineServiceTest.java index 38a4b53497..0df4d233b4 100644 --- a/backend/src/test/java/heartbeat/service/report/PipelineServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/PipelineServiceTest.java @@ -28,6 +28,7 @@ import org.mockito.quality.Strictness; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -61,20 +62,23 @@ class FetchGithubData { @Test void shouldReturnEmptyBuildInfosListAndEmptyLeadTimeWhenDeploymentEnvironmentsIsEmpty() { + String token = "token"; GenerateReportRequest request = GenerateReportRequest.builder() - .buildKiteSetting(BuildKiteSetting.builder().deploymentEnvList(new ArrayList()).build()) + .buildKiteSetting(BuildKiteSetting.builder().deploymentEnvList(new ArrayList<>()).build()) .metrics(new ArrayList<>()) + .codebaseSetting(CodebaseSetting.builder().token(token).build()) .build(); FetchedData.BuildKiteData result = pipelineService.fetchGithubData(request); assertEquals(0, result.getBuildInfosList().size()); verify(buildKiteService, never()).countDeployTimes(any(), any(), any(), any()); + verify(gitHubService).fetchPipelinesLeadTime(List.of(), new HashMap<>(), token); } @Test void shouldReturnEmptyPipelineLeadTimeWhenCodebaseSettingIsEmpty() { GenerateReportRequest request = GenerateReportRequest.builder() - .buildKiteSetting(BuildKiteSetting.builder().deploymentEnvList(new ArrayList()).build()) + .buildKiteSetting(BuildKiteSetting.builder().deploymentEnvList(new ArrayList<>()).build()) .metrics(new ArrayList<>()) .build(); FetchedData.BuildKiteData result = pipelineService.fetchGithubData(request); @@ -84,7 +88,7 @@ void shouldReturnEmptyPipelineLeadTimeWhenCodebaseSettingIsEmpty() { } @Test - void shouldGetPipelineLeadTimeFromGithubServiceAndBuildkiteServiceWhenCodebaseSettingIsNotEmpty() { + void shouldGetPipelineLeadTimeFromGithubServiceAndBuildKiteServiceWhenCodebaseSettingIsNotEmpty() { List fakeBuildKiteBuildInfos = new ArrayList<>(); String startTime = "startTime"; String endTime = "endTime"; @@ -197,7 +201,7 @@ class FetchBuildKiteInfo { @Test void shouldReturnEmptyWhenDeploymentEnvListIsEmpty() { GenerateReportRequest request = GenerateReportRequest.builder() - .buildKiteSetting(BuildKiteSetting.builder().deploymentEnvList(new ArrayList()).build()) + .buildKiteSetting(BuildKiteSetting.builder().deploymentEnvList(new ArrayList<>()).build()) .metrics(new ArrayList<>()) .build(); FetchedData.BuildKiteData result = pipelineService.fetchBuildKiteInfo(request); diff --git a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java index 68d4d29e60..68d248f399 100644 --- a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java @@ -6,7 +6,6 @@ import heartbeat.controller.report.dto.response.MetricsDataCompleted; import heartbeat.exception.NotFoundException; import heartbeat.handler.AsyncMetricsDataHandler; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -29,7 +28,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -110,15 +108,12 @@ void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() thro doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request); reportService.generateReportByType(request, MetricType.BOARD); Thread.sleep(100); - verify(asyncMetricsDataHandler).putMetricsDataCompleted( - argThat("csvTimeStamp"::equals), - argThat(metricsDataCompleted -> { - Assertions.assertFalse(metricsDataCompleted.boardMetricsCompleted()); - Assertions.assertTrue(metricsDataCompleted.pipelineMetricsCompleted()); - Assertions.assertFalse(metricsDataCompleted.sourceControlMetricsCompleted()); - return true; - }) - ); + MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder() + .boardMetricsCompleted(false) + .pipelineMetricsCompleted(true) + .sourceControlMetricsCompleted(false) + .build(); + verify(asyncMetricsDataHandler).putMetricsDataCompleted("csvTimeStamp", expectMetricsDataResult); verify(generateReporterService).generateBoardReport(request); verify(generateReporterService, never()).generateDoraReport(request); } From 9b63d5fcf0ff211ed3891f5cd09fc17c1f703cff Mon Sep 17 00:00:00 2001 From: yulongcai Date: Wed, 31 Jan 2024 14:34:08 +0800 Subject: [PATCH 3/7] ADM-794:[backend]fix: add test for pipeline --- .../report/GenerateReporterServiceTest.java | 14 +++---- .../service/report/KanbanFixture.java | 41 +++++++++++++++++++ .../service/report/KanbanServiceTest.java | 11 ++++- .../service/report/ReportServiceTest.java | 9 +++- 4 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 backend/src/test/java/heartbeat/service/report/KanbanFixture.java diff --git a/backend/src/test/java/heartbeat/service/report/GenerateReporterServiceTest.java b/backend/src/test/java/heartbeat/service/report/GenerateReporterServiceTest.java index 5243a82a54..d7311c9a64 100644 --- a/backend/src/test/java/heartbeat/service/report/GenerateReporterServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/GenerateReporterServiceTest.java @@ -641,7 +641,7 @@ void shouldGetDataFromCache() { assertEquals(EXPORT_CSV_VALIDITY_TIME, res.getExportValidityTime()); assertEquals(true, res.getAllMetricsCompleted()); - assertEquals(null, res.getReportMetricsError().getBoardMetricsError()); + assertNull(res.getReportMetricsError().getBoardMetricsError()); } @Test @@ -662,7 +662,7 @@ void shouldReturnCompletedFalseGivenResponseNullMetricsDataCompletedFalseWhenGet assertEquals(false, res.getBoardMetricsCompleted()); assertEquals(false, res.getPipelineMetricsCompleted()); assertEquals(false, res.getSourceControlMetricsCompleted()); - assertEquals(null, res.getReportMetricsError().getBoardMetricsError()); + assertNull(res.getReportMetricsError().getBoardMetricsError()); } @Test @@ -683,7 +683,7 @@ void shouldReturnCompletedTrueGivenResponseNonNullMetricsDataCompletedFalseWhenG assertEquals(true, res.getBoardMetricsCompleted()); assertEquals(true, res.getPipelineMetricsCompleted()); assertEquals(true, res.getSourceControlMetricsCompleted()); - assertEquals(null, res.getReportMetricsError().getBoardMetricsError()); + assertNull(res.getReportMetricsError().getBoardMetricsError()); } @Test @@ -697,10 +697,10 @@ void shouldReturnMetricsCompletedStatusIsNullWhenAsyncMetricsStatusIsNull() { assertEquals(EXPORT_CSV_VALIDITY_TIME, res.getExportValidityTime()); assertEquals(false, res.getAllMetricsCompleted()); - assertEquals(null, res.getBoardMetricsCompleted()); - assertEquals(null, res.getPipelineMetricsCompleted()); - assertEquals(null, res.getSourceControlMetricsCompleted()); - assertEquals(null, res.getReportMetricsError().getBoardMetricsError()); + assertNull(res.getBoardMetricsCompleted()); + assertNull(res.getPipelineMetricsCompleted()); + assertNull(res.getSourceControlMetricsCompleted()); + assertNull(res.getReportMetricsError().getBoardMetricsError()); } @Test diff --git a/backend/src/test/java/heartbeat/service/report/KanbanFixture.java b/backend/src/test/java/heartbeat/service/report/KanbanFixture.java new file mode 100644 index 0000000000..f508a58c67 --- /dev/null +++ b/backend/src/test/java/heartbeat/service/report/KanbanFixture.java @@ -0,0 +1,41 @@ +package heartbeat.service.report; + +import heartbeat.controller.board.dto.request.RequestJiraBoardColumnSetting; +import heartbeat.controller.board.dto.request.StoryPointsAndCycleTimeRequest; +import heartbeat.controller.board.dto.response.TargetField; +import heartbeat.controller.report.dto.request.JiraBoardSetting; + +import java.util.List; + +public class KanbanFixture { + + public static JiraBoardSetting MOCK_JIRA_BOARD_SETTING() { + return JiraBoardSetting.builder() + .users(List.of("user1")) + .token("token") + .type("jira") + .site("site") + .projectKey("ADM") + .boardId("2") + .boardColumns(List.of(RequestJiraBoardColumnSetting.builder().value("DONE").name("DONE").build())) + .targetFields(List.of(TargetField.builder().key("customer").build())) + .treatFlagCardAsBlock(true) + .assigneeFilter("assignee") + .build(); + } + + public static StoryPointsAndCycleTimeRequest MOCK_EXPECT_STORY_POINT_AND_CYCLE_TIME_REQUEST() { + return StoryPointsAndCycleTimeRequest.builder() + .token("token") + .type("jira") + .site("site") + .project("ADM") + .boardId("2") + .targetFields(List.of(TargetField.builder().key("customer").build())) + .treatFlagCardAsBlock(true) + .startTime("startTime") + .endTime("endTime") + .build(); + } + +} diff --git a/backend/src/test/java/heartbeat/service/report/KanbanServiceTest.java b/backend/src/test/java/heartbeat/service/report/KanbanServiceTest.java index 2e7b376cf0..99b0732bc7 100644 --- a/backend/src/test/java/heartbeat/service/report/KanbanServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/KanbanServiceTest.java @@ -33,8 +33,11 @@ class KanbanServiceTest { @Test void shouldCallCsvServiceToGenerateScvInfo() { + JiraBoardSetting mockJiraBoardSetting = KanbanFixture.MOCK_JIRA_BOARD_SETTING(); GenerateReportRequest request = GenerateReportRequest.builder() - .jiraBoardSetting(JiraBoardSetting.builder().treatFlagCardAsBlock(true).build()) + .jiraBoardSetting(mockJiraBoardSetting) + .startTime("startTime") + .endTime("endTime") .build(); CardCollection realDoneCardCollection = CardCollection.builder().build(); CardCollection nonDoneCardCollection = CardCollection.builder().build(); @@ -49,6 +52,12 @@ void shouldCallCsvServiceToGenerateScvInfo() { assertEquals(realDoneCardCollection, result.getRealDoneCardCollection()); assertEquals(nonDoneCardCollection, result.getNonDoneCardCollection()); verify(kanbanCsvService).generateCsvInfo(request, realDoneCardCollection, nonDoneCardCollection); + verify(jiraService).getStoryPointsAndCycleTimeForNonDoneCards( + KanbanFixture.MOCK_EXPECT_STORY_POINT_AND_CYCLE_TIME_REQUEST(), mockJiraBoardSetting.getBoardColumns(), + mockJiraBoardSetting.getUsers()); + verify(jiraService).getStoryPointsAndCycleTimeForDoneCards( + KanbanFixture.MOCK_EXPECT_STORY_POINT_AND_CYCLE_TIME_REQUEST(), mockJiraBoardSetting.getBoardColumns(), + mockJiraBoardSetting.getUsers(), mockJiraBoardSetting.getAssigneeFilter()); } } diff --git a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java index 68d248f399..dc6ebfcaec 100644 --- a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java @@ -103,8 +103,12 @@ void ShouldInitializeMetricsDataCompletedInHandlerWithPreOneWhenPreOneExisted() @Test void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() throws InterruptedException { - GenerateReportRequest request = GenerateReportRequest.builder().csvTimeStamp("csvTimeStamp").metrics(List.of("velocity", "lead time for changes")).build(); - when(asyncMetricsDataHandler.getMetricsDataCompleted(any())).thenReturn(MetricsDataCompleted.builder().pipelineMetricsCompleted(true).build()); + GenerateReportRequest request = GenerateReportRequest.builder() + .csvTimeStamp("csvTimeStamp") + .metrics(List.of("velocity", "lead time for changes")) + .build(); + when(asyncMetricsDataHandler.getMetricsDataCompleted(any())) + .thenReturn(MetricsDataCompleted.builder().pipelineMetricsCompleted(true).build()); doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request); reportService.generateReportByType(request, MetricType.BOARD); Thread.sleep(100); @@ -117,4 +121,5 @@ void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() thro verify(generateReporterService).generateBoardReport(request); verify(generateReporterService, never()).generateDoraReport(request); } + } From 65b2d4adfefaf9350d814175d8947daf24b1a4b2 Mon Sep 17 00:00:00 2001 From: yulongcai Date: Thu, 1 Feb 2024 10:07:26 +0800 Subject: [PATCH 4/7] ADM-794:[backend]fix: refactor code --- .../service/report/KanbanServiceTest.java | 2 +- .../service/report/PipelineServiceTest.java | 50 ++++++------------- .../service/report/ReportServiceTest.java | 8 +-- 3 files changed, 20 insertions(+), 40 deletions(-) diff --git a/backend/src/test/java/heartbeat/service/report/KanbanServiceTest.java b/backend/src/test/java/heartbeat/service/report/KanbanServiceTest.java index 99b0732bc7..a71c692b7a 100644 --- a/backend/src/test/java/heartbeat/service/report/KanbanServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/KanbanServiceTest.java @@ -32,7 +32,7 @@ class KanbanServiceTest { private KanbanCsvService kanbanCsvService; @Test - void shouldCallCsvServiceToGenerateScvInfo() { + void shouldCallCsvServiceToGenerateCSVInfoWhenJiraBoardSettingIsNotNull() { JiraBoardSetting mockJiraBoardSetting = KanbanFixture.MOCK_JIRA_BOARD_SETTING(); GenerateReportRequest request = GenerateReportRequest.builder() .jiraBoardSetting(mockJiraBoardSetting) diff --git a/backend/src/test/java/heartbeat/service/report/PipelineServiceTest.java b/backend/src/test/java/heartbeat/service/report/PipelineServiceTest.java index 0df4d233b4..556def51e7 100644 --- a/backend/src/test/java/heartbeat/service/report/PipelineServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/PipelineServiceTest.java @@ -16,6 +16,7 @@ import heartbeat.service.report.calculator.model.FetchedData; import heartbeat.service.source.github.GitHubService; import org.assertj.core.util.Lists; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -57,12 +58,24 @@ public class PipelineServiceTest { @Captor ArgumentCaptor> roadMapArgumentCaptor; + private String token; + + private String startTime; + + private String endTime; + + @BeforeEach + public void setup() { + startTime = "startTime"; + endTime = "endTime"; + token = "token"; + } + @Nested class FetchGithubData { @Test void shouldReturnEmptyBuildInfosListAndEmptyLeadTimeWhenDeploymentEnvironmentsIsEmpty() { - String token = "token"; GenerateReportRequest request = GenerateReportRequest.builder() .buildKiteSetting(BuildKiteSetting.builder().deploymentEnvList(new ArrayList<>()).build()) .metrics(new ArrayList<>()) @@ -90,9 +103,6 @@ void shouldReturnEmptyPipelineLeadTimeWhenCodebaseSettingIsEmpty() { @Test void shouldGetPipelineLeadTimeFromGithubServiceAndBuildKiteServiceWhenCodebaseSettingIsNotEmpty() { List fakeBuildKiteBuildInfos = new ArrayList<>(); - String startTime = "startTime"; - String endTime = "endTime"; - String token = "token"; GenerateReportRequest request = GenerateReportRequest.builder() .buildKiteSetting(BuildKiteSetting.builder() .deploymentEnvList(List.of(DeploymentEnvironment.builder().id("env1").repository("repo1").build(), @@ -124,9 +134,6 @@ void shouldGetPipelineLeadTimeFromGithubServiceAndBuildKiteServiceWhenCodebaseSe @Test void shouldGetSecondValueInRoadMapWhenDeployEnvironmentListHasTwoElementWithSameKey() { List fakeBuildKiteBuildInfos = new ArrayList<>(); - String startTime = "startTime"; - String endTime = "endTime"; - String token = "token"; GenerateReportRequest request = GenerateReportRequest.builder() .buildKiteSetting(BuildKiteSetting.builder() .deploymentEnvList(List.of(DeploymentEnvironment.builder().id("env1").repository("repo1").build(), @@ -161,9 +168,6 @@ void shouldFilterAuthorByInputCrews() { BuildKiteBuildInfo.builder() .author(BuildKiteBuildInfo.Author.builder().name("test-author2").build()) .build()); - String startTime = "startTime"; - String endTime = "endTime"; - String token = "token"; GenerateReportRequest request = GenerateReportRequest.builder() .buildKiteSetting(BuildKiteSetting.builder() .deploymentEnvList(List.of(DeploymentEnvironment.builder().id("env1").repository("repo1").build())) @@ -217,9 +221,6 @@ void shouldReturnValueWhenDeploymentEnvListIsNotEmpty() { List fakeBuildKiteBuildInfos = List.of(BuildKiteBuildInfo.builder() .creator(BuildKiteBuildInfo.Creator.builder().name("someone").build()) .build()); - String startTime = "startTime"; - String endTime = "endTime"; - String token = "token"; GenerateReportRequest request = GenerateReportRequest.builder() .buildKiteSetting(BuildKiteSetting.builder() .token(token) @@ -254,9 +255,6 @@ void shouldFilterCreatorByInputCrews() { .creator(BuildKiteBuildInfo.Creator.builder().name("test-creator2").build()) .build(), BuildKiteBuildInfo.builder().creator(null).build()); - String startTime = "startTime"; - String endTime = "endTime"; - String token = "token"; GenerateReportRequest request = GenerateReportRequest.builder() .buildKiteSetting(BuildKiteSetting.builder() .deploymentEnvList(List.of(DeploymentEnvironment.builder().id("env1").repository("repo1").build())) @@ -288,8 +286,6 @@ class GenerateCSVForPipelineWithCodebase { @Test void shouldReturnEmptyWhenDeploymentEnvironmentsIsEmpty() { - String startTime = "startTime"; - String endTime = "endTime"; List result = pipelineService.generateCSVForPipelineWithCodebase( CodebaseSetting.builder().build(), startTime, endTime, FetchedData.BuildKiteData.builder().build(), Lists.list()); @@ -300,8 +296,6 @@ void shouldReturnEmptyWhenDeploymentEnvironmentsIsEmpty() { @Test void shouldReturnEmptyWhenNoBuildInfoFoundForDeploymentEnvironment() { - String startTime = "startTime"; - String endTime = "endTime"; List result = pipelineService.generateCSVForPipelineWithCodebase( CodebaseSetting.builder().build(), startTime, endTime, FetchedData.BuildKiteData.builder().buildInfosList(List.of(Map.entry("env1", List.of()))).build(), @@ -313,8 +307,6 @@ void shouldReturnEmptyWhenNoBuildInfoFoundForDeploymentEnvironment() { @Test void shouldReturnEmptyWhenPipelineStepsIsEmpty() { - String startTime = "startTime"; - String endTime = "endTime"; List kiteBuildInfos = List.of(BuildKiteBuildInfo.builder().build()); when(buildKiteService.getPipelineStepNames(eq(kiteBuildInfos))).thenReturn(List.of()); @@ -331,8 +323,6 @@ void shouldReturnEmptyWhenPipelineStepsIsEmpty() { @Test void shouldReturnEmptyWhenBuildJobIsEmpty() { - String startTime = "startTime"; - String endTime = "endTime"; List kiteBuildInfos = List.of(BuildKiteBuildInfo.builder().build()); when(buildKiteService.getPipelineStepNames(eq(kiteBuildInfos))).thenReturn(List.of("check")); when(buildKiteService.getBuildKiteJob(any(), any(), any(), eq(startTime), eq(endTime))).thenReturn(null); @@ -351,8 +341,6 @@ void shouldReturnEmptyWhenBuildJobIsEmpty() { @Test void shouldFilterOutInvalidBuildOfCommentIsEmtpy() { - String startTime = "startTime"; - String endTime = "endTime"; List kiteBuildInfos = List.of(BuildKiteBuildInfo.builder().commit("").build()); when(buildKiteService.getPipelineStepNames(eq(kiteBuildInfos))).thenReturn(List.of("check")); when(buildKiteService.getStepsBeforeEndStep(any(), any())).thenReturn(List.of("check")); @@ -373,8 +361,6 @@ void shouldFilterOutInvalidBuildOfCommentIsEmtpy() { @Test void shouldGenerateValueWithoutCommitWhenCodebaseSettingIsEmpty() { - String startTime = "startTime"; - String endTime = "endTime"; List kiteBuildInfos = List.of(BuildKiteBuildInfo.builder().commit("commit").build()); when(buildKiteService.getPipelineStepNames(eq(kiteBuildInfos))).thenReturn(List.of("check")); when(buildKiteService.getStepsBeforeEndStep(any(), any())).thenReturn(List.of("check")); @@ -397,8 +383,6 @@ void shouldGenerateValueWithoutCommitWhenCodebaseSettingIsEmpty() { @Test void shouldGenerateValueWithoutCommitWhenCodebaseSettingTokenIsEmpty() { - String startTime = "startTime"; - String endTime = "endTime"; List kiteBuildInfos = List.of(BuildKiteBuildInfo.builder().commit("commit").build()); when(buildKiteService.getPipelineStepNames(eq(kiteBuildInfos))).thenReturn(List.of("check")); when(buildKiteService.getStepsBeforeEndStep(any(), any())).thenReturn(List.of("check")); @@ -422,8 +406,6 @@ void shouldGenerateValueWithoutCommitWhenCodebaseSettingTokenIsEmpty() { @Test void shouldGenerateValueWithoutCommitWhenCommitIdIsEmpty() { - String startTime = "startTime"; - String endTime = "endTime"; List kiteBuildInfos = List.of(BuildKiteBuildInfo.builder().commit("commit").build()); when(buildKiteService.getPipelineStepNames(eq(kiteBuildInfos))).thenReturn(List.of("check")); when(buildKiteService.getStepsBeforeEndStep(any(), any())).thenReturn(List.of("check")); @@ -447,8 +429,6 @@ void shouldGenerateValueWithoutCommitWhenCommitIdIsEmpty() { @Test void shouldGenerateValueHasCommit() { - String startTime = "startTime"; - String endTime = "endTime"; List kiteBuildInfos = List.of(BuildKiteBuildInfo.builder().commit("commit").build()); CommitInfo fakeCommitInfo = CommitInfo.builder().build(); when(buildKiteService.getPipelineStepNames(eq(kiteBuildInfos))).thenReturn(List.of("check")); @@ -477,8 +457,6 @@ void shouldGenerateValueHasCommit() { @Test void shouldGenerateValueWithLeadTimeWhenLeadTimeExisting() { - String startTime = "startTime"; - String endTime = "endTime"; List kiteBuildInfos = List.of(BuildKiteBuildInfo.builder().commit("commit").build()); CommitInfo fakeCommitInfo = CommitInfo.builder().build(); when(buildKiteService.getPipelineStepNames(eq(kiteBuildInfos))).thenReturn(List.of("check")); diff --git a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java index dc6ebfcaec..3e4d0fbb4a 100644 --- a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java @@ -24,6 +24,8 @@ import java.util.List; import java.util.stream.Collectors; +import static heartbeat.controller.report.dto.request.MetricEnum.LEAD_TIME_FOR_CHANGES; +import static heartbeat.controller.report.dto.request.MetricEnum.VELOCITY; import static heartbeat.service.report.scheduler.DeleteExpireCSVScheduler.EXPORT_CSV_VALIDITY_TIME; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -102,16 +104,16 @@ void ShouldInitializeMetricsDataCompletedInHandlerWithPreOneWhenPreOneExisted() } @Test - void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() throws InterruptedException { + void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() { GenerateReportRequest request = GenerateReportRequest.builder() .csvTimeStamp("csvTimeStamp") - .metrics(List.of("velocity", "lead time for changes")) + .metrics(List.of(VELOCITY.getValue(), LEAD_TIME_FOR_CHANGES.getValue())) .build(); when(asyncMetricsDataHandler.getMetricsDataCompleted(any())) .thenReturn(MetricsDataCompleted.builder().pipelineMetricsCompleted(true).build()); doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request); + reportService.generateReportByType(request, MetricType.BOARD); - Thread.sleep(100); MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder() .boardMetricsCompleted(false) .pipelineMetricsCompleted(true) From 5cb1dfc3149f55c4e539d632fcaae0189b528f15 Mon Sep 17 00:00:00 2001 From: yulongcai Date: Thu, 1 Feb 2024 11:39:00 +0800 Subject: [PATCH 5/7] ADM-794:[backend]fix: refactor for report service test --- .../service/report/ReportServiceTest.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java index 3e4d0fbb4a..f00fb67047 100644 --- a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java @@ -76,8 +76,10 @@ void exportCsvShouldThrowNotFoundExceptionWhenTimestampIsValid() { void generateBoardReportByTypeShouldCallGenerateBoardReport() throws InterruptedException { GenerateReportRequest request = GenerateReportRequest.builder().metrics(new ArrayList<>()).build(); doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request); + reportService.generateReportByType(request, MetricType.BOARD); Thread.sleep(100); + verify(generateReporterService).generateBoardReport(request); verify(generateReporterService, never()).generateDoraReport(request); } @@ -86,8 +88,10 @@ void generateBoardReportByTypeShouldCallGenerateBoardReport() throws Interrupted void generateDoraReportByTypeShouldCallGenerateDoraReport() throws InterruptedException { GenerateReportRequest request = GenerateReportRequest.builder().metrics(new ArrayList<>()).build(); doAnswer(invocation -> null).when(generateReporterService).generateDoraReport(request); + reportService.generateReportByType(request, MetricType.DORA); Thread.sleep(100); + verify(generateReporterService).generateDoraReport(request); verify(generateReporterService, never()).generateBoardReport(request); } @@ -97,14 +101,21 @@ void ShouldInitializeMetricsDataCompletedInHandlerWithPreOneWhenPreOneExisted() GenerateReportRequest request = GenerateReportRequest.builder().metrics(new ArrayList<>()).build(); when(asyncMetricsDataHandler.getMetricsDataCompleted(any())).thenReturn(MetricsDataCompleted.builder().build()); doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request); + reportService.generateReportByType(request, MetricType.BOARD); Thread.sleep(100); + verify(generateReporterService).generateBoardReport(request); verify(generateReporterService, never()).generateDoraReport(request); } @Test void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() { + MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder() + .boardMetricsCompleted(false) + .pipelineMetricsCompleted(true) + .sourceControlMetricsCompleted(false) + .build(); GenerateReportRequest request = GenerateReportRequest.builder() .csvTimeStamp("csvTimeStamp") .metrics(List.of(VELOCITY.getValue(), LEAD_TIME_FOR_CHANGES.getValue())) @@ -114,11 +125,7 @@ void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() { doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request); reportService.generateReportByType(request, MetricType.BOARD); - MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder() - .boardMetricsCompleted(false) - .pipelineMetricsCompleted(true) - .sourceControlMetricsCompleted(false) - .build(); + verify(asyncMetricsDataHandler).putMetricsDataCompleted("csvTimeStamp", expectMetricsDataResult); verify(generateReporterService).generateBoardReport(request); verify(generateReporterService, never()).generateDoraReport(request); From 2696ae6a15b99f780eab975cc25551d705a08334 Mon Sep 17 00:00:00 2001 From: yulongcai Date: Thu, 1 Feb 2024 14:41:19 +0800 Subject: [PATCH 6/7] ADM-794:[backend]fix: repair test to add sleep --- .../test/java/heartbeat/service/report/ReportServiceTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java index f00fb67047..563045ea5d 100644 --- a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java @@ -110,7 +110,7 @@ void ShouldInitializeMetricsDataCompletedInHandlerWithPreOneWhenPreOneExisted() } @Test - void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() { + void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() throws InterruptedException { MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder() .boardMetricsCompleted(false) .pipelineMetricsCompleted(true) @@ -124,6 +124,7 @@ void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() { .thenReturn(MetricsDataCompleted.builder().pipelineMetricsCompleted(true).build()); doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request); + Thread.sleep(100); reportService.generateReportByType(request, MetricType.BOARD); verify(asyncMetricsDataHandler).putMetricsDataCompleted("csvTimeStamp", expectMetricsDataResult); From 4e9ed2f4e59c526ce29f7992fbb82cb3402da70d Mon Sep 17 00:00:00 2001 From: yulongcai Date: Thu, 1 Feb 2024 15:40:57 +0800 Subject: [PATCH 7/7] ADM-794:[backend]fix: repair test --- .../test/java/heartbeat/service/report/ReportServiceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java index 563045ea5d..cdbd4e6542 100644 --- a/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java +++ b/backend/src/test/java/heartbeat/service/report/ReportServiceTest.java @@ -124,8 +124,8 @@ void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() thro .thenReturn(MetricsDataCompleted.builder().pipelineMetricsCompleted(true).build()); doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request); - Thread.sleep(100); reportService.generateReportByType(request, MetricType.BOARD); + Thread.sleep(100); verify(asyncMetricsDataHandler).putMetricsDataCompleted("csvTimeStamp", expectMetricsDataResult); verify(generateReporterService).generateBoardReport(request);