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 43534d321c..4a94ebe235 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 @@ -2,10 +2,7 @@ import static io.openbas.rest.exercise.ExerciseApi.EXERCISE_URI; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; -import static net.javacrumbs.jsonunit.core.ConfigurationWhen.path; -import static net.javacrumbs.jsonunit.core.ConfigurationWhen.then; import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER; -import static net.javacrumbs.jsonunit.core.Option.IGNORING_EXTRA_ARRAY_ITEMS; import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -24,15 +21,14 @@ import io.openbas.utils.fixtures.composers.*; import io.openbas.utils.mockUser.WithMockAdminUser; import jakarta.annotation.Resource; +import java.util.Arrays; +import java.util.List; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.transaction.annotation.Transactional; -import java.util.Arrays; -import java.util.List; - @Transactional @TestInstance(PER_CLASS) public class ExerciseApiExportTest extends IntegrationTest { @@ -140,50 +136,53 @@ public void given_a_valid_simulation_the_export_file_is_found_in_zip_and_correct ExerciseFileExport.fromExercise(ex, exportMapper, variableService, challengeService) .withOptions(0)); - assertThatJson(expectedJson) - .isObject() - .isEqualTo(actualJson); + assertThatJson(expectedJson).isObject().isEqualTo(actualJson); } @DisplayName("Given a valid simulation and default options, exported tags are correct") @Test @WithMockAdminUser - public void given_a_valid_simulation_and_default_options_exported_tags_are_correct() throws Exception { + public void given_a_valid_simulation_and_default_options_exported_tags_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(); + 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(Tag.class, ExerciseExportMixins.Tag.class); - List expectedTags = tagComposer.generatedItems.stream().filter(tag -> Arrays.asList("exercise tag", "document tag", "challenge tag", "inject tag").contains(tag.getName())).toList(); + List expectedTags = + tagComposer.generatedItems.stream() + .filter( + tag -> + Arrays.asList("exercise tag", "document tag", "challenge tag", "inject tag") + .contains(tag.getName())) + .toList(); String tagsJson = objectMapper.writeValueAsString(expectedTags); - assertThatJson(actualJson) - .when(IGNORING_ARRAY_ORDER) - .node("exercise_tags") - .isEqualTo(tagsJson); + assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_tags").isEqualTo(tagsJson); } @DisplayName("Given a valid simulation and default options, exported objectives are correct") @Test @WithMockAdminUser - public void given_a_valid_simulation_and_default_options_exported_objectives_are_correct() throws Exception { + public void given_a_valid_simulation_and_default_options_exported_objectives_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(); + 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())); @@ -191,24 +190,25 @@ public void given_a_valid_simulation_and_default_options_exported_objectives_are String objectiveJson = objectMapper.writeValueAsString(objectiveComposer.generatedItems); assertThatJson(actualJson) - .when(IGNORING_ARRAY_ORDER) - .node("exercise_objectives") - .isEqualTo(objectiveJson); + .when(IGNORING_ARRAY_ORDER) + .node("exercise_objectives") + .isEqualTo(objectiveJson); } @DisplayName("Given a valid simulation and default options, exported challenges are correct") @Test @WithMockAdminUser - public void given_a_valid_simulation_and_default_options_exported_challenges_are_correct() throws Exception { + public void given_a_valid_simulation_and_default_options_exported_challenges_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(); + 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())); @@ -216,24 +216,25 @@ public void given_a_valid_simulation_and_default_options_exported_challenges_are String challengeJson = objectMapper.writeValueAsString(challengeComposer.generatedItems); assertThatJson(actualJson) - .when(IGNORING_ARRAY_ORDER) - .node("exercise_challenges") - .isEqualTo(challengeJson); + .when(IGNORING_ARRAY_ORDER) + .node("exercise_challenges") + .isEqualTo(challengeJson); } @DisplayName("Given a valid simulation and default options, exported articles are correct") @Test @WithMockAdminUser - public void given_a_valid_simulation_and_default_options_exported_articles_are_correct() throws Exception { + public void given_a_valid_simulation_and_default_options_exported_articles_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(); + 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())); @@ -241,24 +242,25 @@ public void given_a_valid_simulation_and_default_options_exported_articles_are_c String articleJson = objectMapper.writeValueAsString(articleComposer.generatedItems); assertThatJson(actualJson) - .when(IGNORING_ARRAY_ORDER) - .node("exercise_articles") - .isEqualTo(articleJson); + .when(IGNORING_ARRAY_ORDER) + .node("exercise_articles") + .isEqualTo(articleJson); } @DisplayName("Given a valid simulation and default options, exported channels are correct") @Test @WithMockAdminUser - public void given_a_valid_simulation_and_default_options_exported_channels_are_correct() throws Exception { + public void given_a_valid_simulation_and_default_options_exported_channels_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(); + 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())); @@ -266,24 +268,25 @@ public void given_a_valid_simulation_and_default_options_exported_channels_are_c String channelJson = objectMapper.writeValueAsString(channelComposer.generatedItems); assertThatJson(actualJson) - .when(IGNORING_ARRAY_ORDER) - .node("exercise_channels") - .isEqualTo(channelJson); + .when(IGNORING_ARRAY_ORDER) + .node("exercise_channels") + .isEqualTo(channelJson); } @DisplayName("Given a valid simulation and default options, exported documents are correct") @Test @WithMockAdminUser - public void given_a_valid_simulation_and_default_options_exported_documents_are_correct() throws Exception { + public void given_a_valid_simulation_and_default_options_exported_documents_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(); + 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())); @@ -291,25 +294,25 @@ public void given_a_valid_simulation_and_default_options_exported_documents_are_ String documentJson = objectMapper.writeValueAsString(documentComposer.generatedItems); assertThatJson(actualJson) - .when(IGNORING_ARRAY_ORDER) - .node("exercise_documents") - .isEqualTo(documentJson); + .when(IGNORING_ARRAY_ORDER) + .node("exercise_documents") + .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 { + 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(); + 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())); @@ -317,71 +320,65 @@ public void given_a_valid_simulation_and_default_options_exported_variables_have 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") @Test @WithMockAdminUser - public void given_a_valid_simulation_and_default_options_exported_teams_is_empty_array() throws Exception { + public void given_a_valid_simulation_and_default_options_exported_teams_is_empty_array() + throws Exception { 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())); - assertThatJson(actualJson) - .when(IGNORING_ARRAY_ORDER) - .node("exercise_teams") - .isEqualTo("[]"); + assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_teams").isEqualTo("[]"); } @DisplayName("Given a valid simulation and default options, exported users is absent key") @Test @WithMockAdminUser - public void given_a_valid_simulation_and_default_options_exported_users_is_absent_key() throws Exception { + public void given_a_valid_simulation_and_default_options_exported_users_is_absent_key() + throws Exception { 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())); - assertThatJson(actualJson) - .when(IGNORING_ARRAY_ORDER) - .node("exercise_users") - .isAbsent(); + assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_users").isAbsent(); } @DisplayName("Given a valid simulation and default options, exported organisations is absent key") @Test @WithMockAdminUser - public void given_a_valid_simulation_and_default_options_exported_organisations_is_absent_key() throws Exception { + public void given_a_valid_simulation_and_default_options_exported_organisations_is_absent_key() + throws Exception { 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())); - assertThatJson(actualJson) - .when(IGNORING_ARRAY_ORDER) - .node("exercise_users") - .isAbsent(); + assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_users").isAbsent(); } } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/Store.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/Store.java index 44b1b12521..ce12cd3838 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/Store.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/Store.java @@ -4,12 +4,12 @@ import java.util.List; import java.util.function.Supplier; -public class Store{ - public List items = new ArrayList<>(); +public class Store { + public List items = new ArrayList<>(); - public T memorise(Supplier f) { - T result = f.get(); - items.add(result); - return result; - } + public T memorise(Supplier f) { + T result = f.get(); + items.add(result); + return result; + } } 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 0edac1429c..bc2aa0f0ba 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 @@ -1,16 +1,15 @@ 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; - } + 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/ChallengeComposer.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ChallengeComposer.java index f148164ba2..578d0b44af 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ChallengeComposer.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ChallengeComposer.java @@ -1,6 +1,5 @@ package io.openbas.utils.fixtures.composers; -import io.openbas.database.model.Article; import io.openbas.database.model.Challenge; import io.openbas.database.model.Tag; import io.openbas.database.repository.ChallengeRepository; @@ -11,7 +10,7 @@ import org.springframework.stereotype.Component; @Component -public class ChallengeComposer extends ComposerBase { +public class ChallengeComposer extends ComposerBase { @Autowired private ChallengeRepository challengeRepository; public class Composer extends InnerComposerBase { diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ComposerBase.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ComposerBase.java index 8027f6e3a1..55ee79983c 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ComposerBase.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/ComposerBase.java @@ -1,14 +1,12 @@ package io.openbas.utils.fixtures.composers; -import io.openbas.database.model.Document; - import java.util.ArrayList; import java.util.List; public class ComposerBase { - public List generatedItems = new ArrayList<>(); + public List generatedItems = new ArrayList<>(); - public void reset() { - generatedItems.clear(); - } + public void reset() { + generatedItems.clear(); + } } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/InjectComposer.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/InjectComposer.java index 419a937303..3591a45375 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/InjectComposer.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/InjectComposer.java @@ -11,7 +11,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/TagComposer.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/TagComposer.java index cb7d5a9461..3324209ee6 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/TagComposer.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/composers/TagComposer.java @@ -5,9 +5,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.List; - @Component public class TagComposer extends ComposerBase { @Autowired private TagRepository tagRepository; 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 index 3ac29c155a..b7153b352f 100644 --- 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 @@ -5,34 +5,31 @@ 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; + @Autowired private VariableRepository variableRepository; - public Composer(Variable variable) { - this.variable = variable; - } + public class Composer extends InnerComposerBase { + private final Variable variable; - @Override - public Composer persist() { - variableRepository.save(variable); - return this; - } + public Composer(Variable variable) { + this.variable = variable; + } - @Override - public Variable get() { - return this.variable; - } + @Override + public Composer persist() { + variableRepository.save(variable); + return this; } - public Composer forVariable(Variable variable) { - generatedItems.add(variable); - return new Composer(variable); + @Override + public Variable get() { + return this.variable; } + } + + public Composer forVariable(Variable variable) { + generatedItems.add(variable); + return new Composer(variable); + } }