From 001558cadda5e619958bfc6f3fa10c0a118a8388 Mon Sep 17 00:00:00 2001 From: Antoine MAZEAS Date: Mon, 13 Jan 2025 10:37:13 +0100 Subject: [PATCH] fix variables test and add exerciseteamuser compsoer Signed-off-by: Antoine MAZEAS --- .../rest/exercise/ExerciseApiExportTest.java | 139 +++++++++++++++++- .../utils/fixtures/VariableFixture.java | 1 - .../fixtures/composers/ExerciseComposer.java | 16 ++ 3 files changed, 154 insertions(+), 2 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/exercise/ExerciseApiExportTest.java b/openbas-api/src/test/java/io/openbas/rest/exercise/ExerciseApiExportTest.java index 4a94ebe235..cc1705a89e 100644 --- a/openbas-api/src/test/java/io/openbas/rest/exercise/ExerciseApiExportTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/exercise/ExerciseApiExportTest.java @@ -14,6 +14,7 @@ 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.service.ChallengeService; import io.openbas.service.VariableService; import io.openbas.utils.ZipUtils; @@ -96,6 +97,7 @@ private Exercise getExercise() { .withTag( tagComposer.forTag( TagFixture.getTagWithText("Organization tag")))))) + .withTeamUsers() .withInject( injectComposer .forInject(InjectFixture.getInjectWithoutContract()) @@ -161,7 +163,7 @@ public void given_a_valid_simulation_and_default_options_exported_tags_are_corre tagComposer.generatedItems.stream() .filter( tag -> - Arrays.asList("exercise tag", "document tag", "challenge tag", "inject tag") + Arrays.asList("exercise tag", "document tag", "challenge tag", "inject tag", "organization tag") .contains(tag.getName())) .toList(); String tagsJson = objectMapper.writeValueAsString(expectedTags); @@ -299,6 +301,32 @@ public void given_a_valid_simulation_and_default_options_exported_documents_are_ .isEqualTo(documentJson); } + @DisplayName("Given a valid simulation and default options, exported exercise info are correct") + @Test + @WithMockAdminUser + public void given_a_valid_simulation_and_default_options_exported_exercise_info_are_correct() + throws Exception { + ObjectMapper objectMapper = mapper.copy(); + Exercise ex = getExercise(); + byte[] response = + mvc.perform( + get(EXERCISE_URI + "/" + ex.getId() + "/export").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsByteArray(); + + String actualJson = ZipUtils.getZipEntryAsString(response, "%s.json".formatted(ex.getName())); + + objectMapper.addMixIn(Exercise.class, ExerciseExportMixins.Exercise.class); + String exerciseJson = objectMapper.writeValueAsString(ex); + + assertThatJson(actualJson) + .when(IGNORING_ARRAY_ORDER) + .node("exercise_information") + .isEqualTo(exerciseJson); + } + @DisplayName("Given a valid simulation and default options, exported variables have no value") @Test @WithMockAdminUser @@ -325,6 +353,34 @@ public void given_a_valid_simulation_and_default_options_exported_variables_have .isEqualTo(variableJson); } + @DisplayName("Given a valid simulation, given isWithVariableValues options, exported variables have values") + @Test + @WithMockAdminUser + public void given_a_valid_simulation_given_isWithVariableValues_option_exported_variables_have_values() + throws Exception { + ObjectMapper objectMapper = mapper.copy(); + Exercise ex = getExercise(); + byte[] response = + mvc.perform( + get(EXERCISE_URI + "/" + ex.getId() + "/export") + .queryParam("isWithVariableValues", "true") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsByteArray(); + + String actualJson = ZipUtils.getZipEntryAsString(response, "%s.json".formatted(ex.getName())); + + objectMapper.addMixIn(Variable.class, VariableWithValueMixin.class); + String variableJson = objectMapper.writeValueAsString(variableComposer.generatedItems); + + assertThatJson(actualJson) + .when(IGNORING_ARRAY_ORDER) + .node("exercise_variables") + .isEqualTo(variableJson); + } + @DisplayName("Given a valid simulation and default options, exported teams is empty array") @Test @WithMockAdminUser @@ -344,6 +400,36 @@ public void given_a_valid_simulation_and_default_options_exported_teams_is_empty assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_teams").isEqualTo("[]"); } + @DisplayName("Given a valid simulation, given isWithTeams and NOT isWithPlayers options, exported teams have empty users") + @Test + @WithMockAdminUser + public void given_a_valid_simulation_given_isWithTeams_and_not_isWithPlayers_options_exported_teams_have_empty_users() + throws Exception { + ObjectMapper objectMapper = mapper.copy(); + Exercise ex = getExercise(); + byte[] response = + mvc.perform( + get(EXERCISE_URI + "/" + ex.getId() + "/export") + .queryParam("isWithTeams", "true") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsByteArray(); + + String actualJson = ZipUtils.getZipEntryAsString(response, "%s.json".formatted(ex.getName())); + + objectMapper.addMixIn(Team.class, ExerciseExportMixins.EmptyTeam.class); + String teamsJson = objectMapper.writeValueAsString(teamComposer.generatedItems); + + assertThatJson(actualJson) + .when(IGNORING_ARRAY_ORDER) + .node("exercise_teams") + .isEqualTo(teamsJson); + // users still not included + assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_users").isAbsent(); + } + @DisplayName("Given a valid simulation and default options, exported users is absent key") @Test @WithMockAdminUser @@ -363,6 +449,32 @@ public void given_a_valid_simulation_and_default_options_exported_users_is_absen assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_users").isAbsent(); } + @DisplayName("Given a valid simulation, given isWithPlayers and NOT isWithTeams option, exported users are correct") + @Test + @WithMockAdminUser + public void given_a_valid_simulation_given_isWithPlayers_and_not_isWithTeams_options_exported_users_are_correct() + throws Exception { + ObjectMapper objectMapper = mapper.copy(); + Exercise ex = getExercise(); + byte[] response = + mvc.perform( + get(EXERCISE_URI + "/" + ex.getId() + "/export") + .queryParam("isWithPlayers", "true") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsByteArray(); + + String actualJson = ZipUtils.getZipEntryAsString(response, "%s.json".formatted(ex.getName())); + + objectMapper.addMixIn(User.class, ExerciseExportMixins.User.class); + String usersJson = objectMapper.writeValueAsString(userComposer.generatedItems); + + assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_users").isEqualTo(usersJson); + assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_teams").isEqualTo("[]"); + } + @DisplayName("Given a valid simulation and default options, exported organisations is absent key") @Test @WithMockAdminUser @@ -381,4 +493,29 @@ public void given_a_valid_simulation_and_default_options_exported_organisations_ assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_users").isAbsent(); } + + @DisplayName("Given a valid simulation, given isWithPlayers option, exported organisations are correct") + @Test + @WithMockAdminUser + public void given_a_valid_simulation_given_isWithPlayers_option_exported_organisations_are_correct() + throws Exception { + ObjectMapper objectMapper = mapper.copy(); + Exercise ex = getExercise(); + byte[] response = + mvc.perform( + get(EXERCISE_URI + "/" + ex.getId() + "/export") + .queryParam("isWithPlayers", "true") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsByteArray(); + + String actualJson = ZipUtils.getZipEntryAsString(response, "%s.json".formatted(ex.getName())); + + objectMapper.addMixIn(Organization.class, ExerciseExportMixins.Organization.class); + String orgJson = objectMapper.writeValueAsString(organizationComposer.generatedItems); + + assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_organizations").isEqualTo(orgJson); + } } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/VariableFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/VariableFixture.java index bc2aa0f0ba..474cf682d4 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/VariableFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/VariableFixture.java @@ -6,7 +6,6 @@ public class VariableFixture { public static Variable getVariable() { Variable var = new Variable(); - var.setId(UUID.randomUUID().toString()); var.setKey("variable_key"); var.setValue("variable value"); var.setDescription("variable description"); diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ExerciseComposer.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ExerciseComposer.java index 6531678c98..43eef988f2 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ExerciseComposer.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ExerciseComposer.java @@ -6,6 +6,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -58,6 +60,20 @@ public Composer withTeam(TeamComposer.Composer teamComposer) { return this; } + // special composition that injects the currently set users in currently set teams as "ExerciseTeamUsers" + public Composer withTeamUsers() { + List tempTeamUsers = new ArrayList<>(); + this.exercise.getTeams().forEach(team -> team.getUsers().forEach(user -> { + ExerciseTeamUser exerciseTeamUser = new ExerciseTeamUser(); + exerciseTeamUser.setExercise(exercise); + exerciseTeamUser.setUser(user); + exerciseTeamUser.setTeam(team); + tempTeamUsers.add(exerciseTeamUser); + })); + this.exercise.setTeamUsers(tempTeamUsers); + return this; + } + public Composer withArticle(ArticleComposer.Composer articleComposer) { this.articleComposers.add(articleComposer); List
tempArticles = this.exercise.getArticles();