Skip to content

Commit

Permalink
Avoid IOException on constructor apache#225
Browse files Browse the repository at this point in the history
- 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 <apupier@redhat.com>
  • Loading branch information
apupier committed May 4, 2021
1 parent 812b868 commit a680585
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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.");
}

}

0 comments on commit a680585

Please sign in to comment.