-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[frontend/backend] add tests + improve code
- Loading branch information
Showing
10 changed files
with
315 additions
and
15 deletions.
There are no files selected for viewing
169 changes: 169 additions & 0 deletions
169
openbas-api/src/test/java/io/openbas/rest/ReportApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package io.openbas.rest; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.jayway.jsonpath.JsonPath; | ||
import io.openbas.database.model.*; | ||
import io.openbas.rest.exercise.ExerciseService; | ||
import io.openbas.rest.mapper.MapperApi; | ||
import io.openbas.rest.report.ReportApi; | ||
import io.openbas.rest.report.form.ReportInput; | ||
import io.openbas.service.ReportService; | ||
import io.openbas.utils.fixtures.PaginationFixture; | ||
import io.openbas.utils.mockUser.WithMockPlannerUser; | ||
import org.junit.jupiter.api.*; | ||
import org.mockito.Mock; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static io.openbas.utils.JsonUtils.asJsonString; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
@TestInstance(PER_CLASS) | ||
public class ReportApiTest { | ||
|
||
private MockMvc mvc; | ||
|
||
@Mock | ||
private ReportService reportService; | ||
@Mock | ||
private ExerciseService exerciseService; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
private Exercise exercise; | ||
private Report report; | ||
private ReportInput reportInput; | ||
|
||
@BeforeEach | ||
void before() throws IllegalAccessException, NoSuchFieldException { | ||
ReportApi reportApi = new ReportApi(exerciseService, reportService); | ||
Field sessionContextField = MapperApi.class.getSuperclass().getDeclaredField("mapper"); | ||
sessionContextField.setAccessible(true); | ||
sessionContextField.set(reportApi, objectMapper); | ||
mvc = MockMvcBuilders.standaloneSetup(reportApi) | ||
.build(); | ||
|
||
exercise = new Exercise(); | ||
exercise.setName("Exercise name"); | ||
exercise.setId("exercise123"); | ||
report = new Report(); | ||
report.setId(UUID.randomUUID().toString()); | ||
reportInput = new ReportInput(); | ||
reportInput.setName("Report name"); | ||
} | ||
|
||
@Nested | ||
@WithMockPlannerUser | ||
@DisplayName("Reports for exercise") | ||
class ReportsForExercise { | ||
@DisplayName("Create report") | ||
@Test | ||
void createReportForExercise() throws Exception { | ||
// -- PREPARE -- | ||
when(exerciseService.exercise(anyString())).thenReturn(exercise); | ||
when(reportService.updateReport(any(Report.class), any(ReportInput.class))) | ||
.thenReturn(report); | ||
|
||
// -- EXECUTE -- | ||
String response = mvc | ||
.perform(MockMvcRequestBuilders.post("/api/exercises/"+exercise.getId()+"/reports") | ||
.content(asJsonString(reportInput)) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().is2xxSuccessful()) | ||
.andReturn() | ||
.getResponse() | ||
.getContentAsString(); | ||
|
||
// -- ASSERT -- | ||
verify(exerciseService).exercise(exercise.getId()); | ||
assertNotNull(response); | ||
assertEquals(JsonPath.read(response, "$.report_id"), report.getId()); | ||
} | ||
|
||
@DisplayName("Retrieve reports") | ||
@Test | ||
void retrieveReportForExercise() throws Exception { | ||
// PREPARE | ||
List<Report> reports = List.of(report); | ||
when(reportService.reportsFromExercise(anyString())).thenReturn(reports); | ||
|
||
// -- EXECUTE -- | ||
String response = mvc | ||
.perform(MockMvcRequestBuilders.get( "/api/exercises/fakeExercisesId123/reports") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().is2xxSuccessful()) | ||
.andReturn() | ||
.getResponse() | ||
.getContentAsString(); | ||
|
||
// -- ASSERT -- | ||
verify(reportService).reportsFromExercise("fakeExercisesId123"); | ||
assertNotNull(response); | ||
assertEquals(JsonPath.read(response, "$[0].report_id"), report.getId()); | ||
} | ||
|
||
@DisplayName("Update Report") | ||
@Test | ||
void updateReportForExercise() throws Exception { | ||
// -- PREPARE -- | ||
report.setExercise(exercise); | ||
when(reportService.report(any())).thenReturn(report); | ||
when(reportService.updateReport(any(Report.class), any(ReportInput.class))) | ||
.thenReturn(report); | ||
|
||
// -- EXECUTE -- | ||
String response = mvc | ||
.perform(MockMvcRequestBuilders.put("/api/exercises/"+ exercise.getId() +"/reports/"+ report.getId()) | ||
.content(asJsonString(reportInput)) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().is2xxSuccessful()) | ||
.andReturn() | ||
.getResponse() | ||
.getContentAsString(); | ||
|
||
// -- ASSERT -- | ||
report.setName("fake"); | ||
verify(reportService).report(UUID.fromString(report.getId())); | ||
verify(reportService).updateReport(report, reportInput); | ||
assertNotNull(response); | ||
assertEquals(JsonPath.read(response, "$.report_id"), report.getId()); | ||
} | ||
|
||
@DisplayName("Delete Report") | ||
@Test | ||
void deleteReportForExercise() throws Exception { | ||
// -- PREPARE -- | ||
report.setExercise(exercise); | ||
when(reportService.report(any())).thenReturn(report); | ||
|
||
// -- EXECUTE -- | ||
mvc.perform(MockMvcRequestBuilders.delete("/api/exercises/" + exercise.getId() +"/reports/"+ report.getId()) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(asJsonString(PaginationFixture.getDefault().textSearch("").build()))) | ||
.andExpect(status().is2xxSuccessful()); | ||
|
||
// -- ASSERT -- | ||
verify(reportService, times(1)).deleteReport(UUID.fromString(report.getId())); | ||
} | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
openbas-api/src/test/java/io/openbas/service/ReportServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package io.openbas.service; | ||
|
||
import io.openbas.database.model.Report; | ||
import io.openbas.database.model.ReportInformation; | ||
import io.openbas.database.model.ReportInformationsType; | ||
import io.openbas.database.repository.ReportRepository; | ||
import io.openbas.rest.report.form.ReportInformationInput; | ||
import io.openbas.rest.report.form.ReportInput; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootTest | ||
@ExtendWith(MockitoExtension.class) | ||
public class ReportServiceTest { | ||
|
||
@Mock | ||
private ReportRepository reportRepository; | ||
|
||
private ReportService reportService; | ||
|
||
@BeforeEach | ||
void before() { | ||
// Injecting mocks into the controller | ||
reportService = new ReportService(reportRepository); | ||
} | ||
|
||
@DisplayName("Test create a report") | ||
@Test | ||
void createReport() throws Exception { | ||
// -- PREPARE -- | ||
Report report = new Report(); | ||
|
||
ReportInput reportInput = new ReportInput(); | ||
reportInput.setName("test"); | ||
ReportInformationInput reportInformationInput = new ReportInformationInput(); | ||
reportInformationInput.setReportInformationsType(ReportInformationsType.MAIN_INFORMATION); | ||
reportInformationInput.setReportInformationsDisplay(true); | ||
ReportInformationInput reportInformationInput2 = new ReportInformationInput(); | ||
reportInformationInput2.setReportInformationsType(ReportInformationsType.SCORE_DETAILS); | ||
reportInformationInput2.setReportInformationsDisplay(true); | ||
reportInput.setReportInformations(List.of(reportInformationInput, reportInformationInput2)); | ||
|
||
when(reportRepository.save(any(Report.class))).thenReturn(report); | ||
|
||
// -- EXECUTE -- | ||
reportService.updateReport(report, reportInput); | ||
|
||
// -- ASSERT -- | ||
ArgumentCaptor<Report> reportCaptor = ArgumentCaptor.forClass(Report.class); | ||
verify(reportRepository).save(reportCaptor.capture()); | ||
Report capturedReport= reportCaptor.getValue(); | ||
assertEquals(reportInput.getName(), capturedReport.getName()); | ||
assertEquals(2, capturedReport.getReportInformations().size()); | ||
ReportInformation reportInformationCaptured = capturedReport.getReportInformations().stream() | ||
.filter(r -> r.getReportInformationsType() == ReportInformationsType.MAIN_INFORMATION) | ||
.findFirst() | ||
.orElse(null); | ||
assert reportInformationCaptured != null; | ||
assertEquals(true, reportInformationCaptured.getReportInformationsDisplay()); | ||
ReportInformation reportInformationCaptured2 = capturedReport.getReportInformations().stream() | ||
.filter(r -> r.getReportInformationsType() == ReportInformationsType.SCORE_DETAILS) | ||
.findFirst() | ||
.orElse(null); | ||
assert reportInformationCaptured2 != null; | ||
assertEquals(true, reportInformationCaptured2.getReportInformationsDisplay()); | ||
} | ||
|
||
@DisplayName("Test update a report") | ||
@Test | ||
void updateReport() throws Exception { | ||
// -- PREPARE -- | ||
Report report = new Report(); | ||
report.setName("test"); | ||
ReportInformation reportInformation = new ReportInformation(); | ||
reportInformation.setReportInformationsType(ReportInformationsType.MAIN_INFORMATION); | ||
reportInformation.setReportInformationsDisplay(false); | ||
report.setReportInformations(List.of(reportInformation)); | ||
|
||
ReportInput reportInput = new ReportInput(); | ||
reportInput.setName("new name test"); | ||
ReportInformationInput reportInformationInput = new ReportInformationInput(); | ||
reportInformationInput.setReportInformationsType(ReportInformationsType.MAIN_INFORMATION); | ||
reportInformationInput.setReportInformationsDisplay(true); | ||
reportInput.setReportInformations(List.of(reportInformationInput)); | ||
|
||
when(reportRepository.save(any(Report.class))).thenReturn(report); | ||
|
||
// -- EXECUTE -- | ||
reportService.updateReport(report, reportInput); | ||
|
||
// -- ASSERT -- | ||
ArgumentCaptor<Report> reportCaptor = ArgumentCaptor.forClass(Report.class); | ||
verify(reportRepository).save(reportCaptor.capture()); | ||
Report capturedReport= reportCaptor.getValue(); | ||
assertEquals(reportInput.getName(), capturedReport.getName()); | ||
assertEquals(1, capturedReport.getReportInformations().size()); | ||
assertEquals(true, capturedReport.getReportInformations().getFirst().getReportInformationsDisplay()); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
openbas-front/src/admin/components/simulations/simulation/ExerciseMainInformation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.