Skip to content

Commit

Permalink
Fix JSON Jackson jacksonConversionPojo test apache#2726
Browse files Browse the repository at this point in the history
  • Loading branch information
ffang committed Jun 7, 2021
1 parent 8f7747f commit e5bb191
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Expand Up @@ -37,12 +37,15 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.jackson.JacksonConstants;
import org.apache.camel.component.jackson.converter.JacksonTypeConverters;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.converter.DefaultTypeConverter;
import org.apache.camel.quarkus.component.dataformats.json.model.DummyObject;
import org.apache.camel.quarkus.component.dataformats.json.model.Order;
import org.apache.camel.quarkus.component.dataformats.json.model.Pojo;
Expand All @@ -51,6 +54,7 @@
import org.apache.camel.quarkus.component.dataformats.json.model.TestPojo;
import org.apache.camel.quarkus.component.dataformats.json.model.TestPojoView;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.support.SimpleTypeConverter;
import org.jboss.logging.Logger;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -493,6 +497,13 @@ public void jacksonUnmarshalListJackson(String body) throws Exception {
@GET
public void jacksonConversionPojo(String body) throws Exception {
synchronized (context) {
DefaultTypeConverter registry = (DefaultTypeConverter) context.getTypeConverterRegistry();
//re-registry JacksonTypeConverters so we can get a clean test environment
//so the context global options we set here can take effect.
registry.addFallbackTypeConverter(
new SimpleTypeConverter(false,
(type, exchange, value) -> getJacksonTypeConverters().convertTo(type, exchange, value, registry)),
false);
context.getGlobalOptions().put(JacksonConstants.ENABLE_TYPE_CONVERTER, "true");
context.getGlobalOptions().put(JacksonConstants.TYPE_CONVERTER_TO_POJO, "true");

Expand All @@ -504,6 +515,14 @@ public void jacksonConversionPojo(String body) throws Exception {
String json = (String) producerTemplate.requestBody("direct:jackson-conversion-pojo-test", order);
assertEquals("{\"id\":0,\"partName\":\"Camel\",\"amount\":1,\"customerName\":\"Acme\"}", json);

//re-registry JacksonTypeConverters so we can get a clean test environment
//so the context global options we set here can take effect.
registry.addFallbackTypeConverter(
new SimpleTypeConverter(false,
(type, exchange, value) -> getJacksonTypeConverters().convertTo(type, exchange, value, registry)),
false);
context.getGlobalOptions().put(JacksonConstants.ENABLE_TYPE_CONVERTER, "true");
context.getGlobalOptions().put(JacksonConstants.TYPE_CONVERTER_TO_POJO, "true");
context.getGlobalOptions().put(JacksonConstants.TYPE_CONVERTER_MODULE_CLASS_NAMES,
JaxbAnnotationModule.class.getName());

Expand All @@ -513,16 +532,18 @@ public void jacksonConversionPojo(String body) throws Exception {
order.setPartName("Camel");

json = (String) producerTemplate.requestBody("direct:jackson-conversion-pojo-test", order);
/*
* somehow jaxb annotation @XmlAttribute(name = "customer_name") can't be taken into account so the
* following asserts failed, need to investigate more
* assertEquals("{\"id\":0,\"partName\":\"Camel\",\"amount\":1,\"customer_name\":\"Acme\"}",
* json);
*/
assertEquals("{\"id\":0,\"partName\":\"Camel\",\"amount\":1,\"customer_name\":\"Acme\"}", json);
}

}

private JacksonTypeConverters getJacksonTypeConverters() {
JacksonTypeConverters jacksonTypeConverters = new JacksonTypeConverters();
CamelContextAware.trySetCamelContext(jacksonTypeConverters, context);

return jacksonTypeConverters;
}

@Path("jackson/conversion")
@GET
public void jacksonConversion(String body) throws Exception {
Expand Down
Expand Up @@ -18,7 +18,6 @@

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

import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -146,7 +145,6 @@ void jacksonUnmarshalListJackson() {
}

@Test
@Disabled("https://github.com/apache/camel-quarkus/issues/2726")
void jacksonConversionPojo() {
RestAssured.get("/dataformats-json/jackson/conversion-pojo")
.then()
Expand Down

0 comments on commit e5bb191

Please sign in to comment.