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 fe3e4a2c9a Improve/clean-up camel-quarkus-validator extension tests.
fe3e4a2c9a is described below
commit fe3e4a2c9a2e72c3a3734fc53afebe4e831587e6
Author: svkcemk <[email protected]>
AuthorDate: Tue Aug 9 18:41:43 2022 +0530
Improve/clean-up camel-quarkus-validator extension tests.
---
integration-tests/validator/pom.xml | 8 +-
.../component/validator/it/ValidatorResource.java | 29 +++-----
.../validator/it/ValidatorRouteBuilder.java | 38 +++++++++-
.../src/main/resources/application.properties | 3 +-
.../src/main/resources/resources-config.json | 7 --
.../component/validator/it/ValidatorTest.java | 85 +++++++---------------
.../validator/it/ValidatorTestResource.java | 68 +++++++++++++++++
7 files changed, 147 insertions(+), 91 deletions(-)
diff --git a/integration-tests/validator/pom.xml
b/integration-tests/validator/pom.xml
index 04ceaed531..33a62a975a 100644
--- a/integration-tests/validator/pom.xml
+++ b/integration-tests/validator/pom.xml
@@ -45,7 +45,7 @@
</dependency>
- <!-- test dependencies -->
+ <!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
@@ -56,9 +56,13 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.camel.quarkus</groupId>
+ <artifactId>camel-quarkus-integration-wiremock-support</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
-
<profiles>
<profile>
<id>native</id>
diff --git
a/integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorResource.java
b/integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorResource.java
index b8b979cfdb..43ffee31b6 100644
---
a/integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorResource.java
+++
b/integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorResource.java
@@ -21,8 +21,10 @@ import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
import org.apache.camel.ProducerTemplate;
@@ -32,28 +34,15 @@ public class ValidatorResource {
@Inject
ProducerTemplate producerTemplate;
- @Path("/classpath")
+ @Path("/validate/{scheme}")
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.TEXT_PLAIN)
- public String processOrderInXmlFromClassPath(String statement) {
- return producerTemplate.requestBody("direct:classpath", statement,
String.class);
+ public Response processOrderInXml(String statement, @PathParam("scheme")
String scheme) {
+ try {
+ return Response.ok().entity(producerTemplate.requestBody("direct:"
+ scheme, statement, String.class)).build();
+ } catch (Exception e) {
+ return Response.serverError().entity(e.getMessage()).build();
+ }
}
-
- @Path("/filesystem")
- @POST
- @Consumes(MediaType.APPLICATION_XML)
- @Produces(MediaType.TEXT_PLAIN)
- public String processOrderInXmlFromFileSystem(String statement) {
- return producerTemplate.requestBody("direct:filesystem", statement,
String.class);
- }
-
- @Path("/http")
- @POST
- @Consumes(MediaType.APPLICATION_XML)
- @Produces(MediaType.TEXT_PLAIN)
- public String processOrderInXmlFromHTTP(String statement) {
- return producerTemplate.requestBody("direct:http", statement,
String.class);
- }
-
}
diff --git
a/integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorRouteBuilder.java
b/integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorRouteBuilder.java
index ea64124729..f3024468a5 100644
---
a/integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorRouteBuilder.java
+++
b/integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorRouteBuilder.java
@@ -16,19 +16,53 @@
*/
package org.apache.camel.quarkus.component.validator.it;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+
import org.apache.camel.builder.RouteBuilder;
+import org.eclipse.microprofile.config.ConfigProvider;
public class ValidatorRouteBuilder extends RouteBuilder {
+
@Override
public void configure() {
+ // validator from the classpath resource
from("direct:classpath")
.to("validator:message.xsd");
+ // validator from the filesytem
+ String xsdLocation = createTempXsd("message.xsd");
+
from("direct:filesystem")
- .to("validator:file:src/main/resources/message.xsd");
+ .to("validator:file:" + xsdLocation);
+ // validator from a http endpoint.
+ String serverURL = ConfigProvider.getConfig()
+ .getConfigValue("xsd.server-url")
+ .getRawValue();
from("direct:http")
-
.toD("validator:https://raw.githubusercontent.com/apache/camel-quarkus/main/integration-tests/validator/src/main/resources/message.xsd");
+ .toD("validator:" + serverURL + "/xsd");
+
+ }
+
+ public String createTempXsd(String sourceXsd) {
+ Path tempXsd = null;
+ try (InputStream resourceAsStream =
Thread.currentThread().getContextClassLoader()
+ .getResourceAsStream(sourceXsd)) {
+ tempXsd = Files.createTempFile("temp", ".xsd");
+ Files.copy(resourceAsStream, tempXsd,
StandardCopyOption.REPLACE_EXISTING);
+ return tempXsd.toAbsolutePath().toString();
+ } catch (IOException e) {
+ if (tempXsd != null) {
+ throw new RuntimeException("Could not read " + sourceXsd + "
from classpath or copy it to " + tempXsd,
+ e);
+ } else {
+ throw new RuntimeException("Failed to create a temp xsd file"
+ e);
+ }
+ }
}
}
diff --git
a/integration-tests/validator/src/main/resources/application.properties
b/integration-tests/validator/src/main/resources/application.properties
index 68644f628d..8658f3529d 100644
--- a/integration-tests/validator/src/main/resources/application.properties
+++ b/integration-tests/validator/src/main/resources/application.properties
@@ -15,5 +15,4 @@
## limitations under the License.
## ---------------------------------------------------------------------------
-quarkus.native.additional-build-args
=-H:ResourceConfigurationFiles=resources-config.json
-quarkus.ssl.native=true
\ No newline at end of file
+quarkus.native.resources.includes = *.xsd
\ No newline at end of file
diff --git
a/integration-tests/validator/src/main/resources/resources-config.json
b/integration-tests/validator/src/main/resources/resources-config.json
deleted file mode 100644
index 679fed9b65..0000000000
--- a/integration-tests/validator/src/main/resources/resources-config.json
+++ /dev/null
@@ -1,7 +0,0 @@
-{
- "resources": [
- {
- "pattern": ".*\\.xsd$"
- }
- ]
-}
\ No newline at end of file
diff --git
a/integration-tests/validator/src/test/java/org/apache/camel/quarkus/component/validator/it/ValidatorTest.java
b/integration-tests/validator/src/test/java/org/apache/camel/quarkus/component/validator/it/ValidatorTest.java
index f5fadcb64e..d204e2612a 100644
---
a/integration-tests/validator/src/test/java/org/apache/camel/quarkus/component/validator/it/ValidatorTest.java
+++
b/integration-tests/validator/src/test/java/org/apache/camel/quarkus/component/validator/it/ValidatorTest.java
@@ -16,84 +16,53 @@
*/
package org.apache.camel.quarkus.component.validator.it;
+import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.hamcrest.Matchers.containsString;
@QuarkusTest
+@QuarkusTestResource(ValidatorTestResource.class)
class ValidatorTest {
- @Test
- public void validXMLFromClassPath() {
-
- RestAssured.given()
- .contentType(ContentType.XML)
-
.body("<message><firstName>MyFirstname</firstName><lastName>MyLastname</lastName></message>")
- .post("/validator/classpath")
- .then()
- .statusCode(200);
-
- }
-
- @Test
- public void invalidXMLFromClassPath() {
-
- RestAssured.given()
- .contentType(ContentType.XML)
- .body("<message><firstName>MyFirstname</firstName></message>")
- .post("/validator/classpath")
- .then()
- .statusCode(500);
-
- }
-
- @Test
- public void validXMLFromFileSystem() {
-
- RestAssured.given()
- .contentType(ContentType.XML)
-
.body("<message><firstName>MyFirstname</firstName><lastName>MyLastname</lastName></message>")
- .post("/validator/filesystem")
- .then()
- .statusCode(200);
-
- }
+ @ParameterizedTest
+ @ValueSource(strings = { "classpath", "filesystem", "http" })
+ public void validXML(String scheme) {
- @Test
- public void invalidXMLFromFileSystem() {
+ String requestBody =
"<message><firstName>MyFirstname</firstName><lastName>MyLastname</lastName></message>";
RestAssured.given()
.contentType(ContentType.XML)
- .body("<message><firstName>MyFirstname</firstName></message>")
- .post("/validator/filesystem")
+ .body(requestBody)
+ .post("/validator/validate/" + scheme)
.then()
- .statusCode(500);
+ .statusCode(200)
+ .assertThat()
+ .body(containsString("MyFirstname"))
+ .and()
+ .body(containsString("MyLastname"));
}
- @Test
- public void validXMLFromHTTPEndPoint() {
+ @ParameterizedTest
+ @ValueSource(strings = { "classpath", "filesystem", "http" })
+ public void inValidXML(String scheme) {
- RestAssured.given()
- .contentType(ContentType.XML)
-
.body("<message><firstName>MyFirstname</firstName><lastName>MyLastname</lastName></message>")
- .post("/validator/http")
- .then()
- .statusCode(200);
-
- }
-
- @Test
- public void invalidXMLFromHTTPEndPoint() {
+ String requestBody =
"<message><firstName>MyFirstname</firstName></message>";
+ String errorResponse = "Exception occurred during execution on the
exchange";
RestAssured.given()
.contentType(ContentType.XML)
- .body("<message><firstName>MyFirstname</firstName></message>")
- .post("/validator/http")
+ .body(requestBody)
+ .post("/validator/validate/" + scheme)
.then()
- .statusCode(500);
+ .statusCode(500)
+ .assertThat()
+ .body(containsString(errorResponse));
}
-
}
diff --git
a/integration-tests/validator/src/test/java/org/apache/camel/quarkus/component/validator/it/ValidatorTestResource.java
b/integration-tests/validator/src/test/java/org/apache/camel/quarkus/component/validator/it/ValidatorTestResource.java
new file mode 100644
index 0000000000..0826e204e6
--- /dev/null
+++
b/integration-tests/validator/src/test/java/org/apache/camel/quarkus/component/validator/it/ValidatorTestResource.java
@@ -0,0 +1,68 @@
+/*
+ * 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.validator.it;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
+import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+
+public class ValidatorTestResource implements
QuarkusTestResourceLifecycleManager {
+
+ private WireMockServer server;
+
+ @Override
+ public Map<String, String> start() {
+
+ String responseBody = "<xs:schema
xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" "
+ + " elementFormDefault=\"qualified\"
attributeFormDefault=\"unqualified\">"
+ + "<xs:element name=\"message\">"
+ + "<xs:complexType>"
+ + "<xs:sequence>"
+ + "<xs:element name=\"firstName\" type=\"xs:string\"/>"
+ + "<xs:element name=\"lastName\" type=\"xs:string\"/>"
+ + "</xs:sequence>"
+ + "</xs:complexType>"
+ + "</xs:element>"
+ + "</xs:schema>";
+
+ server = new WireMockServer(WireMockConfiguration.DYNAMIC_PORT);
+ server.start();
+ server.stubFor(
+ get(urlEqualTo("/xsd"))
+ .willReturn(aResponse()
+ .withHeader("Content-Type", "application/XML")
+ .withBody(responseBody)));
+ Map<String, String> conf = new HashMap<>();
+ conf.put("xsd.server-url", server.baseUrl());
+ return conf;
+ }
+
+ @Override
+ public void stop() {
+ if (server != null) {
+ server.stop();
+ }
+ }
+
+}