Skip to content

Commit

Permalink
Add tests for FastCamelContext JSON schema lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Apr 4, 2024
1 parent a9b6a21 commit 3e9a7ca
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 18 deletions.
5 changes: 5 additions & 0 deletions extensions-core/core/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<artifactId>camel-timer</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-zipfile</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.core.deployment;

import java.io.IOException;

import io.quarkus.test.QuarkusUnitTest;
import jakarta.inject.Inject;
import org.apache.camel.CamelContext;
import org.apache.camel.quarkus.core.FastCamelContext;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class CamelContextJsonSchemaTest {
@RegisterExtension
static final QuarkusUnitTest CONFIG = new QuarkusUnitTest().withEmptyApplication();

@Inject
CamelContext context;

@Test
void resolveComponentJsonSchema() throws IOException {
String json = ((FastCamelContext) context).getComponentParameterJsonSchema("timer");
Assertions.assertNotNull(json);
}

@Test
void resolveDataFormatJsonSchema() throws IOException {
String json = ((FastCamelContext) context).getDataFormatParameterJsonSchema("zipFile");
Assertions.assertNotNull(json);
}

@Test
void resolveLanguageJsonSchema() throws IOException {
String json = ((FastCamelContext) context).getLanguageParameterJsonSchema("bean");
Assertions.assertNotNull(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.camel.Component;
import org.apache.camel.TypeConverter;
import org.apache.camel.component.microprofile.config.CamelMicroProfilePropertiesSource;
import org.apache.camel.console.DevConsole;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.engine.DefaultComponentResolver;
import org.apache.camel.impl.engine.DefaultDataFormatResolver;
Expand Down Expand Up @@ -245,20 +244,7 @@ public String getDataFormatParameterJsonSchema(String dataFormatName) throws IOE

@Override
public String getDevConsoleParameterJsonSchema(String devConsoleName) throws IOException {
Class<?> clazz;

Object instance = getRegistry().lookupByNameAndType(devConsoleName, DevConsole.class);
if (instance != null) {
clazz = instance.getClass();
} else {
clazz = getCamelContextExtension().getFactoryFinder(DefaultDevConsoleResolver.DEV_CONSOLE_RESOURCE_PATH)
.findClass(devConsoleName).orElse(null);
if (clazz == null) {
return null;
}
}

return getJsonSchema(clazz.getPackage().getName(), devConsoleName);
return getJsonSchema(DefaultDevConsoleResolver.DEV_CONSOLE_RESOURCE_PATH, devConsoleName);
}

@Override
Expand All @@ -280,17 +266,36 @@ public String getLanguageParameterJsonSchema(String languageName) throws IOExcep
}

private String getJsonSchema(String packageName, String name) throws IOException {
String path = "META-INF/" + packageName.replace('.', '/') + "/" + name + ".json";
InputStream inputStream = getClassResolver().loadResourceAsStream(path);
String fileName = name + ".json";
String path;
if (packageName.contains("/")) {
path = packageName + fileName;
} else {
path = "META-INF/" + packageName.replace('.', '/') + "/" + fileName;
}

String json = loadJsonSchema(path);
if (json == null && path.startsWith("META-INF")) {
// Try fallback to the 'classic' service path without the META-INF prefix
json = loadJsonSchema(path.replace("META-INF/", ""));

if (json == null) {
// Try fallback to non-service paths with the META-INF prefix
json = loadJsonSchema(path.replace("META-INF/services", "META-INF"));
}
}
return json;
}

private String loadJsonSchema(String path) throws IOException {
InputStream inputStream = getClassResolver().loadResourceAsStream(path);
if (inputStream != null) {
try {
return IOHelper.loadText(inputStream);
} finally {
IOHelper.close(inputStream);
}
}

return null;
}

Expand Down
11 changes: 11 additions & 0 deletions extensions-jvm/console/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-console</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stub</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.console;

import java.io.IOException;

import io.quarkus.test.QuarkusUnitTest;
import jakarta.inject.Inject;
import org.apache.camel.CamelContext;
import org.apache.camel.quarkus.core.FastCamelContext;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class CamelContextJsonSchemaTest {
@RegisterExtension
static final QuarkusUnitTest CONFIG = new QuarkusUnitTest().withEmptyApplication();

@Inject
CamelContext context;

@Test
void resolveDevConsoleJsonSchema() throws IOException {
String propertiesJson = ((FastCamelContext) context).getDevConsoleParameterJsonSchema("properties");
Assertions.assertNotNull(propertiesJson);

String stubJson = ((FastCamelContext) context).getDevConsoleParameterJsonSchema("stub");
Assertions.assertNotNull(stubJson);
}
}

0 comments on commit 3e9a7ca

Please sign in to comment.