Skip to content

Commit

Permalink
fix variables test and add exerciseteamuser compsoer
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 ae891b3 commit 001558c
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,6 +97,7 @@ private Exercise getExercise() {
.withTag(
tagComposer.forTag(
TagFixture.getTagWithText("Organization tag"))))))
.withTeamUsers()
.withInject(
injectComposer
.forInject(InjectFixture.getInjectWithoutContract())
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<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.setTeamUsers(tempTeamUsers);
return this;
}

public Composer withArticle(ArticleComposer.Composer articleComposer) {
this.articleComposers.add(articleComposer);
List<Article> tempArticles = this.exercise.getArticles();
Expand Down

0 comments on commit 001558c

Please sign in to comment.