You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2023/05/03 14:22:41 UTC

[camel-quarkus] branch main updated: [MapStruct] Add tests

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

ppalaga 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 b9e3722940 [MapStruct] Add tests
b9e3722940 is described below

commit b9e37229400dc94154d61ffd7e55c4087a93b346
Author: Andrej Vano <av...@redhat.com>
AuthorDate: Wed Apr 26 14:54:51 2023 +0200

    [MapStruct] Add tests
---
 integration-tests-jvm/mapstruct/pom.xml            | 23 +++++++
 .../component/mapstruct/it/MapStructResource.java  | 55 ++++++++++++++++
 .../component/mapstruct/it/MapStructRoutes.java    | 37 +++++++++++
 .../component/mapstruct/it/mapper/CarMapper.java   | 30 +++++++++
 .../quarkus/component/mapstruct/it/model/Car.java  | 73 ++++++++++++++++++++++
 .../component/mapstruct/it/model/Vehicle.java      | 73 ++++++++++++++++++++++
 .../component/mapstruct/it/MapStructIT.java        | 23 +++++++
 .../component/mapstruct/it/MapStructTest.java      | 49 +++++++++++++++
 pom.xml                                            |  1 +
 9 files changed, 364 insertions(+)

diff --git a/integration-tests-jvm/mapstruct/pom.xml b/integration-tests-jvm/mapstruct/pom.xml
index f9b5ecb66d..30873e2979 100644
--- a/integration-tests-jvm/mapstruct/pom.xml
+++ b/integration-tests-jvm/mapstruct/pom.xml
@@ -37,6 +37,11 @@
             <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-mapstruct</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-direct</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-resteasy</artifactId>
@@ -81,4 +86,22 @@
             </dependencies>
         </profile>
     </profiles>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <annotationProcessorPaths>
