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 20, 2025
1 parent 9ead5a7 commit 5cf5ed8
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.openbas.utils.fixtures.composers.*;
import io.openbas.utils.mockUser.WithMockAdminUser;
import jakarta.annotation.Resource;

import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -132,7 +131,8 @@ public void given_a_valid_simulation_the_export_file_is_found_in_zip_and_correct
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
ObjectMapper exportMapper = mapper.copy();
String expectedJson =
exportMapper.writeValueAsString(
Expand Down Expand Up @@ -161,7 +161,8 @@ public void given_a_valid_simulation_the_export_file_is_found_in_zip_and_correct
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
ObjectMapper exportMapper = mapper.copy();
String expectedJson =
exportMapper.writeValueAsString(
Expand All @@ -185,7 +186,8 @@ public void given_a_valid_simulation_and_default_options_exported_tags_are_corre
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Tag.class, ExerciseExportMixins.Tag.class);
List<Tag> expectedTags =
Expand Down Expand Up @@ -220,7 +222,8 @@ public void given_a_valid_simulation_and_default_options_exported_objectives_are
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Objective.class, ExerciseExportMixins.Objective.class);
String objectiveJson = objectMapper.writeValueAsString(objectiveComposer.generatedItems);
Expand All @@ -246,7 +249,8 @@ public void given_a_valid_simulation_and_default_options_exported_challenges_are
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Challenge.class, ExerciseExportMixins.Challenge.class);
String challengeJson = objectMapper.writeValueAsString(challengeComposer.generatedItems);
Expand All @@ -272,7 +276,8 @@ public void given_a_valid_simulation_and_default_options_exported_articles_are_c
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Article.class, ExerciseExportMixins.Article.class);
String articleJson = objectMapper.writeValueAsString(articleComposer.generatedItems);
Expand All @@ -298,7 +303,8 @@ public void given_a_valid_simulation_and_default_options_exported_channels_are_c
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Channel.class, ExerciseExportMixins.Channel.class);
String channelJson = objectMapper.writeValueAsString(channelComposer.generatedItems);
Expand All @@ -324,7 +330,8 @@ public void given_a_valid_simulation_and_default_options_exported_documents_are_
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Document.class, ExerciseExportMixins.Document.class);
String documentJson = objectMapper.writeValueAsString(documentComposer.generatedItems);
Expand All @@ -350,7 +357,8 @@ public void given_a_valid_simulation_and_default_options_exported_exercise_info_
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Exercise.class, ExerciseExportMixins.Exercise.class);
String exerciseJson = objectMapper.writeValueAsString(ex);
Expand All @@ -376,7 +384,8 @@ public void given_a_valid_simulation_and_default_options_exported_variables_have
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Variable.class, VariableMixin.class);
String variableJson = objectMapper.writeValueAsString(variableComposer.generatedItems);
Expand Down Expand Up @@ -406,7 +415,8 @@ public void given_a_valid_simulation_and_default_options_exported_variables_have
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Variable.class, VariableWithValueMixin.class);
String variableJson = objectMapper.writeValueAsString(variableComposer.generatedItems);
Expand All @@ -431,7 +441,8 @@ public void given_a_valid_simulation_and_default_options_exported_teams_is_empty
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_teams").isEqualTo("[]");
}
Expand All @@ -455,7 +466,8 @@ public void given_a_valid_simulation_and_default_options_exported_teams_is_empty
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Team.class, ExerciseExportMixins.EmptyTeam.class);
String teamsJson = objectMapper.writeValueAsString(teamComposer.generatedItems);
Expand All @@ -482,7 +494,8 @@ public void given_a_valid_simulation_and_default_options_exported_users_is_absen
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_users").isAbsent();
}
Expand All @@ -506,7 +519,8 @@ public void given_a_valid_simulation_and_default_options_exported_users_is_absen
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(User.class, ExerciseExportMixins.User.class);
String usersJson = objectMapper.writeValueAsString(userComposer.generatedItems);
Expand All @@ -532,7 +546,8 @@ public void given_a_valid_simulation_and_default_options_exported_organisations_
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

assertThatJson(actualJson).when(IGNORING_ARRAY_ORDER).node("exercise_users").isAbsent();
}
Expand All @@ -556,7 +571,8 @@ public void given_a_valid_simulation_and_default_options_exported_organisations_
.getResponse()
.getContentAsByteArray();

String actualJson = ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);
String actualJson =
ZipUtils.getZipEntry(response, "%s.json".formatted(ex.getName()), ZipUtils::streamToString);

objectMapper.addMixIn(Organization.class, ExerciseExportMixins.Organization.class);
String orgJson = objectMapper.writeValueAsString(organizationComposer.generatedItems);
Expand All @@ -567,30 +583,30 @@ public void given_a_valid_simulation_and_default_options_exported_organisations_
.isEqualTo(orgJson);
}

