You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2021/06/08 10:32:13 UTC

[camel-quarkus] branch main updated: Add integration test for discovering custom ProtobufMapper beans

This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new 21b0e34  Add integration test for discovering custom ProtobufMapper beans
21b0e34 is described below

commit 21b0e34fefc5d0717b09b0b07311efaaec998c54
Author: James Netherton <ja...@gmail.com>
AuthorDate: Tue Jun 8 09:12:25 2021 +0100

    Add integration test for discovering custom ProtobufMapper beans
---
 .../protobuf/it/JacksonProtobufResource.java       | 13 +++++-
 .../protobuf/it/UppercaseTextDeserializer.java     | 49 ++++++++++++++++++++++
 .../jackson/protobuf/it/JacksonProtobufTest.java   |  4 +-
 3 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/integration-tests/jackson-protobuf/src/main/java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufResource.java b/integration-tests/jackson-protobuf/src/main/java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufResource.java
index 50f1b31..fab4a62 100644
--- a/integration-tests/jackson-protobuf/src/main/java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufResource.java
+++ b/integration-tests/jackson-protobuf/src/main/java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufResource.java
@@ -29,6 +29,8 @@ import javax.ws.rs.PathParam;
 import javax.ws.rs.core.Response;
 
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;
 import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
 import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader;
 import org.apache.camel.ProducerTemplate;
@@ -58,7 +60,7 @@ public class JacksonProtobufResource {
         Response.ResponseBuilder builder = Response.ok();
         String directUri = "direct:unmarshal-" + type;
 
-        if (type.equals("pojo") || type.equals("defined-dataformat")) {
+        if (type.equals("pojo") || type.equals("defined-dataformat") || type.equals("quarkus-objectmapper")) {
             Pojo result = producerTemplate.requestBody(directUri, message, Pojo.class);
             builder.entity(result.getText());
         } else if (type.equals("json-node")) {
@@ -86,4 +88,13 @@ public class JacksonProtobufResource {
         dataFormat.setUnmarshalType(Pojo.class);
         return dataFormat;
     }
+
+    @Named
+    public ProtobufMapper protobufMapper() {
+        ProtobufMapper protobufMapper = new ProtobufMapper();
+        SimpleModule module = new SimpleModule();
+        module.addDeserializer(Pojo.class, new UppercaseTextDeserializer());
+        protobufMapper.registerModule(module);
+        return protobufMapper;
+    }
 }
diff --git a/integration-tests/jackson-protobuf/src/main/java/org/apache/camel/quarkus/component/jackson/protobuf/it/UppercaseTextDeserializer.java b/integration-tests/jackson-protobuf/src/main/java/org/apache/camel/quarkus/component/jackson/protobuf/it/UppercaseTextDeserializer.java
new file mode 100644
index 0000000..77ad4b0
--- /dev/null
+++ b/integration-tests/jackson-protobuf/src/main/java/org/apache/camel/quarkus/component/jackson/protobuf/it/UppercaseTextDeserializer.java
@@ -0,0 +1,49 @@
+/*
+ * 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.jackson.protobuf.it;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+
+/**
+ * Simple deserializer to uppercase the text field on a {@link Pojo} instance
+ */
+public class UppercaseTextDeserializer extends StdDeserializer<Pojo> {
+
+    public UppercaseTextDeserializer() {
+        this(null);
+    }
+
+    public UppercaseTextDeserializer(Class<?> src) {
+        super(src);
+    }
+
+    @Override
+    public Pojo deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
+        JsonNode jsonNode = p.getCodec().readTree(p);
+        String text = jsonNode.get("text").asText();
+        Pojo pojo = new Pojo();
+        pojo.setText(text.toUpperCase(Locale.US));
+        return pojo;
+    }
+}
diff --git a/integration-tests/jackson-protobuf/src/test/java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufTest.java b/integration-tests/jackson-protobuf/src/test/java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufTest.java
index 146df70..071c82c 100644
--- a/integration-tests/jackson-protobuf/src/test/java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufTest.java
+++ b/integration-tests/jackson-protobuf/src/test/java/org/apache/camel/quarkus/component/jackson/protobuf/it/JacksonProtobufTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.quarkus.component.jackson.protobuf.it;
 
+import java.util.Locale;
+
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
 import org.junit.jupiter.api.Test;
@@ -60,6 +62,6 @@ class JacksonProtobufTest {
                 .post("/jackson-protobuf/unmarshal/defined-dataformat")
                 .then()
                 .statusCode(200)
-                .body(equalTo(message));
+                .body(equalTo(message.toUpperCase(Locale.US)));
     }
 }