This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch camel-quarkus-main in repository https://gitbox.apache.org/repos/asf/camel-quarkus-examples.git
commit f4d37cf102c46f8dcdf340822e8e0a35c13802a5 Author: James Netherton <[email protected]> AuthorDate: Thu Sep 12 15:00:50 2024 +0100 Test kamelet-chucknorris project with WireMock --- kamelet-chucknorris/pom.xml | 8 +++ .../src/main/resources/application.properties | 2 + .../kamelets/chuck-norris-source.kamelet.yaml | 2 +- .../java/org/acme/kamelet/ChuckNorrisTest.java | 4 +- .../org/acme/kamelet/ChuckNorrisTestResource.java | 66 ++++++++++++++++++++++ .../test/resources/mappings/chuck-norris-joke.json | 28 +++++++++ 6 files changed, 108 insertions(+), 2 deletions(-) diff --git a/kamelet-chucknorris/pom.xml b/kamelet-chucknorris/pom.xml index 5d5aaf3..a4ba2de 100644 --- a/kamelet-chucknorris/pom.xml +++ b/kamelet-chucknorris/pom.xml @@ -50,6 +50,8 @@ <maven-jar-plugin.version>3.4.2</maven-jar-plugin.version> <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version> <maven-surefire-plugin.version>3.5.0</maven-surefire-plugin.version> + + <wiremock.version>3.9.1</wiremock.version> </properties> <dependencyManagement> @@ -114,6 +116,12 @@ <artifactId>awaitility</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.wiremock</groupId> + <artifactId>wiremock-standalone</artifactId> + <version>${wiremock.version}</version> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/kamelet-chucknorris/src/main/resources/application.properties b/kamelet-chucknorris/src/main/resources/application.properties index 8c1f12d..8b11b0f 100644 --- a/kamelet-chucknorris/src/main/resources/application.properties +++ b/kamelet-chucknorris/src/main/resources/application.properties @@ -23,3 +23,5 @@ quarkus.log.file.enable = true # Camel quarkus.camel.kamelet.identifiers = chuck-norris-source + +chuck.norris.api.url = https://api.chucknorris.io/jokes/random?category=dev diff --git a/kamelet-chucknorris/src/main/resources/kamelets/chuck-norris-source.kamelet.yaml b/kamelet-chucknorris/src/main/resources/kamelets/chuck-norris-source.kamelet.yaml index 5bdee7d..bdef41b 100644 --- a/kamelet-chucknorris/src/main/resources/kamelets/chuck-norris-source.kamelet.yaml +++ b/kamelet-chucknorris/src/main/resources/kamelets/chuck-norris-source.kamelet.yaml @@ -53,7 +53,7 @@ spec: parameters: period: "{{period}}" steps: - - to: "https://api.chucknorris.io/jokes/random?category=dev" + - to: "{{chuck.norris.api.url}}" - setBody: jsonpath: "$.value" - to: "kamelet:sink" diff --git a/kamelet-chucknorris/src/test/java/org/acme/kamelet/ChuckNorrisTest.java b/kamelet-chucknorris/src/test/java/org/acme/kamelet/ChuckNorrisTest.java index 6b69770..8d0f9f9 100644 --- a/kamelet-chucknorris/src/test/java/org/acme/kamelet/ChuckNorrisTest.java +++ b/kamelet-chucknorris/src/test/java/org/acme/kamelet/ChuckNorrisTest.java @@ -21,15 +21,17 @@ import java.nio.file.Paths; import java.util.List; import java.util.concurrent.TimeUnit; +import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import org.junit.jupiter.api.Test; import static org.awaitility.Awaitility.await; +@QuarkusTestResource(ChuckNorrisTestResource.class) @QuarkusTest public class ChuckNorrisTest { @Test - public void testTimerLogMain() { + public void chuckNorrisKamelet() { await().atMost(10L, TimeUnit.SECONDS).pollDelay(1, TimeUnit.SECONDS).until(() -> { List<String> log = Files.readAllLines(Paths.get("target/quarkus.log")); return log.stream().anyMatch(line -> line.matches(".*timer.*Chuck.*")); diff --git a/kamelet-chucknorris/src/test/java/org/acme/kamelet/ChuckNorrisTestResource.java b/kamelet-chucknorris/src/test/java/org/acme/kamelet/ChuckNorrisTestResource.java new file mode 100644 index 0000000..b500928 --- /dev/null +++ b/kamelet-chucknorris/src/test/java/org/acme/kamelet/ChuckNorrisTestResource.java @@ -0,0 +1,66 @@ +/* + * 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.acme.kamelet; + +import java.util.Map; + +import com.github.tomakehurst.wiremock.WireMockServer; +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; +import org.jboss.logging.Logger; + +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; + +public class ChuckNorrisTestResource implements QuarkusTestResourceLifecycleManager { + private static final Logger LOG = Logger.getLogger(ChuckNorrisTestResource.class); + private static final String WIREMOCK_RECORD = System.getProperty("wiremock.record"); + private WireMockServer wireMock; + + @Override + public Map<String, String> start() { + LOG.info("Starting WireMock"); + + wireMock = new WireMockServer(wireMockConfig().dynamicPort()); + if (WIREMOCK_RECORD != null && WIREMOCK_RECORD.equals("true")) { + LOG.info("Enable WireMock record mode"); + wireMock.resetMappings(); + wireMock.startRecording("https://api.chucknorris.io/jokes/random"); + } + + wireMock.start(); + + String endpointUri = "http://localhost:%d".formatted(wireMock.port()); + LOG.infof("Started WireMock on %s", endpointUri); + + return Map.of( + "chuck.norris.api.url", endpointUri, + "camel.main.globalOptions[http.proxyHost]", "localhost", + "camel.main.globalOptions[http.proxyPort]", String.valueOf(wireMock.port()), + "camel.main.globalOptions[https.proxyHost]", "localhost", + "camel.main.globalOptions[https.proxyPort]", String.valueOf(wireMock.port())); + } + + @Override + public void stop() { + if (wireMock != null) { + if (WIREMOCK_RECORD != null && WIREMOCK_RECORD.equals("true")) { + wireMock.stopRecording(); + wireMock.saveMappings(); + } + wireMock.stop(); + } + } +} diff --git a/kamelet-chucknorris/src/test/resources/mappings/chuck-norris-joke.json b/kamelet-chucknorris/src/test/resources/mappings/chuck-norris-joke.json new file mode 100644 index 0000000..490c4d1 --- /dev/null +++ b/kamelet-chucknorris/src/test/resources/mappings/chuck-norris-joke.json @@ -0,0 +1,28 @@ +{ + "id" : "abdc7f6a-5112-44ff-97ac-a0710e342d03", + "name" : "", + "request" : { + "url" : "/", + "method" : "GET" + }, + "response" : { + "status" : 200, + "body" : "{\"categories\":[\"dev\"],\"created_at\":\"2020-01-05 13:42:19.104863\",\"icon_url\":\"https://api.chucknorris.io/img/avatar/chuck-norris.png\",\"id\":\"ye0_hnd3rgq68e_pfvsqqg\",\"updated_at\":\"2020-01-05 13:42:19.104863\",\"url\":\"https://api.chucknorris.io/jokes/ye0_hnd3rgq68e_pfvsqqg\",\"value\":\"Chuck Norris can instantiate an abstract class.\"}", + "headers" : { + "CF-RAY" : "8c205cf67a8f6545-LHR", + "Server" : "cloudflare", + "cf-cache-status" : "DYNAMIC", + "Reporting-Endpoints" : "heroku-nel=https://nel.heroku.com/reports?ts=1726148990&sid=812dcc77-0bd0-43b1-a5f1-b25750382959&s=DWG%2F2oGJJCxAbiAESBX%2BG7xgCh%2B01nvzoILE2LS0jo4%3D", + "Date" : "Thu, 12 Sep 2024 13:49:50 GMT", + "Via" : "1.1 vegur", + "Nel" : "{\"report_to\":\"heroku-nel\",\"max_age\":3600,\"success_fraction\":0.005,\"failure_fraction\":0.05,\"response_headers\":[\"Via\"]}", + "Report-To" : "{\"group\":\"heroku-nel\",\"max_age\":3600,\"endpoints\":[{\"url\":\"https://nel.heroku.com/reports?ts=1726148990&sid=812dcc77-0bd0-43b1-a5f1-b25750382959&s=DWG%2F2oGJJCxAbiAESBX%2BG7xgCh%2B01nvzoILE2LS0jo4%3D\"}]}", + "Vary" : [ "Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers" ], + "alt-svc" : "h3=\":443\"; ma=86400", + "Content-Type" : "application/json" + } + }, + "uuid" : "abdc7f6a-5112-44ff-97ac-a0710e342d03", + "persistent" : true, + "insertionIndex" : 1 +} \ No newline at end of file
