Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid IOException on constructor #225 #226

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,30 @@
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,24 @@
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();
}

Expand Down Expand Up @@ -93,4 +94,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.");
}

}