+                        <path>
+                            <groupId>org.mapstruct</groupId>
+                            <artifactId>mapstruct-processor</artifactId>
+                            <version>${mapstruct.version}</version>
+                        </path>
+                    </annotationProcessorPaths>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 </project>
diff --git a/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructResource.java b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructResource.java
new file mode 100644
index 0000000000..7bad4fdf11
--- /dev/null
+++ b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructResource.java
@@ -0,0 +1,55 @@
+/*
+ * 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.mapstruct.it;
+
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.core.Response;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.quarkus.component.mapstruct.it.model.Car;
+import org.apache.camel.quarkus.component.mapstruct.it.model.Vehicle;
+
+@Path("/mapstruct")
+@ApplicationScoped
+public class MapStructResource {
+    @Inject
+    ProducerTemplate producerTemplate;
+
+    @Path("/component")
+    @POST
+    @Consumes("text/plain")
+    @Produces("text/plain")
+    public Response componentTest(String vehicleString) {
+        return Response.ok(testMapping("component", vehicleString)).build();
+    }
+
+    @Path("/converter")
+    @POST
+    @Consumes("text/plain")
+    @Produces("text/plain")
+    public Response converterTest(String vehicleString) {
+        return Response.ok(testMapping("converter", vehicleString)).build();
+    }
+
+    private String testMapping(String endpoint, String vehicleString) {
+        return producerTemplate.requestBody("direct:" + endpoint, Vehicle.fromString(vehicleString), Car.class).toString();
+    }
+}
diff --git a/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructRoutes.java b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructRoutes.java
new file mode 100644
index 0000000000..3671184da9
--- /dev/null
+++ b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructRoutes.java
@@ -0,0 +1,37 @@
+/*
+ * 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.mapstruct.it;
+
+import jakarta.inject.Named;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mapstruct.MapstructComponent;
+import org.apache.camel.quarkus.component.mapstruct.it.model.Car;
+
+public class MapStructRoutes extends RouteBuilder {
+    @Named("mapstruct")
+    MapstructComponent mapstruct() {
+        MapstructComponent mapstruct = new MapstructComponent();
+        mapstruct.setMapperPackageName("org.apache.camel.quarkus.component.mapstruct.it.mapper");
+        return mapstruct;
+    }
+
+    @Override
+    public void configure() throws Exception {
+        from("direct:component").to("mapstruct:" + Car.class.getName());
+        from("direct:converter").convertBodyTo(Car.class);
+    }
+}
diff --git a/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/mapper/CarMapper.java b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/mapper/CarMapper.java
new file mode 100644
index 0000000000..0a7f236df2
--- /dev/null
+++ b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/mapper/CarMapper.java
@@ -0,0 +1,30 @@
+/*
+ * 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.mapstruct.it.mapper;
+
+import org.apache.camel.quarkus.component.mapstruct.it.model.Car;
+import org.apache.camel.quarkus.component.mapstruct.it.model.Vehicle;
+import org.mapstruct.Mapper;
+import org.mapstruct.Mapping;
+
+@Mapper
+public interface CarMapper {
+    @Mapping(source = "company", target = "brand")
+    @Mapping(source = "name", target = "model")
+    @Mapping(source = "power", target = "electric")
+    Car toCar(Vehicle vehicle);
+}
diff --git a/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/model/Car.java b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/model/Car.java
new file mode 100644
index 0000000000..09eda2f054
--- /dev/null
+++ b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/model/Car.java
@@ -0,0 +1,73 @@
+/*
+ * 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.mapstruct.it.model;
+
+public class Car {
+    private String brand;
+    private String model;
+    private int year;
+    private boolean electric;
+
+    public Car(String brand, String model, int year, boolean electric) {
+        this.brand = brand;
+        this.model = model;
+        this.year = year;
+        this.electric = electric;
+    }
+
+    public String getBrand() {
+        return brand;
+    }
+
+    public void setBrand(String brand) {
+        this.brand = brand;
+    }
+
+    public String getModel() {
+        return model;
+    }
+
+    public void setModel(String model) {
+        this.model = model;
+    }
+
+    public int getYear() {
+        return year;
+    }
+
+    public void setYear(int year) {
+        this.year = year;
+    }
+
+    public boolean isElectric() {
+        return electric;
+    }
+
+    public void setElectric(boolean electric) {
+        this.electric = electric;
+    }
+
+    @Override
+    public String toString() {
+        return String.join(",", brand, model, year + "", electric + "");
+    }
+
+    public static Car fromString(String carString) {
+        final String[] split = carString.split(",");
+        return new Car(split[0], split[1], Integer.parseInt(split[2]), Boolean.parseBoolean(split[3]));
+    }
+}
diff --git a/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/model/Vehicle.java b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/model/Vehicle.java
new file mode 100644
index 0000000000..22566c9c34
--- /dev/null
+++ b/integration-tests-jvm/mapstruct/src/main/java/org/apache/camel/quarkus/component/mapstruct/it/model/Vehicle.java
@@ -0,0 +1,73 @@
+/*
+ * 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.mapstruct.it.model;
+
+public class Vehicle {
+    private String company;
+    private String name;
+    private String power;
+    private int year;
+
+    public Vehicle(String company, String name, String power, int year) {
+        this.company = company;
+        this.name = name;
+        this.power = power;
+        this.year = year;
+    }
+
+    public String getCompany() {
+        return company;
+    }
+
+    public void setCompany(String company) {
+        this.company = company;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getPower() {
+        return power;
+    }
+
+    public void setPower(String power) {
+        this.power = power;
+    }
+
+    public int getYear() {
+        return year;
+    }
+
+    public void setYear(int year) {
+        this.year = year;
+    }
+
+    @Override
+    public String toString() {
+        return String.join(",", company, name, power, year + "");
+    }
+
+    public static Vehicle fromString(String vehicleString) {
+        final String[] split = vehicleString.split(",");
+        return new Vehicle(split[0], split[1], split[2], Integer.parseInt(split[3]));
+    }
+}
diff --git a/integration-tests-jvm/mapstruct/src/test/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructIT.java b/integration-tests-jvm/mapstruct/src/test/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructIT.java
new file mode 100644
index 0000000000..c992d42f58
--- /dev/null
+++ b/integration-tests-jvm/mapstruct/src/test/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructIT.java
@@ -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.component.mapstruct.it;
+
+import io.quarkus.test.junit.QuarkusIntegrationTest;
+
+@QuarkusIntegrationTest
+public class MapStructIT extends MapStructTest {
+}
diff --git a/integration-tests-jvm/mapstruct/src/test/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructTest.java b/integration-tests-jvm/mapstruct/src/test/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructTest.java
new file mode 100644
index 0000000000..c748bbe435
--- /dev/null
+++ b/integration-tests-jvm/mapstruct/src/test/java/org/apache/camel/quarkus/component/mapstruct/it/MapStructTest.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.mapstruct.it;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
+import org.apache.camel.quarkus.component.mapstruct.it.model.Car;
+import org.apache.camel.quarkus.component.mapstruct.it.model.Vehicle;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+@QuarkusTest
+public class MapStructTest {
+    private static final Vehicle VEHICLE = new Vehicle("Volvo", "XC60", "true", 2021);
+
+    @ParameterizedTest
+    @ValueSource(strings = { "component", "converter" })
+    public void testMapping(String value) {
+        String response = RestAssured.given()
+                .body(VEHICLE.toString())
+                .post("/mapstruct/" + value)
+                .then()
+                .statusCode(200)
+                .extract().body().asString();
+
+        Car car = Car.fromString(response);
+
+        assertEquals(car.getBrand(), VEHICLE.getCompany());
+        assertEquals(car.getModel(), VEHICLE.getName());
+        assertEquals(car.getYear(), VEHICLE.getYear());
+        assertEquals(car.isElectric(), Boolean.parseBoolean(VEHICLE.getPower()));
+    }
+}
diff --git a/pom.xml b/pom.xml
index 2453dd599e..4b7b27e028 100644
--- a/pom.xml
+++ b/pom.xml
@@ -131,6 +131,7 @@
         <kudu.version>${kudu-version}</kudu.version>
         <kotlin.version>1.8.10</kotlin.version><!-- @sync io.quarkus:quarkus-bom:${quarkus.version} dep:org.jetbrains.kotlin:kotlin-stdlib -->
         <kryo.version>2.24.0</kryo.version><!-- @sync org.apache.flink:flink-core:${flink-version} dep:com.esotericsoftware.kryo:kryo -->
+        <mapstruct.version>${mapstruct-version}</mapstruct.version>
         <minio.version>8.2.2</minio.version><!-- @sync io.quarkiverse.minio:quarkus-minio-parent:${quarkiverse-minio.version} prop:minio.version -->
         <msal4j.version>1.13.3</msal4j.version><!-- @sync com.azure:azure-identity:${azure-identity.version} dep:com.microsoft.azure:msal4j -->
         <mvel2.version>2.4.14.Final</mvel2.version><!-- @sync org.apache.camel:camel-dependencies:${camel.version} prop:mvel-version -->