From 3c4cbe37cff92f322c05e6e80658c027b47d0d37 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Thu, 21 Nov 2024 22:27:47 +0100 Subject: [PATCH] Fix missing URL decoding in test (#17782) Signed-off-by: Jacob Laursen Signed-off-by: Ciprian Pascu --- .../internal/DefinitionParserTest.java | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/bundles/org.openhab.binding.solarman/src/test/java/org/openhab/binding/solarman/internal/DefinitionParserTest.java b/bundles/org.openhab.binding.solarman/src/test/java/org/openhab/binding/solarman/internal/DefinitionParserTest.java index 4d09d2d246d..8dbbf3e9276 100644 --- a/bundles/org.openhab.binding.solarman/src/test/java/org/openhab/binding/solarman/internal/DefinitionParserTest.java +++ b/bundles/org.openhab.binding.solarman/src/test/java/org/openhab/binding/solarman/internal/DefinitionParserTest.java @@ -16,9 +16,12 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.io.IOException; +import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.List; @@ -27,45 +30,44 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Collectors; import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.junit.jupiter.api.Test; import org.openhab.binding.solarman.internal.defmodel.InverterDefinition; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * @author Catalin Sanda - Initial contribution */ @NonNullByDefault public class DefinitionParserTest { - private final Logger logger = LoggerFactory.getLogger(DefinitionParserTest.class); + private static final String YAML_EXTENSION = ".yaml"; + private static final String DIRECTORY_PREFIX = "definitions/"; + private final DefinitionParser definitionParser = new DefinitionParser(); @Test void testInverterDefinitionsCanBeLoaded() throws IOException { - List yamlFiles = scanForYamlFiles("definitions"); + List yamlFiles = scanForYamlFiles(DIRECTORY_PREFIX); List definitionIds = extractDefinitionIdFromYamlFiles(yamlFiles); - assertFalse(definitionIds.isEmpty()); + assertFalse(definitionIds.isEmpty(), "No YAML files found in directory: " + DIRECTORY_PREFIX); definitionIds.forEach(definitionId -> { @Nullable InverterDefinition inverterDefinition = definitionParser.parseDefinition(definitionId); - assertNotNull(inverterDefinition); + assertNotNull(inverterDefinition, "Failed to parse inverter definition: " + definitionId); }); } public static List extractDefinitionIdFromYamlFiles(List yamlFiles) { - Pattern pattern = Pattern.compile("definitions/(.*)\\.yaml"); + Pattern pattern = Pattern.compile("definitions/(.*)\\" + YAML_EXTENSION); return yamlFiles.stream().map(file -> { Matcher matcher = pattern.matcher(file); return matcher.matches() ? matcher.group(1) : file; - }).collect(Collectors.toList()); + }).toList(); } public List scanForYamlFiles(String directoryPath) throws IOException { @@ -75,19 +77,19 @@ public class DefinitionParserTest { Collections.list(resources).stream().flatMap(resource -> { try { - if (resource.getProtocol().equals("jar")) { + if ("jar".equals(resource.getProtocol())) { String path = resource.getPath(); - String jarPath = path.substring(5, path.indexOf("!")); + String jarPath = path.substring(path.indexOf("file:") + 5, path.indexOf("!")); try (JarFile jarFile = new JarFile(jarPath)) { - return jarFile.stream() - .filter(e -> e.getName().startsWith(directoryPath) && e.getName().endsWith(".yaml")) + return jarFile.stream().filter( + e -> e.getName().startsWith(directoryPath) && e.getName().endsWith(YAML_EXTENSION)) .map(JarEntry::getName); } - } else if (resource.getProtocol().equals("file")) { + } else if ("file".equals(resource.getProtocol())) { return scanDirectory(directoryPath).stream(); } - } catch (IOException e) { - logger.error(e.getMessage(), e); + } catch (IOException | URISyntaxException e) { + throw new IllegalStateException("Error processing resource: " + resource, e); } return Stream.empty(); }).forEach(yamlFiles::add); @@ -95,15 +97,18 @@ public class DefinitionParserTest { return yamlFiles; } - private static List scanDirectory(String directoryPath) throws IOException { + private static List scanDirectory(String directoryPath) throws IOException, URISyntaxException { URL url = Objects.requireNonNull(DefinitionParserTest.class.getClassLoader()).getResource(directoryPath); - if (url == null) { + Path directory = Paths.get(url.toURI()); + + if (!Files.isDirectory(directory)) { throw new IllegalArgumentException("Invalid directory path: " + directoryPath); } - String[] files = new java.io.File(url.getPath()).list((dir, name) -> name.endsWith(".yaml")); - if (files != null) { - return Arrays.stream(files).map(file -> directoryPath + "/" + file).toList(); + + try (Stream stream = Files.list(directory)) { + List files = stream.filter(file -> file.getFileName().toString().endsWith(YAML_EXTENSION)) + .map(file -> directoryPath + "/" + file.getFileName().toString()).toList(); + return files; } - return Collections.emptyList(); } }