Skip to content

Commit

Permalink
fixes in type convertors (because of CAMEL-19897)x
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Jul 2, 2024
1 parent f28d97b commit 6394df7
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 71 deletions.
6 changes: 6 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/core.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ Filter for tracing messages.
| `string`
|

|icon:lock[title=Fixed at build time] [[quarkus.camel.type-converter.statistics-enabled]]`link:#quarkus.camel.type-converter.statistics-enabled[quarkus.camel.type-converter.statistics-enabled]`

Build time configuration options for enable/disable type converter statistics.
| `boolean`
| `true`

|icon:lock[title=Fixed at build time] [[quarkus.camel.main.shutdown.timeout]]`link:#quarkus.camel.main.shutdown.timeout[quarkus.camel.main.shutdown.timeout]`

A timeout (with millisecond precision) to wait for `CamelMain++#++stop()` to finish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,16 @@ void camelServices(
@BuildStep
CamelTypeConverterRegistryBuildItem typeConverterRegistry(
CamelRecorder recorder,
CamelConfig camelConfig,
ApplicationArchivesBuildItem applicationArchives,
List<CamelTypeConverterLoaderBuildItem> additionalLoaders,
CombinedIndexBuildItem combinedIndex,
BuildProducer<UnremovableBeanBuildItem> unremovableBean) {

IndexView index = combinedIndex.getIndex();

RuntimeValue<TypeConverterRegistry> typeConverterRegistry = recorder.createTypeConverterRegistry();
RuntimeValue<TypeConverterRegistry> typeConverterRegistry = recorder
.createTypeConverterRegistry(camelConfig.typeConverter.statisticsEnabled);

//
// This should be simplified by searching for classes implementing TypeConverterLoader but that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public enum FailureRemedy {
@ConfigItem
public TraceConfig trace;

/**
* Build time configuration options for the Camel type converter.
*/
@ConfigItem
public TypeConverterConfig typeConverter;

@ConfigGroup
public static class BootstrapConfig {
/**
Expand Down Expand Up @@ -504,4 +510,14 @@ public static class TraceConfig {
@ConfigItem
public Optional<String> traceFilter;
}

@ConfigGroup
public static class TypeConverterConfig {

/**
* Build time configuration options for enable/disable type converter statistics.
*/
@ConfigItem(defaultValue = "true")
public boolean statisticsEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public RuntimeValue<Registry> createRegistry(Map<String, CamelBeanQualifierResol
return new RuntimeValue<>(new RuntimeRegistry(beanQualifierResolvers));
}

public RuntimeValue<TypeConverterRegistry> createTypeConverterRegistry() {
return new RuntimeValue<>(new FastTypeConverter());
public RuntimeValue<TypeConverterRegistry> createTypeConverterRegistry(boolean statisticsEnabled) {
return new RuntimeValue<>(new FastTypeConverter(statisticsEnabled));
}

public void addTypeConverterLoader(RuntimeValue<TypeConverterRegistry> registry, RuntimeValue<TypeConverterLoader> loader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
public class FastTypeConverter extends DefaultTypeConverter {
private static final Logger LOG = LoggerFactory.getLogger(FastTypeConverter.class);

public FastTypeConverter() {
super(null, null, null, false);
public FastTypeConverter(boolean statisticsEnabled) {
super(null, null, null, false, statisticsEnabled);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,11 @@ public MyNullablePair convertMyNullablePair(String input) {
return context.getTypeConverter().convertTo(MyNullablePair.class, input);
}

@Path("/setStatisticsEnabled")
@POST
@Path("/resetStatistics")
@GET
@Produces(MediaType.TEXT_PLAIN)
public void converterSetStatisticsEnabled(boolean value) {
context.getTypeConverterRegistry().getStatistics().setStatisticsEnabled(value);
if (value) {
context.getTypeConverterRegistry().getStatistics().reset();
}
public void resetStatistics() {
context.getTypeConverterRegistry().getStatistics().reset();
}

@Path("/getStatisticsHit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.quarkus.core.converter.it;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -47,28 +46,6 @@ void testConverterFromAnnotationAsCdiBean() {
testConverter("/converter/myTestPair/float", "2.0", "test_2.0", "3.0");
}

@Test
void testConverterToNull() {
enableStatistics(true);

testConverterReturningNull("/converter/myNullablePair", "null");

RestAssured.when().get("/converter/getStatisticsHit").then().body("hit", is(1), "miss", is(0));

enableStatistics(false);
}

@Test
void testNotRegisteredConverter() {
enableStatistics(true);

testConverterReturningNull("/converter/myNotRegisteredPair", "a:b");

RestAssured.when().get("/converter/getStatisticsHit").then().body("hit", is(0), "miss", is(1));

enableStatistics(false);
}

@Test
void testBulkConverters() {
//converters generated with @Converter(generateBulkLoader = true)
Expand All @@ -81,16 +58,4 @@ void testLoaderConverters() {
//converters generated with @Converter(generateLoader = true)
testConverter("/converter/myLoaderPair", "a:b", "loader_a", "b");
}

@Test
void testConverterGetStatistics() {
enableStatistics(true);

//cause 1 hit
testConverterFromAnnotationWithStaticMethods();

RestAssured.when().get("/converter/getStatisticsHit").then().body("hit", is(1), "miss", is(0));

enableStatistics(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

public abstract class ConverterTestBase {

void enableStatistics(boolean b) {
void resetStatistics() {
RestAssured.given()
.contentType(ContentType.TEXT).body(b)
.post("/converter/setStatisticsEnabled")
.contentType(ContentType.TEXT)
.get("/converter/resetStatistics")
.then()
.statusCode(204);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.converter.it;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class ConverterWithStatisticsIT extends ConverterWithStatisticsTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.converter.it;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.is;

/**
* The test requires the `quarkus.camel.type-converter.statistics-enabled=true`.
*
* For JVM mode, such behavior is achieved by the testProfile.
* For native mode, the quarkus-maven-plugin is executed twice with both false/true options in the property.
* Test profile changes the value to `true` for this class.
* Quarkus the uses `-Dquarkus.configuration.build-time-mismatch-at-runtime=fail
* -Dquarkus.camel.type-converter.statistics-enabled=true`.
* See https://quarkus.io/guides/reaugmentation for more details.
*/
@QuarkusTest
public class ConverterWithStatisticsTest extends ConverterTestBase {

@BeforeEach
void beforeEach() {
resetStatistics();
}

@AfterEach
void afterEach() {
resetStatistics();
}

@Test
void testConverterToNull() {
testConverterReturningNull("/converter/myNullablePair", "null");

RestAssured.when().get("/converter/getStatisticsHit").then().body("hit", is(1), "miss", is(0));

resetStatistics();
}

@Test
void testNotRegisteredConverter() {
testConverterReturningNull("/converter/myNotRegisteredPair", "a:b");

RestAssured.when().get("/converter/getStatisticsHit").then().body("hit", is(0), "miss", is(1));

resetStatistics();
}

@Test
void testConverterGetStatistics() {
//cause 1 hit
testConverter("/converter/myTestPair/string", "a:b", "test_a", "b");

RestAssured.when().get("/converter/getStatisticsHit").then().body("hit", is(1), "miss", is(0));

resetStatistics();
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<github-api.version>1.313</github-api.version><!-- Used in a Groovy script bellow -->
<google-auth-library.version>1.23.0</google-auth-library.version><!-- @sync com.google.cloud:google-cloud-pubsub:${google-cloud-pubsub.version} dep:com.google.auth:google-auth-library-oauth2-http -->
<google-oauth-client.version>${google-oauth-client-version}</google-oauth-client.version>
<google-cloud-bom.version>0.222.0</google-cloud-bom.version><!-- @sync com.google.cloud:libraries-bom:${google-cloud-bom-version} dep:com.google.cloud:google-cloud-bom -->
<google-cloud-bom.version>0.223.0</google-cloud-bom.version><!-- @sync com.google.cloud:libraries-bom:${google-cloud-bom-version} dep:com.google.cloud:google-cloud-bom -->
<google-cloud-pubsub-bom.version>1.130.0</google-cloud-pubsub-bom.version><!-- @sync com.google.cloud:google-cloud-bom:${google-cloud-bom.version} dep:com.google.cloud:google-cloud-pubsub-bom -->
<google-cloud-pubsub.version>1.130.0</google-cloud-pubsub.version><!-- @sync com.google.cloud:google-cloud-pubsub-bom:${google-cloud-pubsub-bom.version} dep:com.google.cloud:google-cloud-pubsub -->
<graalvm.version>23.1.2</graalvm.version><!-- @sync io.quarkus:quarkus-bom:${quarkus.version} dep:org.graalvm.sdk:graal-sdk -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@
*/
package org.apache.camel.quarkus.k.tooling.maven;

import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.apicurio.datamodels.Library;
import io.apicurio.datamodels.models.openapi.OpenApiDocument;
import io.apicurio.datamodels.models.util.JsonUtil;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
import org.apache.camel.CamelContext;
import org.apache.camel.generator.openapi.RestDslXmlGenerator;
import org.apache.camel.impl.DefaultCamelContext;
Expand Down Expand Up @@ -59,18 +53,8 @@ public void execute() throws MojoExecutionException {
}

try {
JsonFactory factory = null;
if (inputFile.endsWith(".yaml") || inputFile.endsWith(".yml")) {
factory = new YAMLFactory();
}

ObjectMapper mapper = new ObjectMapper(factory);
mapper.findAndRegisterModules();

FileInputStream fis = new FileInputStream(inputFile);

JsonNode node = mapper.readTree(fis);
OpenApiDocument document = (OpenApiDocument) Library.readDocument(JsonUtil.toObject(node));
OpenAPIV3Parser parser = new OpenAPIV3Parser();
OpenAPI document = parser.read(input.toFile().getAbsolutePath());

final Writer writer;

Expand Down

0 comments on commit 6394df7

Please sign in to comment.