From 8246a4d5d4b6521267eccbb0fc9a1b1220ec4723 Mon Sep 17 00:00:00 2001 From: Antoine MAZEAS Date: Fri, 10 Jan 2025 17:09:09 +0100 Subject: [PATCH] add vriable compsoer + test Signed-off-by: Antoine MAZEAS --- .../rest/exercise/ExerciseApiExportTest.java | 30 +++++++++++++++ .../utils/fixtures/VariableFixture.java | 16 ++++++++ .../fixtures/composers/ExerciseComposer.java | 8 ++++ .../fixtures/composers/VariableComposer.java | 38 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 openbas-api/src/test/java/io/openbas/utils/fixtures/VariableFixture.java create mode 100644 openbas-api/src/test/java/io/openbas/utils/fixtures/composers/VariableComposer.java 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 5d783f65b5..43534d321c 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 @@ -16,6 +16,7 @@ import io.openbas.database.model.Tag; import io.openbas.rest.exercise.exports.ExerciseExportMixins; import io.openbas.rest.exercise.exports.ExerciseFileExport; +import io.openbas.rest.exercise.exports.VariableMixin; import io.openbas.service.ChallengeService; import io.openbas.service.VariableService; import io.openbas.utils.ZipUtils; @@ -41,6 +42,7 @@ public class ExerciseApiExportTest extends IntegrationTest { @Autowired private ChannelComposer channelComposer; @Autowired private LessonsQuestionsComposer lessonsQuestionsComposer; @Autowired private LessonsCategoryComposer lessonsCategoryComposer; + @Autowired private VariableComposer variableComposer; @Autowired private TeamComposer teamComposer; @Autowired private UserComposer userComposer; @Autowired private OrganizationComposer organizationComposer; @@ -59,6 +61,7 @@ void before() { lessonsCategoryComposer.reset(); teamComposer.reset(); userComposer.reset(); + variableComposer.reset(); organizationComposer.reset(); injectComposer.reset(); challengeComposer.reset(); @@ -111,6 +114,7 @@ private Exercise getExercise() { .withTag(tagComposer.forTag(TagFixture.getTagWithText("Document tag")))) .withObjective(objectiveComposer.forObjective(ObjectiveFixture.getObjective())) .withTag(tagComposer.forTag(TagFixture.getTagWithText("Exercise tag"))) + .withVariable(variableComposer.forVariable(VariableFixture.getVariable())) .persist() .get(); } @@ -292,6 +296,32 @@ public void given_a_valid_simulation_and_default_options_exported_documents_are_ .isEqualTo(documentJson); } + + @DisplayName("Given a valid simulation and default options, exported variables have no value") + @Test + @WithMockAdminUser + public void given_a_valid_simulation_and_default_options_exported_variables_have_no_value() 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(Variable.class, VariableMixin.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 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 new file mode 100644 index 0000000000..0edac1429c --- /dev/null +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/VariableFixture.java @@ -0,0 +1,16 @@ +package io.openbas.utils.fixtures; + +import io.openbas.database.model.Variable; + +import java.util.UUID; + +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"); + return var; + } +} 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 f89b18b19a..6531678c98 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 @@ -22,11 +22,18 @@ public class Composer extends InnerComposerBase { private final List objectiveComposers = new ArrayList<>(); private final List tagComposers = new ArrayList<>(); private final List documentComposers = new ArrayList<>(); + private final List variableComposers = new ArrayList<>(); public Composer(Exercise exercise) { this.exercise = exercise; } + public Composer withVariable(VariableComposer.Composer variableComposer) { + variableComposers.add(variableComposer); + variableComposer.get().setExercise(exercise); + return this; + } + public Composer withInject(InjectComposer.Composer injectComposer) { injectComposers.add(injectComposer); List injects = exercise.getInjects(); @@ -93,6 +100,7 @@ public Composer persist() { this.tagComposers.forEach(TagComposer.Composer::persist); this.documentComposers.forEach(DocumentComposer.Composer::persist); exerciseRepository.save(exercise); + this.variableComposers.forEach(VariableComposer.Composer::persist); return this; } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/VariableComposer.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/VariableComposer.java new file mode 100644 index 0000000000..3ac29c155a --- /dev/null +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/VariableComposer.java @@ -0,0 +1,38 @@ +package io.openbas.utils.fixtures.composers; + +import io.openbas.database.model.Variable; +import io.openbas.database.repository.VariableRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class VariableComposer extends ComposerBase { + @Autowired private VariableRepository variableRepository; + + public class Composer extends InnerComposerBase { + private final Variable variable; + + public Composer(Variable variable) { + this.variable = variable; + } + + @Override + public Composer persist() { + variableRepository.save(variable); + return this; + } + + @Override + public Variable get() { + return this.variable; + } + } + + public Composer forVariable(Variable variable) { + generatedItems.add(variable); + return new Composer(variable); + } +}