This is an automated email from the ASF dual-hosted git repository. jamesnetherton 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 ca40252 Use WireMock for GitHub tests ca40252 is described below commit ca4025206d1e9bd1ab48243258cabc3e4c060803 Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Thu May 20 11:54:01 2021 +0100 Use WireMock for GitHub tests --- integration-tests/github/README.adoc | 20 ++++++++++++ integration-tests/github/pom.xml | 5 +++ .../component/github/it/GithubResource.java | 38 ++++++++++++++++++++++ .../quarkus/component/github/it/GithubTest.java | 2 ++ .../{GithubTest.java => GithubTestResource.java} | 22 ++++++------- .../resources/mappings/githubGetRepository.json | 38 ++++++++++++++++++++++ .../mappings/githubGetRepositoryFile.json | 37 +++++++++++++++++++++ 7 files changed, 150 insertions(+), 12 deletions(-) diff --git a/integration-tests/github/README.adoc b/integration-tests/github/README.adoc new file mode 100644 index 0000000..1c15527 --- /dev/null +++ b/integration-tests/github/README.adoc @@ -0,0 +1,20 @@ +== Camel Quarkus GitHub Integration Tests + +By default the GitHub integration tests use WireMock to stub the API interactions. + +To run the `camel-quarkus-github` integration tests against the real API, you must first create a https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token[GitHub token]. + +Set the following environment variable: + +[source,shell] +---- +export GITHUB_TOKEN=your-token +---- + +If the WireMock stub recordings need updating, then remove the existing files from `src/test/resources/mappings` and run tests with either: + +System property `-Dwiremock.record=true` + +Or + +Set environment variable `WIREMOCK_RECORD=true` diff --git a/integration-tests/github/pom.xml b/integration-tests/github/pom.xml index f12f0fa..fa63bef 100644 --- a/integration-tests/github/pom.xml +++ b/integration-tests/github/pom.xml @@ -38,6 +38,11 @@ <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-integration-wiremock-support</artifactId> + <scope>test</scope> + </dependency> <!-- test dependencies --> <dependency> diff --git a/integration-tests/github/src/main/java/org/apache/camel/quarkus/component/github/it/GithubResource.java b/integration-tests/github/src/main/java/org/apache/camel/quarkus/component/github/it/GithubResource.java index 0d9f50a..32bd916 100644 --- a/integration-tests/github/src/main/java/org/apache/camel/quarkus/component/github/it/GithubResource.java +++ b/integration-tests/github/src/main/java/org/apache/camel/quarkus/component/github/it/GithubResource.java @@ -16,14 +16,27 @@ */ package org.apache.camel.quarkus.component.github.it; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Optional; + +import javax.enterprise.event.Observes; import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; +import org.apache.camel.CamelContext; import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.github.GitHubConstants; +import org.apache.camel.impl.event.CamelContextStartedEvent; import org.eclipse.egit.github.core.CommitFile; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.service.DataService; +import org.eclipse.egit.github.core.service.RepositoryService; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; @Path("/github") public class GithubResource { @@ -33,6 +46,31 @@ public class GithubResource { @Inject ProducerTemplate producerTemplate; + public void onContextStart(@Observes CamelContextStartedEvent event) { + Config config = ConfigProvider.getConfig(); + Optional<String> wireMockUrl = config.getOptionalValue("wiremock.url", String.class); + if (wireMockUrl.isPresent()) { + // Force the GH client to use the WireMock proxy + try { + CamelContext context = event.getContext(); + URI wireMockUri = new URI(wireMockUrl.get()); + GitHubClient client = new GitHubClient(wireMockUri.getHost(), wireMockUri.getPort(), wireMockUri.getScheme()) { + @Override + protected String configureUri(String uri) { + // Prevent the original impl adding an unwanted /v3 path prefix + return uri; + } + }; + DataService dataService = new DataService(client); + RepositoryService repositoryService = new RepositoryService(client); + context.getRegistry().bind(GitHubConstants.GITHUB_DATA_SERVICE, dataService); + context.getRegistry().bind(GitHubConstants.GITHUB_REPOSITORY_SERVICE, repositoryService); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + } + @Path("/get") @GET @Produces(MediaType.TEXT_PLAIN) diff --git a/integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTest.java b/integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTest.java index ee5c545..3eb2ffe 100644 --- a/integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTest.java +++ b/integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTest.java @@ -16,6 +16,7 @@ */ package org.apache.camel.quarkus.component.github.it; +import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; import org.junit.jupiter.api.Test; @@ -23,6 +24,7 @@ import org.junit.jupiter.api.Test; import static org.hamcrest.Matchers.containsString; @QuarkusTest +@QuarkusTestResource(GithubTestResource.class) class GithubTest { @Test diff --git a/integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTest.java b/integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTestResource.java similarity index 66% copy from integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTest.java copy to integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTestResource.java index ee5c545..0bae719 100644 --- a/integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTest.java +++ b/integration-tests/github/src/test/java/org/apache/camel/quarkus/component/github/it/GithubTestResource.java @@ -16,21 +16,19 @@ */ package org.apache.camel.quarkus.component.github.it; -import io.quarkus.test.junit.QuarkusTest; -import io.restassured.RestAssured; -import org.junit.jupiter.api.Test; +import org.apache.camel.quarkus.test.wiremock.WireMockTestResourceLifecycleManager; -import static org.hamcrest.Matchers.containsString; +public class GithubTestResource extends WireMockTestResourceLifecycleManager { -@QuarkusTest -class GithubTest { + private static final String GITHUB_ENV_TOKEN = "GITHUB_TOKEN"; - @Test - public void test() { - RestAssured.get("/github/get") - .then() - .statusCode(200) - .body(containsString("Apache Camel extensions for Quarkus")); + @Override + protected String getRecordTargetBaseUrl() { + return "https://api.github.com"; } + @Override + protected boolean isMockingEnabled() { + return !envVarsPresent(GITHUB_ENV_TOKEN); + } } diff --git a/integration-tests/github/src/test/resources/mappings/githubGetRepository.json b/integration-tests/github/src/test/resources/mappings/githubGetRepository.json new file mode 100644 index 0000000..30df6a2 --- /dev/null +++ b/integration-tests/github/src/test/resources/mappings/githubGetRepository.json @@ -0,0 +1,38 @@ +{ + "id" : "85e3cc39-9d71-45a1-863c-7989052af075", + "name" : "repos_apache_camel-quarkus", + "request" : { + "url" : "/repos/apache/camel-quarkus", + "method" : "GET" + }, + "response" : { + "status" : 200, + "body" : "{\"id\":193065376,\"node_id\":\"MDEwOlJlcG9zaXRvcnkxOTMwNjUzNzY=\",\"name\":\"camel-quarkus\",\"full_name\":\"apache/camel-quarkus\",\"private\":false,\"owner\":{\"login\":\"apache\",\"id\":47359,\"node_id\":\"MDEyOk9yZ2FuaXphdGlvbjQ3MzU5\",\"avatar_url\":\"https://avatars.githubusercontent.com/u/47359?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/apache\",\"html_url\":\"https://github.com/apache\",\"followers_url\":\"https://api.github.com/users/apache/ [...] + "headers" : { + "Server" : "GitHub.com", + "Date" : "Thu, 20 May 2021 10:38:18 GMT", + "Content-Type" : "application/json; charset=utf-8", + "Cache-Control" : "public, max-age=60, s-maxage=60", + "Vary" : "Accept, Accept-Encoding, Accept, X-Requested-With", + "ETag" : "W/\"7a87e6c15f634559e9085861786f1e47ae9949c046cd43a5f12af7b3cf813183\"", + "Last-Modified" : "Thu, 20 May 2021 03:40:17 GMT", + "X-GitHub-Media-Type" : "github.beta; format=json", + "Strict-Transport-Security" : "max-age=31536000; includeSubdomains; preload", + "X-Frame-Options" : "deny", + "X-Content-Type-Options" : "nosniff", + "X-XSS-Protection" : "0", + "Referrer-Policy" : "origin-when-cross-origin, strict-origin-when-cross-origin", + "Content-Security-Policy" : "default-src 'none'", + "X-RateLimit-Limit" : "60", + "X-RateLimit-Remaining" : "58", + "X-RateLimit-Reset" : "1621510517", + "X-RateLimit-Resource" : "core", + "X-RateLimit-Used" : "2", + "Accept-Ranges" : "bytes", + "X-GitHub-Request-Id" : "A086:97B8:1BF790:1E9A3C:60A63C19" + } + }, + "uuid" : "85e3cc39-9d71-45a1-863c-7989052af075", + "persistent" : true, + "insertionIndex" : 1 +} \ No newline at end of file diff --git a/integration-tests/github/src/test/resources/mappings/githubGetRepositoryFile.json b/integration-tests/github/src/test/resources/mappings/githubGetRepositoryFile.json new file mode 100644 index 0000000..7992089 --- /dev/null +++ b/integration-tests/github/src/test/resources/mappings/githubGetRepositoryFile.json @@ -0,0 +1,37 @@ +{ + "id" : "b61eef33-a160-48e7-95e1-40349b7f5e1e", + "name" : "repos_apache_camel-quarkus_git_blobs_6195efafd0a8100795247e35942b5c61fea79267", + "request" : { + "url" : "/repos/apache/camel-quarkus/git/blobs/6195efafd0a8100795247e35942b5c61fea79267", + "method" : "GET" + }, + "response" : { + "status" : 200, + "body" : "{\"sha\":\"6195efafd0a8100795247e35942b5c61fea79267\",\"node_id\":\"MDQ6QmxvYjE5MzA2NTM3Njo2MTk1ZWZhZmQwYTgxMDA3OTUyNDdlMzU5NDJiNWM2MWZlYTc5MjY3\",\"size\":1614,\"url\":\"https://api.github.com/repos/apache/camel-quarkus/git/blobs/6195efafd0a8100795247e35942b5c61fea79267\",\"content\":\"PSBBcGFjaGUgQ2FtZWwgZXh0ZW5zaW9ucyBmb3IgUXVhcmt1cwoKaW1hZ2U6\\naHR0cHM6Ly9pbWcuc2hpZWxkcy5pby9tYXZlbi1jZW50cmFsL3Yvb3JnLmFw\\nYWNoZS5jYW1lbC5xdWFya3VzL2NhbWVsLXF1YXJrdXMtYm9tLnN2Z1siTWF2\\nZ [...] + "headers" : { + "Server" : "GitHub.com", + "Date" : "Thu, 20 May 2021 10:38:31 GMT", + "Content-Type" : "application/json; charset=utf-8", + "Cache-Control" : "public, max-age=60, s-maxage=60", + "Vary" : "Accept, Accept-Encoding, Accept, X-Requested-With", + "ETag" : "W/\"f7af62dc21539a2acbc6159b8869ab60dccd4da5e92eeead36caddd9281d0ae3\"", + "X-GitHub-Media-Type" : "github.beta; format=json", + "Strict-Transport-Security" : "max-age=31536000; includeSubdomains; preload", + "X-Frame-Options" : "deny", + "X-Content-Type-Options" : "nosniff", + "X-XSS-Protection" : "0", + "Referrer-Policy" : "origin-when-cross-origin, strict-origin-when-cross-origin", + "Content-Security-Policy" : "default-src 'none'", + "X-RateLimit-Limit" : "60", + "X-RateLimit-Remaining" : "57", + "X-RateLimit-Reset" : "1621510517", + "X-RateLimit-Resource" : "core", + "X-RateLimit-Used" : "3", + "Accept-Ranges" : "bytes", + "X-GitHub-Request-Id" : "A086:97B8:1BFAA5:1E9D8A:60A63C27" + } + }, + "uuid" : "b61eef33-a160-48e7-95e1-40349b7f5e1e", + "persistent" : true, + "insertionIndex" : 2 +} \ No newline at end of file