You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "zbendhiba (via GitHub)" <gi...@apache.org> on 2023/03/10 16:13:04 UTC

[GitHub] [camel-quarkus] zbendhiba commented on a diff in pull request #4641: camel-quarkus-language: Added language, resource and options tests

zbendhiba commented on code in PR #4641:
URL: https://github.com/apache/camel-quarkus/pull/4641#discussion_r1132562128


##########
integration-test-groups/foundation/language/src/test/java/org/apache/camel/quarkus/component/language/it/LanguageTest.java:
##########
@@ -16,35 +16,216 @@
  */
 package org.apache.camel.quarkus.component.language.it;
 
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Collection;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import io.quarkus.test.common.QuarkusTestResource;
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
+import io.restassured.builder.RequestSpecBuilder;
 import io.restassured.http.ContentType;
+import org.apache.camel.quarkus.test.wiremock.MockServer;
 import org.hamcrest.Matchers;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.request;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 @QuarkusTest
+@QuarkusTestResource(LanguageTestResource.class)
 class LanguageTest {
 
-    @Test
-    public void languageSimpleScript() {
+    @MockServer
+    WireMockServer server;
+
+    /**
+     * Map endpoint names to objects containing exchange bodies and expected output
+     */
+    private static Map<String, TestResult> languageResultsMap = Map.ofEntries(
+            /* Test Languages */
+            Map.entry("languageBeanScript", new TestResult("Hello Bean", "HELLO BEAN")),
+            Map.entry("languageConstantScript", new TestResult("Constant", "Hello from constant language script")),
+            Map.entry("languageExchangePropertyScript",
+                    new TestResult("ExProperty", "Hello ExProperty from exchangeProperty language script")),
+            Map.entry("languageFileScript", new TestResult("", "File name is test-file.txt")),
+            Map.entry("languageHeaderScript", new TestResult("Header", "Hello Header from header language script")),
+            Map.entry("languageHl7terserScript", new TestResult(createHl7Message(), "Patient's surname is Smith")),
+            Map.entry("languageJsonPathScript",
+                    new TestResult("{\"message\": \"Hello from jsonpath\", \"user\": \"camel-quarkus\"}",
+                            "Hello from jsonpath")),
+            Map.entry("languageRefScript", new TestResult("Hello from Ref", "hello from ref")),
+            Map.entry("languageSimpleScript", new TestResult("Simple", "Hello Simple from simple language script")),
+            Map.entry("languageTokenizeScript", new TestResult("Hello,Tokenize", "Hello")),
+            Map.entry("languageXpathScript", new TestResult("<message>Hello from Xpath</message>", "Hello from Xpath")),
+            // Xquery test fails due to CAMEL-19079. Re-enable when CQ is aligned to Camel 4.0.0-M2
+            // Map.entry("languageXqueryScript", new TestResult("<message>Hello from XQuery</message>", "HELLO FROM XQUERY")),
+
+            /* Test Resource Loading */
+            Map.entry("languageSimpleResource", new TestResult("SimpleRes", "Hello SimpleRes from simple language resource")),
+            Map.entry("languageSimpleFile", new TestResult("SimpleFile", "Hello SimpleFile from simple language file")),
+
+            /* Test transform=false option */
+            Map.entry("languageSimpleTransform", new TestResult("SimpleTransform", "SimpleTransform"))
+
+    );
+
+    private static Collection<Object[]> getLanguageResultsMap() {
+        return languageResultsMap.entrySet()
+                .stream()
+                .map((entry) -> new Object[] { entry.getKey(), entry.getValue() })
+                .collect(Collectors.toList());
+    }
+
+    private static final String OUTPUT_DIRECTORY = "target";
+    private static final String TEST_FILE = "test-file.txt";
+    private static final String SIMPLE_FILE = "hello.simple-file.txt";
+
+    @BeforeAll
+    public static void setupTestFiles() throws Exception {
+        Files.createDirectories(Paths.get(OUTPUT_DIRECTORY));
+        // Write the file used in the 'file' language test
+        Files.writeString(Paths.get(OUTPUT_DIRECTORY, TEST_FILE), "Dummy text", StandardCharsets.UTF_8);
+
+        // Copy the simple script from resources to the OUTPUT_DIRECTORY to test reading a script from a file: resource
+        Files.copy(
+                LanguageTest.class.getClassLoader().getResourceAsStream(SIMPLE_FILE),
+                Paths.get(OUTPUT_DIRECTORY, SIMPLE_FILE),
+                StandardCopyOption.REPLACE_EXISTING);
+    }
+
+    @AfterAll
+    public static void deleteTestFiles() throws Exception {
+        Files.delete(Paths.get(OUTPUT_DIRECTORY, TEST_FILE));
+        Files.delete(Paths.get(OUTPUT_DIRECTORY, SIMPLE_FILE));
+    }
+
+    /**
+     * This test is called for each entry in {@link languageResultsMap}.
+     * The body text is passed to the routeName endpoint, which is checked for successful completion
+     * and that the returned body matches the expected result from {@link TestResult}
+     * 
+     * @param routeName - The name of the endpoint exposed in {@link LanguageResource}
+     * @param result    - The {@link TestResource} containing input body and expected result
+     */
+    @ParameterizedTest
+    @MethodSource("getLanguageResultsMap")
+    public void testLanguage(String routeName, TestResult result) {
         RestAssured.given()
                 .contentType(ContentType.TEXT)
-                .body("Dolly")
-                .post("/language/route/languageSimpleScript")
+                .body(result.body)
+                .post("/language/route/" + routeName)
                 .then()
                 .statusCode(200)
-                .body(Matchers.is("Hello Dolly from simple language script"));
+                .body(Matchers.is(result.expected));
     }
 
+    /**
+     * Tests whether scripts can be loaded over HTTP.
+     * 
+     * @throws Exception
+     */
     @Test
-    public void languageSimpleResource() {
+    public void testHttpResource() throws Exception {
+        // Stub the WireMock server to return a simple script on the "/simple" endpoint
+        assertNotNull(server);
+        server.stubFor(request("GET", urlPathEqualTo("/simple"))

Review Comment:
   FYI, when using `WireMockTestResourceLifecycleManager`, we usually put Wiremock requests/responses in json files under resources/mappings, unless we have a reason why we do the stub manually (and we comment about it).
   The stub are automatically created. 
   
   Example: https://github.com/apache/camel-quarkus/tree/main/integration-tests/digitalocean/src/test/resources/mappings



-- 
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