From a680585ba35b8403fb506c7a0445ed041f6a7a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pupier?= Date: Tue, 4 May 2021 22:18:30 +0200 Subject: [PATCH] Avoid IOException on constructor #225 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - when a kamelet file cannot be loaded, a log is provided but other files still can load - provided a test to ensure that we are not embedding some kamelet files that are invalid and that we won't detect anymore with existing tests Signed-off-by: Aurélien Pupier --- .../kamelets/catalog/KameletsCatalog.java | 44 +++++++++++-------- .../kamelets/catalog/KameletsCatalogTest.java | 19 +++++--- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/library/camel-kamelets-catalog/src/main/java/org/apache/camel/kamelets/catalog/KameletsCatalog.java b/library/camel-kamelets-catalog/src/main/java/org/apache/camel/kamelets/catalog/KameletsCatalog.java index a32f1d97f..777c7de97 100644 --- a/library/camel-kamelets-catalog/src/main/java/org/apache/camel/kamelets/catalog/KameletsCatalog.java +++ b/library/camel-kamelets-catalog/src/main/java/org/apache/camel/kamelets/catalog/KameletsCatalog.java @@ -16,18 +16,6 @@ */ package org.apache.camel.kamelets.catalog; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import io.fabric8.camelk.v1alpha1.Kamelet; -import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps; -import io.github.classgraph.ClassGraph; -import io.github.classgraph.ScanResult; -import org.apache.camel.kamelets.catalog.model.KameletLabelNames; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -37,29 +25,47 @@ import java.util.Map; import java.util.stream.Collectors; +import org.apache.camel.kamelets.catalog.model.KameletLabelNames; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + +import io.fabric8.camelk.v1alpha1.Kamelet; +import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps; +import io.github.classgraph.ClassGraph; +import io.github.classgraph.ScanResult; + public class KameletsCatalog { private static final Logger LOG = LoggerFactory.getLogger(KameletsCatalog.class); - private static final String KAMELETS_DIR = "kamelets"; + static final String KAMELETS_DIR = "kamelets"; private static final String KAMELETS_FILE_SUFFIX = ".kamelet.yaml"; private static ObjectMapper mapper = new ObjectMapper(new YAMLFactory()).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); private Map kameletModels = new HashMap<>(); private List kameletNames = new ArrayList<>(); - public KameletsCatalog() throws IOException { + public KameletsCatalog() { initCatalog(); kameletNames = kameletModels.keySet().stream().sorted(Comparator.naturalOrder()).map(x -> x).collect(Collectors.toList()); } - private void initCatalog() throws IOException { + private void initCatalog() { List resourceNames; try (ScanResult scanResult = new ClassGraph().acceptPaths("/" + KAMELETS_DIR + "/").scan()) { resourceNames = scanResult.getAllResources().getPaths(); } - for (String fileName: - resourceNames) { - Kamelet kamelet = mapper.readValue(KameletsCatalog.class.getResourceAsStream("/" + fileName), Kamelet.class); - kameletModels.put(sanitizeFileName(fileName), kamelet); + for (String fileName: resourceNames) { + String pathInJar = "/" + fileName; + try { + Kamelet kamelet = mapper.readValue(KameletsCatalog.class.getResourceAsStream(pathInJar), Kamelet.class); + kameletModels.put(sanitizeFileName(fileName), kamelet); + } catch (IOException e) { + LOG.warn("Cannot init Kamelet Catalog with content of " + pathInJar, e); + } } } diff --git a/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/KameletsCatalogTest.java b/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/KameletsCatalogTest.java index c94557e3f..432bcb37b 100644 --- a/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/KameletsCatalogTest.java +++ b/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/KameletsCatalogTest.java @@ -19,23 +19,21 @@ import com.fasterxml.jackson.databind.JsonNode; import io.fabric8.camelk.v1alpha1.Kamelet; import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps; +import io.github.classgraph.ClassGraph; + import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import java.io.IOException; +import static org.junit.jupiter.api.Assertions.*; + import java.util.List; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - public class KameletsCatalogTest { static KameletsCatalog catalog; @BeforeAll - public static void createKameletsCatalog() throws IOException { + public static void createKameletsCatalog() { catalog = new KameletsCatalog(); } @@ -93,4 +91,11 @@ void testGetKameletsFlow() throws Exception { JsonNode flow = catalog.getKameletFlow("aws-sqs-source"); assertNotNull(flow); } + + @Test + void testAllKameletFilesLoaded() throws Exception { + int numberOfKameletFiles = new ClassGraph().acceptPaths("/" + KameletsCatalog.KAMELETS_DIR + "/").scan().getAllResources().size(); + assertEquals(numberOfKameletFiles, catalog.getKameletsName().size(), "Some embedded kamelet definition files cannot be loaded."); + } + }