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

chore(it): small telegram tests cleanup #912

Merged
merged 1 commit into from
Mar 19, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.stream.Stream;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.support.ResourceHelper;
import org.apache.camel.util.IOHelper;

public class TelegramRoutes extends RouteBuilder {
Expand All @@ -34,44 +33,35 @@ public void configure() throws Exception {

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

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

}

static class ResourceSupplier implements Processor {
private final byte[] bytes;

public ResourceSupplier(String path) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream(IOHelper.DEFAULT_BUFFER_SIZE);
InputStream in = getClass().getClassLoader().getResourceAsStream(path)) {
IOHelper.copy(in, out, IOHelper.DEFAULT_BUFFER_SIZE);
this.bytes = out.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static 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);

@Override
public void process(Exchange exchange) throws Exception {
final Message m = exchange.getMessage();
m.setBody(bytes);
m.setHeader("Content-Length", bytes.length);
m.setHeader("Content-Type", "application/json; charset=UTF-8");
final byte[] bytes = out.toByteArray();
exchange.getMessage().setBody(bytes);
exchange.getMessage().setHeader("Content-Length", bytes.length);
exchange.getMessage().setHeader("Content-Type", "application/json; charset=UTF-8");
} catch (IOException e) {
throw new RuntimeException(e);
}

}

}