Skip to content

Commit

Permalink
spotless
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine MAZEAS <[email protected]>
  • Loading branch information
antoinemzs committed Jan 13, 2025
1 parent 001558c commit 2ad46ec
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,36 @@ public void given_a_valid_simulation_the_export_file_is_found_in_zip_and_correct
assertThatJson(expectedJson).isObject().isEqualTo(actualJson);
}

@DisplayName(
"Given a valid simulation and full options, the export file is found in zip and correct")
@Test
@WithMockAdminUser
public void
given_a_valid_simulation_and_full_options_the_export_file_is_found_in_zip_and_correct()
throws Exception {
Exercise ex = getExercise();
byte[] response =
mvc.perform(
get(EXERCISE_URI + "/" + ex.getId() + "/export")
.queryParam("isWithPlayers", "true")
.queryParam("isWithTeams", "true")
.queryParam("isWithVariableValues", "true")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().is2xxSuccessful())
.andReturn()
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntryAsString(response, "%s.json".formatted(ex.getName()));
ObjectMapper exportMapper = mapper.copy();
String expectedJson =
exportMapper.writeValueAsString(
ExerciseFileExport.fromExercise(ex, exportMapper, variableService, challengeService)
.withOptions(7));

assertThatJson(expectedJson).isObject().isEqualTo(actualJson);
}

@DisplayName("Given a valid simulation and default options, exported tags are correct")
@Test
@WithMockAdminUser
Expand All @@ -163,7 +193,12 @@ 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", "organization tag")
Arrays.asList(
"exercise tag",
"document tag",
"challenge tag",
"inject tag",
"organization tag")
.contains(tag.getName()))
.toList();
String tagsJson = objectMapper.writeValueAsString(expectedTags);
Expand Down Expand Up @@ -305,26 +340,26 @@ public void given_a_valid_simulation_and_default_options_exported_documents_are_
@Test
@WithMockAdminUser
public void given_a_valid_simulation_and_default_options_exported_exercise_info_are_correct()
throws Exception {
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();
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);
.when(IGNORING_ARRAY_ORDER)
.node("exercise_information")
.isEqualTo(exerciseJson);
}

@DisplayName("Given a valid simulation and default options, exported variables have no value")
Expand Down Expand Up @@ -353,32 +388,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")
@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()
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")
mvc.perform(
get(EXERCISE_URI + "/" + ex.getId() + "/export")
.queryParam("isWithVariableValues", "true")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().is2xxSuccessful())
.andReturn()
.getResponse()
.getContentAsByteArray();
.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);
.when(IGNORING_ARRAY_ORDER)
.node("exercise_variables")
.isEqualTo(variableJson);
}

@DisplayName("Given a valid simulation and default options, exported teams is empty array")
Expand All @@ -400,32 +437,34 @@ 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")
@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()
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();
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);
.when(IGNORING_ARRAY_ORDER)
.node("exercise_teams")
.isEqualTo(teamsJson);
// users still not included
assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_users").isAbsent();
}
Expand All @@ -449,29 +488,34 @@ 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")
@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()
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();
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_users")
.isEqualTo(usersJson);
assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_teams").isEqualTo("[]");
}

Expand All @@ -494,28 +538,33 @@ 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")
@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()
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")
mvc.perform(
get(EXERCISE_URI + "/" + ex.getId() + "/export")
.queryParam("isWithPlayers", "true")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().is2xxSuccessful())
.andReturn()
.getResponse()
.getContentAsByteArray();
.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);
assertThatJson(actualJson)
.when(IGNORING_ARRAY_ORDER)
.node("exercise_organizations")
.isEqualTo(orgJson);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.openbas.utils.fixtures;

import io.openbas.database.model.Variable;
import java.util.UUID;

public class VariableFixture {
public static Variable getVariable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
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;

Expand Down Expand Up @@ -60,16 +58,23 @@ public Composer withTeam(TeamComposer.Composer teamComposer) {
return this;
}

// special composition that injects the currently set users in currently set teams as "ExerciseTeamUsers"
// special composition that injects the currently set users in currently set teams as
// "ExerciseTeamUsers"
public Composer withTeamUsers() {
List<ExerciseTeamUser> 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
.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;
}
Expand Down

0 comments on commit 2ad46ec

Please sign in to comment.