You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2022/08/10 09:20:12 UTC

[GitHub] [camel-quarkus] ppalaga commented on a diff in pull request #3972: Improve/clean-up camel-quarkus-validator extension tests.

ppalaga commented on code in PR #3972:
URL: https://github.com/apache/camel-quarkus/pull/3972#discussion_r942212777


##########
integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorRouteBuilder.java:
##########
@@ -16,19 +16,47 @@
  */
 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) {
+        try {
+            InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(sourceXsd);
+            Path tempXsd = Files.createTempFile("temp", ".xsd");
+            Files.copy(resourceAsStream, tempXsd, StandardCopyOption.REPLACE_EXISTING);
+            return tempXsd.toAbsolutePath().toString();
+        } catch (IOException e) {
+            throw new RuntimeException(e);

Review Comment:
   ```suggestion
               throw new RuntimeException("Could not read "+ sourceXsd + " from classpath or copy it to " + tempXsd, e);
   ```
   
   The wrapped exception message does not say related to which file the operation failed. Adding the path to the wrapper exception will help future users to faster figure out where the problem is. 



##########
integration-tests/validator/src/main/java/org/apache/camel/quarkus/component/validator/it/ValidatorRouteBuilder.java:
##########
@@ -16,19 +16,47 @@
  */
 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) {
+        try {
+            InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(sourceXsd);

Review Comment:
   ```suggestion
           try (InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(sourceXsd)) {
   ```
   
   Otherwise the stream is not closed. It may cause problems especially on Windows.



##########
integration-tests/validator/src/test/java/org/apache/camel/quarkus/component/validator/it/ValidatorTest.java:
##########
@@ -16,84 +16,40 @@
  */
 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;
 
 @QuarkusTest
+@QuarkusTestResource(ValidatorTestResource.class)
 class ValidatorTest {
 
-    @Test
-    public void validXMLFromClassPath() {
+    @ParameterizedTest
+    @ValueSource(strings = { "classpath", "filesystem", "http" })
+    public void validXML(String scheme) {
 
         RestAssured.given()
                 .contentType(ContentType.XML)
                 .body("<message><firstName>MyFirstname</firstName><lastName>MyLastname</lastName></message>")
-                .post("/validator/classpath")
+                .post("/validator/validate/" + scheme)
                 .then()
                 .statusCode(200);
 
     }
 
-    @Test
-    public void invalidXMLFromClassPath() {
+    @ParameterizedTest
+    @ValueSource(strings = { "classpath", "filesystem", "http" })
+    public void inValidXML(String scheme) {
 
         RestAssured.given()
                 .contentType(ContentType.XML)
                 .body("<message><firstName>MyFirstname</firstName></message>")
-                .post("/validator/classpath")
+                .post("/validator/validate/" + scheme)
                 .then()
                 .statusCode(500);

Review Comment:
   Here I wonder whether there is some body text (or any other way) from which we could see that what failed is really the XSD validation? 500 could mean a failure somewhere else in the stack, which we definitely would not want to accept as "test passed". 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org