Skip to content

Commit

Permalink
Test kamelet-chucknorris project with WireMock
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Sep 12, 2024
1 parent 6361e22 commit f4d37cf
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
8 changes: 8 additions & 0 deletions kamelet-chucknorris/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions kamelet-chucknorris/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
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.*"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f4d37cf

Please sign in to comment.