Skip to content

Commit

Permalink
ADM-791:[backend]refactor: refactor initialization part and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnjoa committed Feb 4, 2024
1 parent 7286113 commit 5084fab
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 70 deletions.
51 changes: 25 additions & 26 deletions backend/src/main/java/heartbeat/service/report/ReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
import heartbeat.exception.NotFoundException;
import heartbeat.handler.AsyncMetricsDataHandler;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

import static heartbeat.service.report.scheduler.DeleteExpireCSVScheduler.EXPORT_CSV_VALIDITY_TIME;
import static heartbeat.util.ValueUtil.valueOrDefault;

@Service
@RequiredArgsConstructor
Expand All @@ -38,7 +36,7 @@ private boolean isExpiredTimeStamp(long timeStamp) {
}

public void generateReportByType(GenerateReportRequest request, MetricType metricType) {
initializeMetricsDataCompletedInHandler(request);
initializeMetricsDataCompletedInHandler(request.getCsvTimeStamp(), metricType);
CompletableFuture.runAsync(() -> {
switch (metricType) {
case BOARD -> generateReporterService.generateBoardReport(request);
Expand All @@ -50,33 +48,34 @@ public void generateReportByType(GenerateReportRequest request, MetricType metri
});
}

private MetricsDataCompleted createMetricsDataCompleted(MetricsDataCompleted metricProcess,
private MetricsDataCompleted getInitializedData(MetricType metricType,
MetricsDataCompleted previousMetricsCompleted) {
return previousMetricsCompleted == null
? MetricsDataCompleted.builder()
.boardMetricsCompleted(metricProcess.boardMetricsCompleted())
.pipelineMetricsCompleted(metricProcess.pipelineMetricsCompleted())
.sourceControlMetricsCompleted(metricProcess.sourceControlMetricsCompleted())
.build()
: MetricsDataCompleted.builder()
.boardMetricsCompleted(valueOrDefault(previousMetricsCompleted.boardMetricsCompleted(),
metricProcess.boardMetricsCompleted()))
.pipelineMetricsCompleted(valueOrDefault(previousMetricsCompleted.pipelineMetricsCompleted(),
metricProcess.pipelineMetricsCompleted()))
.sourceControlMetricsCompleted(
valueOrDefault(previousMetricsCompleted.sourceControlMetricsCompleted(),
metricProcess.sourceControlMetricsCompleted()))
.build();

MetricsDataCompleted metricsDataCompleted = MetricsDataCompleted.builder().build();
if (previousMetricsCompleted == null) {
if (metricType == MetricType.BOARD) {
metricsDataCompleted.setBoardMetricsCompleted(false);
}
else {
metricsDataCompleted.setDoraMetricsCompleted(false);
}
}
else {
if (metricType == MetricType.BOARD) {
metricsDataCompleted.setBoardMetricsCompleted(false);
metricsDataCompleted.setDoraMetricsCompleted(previousMetricsCompleted.doraMetricsCompleted());
}
else {
metricsDataCompleted.setDoraMetricsCompleted(false);
metricsDataCompleted.setBoardMetricsCompleted(previousMetricsCompleted.boardMetricsCompleted());
}
}
return metricsDataCompleted;
}

private void initializeMetricsDataCompletedInHandler(GenerateReportRequest request) {
MetricsDataCompleted metricsStatus = request.getMetricsStatus(Boolean.FALSE);
String timeStamp = request.getCsvTimeStamp();
public void initializeMetricsDataCompletedInHandler(String timeStamp, MetricType metricType) {
MetricsDataCompleted previousMetricsCompleted = asyncMetricsDataHandler.getMetricsDataCompleted(timeStamp);
MetricsDataCompleted isMetricsDataCompleted = createMetricsDataCompleted(metricsStatus,
previousMetricsCompleted);
asyncMetricsDataHandler.putMetricsDataCompleted(timeStamp, isMetricsDataCompleted);
MetricsDataCompleted initializedData = getInitializedData(metricType, previousMetricsCompleted);
asyncMetricsDataHandler.putMetricsDataCompleted(timeStamp, initializedData);
}

}
164 changes: 120 additions & 44 deletions backend/src/test/java/heartbeat/service/report/ReportServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import heartbeat.controller.report.dto.response.MetricsDataCompleted;
import heartbeat.exception.NotFoundException;
import heartbeat.handler.AsyncMetricsDataHandler;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -72,64 +73,139 @@ void exportCsvShouldThrowNotFoundExceptionWhenTimestampIsValid() {
verify(csvFileGenerator, never()).getDataFromCSV(ReportType.METRIC, invalidTimestamp);
}

@Test
void generateBoardReportByTypeShouldCallGenerateBoardReport() throws InterruptedException {
GenerateReportRequest request = GenerateReportRequest.builder().metrics(new ArrayList<>()).build();
doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request);
@Nested
class GenerateReportByType {

reportService.generateReportByType(request, MetricType.BOARD);
Thread.sleep(100);
String timeStamp = "1683734399999";

verify(generateReporterService).generateBoardReport(request);
verify(generateReporterService, never()).generateDoraReport(request);
}
GenerateReportRequest request = GenerateReportRequest.builder()
.csvTimeStamp(timeStamp)
.metrics(new ArrayList<>())
.build();

@Test
void generateDoraReportByTypeShouldCallGenerateDoraReport() throws InterruptedException {
GenerateReportRequest request = GenerateReportRequest.builder().metrics(new ArrayList<>()).build();
doAnswer(invocation -> null).when(generateReporterService).generateDoraReport(request);
MetricsDataCompleted metricsDataCompleted = MetricsDataCompleted.builder()
.boardMetricsCompleted(true)
.doraMetricsCompleted(true)
.build();

reportService.generateReportByType(request, MetricType.DORA);
Thread.sleep(100);
@Test
void shouldCallGenerateBoardReportWhenMetricTypeIsBoard() throws InterruptedException {
MetricsDataCompleted expected = MetricsDataCompleted.builder()
.boardMetricsCompleted(false)
.doraMetricsCompleted(true)
.build();
when(asyncMetricsDataHandler.getMetricsDataCompleted(timeStamp)).thenReturn(metricsDataCompleted);
doAnswer(invocation -> null).when(asyncMetricsDataHandler).putMetricsDataCompleted(any(), any());
doAnswer(invocation -> null).when(generateReporterService).generateBoardReport(request);

reportService.generateReportByType(request, MetricType.BOARD);
Thread.sleep(100);

verify(asyncMetricsDataHandler).getMetricsDataCompleted(timeStamp);
verify(asyncMetricsDataHandler).putMetricsDataCompleted(timeStamp, expected);
verify(generateReporterService).generateBoardReport(request);
verify(generateReporterService, never()).generateDoraReport(request);
}

@Test
void shouldCallGenerateDoraReportWhenMetricTypeIsDora() throws InterruptedException {
MetricsDataCompleted expected = MetricsDataCompleted.builder()
.boardMetricsCompleted(true)
.doraMetricsCompleted(false)
.build();
when(asyncMetricsDataHandler.getMetricsDataCompleted(timeStamp)).thenReturn(metricsDataCompleted);
doAnswer(invocation -> null).when(asyncMetricsDataHandler).putMetricsDataCompleted(any(), any());
doAnswer(invocation -> null).when(generateReporterService).generateDoraReport(request);

reportService.generateReportByType(request, MetricType.DORA);
Thread.sleep(100);

verify(asyncMetricsDataHandler).getMetricsDataCompleted(timeStamp);
verify(asyncMetricsDataHandler).putMetricsDataCompleted(timeStamp, expected);
verify(generateReporterService).generateDoraReport(request);
verify(generateReporterService, never()).generateBoardReport(request);
}

verify(generateReporterService).generateDoraReport(request);
verify(generateReporterService, never()).generateBoardReport(request);
}

@Test
void ShouldInitializeMetricsDataCompletedInHandlerWithPreOneWhenPreOneExisted() throws InterruptedException {
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);
@Nested
class InitializeMetricsDataCompletedInHandler {

verify(generateReporterService).generateBoardReport(request);
verify(generateReporterService, never()).generateDoraReport(request);
}
String timeStamp = "1683734399999";

@Test
void ShouldInitializeMetricsDataCompletedInHandlerWhenRequestMetricsExist() throws InterruptedException {
MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder()
.boardMetricsCompleted(false)
.pipelineMetricsCompleted(true)
.sourceControlMetricsCompleted(false)
.build();
GenerateReportRequest request = GenerateReportRequest.builder()
.csvTimeStamp("csvTimeStamp")
.csvTimeStamp(timeStamp)
.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);
@Test
void shouldInitializeBoardMetricsCompletedFalseWhenPreviousIsNull() {
MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder()
.boardMetricsCompleted(false)
.doraMetricsCompleted(null)
.build();
when(asyncMetricsDataHandler.getMetricsDataCompleted(any())).thenReturn(null);
doAnswer(invocation -> null).when(asyncMetricsDataHandler).putMetricsDataCompleted(any(), any());

reportService.initializeMetricsDataCompletedInHandler(timeStamp, MetricType.BOARD);

verify(asyncMetricsDataHandler).getMetricsDataCompleted(timeStamp);
verify(asyncMetricsDataHandler).putMetricsDataCompleted(timeStamp, expectMetricsDataResult);
}

@Test
void shouldInitializeDoraMetricsCompletedFalseWhenPreviousIsNull() {
MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder()
.boardMetricsCompleted(null)
.doraMetricsCompleted(false)
.build();
when(asyncMetricsDataHandler.getMetricsDataCompleted(any())).thenReturn(null);
doAnswer(invocation -> null).when(asyncMetricsDataHandler).putMetricsDataCompleted(any(), any());

reportService.initializeMetricsDataCompletedInHandler(timeStamp, MetricType.DORA);

verify(asyncMetricsDataHandler).getMetricsDataCompleted(timeStamp);
verify(asyncMetricsDataHandler).putMetricsDataCompleted(timeStamp, expectMetricsDataResult);
}

@Test
void shouldInitializeBoardMetricsCompletedFalseAndKeepPreviousDoraValueWhenPreviousIsNotNull() {
MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder()
.boardMetricsCompleted(false)
.doraMetricsCompleted(true)
.build();
MetricsDataCompleted previousMetricsData = MetricsDataCompleted.builder()
.boardMetricsCompleted(true)
.doraMetricsCompleted(true)
.build();
when(asyncMetricsDataHandler.getMetricsDataCompleted(any())).thenReturn(previousMetricsData);
doAnswer(invocation -> null).when(asyncMetricsDataHandler).putMetricsDataCompleted(any(), any());

reportService.initializeMetricsDataCompletedInHandler(timeStamp, MetricType.BOARD);

verify(asyncMetricsDataHandler).getMetricsDataCompleted(timeStamp);
verify(asyncMetricsDataHandler).putMetricsDataCompleted(timeStamp, expectMetricsDataResult);
}

@Test
void shouldInitializeDoraMetricsCompletedFalseAndKeepPreviousBoardValueWhenPreviousIsNotNull() {
MetricsDataCompleted expectMetricsDataResult = MetricsDataCompleted.builder()
.boardMetricsCompleted(true)
.doraMetricsCompleted(false)
.build();
MetricsDataCompleted previousMetricsData = MetricsDataCompleted.builder()
.boardMetricsCompleted(true)
.doraMetricsCompleted(true)
.build();
when(asyncMetricsDataHandler.getMetricsDataCompleted(any())).thenReturn(previousMetricsData);
doAnswer(invocation -> null).when(asyncMetricsDataHandler).putMetricsDataCompleted(any(), any());

reportService.initializeMetricsDataCompletedInHandler(timeStamp, MetricType.DORA);

verify(asyncMetricsDataHandler).getMetricsDataCompleted(timeStamp);
verify(asyncMetricsDataHandler).putMetricsDataCompleted(timeStamp, expectMetricsDataResult);
}

verify(asyncMetricsDataHandler).putMetricsDataCompleted("csvTimeStamp", expectMetricsDataResult);
verify(generateReporterService).generateBoardReport(request);
verify(generateReporterService, never()).generateDoraReport(request);
}

}

0 comments on commit 5084fab

Please sign in to comment.