Skip to content

Commit

Permalink
[backend/tests] export of simulations, JSON (chunk 1) (#1489)
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine MAZEAS <[email protected]>
  • Loading branch information
antoinemzs authored and Dimfacion committed Jan 20, 2025
1 parent b4225a0 commit 13d6e1b
Show file tree
Hide file tree
Showing 29 changed files with 1,811 additions and 129 deletions.
6 changes: 6 additions & 0 deletions openbas-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@
<artifactId>logback-jackson</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
129 changes: 13 additions & 116 deletions openbas-api/src/main/java/io/openbas/rest/exercise/ExerciseApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import io.openbas.database.specification.*;
import io.openbas.rest.exception.ElementNotFoundException;
import io.openbas.rest.exception.InputValidationException;
import io.openbas.rest.exercise.exports.ExerciseExportMixins;
import io.openbas.rest.exercise.exports.ExerciseFileExport;
import io.openbas.rest.exercise.exports.VariableMixin;
import io.openbas.rest.exercise.exports.VariableWithValueMixin;
import io.openbas.rest.exercise.exports.*;
import io.openbas.rest.exercise.form.*;
import io.openbas.rest.exercise.response.ExercisesGlobalScoresOutput;
import io.openbas.rest.exercise.service.ExerciseService;
Expand Down Expand Up @@ -752,119 +749,15 @@ public void exerciseExport(
@RequestParam(required = false) final boolean isWithVariableValues,
HttpServletResponse response)
throws IOException {
// Setup the mapper for export
List<String> documentIds = new ArrayList<>();
ObjectMapper objectMapper = mapper.copy();
if (!isWithPlayers) {
objectMapper.addMixIn(
ExerciseFileExport.class, ExerciseExportMixins.ExerciseFileExport.class);
}
// Start exporting exercise
ExerciseFileExport importExport = new ExerciseFileExport();
importExport.setVersion(1);

Exercise exercise =
exerciseRepository.findById(exerciseId).orElseThrow(ElementNotFoundException::new);
objectMapper.addMixIn(Exercise.class, ExerciseExportMixins.Exercise.class);
// Build the export
importExport.setExercise(exercise);
importExport.setDocuments(exercise.getDocuments());
documentIds.addAll(exercise.getDocuments().stream().map(Document::getId).toList());
objectMapper.addMixIn(Document.class, ExerciseExportMixins.Document.class);
List<Tag> exerciseTags = new ArrayList<>(exercise.getTags());
// Objectives
List<Objective> objectives = exercise.getObjectives();
importExport.setObjectives(objectives);
objectMapper.addMixIn(Objective.class, ExerciseExportMixins.Objective.class);
// Lessons categories
List<LessonsCategory> lessonsCategories = exercise.getLessonsCategories();
importExport.setLessonsCategories(lessonsCategories);
objectMapper.addMixIn(LessonsCategory.class, ExerciseExportMixins.LessonsCategory.class);
// Lessons questions
List<LessonsQuestion> lessonsQuestions =
lessonsCategories.stream().flatMap(category -> category.getQuestions().stream()).toList();
importExport.setLessonsQuestions(lessonsQuestions);
objectMapper.addMixIn(LessonsQuestion.class, ExerciseExportMixins.LessonsQuestion.class);
if (isWithTeams) {
// Teams
List<Team> teams = exercise.getTeams();
importExport.setTeams(teams);
objectMapper.addMixIn(
Team.class,
isWithPlayers ? ExerciseExportMixins.Team.class : ExerciseExportMixins.EmptyTeam.class);
exerciseTags.addAll(teams.stream().flatMap(team -> team.getTags().stream()).toList());
}
if (isWithPlayers) {
// players
List<User> players =
exercise.getTeams().stream()
.flatMap(team -> team.getUsers().stream())
.distinct()
.toList();
exerciseTags.addAll(players.stream().flatMap(user -> user.getTags().stream()).toList());
importExport.setUsers(players);
objectMapper.addMixIn(User.class, ExerciseExportMixins.User.class);
// organizations
List<Organization> organizations =
players.stream().map(User::getOrganization).filter(Objects::nonNull).distinct().toList();
exerciseTags.addAll(organizations.stream().flatMap(org -> org.getTags().stream()).toList());
importExport.setOrganizations(organizations);
objectMapper.addMixIn(Organization.class, ExerciseExportMixins.Organization.class);
}
// Injects
List<Inject> injects = exercise.getInjects();
injects.forEach(
inject -> {
exerciseTags.addAll(inject.getTags());
inject
.getInjectorContract()
.ifPresent(
injectorContract -> {
if (injectorContract.getPayload() != null) {
exerciseTags.addAll(injectorContract.getPayload().getTags());
}
});
exerciseTags.addAll(inject.getTags());
});
exerciseTags.addAll(injects.stream().flatMap(inject -> inject.getTags().stream()).toList());
importExport.setInjects(injects);
objectMapper.addMixIn(Inject.class, ExerciseExportMixins.Inject.class);
// Documents
exerciseTags.addAll(
exercise.getDocuments().stream().flatMap(doc -> doc.getTags().stream()).toList());
// Articles / Channels
List<Article> articles = exercise.getArticles();
importExport.setArticles(articles);
objectMapper.addMixIn(Article.class, ExerciseExportMixins.Article.class);
List<Channel> channels = articles.stream().map(Article::getChannel).distinct().toList();
documentIds.addAll(
channels.stream()
.flatMap(channel -> channel.getLogos().stream())
.map(Document::getId)
.toList());
importExport.setChannels(channels);
objectMapper.addMixIn(Channel.class, ExerciseExportMixins.Channel.class);
// Challenges
List<Challenge> challenges = fromIterable(challengeService.getExerciseChallenges(exerciseId));
importExport.setChallenges(challenges);
documentIds.addAll(
challenges.stream()
.flatMap(challenge -> challenge.getDocuments().stream())
.map(Document::getId)
.toList());
objectMapper.addMixIn(Challenge.class, ExerciseExportMixins.Challenge.class);
exerciseTags.addAll(
challenges.stream().flatMap(challenge -> challenge.getTags().stream()).toList());
// Tags
importExport.setTags(exerciseTags.stream().distinct().toList());
objectMapper.addMixIn(Tag.class, ExerciseExportMixins.Tag.class);
// -- Variables --
List<Variable> variables = this.variableService.variablesFromExercise(exerciseId);
importExport.setVariables(variables);
if (isWithVariableValues) {
objectMapper.addMixIn(Variable.class, VariableWithValueMixin.class);
} else {
objectMapper.addMixIn(Variable.class, VariableMixin.class);
}
ExerciseFileExport importExport =
ExerciseFileExport.fromExercise(
exercise, objectMapper, this.variableService, this.challengeService)
.withOptions(ExportOptions.mask(isWithPlayers, isWithTeams, isWithVariableValues));

// Build the response
String infos =
"("
Expand All @@ -882,10 +775,14 @@ public void exerciseExport(
ZipEntry zipEntry = new ZipEntry(exercise.getName() + ".json");
zipEntry.setComment(EXPORT_ENTRY_EXERCISE);
zipExport.putNextEntry(zipEntry);
zipExport.write(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(importExport));
zipExport.write(
importExport
.getObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsBytes(importExport));
zipExport.closeEntry();
// Add the documents
documentIds.stream()
importExport.getAllDocumentIds().stream()
.distinct()
.forEach(
docId -> {
Expand Down
Loading

0 comments on commit 13d6e1b

Please sign in to comment.