Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mock backend logging #1818

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions integration-tests-support/mock-backend/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-tests-support</artifactId>
<version>1.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>camel-quarkus-integration-test-support-mock-backend</artifactId>
<name>Camel Quarkus :: Integration Tests :: Support :: Mock Backend</name>

<dependencies>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.apache.camel.quarkus.test.mock.backend;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;

public class MockBackendUtils {

private static boolean startMockBackend = ConfigProvider.getConfig()
.getOptionalValue("camel.quarkus.start-mock-backend", Boolean.class).orElse(Boolean.TRUE);

private static final Logger LOG = Logger.getLogger(MockBackendUtils.class);

public static void logBackendUsed() {
if (startMockBackend) {
LOG.infof("Mock backend will be used");
} else {
LOG.infof("Real backend will be used");
}
}

public static boolean startMockBackend() {
return startMockBackend(false);
}

public static boolean startMockBackend(boolean printLogMessage) {
if (printLogMessage) {
logBackendUsed();
}
return startMockBackend;
}
}
1 change: 1 addition & 0 deletions integration-tests-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<module>process-executor-support</module>
<module>test-support</module>
<module>testcontainers-support</module>
<module>mock-backend</module>
</modules>

</project>
12 changes: 9 additions & 3 deletions integration-tests/telegram/README.adoc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
== Camel Quarkus Telegram Integration Tests

A work in progress, see https://github.com/apache/camel-quarkus/issues/74

To run `camel-quarkus-telegram` integration tests, you must first create a Telegram bot following this guide: https://www.nicolaferraro.me/2016/05/27/creating-a-telegram-bot-in-5-minutes-with-apache-camel/ .
To run `camel-quarkus-telegram` integration tests against the real remote Telegram API, you must first create
a Telegram bot following this guide:
https://www.nicolaferraro.me/2016/05/27/creating-a-telegram-bot-in-5-minutes-with-apache-camel/

Then set the following environment variables:

Expand All @@ -11,3 +11,9 @@ Then set the following environment variables:
$ export TELEGRAM_AUTHORIZATION_TOKEN=my-autorization-token
$ export TELEGRAM_CHAT_ID=my-chatId
----

If you do not set `TELEGRAM_AUTHORIZATION_TOKEN` environment variable, the test will be run against a mock
Telegram API started on `localhost`.

You may want to `export CAMEL_QUARKUS_START_MOCK_BACKEND=false` to avoid starting he the mock Telegram API
to make sure that you test against the real remote Telegram API.
4 changes: 4 additions & 0 deletions integration-tests/telegram/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-attachments</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support-mock-backend</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,80 @@
import java.io.InputStream;
import java.util.stream.Stream;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;

import io.quarkus.arc.Unremovable;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.telegram.TelegramComponent;
import org.apache.camel.quarkus.test.mock.backend.MockBackendUtils;
import org.apache.camel.support.ResourceHelper;
import org.apache.camel.util.IOHelper;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class TelegramRoutes extends RouteBuilder {

@ConfigProperty(name = "telegram.authorization-token", defaultValue = "default-dummy-token")
String authToken;

@ConfigProperty(name = "quarkus.http.test-port")
int httpTestPort;
@ConfigProperty(name = "quarkus.http.port")
int httpPort;

private String getBaseUri() {
final boolean isNativeMode = "executable".equals(System.getProperty("org.graalvm.nativeimage.kind"));
return "default-dummy-token".equals(authToken)
? "http://localhost:" + (isNativeMode ? httpPort : httpTestPort)
: "https://api.telegram.org";
}

/**
* We need to implement some conditional configuration of the {@link TelegramComponent} thus we create it
* programmatically and publish via CDI.
*
* @return a configured {@link TelegramComponent}
*/
@Produces
@ApplicationScoped
@Unremovable
@Named
TelegramComponent telegram() {
final TelegramComponent result = new TelegramComponent();
result.setCamelContext(getContext());
result.setBaseUri(getBaseUri());
result.setAuthorizationToken(authToken);
return result;
}

@Override
public void configure() throws Exception {
if (MockBackendUtils.startMockBackend(true)) {
/* Start the mock Telegram API unless the user did export CAMEL_QUARKUS_START_MOCK_BACKEND=false */
from("platform-http:/bot" + authToken + "/getUpdates?httpMethodRestrict=GET")
.process(e -> load("mock-messages/getUpdates.json", e));

/* Mock Telegram API */
from("platform-http:/bot{authToken}/getUpdates?httpMethodRestrict=GET")
.process(e -> load("mock-messages/getUpdates.json", e));

Stream.of(
"sendMessage",
"sendAudio",
"sendVideo",
"sendDocument",
"sendPhoto",
"sendVenue",
"sendLocation",
"stopMessageLiveLocation")
.forEach(endpoint -> {
from("platform-http:/{authToken}/" + endpoint + "?httpMethodRestrict=POST")
.process(e -> load("mock-messages/" + endpoint + ".json", e));
});
Stream.of(
"sendMessage",
"sendAudio",
"sendVideo",
"sendDocument",
"sendPhoto",
"sendVenue",
"sendLocation",
"stopMessageLiveLocation")
.forEach(endpoint -> {
from("platform-http:/bot" + authToken + "/" + endpoint + "?httpMethodRestrict=POST")
.process(e -> load("mock-messages/" + endpoint + ".json", e));
});
}

}

private static void load(String path, Exchange exchange) {
private void load(String path, Exchange exchange) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream(IOHelper.DEFAULT_BUFFER_SIZE);
InputStream in = ResourceHelper.resolveMandatoryResourceAsInputStream(exchange.getContext(), path)) {
IOHelper.copy(in, out, IOHelper.DEFAULT_BUFFER_SIZE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ quarkus.native.additional-build-args = -H:IncludeResources=.*mock-messages/.*
#
# Camel :: Telegram
#
camel.component.telegram.authorizationToken={{env:TELEGRAM_AUTHORIZATION_TOKEN:default-dummy-token}}
camel.component.telegram.baseUri={{env:TELEGRAM_BASE_URI:https://api.telegram.org}}
telegram.chatId={{env:TELEGRAM_CHAT_ID:-1}}
# Set the authorization token here or via environment variable TELEGRAM_AUTHORIZATION_TOKEN
#telegram.authorization-token=...

telegram.chatId=${TELEGRAM_CHAT_ID:-1}

# You may want to export CAMEL_QUARKUS_START_MOCK_BACKEND=false to avoid starting he the mock Telegram API
# to make sure that you test against the real remote Telegram API
camel.quarkus.start-mock-backend=true
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package org.apache.camel.quarkus.component.telegram.it;

import io.quarkus.test.junit.NativeImageTest;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

@NativeImageTest
@EnabledIfEnvironmentVariable(named = "TELEGRAM_AUTHORIZATION_TOKEN", matches = "[^ ]+")
class TelegramIT extends TelegramTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.awaitility.Awaitility;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand All @@ -43,7 +42,6 @@

@QuarkusTest
@QuarkusTestResource(TrustStoreResource.class)
@EnabledIfEnvironmentVariable(named = "TELEGRAM_AUTHORIZATION_TOKEN", matches = "[^ ]+")
public class TelegramTest {

private static final Logger LOG = Logger.getLogger(TelegramTest.class);
Expand Down
5 changes: 5 additions & 0 deletions poms/bom-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
<artifactId>camel-quarkus-integration-test-support</artifactId>
<version>${camel-quarkus.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support-mock-backend</artifactId>
<version>${camel-quarkus.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-testcontainers-support</artifactId>
Expand Down