Skip to content

Commit

Permalink
[backend] Small fix on an error when adding a scenario/teams/users (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimfacion authored Sep 27, 2024
1 parent 4ab50ab commit 32464a1
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 12 deletions.
40 changes: 28 additions & 12 deletions openbas-api/src/main/java/io/openbas/service/InjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,20 @@ private void computeInjectInExercise(@NotNull Exercise exercise, @NotNull Iterab
});
inject.getTeams().forEach(team -> team.getUsers().forEach(user -> {
if(!exercise.getTeamUsers().contains(user)) {
ExerciseTeamUser exerciseTeamUser = new ExerciseTeamUser();
exerciseTeamUser.setExercise(exercise);
exerciseTeamUser.setTeam(team);
exerciseTeamUser.setUser(user);
exerciseTeamUserRepository.save(exerciseTeamUser);
exercise.getTeamUsers().add(exerciseTeamUser);
ExerciseTeamUserId compositeId = new ExerciseTeamUserId();
compositeId.setExerciseId(exercise.getId());
compositeId.setTeamId(team.getId());
compositeId.setUserId(user.getId());
boolean exists = exerciseTeamUserRepository.findById(compositeId).isPresent();

if(!exists) {
ExerciseTeamUser exerciseTeamUser = new ExerciseTeamUser();
exerciseTeamUser.setExercise(exercise);
exerciseTeamUser.setTeam(team);
exerciseTeamUser.setUser(user);
exerciseTeamUserRepository.save(exerciseTeamUser);
exercise.getTeamUsers().add(exerciseTeamUser);
}
}
}));
});
Expand All @@ -350,12 +358,20 @@ private void computeInjectInScenario(@NotNull Scenario scenario, @NotNull Iterab
});
inject.getTeams().forEach(team -> team.getUsers().forEach(user -> {
if(!scenario.getTeamUsers().contains(user)) {
ScenarioTeamUser scenarioTeamUser = new ScenarioTeamUser();
scenarioTeamUser.setScenario(scenario);
scenarioTeamUser.setTeam(team);
scenarioTeamUser.setUser(user);
scenarioTeamUserRepository.save(scenarioTeamUser);
scenario.getTeamUsers().add(scenarioTeamUser);
ScenarioTeamUserId compositeId = new ScenarioTeamUserId();
compositeId.setScenarioId(scenario.getId());
compositeId.setTeamId(team.getId());
compositeId.setUserId(user.getId());
boolean exists = scenarioTeamUserRepository.findById(compositeId).isPresent();

if(!exists) {
ScenarioTeamUser scenarioTeamUser = new ScenarioTeamUser();
scenarioTeamUser.setScenario(scenario);
scenarioTeamUser.setTeam(team);
scenarioTeamUser.setUser(user);
scenarioTeamUserRepository.save(scenarioTeamUser);
scenario.getTeamUsers().add(scenarioTeamUser);
}
}
}));
});
Expand Down
109 changes: 109 additions & 0 deletions openbas-api/src/test/java/io/openbas/service/InjectServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class InjectServiceTest {

private Scenario mockedScenario;

private Exercise mockedExercise;

private ImportMapper mockedImportMapper;

private InjectsImportInput mockedInjectsImportInput;
Expand All @@ -82,6 +84,7 @@ void setUp() {
assetRepository, assetGroupRepository, scenarioTeamUserRepository, exerciseTeamUserRepository, teamRepository, userRepository);

mockedScenario = new Scenario();
mockedExercise = new Exercise();
mapper = new ObjectMapper();
}

Expand Down Expand Up @@ -250,11 +253,64 @@ void testImportXlsAbsoluteDate() throws IOException {
when(userRepository.findById(any())).thenReturn(Optional.of(mockedUser));
Team team1 = new Team();
team1.setName("team1");
team1.setUsers(List.of(new User()));
Team team2 = new Team();
team2.setName("team2");
team1.setUsers(List.of(new User()));
when(teamRepository.findAll()).thenReturn(List.of(team1));
when(teamRepository.save(any())).thenReturn(team2);

when(injectRepository.saveAll(any())).thenReturn(List.of(createNewInject(List.of(team1)), new Inject()));

sessionHelper.when(SessionHelper::currentUser).thenReturn(new OpenBASOAuth2User(mockedUser));
ImportTestSummary importTestSummary =
injectService.importInjectIntoScenarioFromXLS(mockedScenario,
mockedImportMapper, fileID, "CHECKLIST", 120, true);

assertTrue(LocalDateTime.of(2024, Month.JUNE, 26, 0, 0)
.toInstant(ZoneOffset.of("Z"))
.equals(mockedScenario.getRecurrenceStart()));
assertTrue("0 0 7 * * *".equals(mockedScenario.getRecurrence()));
}
}

