Skip to content

Commit

Permalink
add vriable compsoer + test
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 c4bec76 commit 8246a4d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -59,6 +61,7 @@ void before() {
lessonsCategoryComposer.reset();
teamComposer.reset();
userComposer.reset();
variableComposer.reset();
organizationComposer.reset();
injectComposer.reset();
challengeComposer.reset();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ public class Composer extends InnerComposerBase<Exercise> {
private final List<ObjectiveComposer.Composer> objectiveComposers = new ArrayList<>();
private final List<TagComposer.Composer> tagComposers = new ArrayList<>();
private final List<DocumentComposer.Composer> documentComposers = new ArrayList<>();
private final List<VariableComposer.Composer> 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<Inject> injects = exercise.getInjects();
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Variable> {
@Autowired private VariableRepository variableRepository;

public class Composer extends InnerComposerBase<Variable> {
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);
}
}

0 comments on commit 8246a4d

Please sign in to comment.