This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kamelets.git


The following commit(s) were added to refs/heads/main by this push:
     new 4e4bae2  Avoid IOException on constructor #225
4e4bae2 is described below

commit 4e4bae2f4043820b4d10e7d69a7cb4cbf350ab4e
Author: AurĂ©lien Pupier <[email protected]>
AuthorDate: Tue May 4 22:18:30 2021 +0200

    Avoid IOException on constructor #225
    
    - 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 <[email protected]>
---
 .../apache/camel/kamelets/catalog/KameletsCatalog.java | 18 +++++++++++-------
 .../camel/kamelets/catalog/KameletsCatalogTest.java    | 18 +++++++++++++-----
 2 files changed, 24 insertions(+), 12 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 a32f1d9..99416ab 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
@@ -40,26 +40,30 @@ import java.util.stream.Collectors;
 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<String, Kamelet> kameletModels = new HashMap<>();
     private List<String> 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<String> 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 c94557e..7aba2d0 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,24 @@ package org.apache.camel.kamelets.catalog;
 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 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;
 
+import java.util.List;
+import java.util.Map;
+
 public class KameletsCatalogTest {
     static KameletsCatalog catalog;
 
     @BeforeAll
-    public static void createKameletsCatalog() throws IOException {
+    public static void createKameletsCatalog() {
         catalog = new KameletsCatalog();
     }
 
@@ -93,4 +94,11 @@ public class KameletsCatalogTest {
         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.");
+    }
+
 }

Reply via email to