Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend] Fix raw teams request (#1704) #1721

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 130 additions & 118 deletions openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,124 +17,136 @@

public class TeamHelper {

public static List<TeamSimple> rawTeamToSimplerTeam(List<RawTeam> teams,
InjectExpectationRepository injectExpectationRepository,
InjectRepository injectRepository,
CommunicationRepository communicationRepository,
ExerciseTeamUserRepository exerciseTeamUserRepository,
ScenarioRepository scenarioRepository) {
// Getting a map of inject expectations
Map<String, RawInjectExpectation> mapInjectExpectation = injectExpectationRepository.rawByIds(
teams.stream().flatMap(rawTeam -> rawTeam.getTeam_expectations().stream()).toList()
)
.stream().collect(Collectors.toMap(RawInjectExpectation::getInject_expectation_id, Function.identity()));

// Getting a map of communications
Map<String, RawCommunication> mapCommunication = communicationRepository.rawByIds(
teams.stream().flatMap(rawTeam -> rawTeam.getTeam_communications().stream()).toList()
)
.stream().collect(Collectors.toMap(RawCommunication::getCommunication_id, Function.identity()));

// Getting a map of exercises team users by team id
Map<String, List<RawExerciseTeamUser>> mapExerciseTeamUser = exerciseTeamUserRepository.rawByTeamIds(
teams.stream().map(RawTeam::getTeam_id).toList()
)
.stream().collect(Collectors.groupingBy(RawExerciseTeamUser::getTeam_id));

// Getting a map of Injects by scenarios ids
Map<String, Set<String>> mapInjectsByScenarioIds = scenarioRepository.rawInjectsFromScenarios(
teams.stream().flatMap(rawTeam -> rawTeam.getTeam_scenarios().stream()).toList()
).stream().collect(Collectors.toMap(RawScenario::getScenario_id, RawScenario::getScenario_injects));

// Then, for all the raw teams, we will create a simpler team object and then send it back to the front
return teams.stream().map(rawTeam -> {
// We create the simpler team object using the raw one
TeamSimple teamSimple = new TeamSimple(rawTeam);

// We set the inject expectations
teamSimple.setInjectExpectations(
rawTeam.getTeam_expectations().stream().map(
expectation -> {
// We set the inject expectation using the map we generated earlier
InjectExpectation injectExpectation = new InjectExpectation();
Optional<RawInjectExpectation> raw = Optional.ofNullable(mapInjectExpectation.get(expectation));
raw.ifPresent(toProcess -> {
injectExpectation.setScore(toProcess.getInject_expectation_score());
injectExpectation.setExpectedScore(toProcess.getInject_expectation_expected_score());
injectExpectation.setId(toProcess.getInject_expectation_id());
injectExpectation.setExpectedScore(toProcess.getInject_expectation_expected_score());
if (toProcess.getExercise_id() != null) {
injectExpectation.setExercise(new Exercise());
injectExpectation.getExercise().setId(toProcess.getExercise_id());
}
injectExpectation.setTeam(new Team());
injectExpectation.getTeam().setId(rawTeam.getTeam_id());
injectExpectation.setType(InjectExpectation.EXPECTATION_TYPE.valueOf(toProcess.getInject_expectation_type()));
});
return injectExpectation;
}
).toList()
);

// We set the communications using the map we generated earlier
// This object has content, content_html and attachments ignored because WE DON'T WANT THE FULL EXTENT
teamSimple.setCommunications(
rawTeam.getTeam_communications().stream().map(communicationId -> {
RawCommunication raw = mapCommunication.get(communicationId);
Communication communication = new Communication();
communication.setAck(raw.getCommunication_ack());
communication.setId(raw.getCommunication_id());
communication.setIdentifier(raw.getCommunication_message_id());
communication.setReceivedAt(raw.getCommunication_received_at());
communication.setSentAt(raw.getCommunication_sent_at());
communication.setSubject(raw.getCommunication_subject());
Inject inject = new Inject();
inject.setId(raw.getCommunication_inject());
Exercise exercise = new Exercise();
exercise.setId(raw.getCommunication_exercise());
inject.setExercise(exercise);
communication.setInject(inject);
communication.setUsers(raw.getCommunication_users().stream().map(id -> {
User user = new User();
user.setId(id);
return user;
}).toList());
communication.setAnimation(raw.getCommunication_animation());
communication.setFrom(raw.getCommunication_from());
communication.setTo(raw.getCommunication_to());
return communication;
}).toList()
);

// We set the tuple of exercise/user/team
List<RawExerciseTeamUser> exerciseTeamUsers = mapExerciseTeamUser.get(rawTeam);
if(exerciseTeamUsers != null) {
teamSimple.setExerciseTeamUsers(exerciseTeamUsers.stream().map(
ExerciseTeamUser::fromRawExerciseTeamUser
).collect(Collectors.toSet()));
}

// We set the injects linked to the scenarios
teamSimple.setScenariosInjects(
getInjectTeamsIds(teamSimple.getId(),
rawTeam.getTeam_scenarios().stream().flatMap(
scenario -> mapInjectsByScenarioIds.get(scenario).stream()
).collect(Collectors.toSet()),
injectRepository)
);

// We set the injects linked to the exercises
teamSimple.setExercisesInjects(
getInjectTeamsIds(teamSimple.getId(),
rawTeam.getTeam_exercise_injects(),
injectRepository)
);

return teamSimple;
}).collect(Collectors.toList());
}

private static Set<String> getInjectTeamsIds(final String teamId, Set<String> injectIds, final InjectRepository injectRepository) {
public static List<TeamSimple> rawTeamToSimplerTeam(List<RawTeam> teams,
InjectExpectationRepository injectExpectationRepository,
InjectRepository injectRepository,
CommunicationRepository communicationRepository,
ExerciseTeamUserRepository exerciseTeamUserRepository,
ScenarioRepository scenarioRepository) {
// Getting a map of inject expectations
Map<String, RawInjectExpectation> mapInjectExpectation = injectExpectationRepository.rawByIds(
teams.stream().flatMap(rawTeam -> rawTeam.getTeam_expectations().stream()).toList()
)
.stream().collect(Collectors.toMap(RawInjectExpectation::getInject_expectation_id, Function.identity()));

// Getting a map of communications
Map<String, RawCommunication> mapCommunication = communicationRepository.rawByIds(
teams.stream().flatMap(rawTeam -> rawTeam.getTeam_communications().stream()).toList()
)
.stream().collect(Collectors.toMap(RawCommunication::getCommunication_id, Function.identity()));

// Getting a map of exercises team users by team id
Map<String, List<RawExerciseTeamUser>> mapExerciseTeamUser = exerciseTeamUserRepository.rawByTeamIds(
teams.stream().map(RawTeam::getTeam_id).toList()
)
.stream().collect(Collectors.groupingBy(RawExerciseTeamUser::getTeam_id));

// Getting a map of Injects by scenarios ids
Map<String, Set<String>> mapInjectsByScenarioIds = scenarioRepository.rawInjectsFromScenarios(
teams.stream().flatMap(rawTeam -> rawTeam.getTeam_scenarios().stream()).toList()
).stream().collect(Collectors.toMap(RawScenario::getScenario_id, RawScenario::getScenario_injects));

// Then, for all the raw teams, we will create a simpler team object and then send it back to the front
return teams.stream().map(rawTeam -> {
// We create the simpler team object using the raw one
TeamSimple teamSimple = new TeamSimple(rawTeam);

// We set the inject expectations
teamSimple.setInjectExpectations(
rawTeam.getTeam_expectations().stream().map(
expectation -> {
// We set the inject expectation using the map we generated earlier
InjectExpectation injectExpectation = new InjectExpectation();
Optional<RawInjectExpectation> raw = Optional.ofNullable(mapInjectExpectation.get(expectation));
raw.ifPresent(toProcess -> {
injectExpectation.setScore(toProcess.getInject_expectation_score());
injectExpectation.setExpectedScore(toProcess.getInject_expectation_expected_score());
injectExpectation.setId(toProcess.getInject_expectation_id());
injectExpectation.setExpectedScore(toProcess.getInject_expectation_expected_score());

Check warning on line 65 in openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java#L59-L65

Added lines #L59 - L65 were not covered by tests
if (toProcess.getExercise_id() != null) {
injectExpectation.setExercise(new Exercise());
injectExpectation.getExercise().setId(toProcess.getExercise_id());

Check warning on line 68 in openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java#L67-L68

Added lines #L67 - L68 were not covered by tests
}
injectExpectation.setTeam(new Team());
injectExpectation.getTeam().setId(rawTeam.getTeam_id());
injectExpectation.setType(
InjectExpectation.EXPECTATION_TYPE.valueOf(toProcess.getInject_expectation_type()));
});
return injectExpectation;

Check warning on line 75 in openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java#L70-L75

Added lines #L70 - L75 were not covered by tests
}
).toList()
);

// We set the communications using the map we generated earlier
// This object has content, content_html and attachments ignored because WE DON'T WANT THE FULL EXTENT
teamSimple.setCommunications(
rawTeam.getTeam_communications().stream().map(communicationId -> {
RawCommunication raw = mapCommunication.get(communicationId);
Communication communication = new Communication();
communication.setAck(raw.getCommunication_ack());
communication.setId(raw.getCommunication_id());
communication.setIdentifier(raw.getCommunication_message_id());
communication.setReceivedAt(raw.getCommunication_received_at());
communication.setSentAt(raw.getCommunication_sent_at());
communication.setSubject(raw.getCommunication_subject());
Inject inject = new Inject();
inject.setId(raw.getCommunication_inject());
Exercise exercise = new Exercise();
exercise.setId(raw.getCommunication_exercise());
inject.setExercise(exercise);
communication.setInject(inject);
communication.setUsers(raw.getCommunication_users().stream().map(id -> {
User user = new User();
user.setId(id);
return user;
}).toList());
communication.setAnimation(raw.getCommunication_animation());
communication.setFrom(raw.getCommunication_from());
communication.setTo(raw.getCommunication_to());
return communication;

Check warning on line 106 in openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java#L84-L106

Added lines #L84 - L106 were not covered by tests
}).toList()
);

// We set the tuple of exercise/user/team
List<RawExerciseTeamUser> exerciseTeamUsers = mapExerciseTeamUser.get(rawTeam);
if (exerciseTeamUsers != null) {
teamSimple.setExerciseTeamUsers(exerciseTeamUsers.stream().map(

Check warning on line 113 in openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java#L113

Added line #L113 was not covered by tests
ExerciseTeamUser::fromRawExerciseTeamUser
).collect(Collectors.toSet()));

Check warning on line 115 in openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java#L115

Added line #L115 was not covered by tests
}

// We set the injects linked to the scenarios
teamSimple.setScenariosInjects(
getInjectTeamsIds(teamSimple.getId(),
rawTeam.getTeam_scenarios().stream().flatMap(
scenario -> mapInjectsByScenarioIds.get(scenario).stream()
).collect(Collectors.toSet()),
injectRepository)
);

// We set the injects linked to the exercises
teamSimple.setExercisesInjects(
getInjectTeamsIds(teamSimple.getId(),
rawTeam.getTeam_exercise_injects(),
injectRepository)
);

return teamSimple;
}).collect(Collectors.toList());
}

public static List<TeamSimple> rawAllTeamToSimplerAllTeam(List<RawTeam> teams) {
// Then, for all the raw teams, we will create a simpler team object and then send it back to the front
return teams.stream().map(rawTeam -> {

Check warning on line 140 in openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java#L140

Added line #L140 was not covered by tests
// We create the simpler team object using the raw one
TeamSimple teamSimple = new TeamSimple(rawTeam);

Check warning on line 142 in openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java#L142

Added line #L142 was not covered by tests

return teamSimple;
}).collect(Collectors.toList());

Check warning on line 145 in openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java#L144-L145

Added lines #L144 - L145 were not covered by tests
}

private static Set<String> getInjectTeamsIds(final String teamId, Set<String> injectIds,
final InjectRepository injectRepository) {
Set<RawInject> rawInjectTeams = injectRepository.findRawInjectTeams(injectIds, teamId);
return rawInjectTeams.stream()
.map(RawInject::getInject_id)
Expand Down
Loading