Skip to content

Commit

Permalink
ADM-791:[backend]test:add status 500 exception for ReporterController
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnjoa committed Feb 4, 2024
1 parent 5084fab commit 0205d81
Showing 1 changed file with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import java.io.ByteArrayInputStream;
import java.io.File;

import static heartbeat.service.report.scheduler.DeleteExpireCSVScheduler.EXPORT_CSV_VALIDITY_TIME;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -57,12 +60,10 @@ class ReporterControllerTest {
private final ObjectMapper mapper = new ObjectMapper();

@Test
void shouldReturnCreatedStatusWhenCheckGenerateReportIsTrue() throws Exception {
void shouldReturnCreatedStatusWhenAllMetricsCompletedIsTrue() throws Exception {
String reportId = Long.toString(System.currentTimeMillis());
ReportResponse expectedReportResponse = mapper.readValue(new File(RESPONSE_FILE_PATH), ReportResponse.class);

when(generateReporterService.checkGenerateReportIsDone(reportId)).thenReturn(true);
when(generateReporterService.getComposedReportResponse(reportId, true)).thenReturn(expectedReportResponse);
when(generateReporterService.getComposedReportResponse(reportId)).thenReturn(expectedReportResponse);

MockHttpServletResponse response = mockMvc
.perform(get("/reports/{reportId}", reportId).contentType(MediaType.APPLICATION_JSON))
Expand All @@ -76,15 +77,14 @@ void shouldReturnCreatedStatusWhenCheckGenerateReportIsTrue() throws Exception {
}

@Test
void shouldReturnOkStatusWhenCheckGenerateReportIsFalse() throws Exception {
void shouldReturnOkStatusWhenAllMetricsCompletedIsFalse() throws Exception {
String reportId = Long.toString(System.currentTimeMillis());
ReportResponse reportResponse = ReportResponse.builder()
.boardMetricsCompleted(false)
.allMetricsCompleted(false)
.build();

when(generateReporterService.checkGenerateReportIsDone(reportId)).thenReturn(false);
when(generateReporterService.getComposedReportResponse(reportId, false)).thenReturn(reportResponse);
when(generateReporterService.getComposedReportResponse(reportId)).thenReturn(reportResponse);

mockMvc.perform(get("/reports/{reportId}", reportId).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
Expand All @@ -94,16 +94,15 @@ void shouldReturnOkStatusWhenCheckGenerateReportIsFalse() throws Exception {
}

@Test
void shouldReturnInternalServerErrorStatusWhenCheckGenerateReportThrowException() throws Exception {
String reportId = Long.toString(System.currentTimeMillis());

when(generateReporterService.checkGenerateReportIsDone(reportId))
.thenThrow(new GenerateReportException("Report time expires"));
void shouldReturn500StatusWhenReportTimeIsExpired() throws Exception {
String reportId = Long.toString(System.currentTimeMillis() - EXPORT_CSV_VALIDITY_TIME - 200L);
doThrow(new GenerateReportException("Failed to get report due to report time expires"))
.when(generateReporterService)
.getComposedReportResponse(any());

mockMvc.perform(get("/reports/{reportId}", reportId).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("$.message").value("Report time expires"))
.andExpect(jsonPath("$.hintInfo").value("Failed to generate report"))
.andExpect(jsonPath("$.message").value("Failed to get report due to report time expires"))
.andReturn()
.getResponse();
}
Expand Down

0 comments on commit 0205d81

Please sign in to comment.