@DisplayName(
"Given documents are provided, exported archive contains the documents")
@DisplayName("Given documents are provided, exported archive contains the documents")
@Test
@WithMockAdminUser
public void
given_documents_are_provided_exported_archive_contains_the_documents()
throws Exception {
public void given_documents_are_provided_exported_archive_contains_the_documents()
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();

List<Document> docs = documentComposer.generatedItems;

for (Document document : docs) {
try(ByteArrayInputStream fis = new ByteArrayInputStream(FileFixture.getPlainTextFileContent().getContentBytes())) {
byte[] docFromZip = ZipUtils.getZipEntry(response, document.getTarget(), ZipUtils::streamToBytes);
try (ByteArrayInputStream fis =
new ByteArrayInputStream(FileFixture.getPlainTextFileContent().getContentBytes())) {
byte[] docFromZip =
ZipUtils.getZipEntry(response, document.getTarget(), ZipUtils::streamToBytes);
byte[] docFromDisk = fis.readAllBytes();

Assertions.assertArrayEquals(docFromZip, docFromDisk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@
import io.openbas.utils.fixtures.*;
import io.openbas.utils.fixtures.composers.*;
import io.openbas.utils.mockUser.WithMockAdminUser;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Optional;

import jakarta.persistence.EntityManager;
import java.util.Optional;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
Expand Down
26 changes: 13 additions & 13 deletions openbas-api/src/test/java/io/openbas/utils/ZipUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.openbas.utils;


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -10,7 +9,8 @@

public class ZipUtils {
// not this only works
public static <T> T getZipEntry(byte[] zipBlob, String entryName, Function<InputStream, T> readFunc) throws IOException {
public static <T> T getZipEntry(
byte[] zipBlob, String entryName, Function<InputStream, T> readFunc) throws IOException {
try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipBlob))) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
Expand All @@ -28,22 +28,22 @@ public static String streamToString(InputStream is) {
byte[] buffer = new byte[1024];
int read = 0;
while (true) {
try {
if (!((read = is.read(buffer, 0, 1024)) >= 0)) break;
} catch (IOException e) {
throw new RuntimeException(e);
}
sb.append(new String(buffer, 0, read));
try {
if (!((read = is.read(buffer, 0, 1024)) >= 0)) break;
} catch (IOException e) {
throw new RuntimeException(e);
}
sb.append(new String(buffer, 0, read));
}
// force exit of test since we have found the correct entry
return sb.toString();
}

public static byte[] streamToBytes(InputStream is) {
try {
return is.readAllBytes();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
return is.readAllBytes();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package io.openbas.utils.fixtures;


import io.openbas.utils.fixtures.files.PlainTextFile;


public class FileFixture {
public static String DEFAULT_PLAIN_TEXT_CONTENT = "default plain text content";
public static String DEFAULT_PLAIN_TEXT_FILENAME = "plain_text.txt";
public static String DEFAULT_PLAIN_TEXT_CONTENT = "default plain text content";
public static String DEFAULT_PLAIN_TEXT_FILENAME = "plain_text.txt";

public static PlainTextFile getPlainTextFileContent() {
return new PlainTextFile(DEFAULT_PLAIN_TEXT_CONTENT, DEFAULT_PLAIN_TEXT_FILENAME);
}
public static PlainTextFile getPlainTextFileContent() {
return new PlainTextFile(DEFAULT_PLAIN_TEXT_CONTENT, DEFAULT_PLAIN_TEXT_FILENAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
import io.openbas.database.model.Document;
import io.openbas.database.model.Tag;
import io.openbas.database.repository.DocumentRepository;

import io.openbas.service.FileService;
import io.openbas.utils.fixtures.files.BaseFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import io.openbas.service.FileService;
import io.openbas.utils.fixtures.files.BaseFile;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
Expand Down Expand Up @@ -57,10 +52,12 @@ public Composer withInMemoryFile(BaseFile file) {
@Override
public Composer persist() {
this.tagComposers.forEach(TagComposer.Composer::persist);
if(companionFile != null) {
try (ByteArrayInputStream bais = new ByteArrayInputStream(companionFile.getContentBytes())) {
if (companionFile != null) {
try (ByteArrayInputStream bais =
new ByteArrayInputStream(companionFile.getContentBytes())) {
MultipartFile mmf = new MockMultipartFile(document.getTarget(), bais.readAllBytes());
fileService.uploadFile(document.getTarget(), mmf.getInputStream(), mmf.getSize(), document.getType());
fileService.uploadFile(
document.getTarget(), mmf.getInputStream(), mmf.getSize(), document.getType());
}
}
documentRepository.save(document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import lombok.Getter;

public abstract class BaseFile<T> {
@Getter
private final T content;
@Getter
private final String fileName;
@Getter private final T content;
@Getter private final String fileName;

public BaseFile(T content, String fileName) {
this.content = content;
this.fileName = fileName;
}
public BaseFile(T content, String fileName) {
this.content = content;
this.fileName = fileName;
}

public abstract String getMimeType();
public abstract byte[] getContentBytes();
public abstract int getContentLength();
public abstract String getMimeType();

public abstract byte[] getContentBytes();

public abstract int getContentLength();
}
Loading

0 comments on commit 5cf5ed8

Please sign in to comment.