From 83d949c7da69b6d3205eace101833f7b8d5223c2 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 8 Oct 2024 19:36:53 +0200 Subject: [PATCH] fix resource reader in ImportHostingAssets --- .../hsadminng/hs/migration/CsvDataImport.java | 14 ++++++-------- .../hs/migration/ImportHostingAssets.java | 18 +++++++++--------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/migration/CsvDataImport.java b/src/test/java/net/hostsharing/hsadminng/hs/migration/CsvDataImport.java index b5b7de8e..d04fc0a5 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/migration/CsvDataImport.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/migration/CsvDataImport.java @@ -15,6 +15,7 @@ import org.opentest4j.AssertionFailedError; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.core.io.Resource; import org.springframework.transaction.support.TransactionTemplate; import jakarta.persistence.EntityManager; @@ -24,7 +25,6 @@ import jakarta.validation.ValidationException; import jakarta.validation.constraints.NotNull; import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; @@ -33,6 +33,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.math.BigDecimal; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.time.LocalDate; import java.util.LinkedHashSet; import java.util.List; @@ -123,13 +124,10 @@ public class CsvDataImport extends ContextBasedTest { } } - protected String resourceAsString(@NotNull final String resourcePath) { - try (InputStream inputStream = requireNonNull(getClass().getClassLoader().getResourceAsStream(resourcePath)); - final var reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { - return reader.lines().collect(Collectors.joining(System.lineSeparator())); - } catch (Exception exc) { - throw new AssertionFailedError("cannot open '" + resourcePath + "'"); - } + @SneakyThrows + protected String resourceAsString(final Resource resource) { + final var lines = Files.readAllLines(resource.getFile().toPath(), StandardCharsets.UTF_8); + return String.join("\n", lines); } protected List withoutHeader(final List records) { diff --git a/src/test/java/net/hostsharing/hsadminng/hs/migration/ImportHostingAssets.java b/src/test/java/net/hostsharing/hsadminng/hs/migration/ImportHostingAssets.java index 904149b4..a831f637 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/migration/ImportHostingAssets.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/migration/ImportHostingAssets.java @@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.hs.migration; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.SneakyThrows; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hash.HashGenerator; import net.hostsharing.hsadminng.hash.HashGenerator.Algorithm; @@ -26,10 +27,9 @@ import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.extension.ExtendWith; -import org.reflections.Reflections; -import org.reflections.scanners.ResourcesScanner; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.Import; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.test.annotation.Commit; import org.springframework.test.annotation.DirtiesContext; @@ -46,7 +46,6 @@ import java.util.TreeMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; -import java.util.regex.Pattern; import static java.util.Arrays.stream; import static java.util.Map.entry; @@ -497,13 +496,14 @@ public class ImportHostingAssets extends BaseOfficeDataImport { @Test @Order(16020) + @SneakyThrows void importZonenfiles() { - final var reflections = new Reflections(MIGRATION_DATA_PATH + "/hosting/zonefiles", new ResourcesScanner()); - final var zonefileFiles = reflections.getResources(Pattern.compile(".*\\.json")).stream().sorted().toList(); - zonefileFiles.forEach(zonenfileName -> { - System.out.println("Processing zonenfile: " + zonenfileName); - importZonefiles(vmName(zonenfileName), resourceAsString(zonenfileName)); - }); + final var resolver = new PathMatchingResourcePatternResolver(); + final var resources = resolver.getResources("/" + MIGRATION_DATA_PATH + "/hosting/zonefiles/*.json"); + for (var resource : resources) { + System.out.println("Processing zonenfile: " + resource); + importZonefiles(vmName(resource.getFilename()), resourceAsString(resource)); + } } @Test