@DisplayName("Import an XLS file with absolute date")
@Test
void testImportXlsAbsoluteDateWithExistingScenarioTeamUser() throws IOException {
try (MockedStatic<SessionHelper> sessionHelper = Mockito.mockStatic(SessionHelper.class)) {
User mockedUser = new User();
String fileID = UUID.randomUUID().toString();
File testFile = ResourceUtils.getFile("classpath:xls-test-files/test_file_2.xlsx");
createTempFile(testFile, fileID);

mockedScenario = new Scenario();
mockedScenario.setId(UUID.randomUUID().toString());

mockedImportMapper = createImportMapper(UUID.randomUUID().toString());
mockedImportMapper.getInjectImporters().forEach(injectImporter -> {
injectImporter.setRuleAttributes(injectImporter.getRuleAttributes().stream()
.map(ruleAttribute -> {
if("trigger_time".equals(ruleAttribute.getName())) {
ruleAttribute.setAdditionalConfig(Map.of("timePattern", "dd/MM/yyyy HH'h'mm"));
}
return ruleAttribute;
}).toList()
);
});
when(userRepository.findById(any())).thenReturn(Optional.of(mockedUser));
Team team1 = new Team();
team1.setName("team1");
team1.setUsers(List.of(new User()));
Team team2 = new Team();
team2.setName("team2");
team1.setUsers(List.of(new User()));
when(scenarioTeamUserRepository.findById(any()))
.thenReturn(Optional.of(new ScenarioTeamUser()));
when(teamRepository.findAll()).thenReturn(List.of(team1));
when(teamRepository.save(any())).thenReturn(team2);

when(injectRepository.saveAll(any())).thenReturn(List.of(createNewInject(List.of(team1)), new Inject()));

sessionHelper.when(SessionHelper::currentUser).thenReturn(new OpenBASOAuth2User(mockedUser));
ImportTestSummary importTestSummary =
injectService.importInjectIntoScenarioFromXLS(mockedScenario,
Expand Down Expand Up @@ -484,6 +540,52 @@ void testImportXlsDefaultValue() throws IOException {
}
}

@DisplayName("Import an XLS file in an exercise")
@Test
void testImportXlsWithExercise() throws IOException {
try (MockedStatic<SessionHelper> sessionHelper = Mockito.mockStatic(SessionHelper.class)) {
User mockedUser = new User();
String fileID = UUID.randomUUID().toString();
File testFile = ResourceUtils.getFile("classpath:xls-test-files/test_file_2.xlsx");
createTempFile(testFile, fileID);

mockedExercise = new Exercise();
mockedExercise.setId(UUID.randomUUID().toString());

mockedImportMapper = createImportMapper(UUID.randomUUID().toString());
mockedImportMapper.getInjectImporters().forEach(injectImporter -> {
injectImporter.setRuleAttributes(injectImporter.getRuleAttributes().stream()
.map(ruleAttribute -> {
if("trigger_time".equals(ruleAttribute.getName())) {
ruleAttribute.setAdditionalConfig(Map.of("timePattern", "dd/MM/yyyy HH'h'mm"));
}
return ruleAttribute;
}).toList()
);
});
when(userRepository.findById(any())).thenReturn(Optional.of(mockedUser));
Team team1 = new Team();
team1.setName("team1");
team1.setUsers(List.of(new User()));
Team team2 = new Team();
team2.setName("team2");
team1.setUsers(List.of(new User()));
when(teamRepository.findAll()).thenReturn(List.of(team1));
when(teamRepository.save(any())).thenReturn(team2);

when(injectRepository.saveAll(any())).thenReturn(List.of(createNewInject(List.of(team1)), new Inject()));

sessionHelper.when(SessionHelper::currentUser).thenReturn(new OpenBASOAuth2User(mockedUser));
ImportTestSummary importTestSummary =
injectService.importInjectIntoExerciseFromXLS(mockedExercise,
mockedImportMapper, fileID, "CHECKLIST", 120, true);

assertTrue(LocalDateTime.of(2024, Month.JUNE, 26, 0, 0)
.toInstant(ZoneOffset.of("Z"))
.equals(mockedExercise.getStart().get()));
}
}

private void createTempFile(File testFile, String fileID) throws IOException {
InputStream in = new FileInputStream(testFile);
MockMultipartFile file = new MockMultipartFile("file",
Expand Down Expand Up @@ -668,4 +770,11 @@ private Object deepCopy(Object objectToCopy, Class classToCopy) throws JsonProce
return objectMapper
.readValue(objectMapper.writeValueAsString(objectToCopy), classToCopy);
}

private Inject createNewInject(List<Team> teams) {
Inject inject = new Inject();
inject.setTeams(teams);
inject.setId(UUID.randomUUID().toString());
return inject;
}
}

0 comments on commit 32464a1

Please sign in to comment.