Skip to content

Commit

Permalink
Test Batch Consumer, charset, filter and sortby options of the file c…
Browse files Browse the repository at this point in the history
…omponent apache#2512
  • Loading branch information
JiriOndrusek committed May 3, 2021
1 parent cc32c2f commit 4ded00b
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 28 deletions.
4 changes: 4 additions & 0 deletions integration-tests/file/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-mock</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.apache.camel.quarkus.component.file.it;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -26,36 +29,102 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;

@Path("/file")
@ApplicationScoped
public class FileResource {

public static String CONSUME_BATCH = "consumeBatch";
public static String SORT_BY = "sortBy";
public static String SEPARATOR = ";";

@Inject
ProducerTemplate producerTemplate;

@Inject
ConsumerTemplate consumerTemplate;

@Inject
CamelContext context;

@Path("/get/{folder}/{name}")
@GET
@POST
@Produces(MediaType.TEXT_PLAIN)
public String getFile(@PathParam("folder") String folder, @PathParam("name") String name) throws Exception {
return consumerTemplate.receiveBodyNoWait("file:target/" + folder + "?fileName=" + name, String.class);
public String getFile(@PathParam("folder") String folder, @PathParam("name") String name,
@QueryParam("charset") String charset) throws Exception {
StringBuilder url = new StringBuilder(String.format("file:target/%s?fileName=%s", folder, name));
if (charset != null && !charset.equals("")) {
url.append("&charset=").append(charset);
}
String s = consumerTemplate.receiveBodyNoWait(url.toString(), String.class);
System.out.println(s);

return s;
}

@Path("/getBatch")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> getBatch() throws Exception {
MockEndpoint mockEndpoint = context.getEndpoint("mock:" + CONSUME_BATCH, MockEndpoint.class);

context.getRouteController().startRoute(CONSUME_BATCH);

Map<String, Object> result = new HashMap<>();

mockEndpoint.getExchanges().stream().forEach(
e -> result.put(e.getIn().getBody(String.class), e.getProperty(Exchange.BATCH_INDEX)));

return result;
}

@Path("/startRoute")
@POST
@Consumes(MediaType.TEXT_PLAIN)
public void startRoute(String routeId) throws Exception {
context.getRouteController().startRoute(routeId);
}

@Path("/getFromMock/{mockId}")
@GET
public String getFromMock(@PathParam("mockId") String mockId) throws Exception {
MockEndpoint mockEndpoint = context.getEndpoint("mock:" + mockId, MockEndpoint.class);

String result = mockEndpoint.getExchanges().stream().map(e -> e.getIn().getBody(String.class))
.collect(Collectors.joining(SEPARATOR));

mockEndpoint.reset();

return result;
}

@Path("/create/{folder}")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response createFile(@PathParam("folder") String folder, String content) throws Exception {
Exchange response = producerTemplate.request("file:target/" + folder, exchange -> exchange.getIn().setBody(content));
public Response createFile(@PathParam("folder") String folder, byte[] content, @QueryParam("charset") String charset,
@QueryParam("fileName") String fileName)
throws Exception {
StringBuilder url = new StringBuilder("file:target/" + folder + "?initialDelay=10");
if (charset != null && !charset.equals("")) {
url.append("&charset=").append(charset);
}
Exchange response = producerTemplate.request(url.toString(),
exchange -> {
exchange.getIn().setBody(content);
if (fileName != null && !fileName.equals("")) {
exchange.getIn().setHeader(Exchange.FILE_NAME, fileName);
}
});
return Response
.created(new URI("https://camel.apache.org/"))
.entity(response.getMessage().getHeader(Exchange.FILE_NAME_PRODUCED))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import javax.enterprise.context.ApplicationScoped;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileFilter;

import static org.apache.camel.quarkus.component.file.it.FileResource.CONSUME_BATCH;
import static org.apache.camel.quarkus.component.file.it.FileResource.SORT_BY;

@ApplicationScoped
public class FileRoutes extends RouteBuilder {
Expand All @@ -42,5 +47,44 @@ public void configure() {

from("file://target/quartz?scheduler=quartz&scheduler.cron=0/1+*+*+*+*+?&repeatCount=0")
.to("file://target/quartz/out");

from("file://target/" + CONSUME_BATCH + "?"
+ "initialDelay=0&delay=100")
.id(CONSUME_BATCH)
.noAutoStartup()
.convertBodyTo(String.class)
.to("mock:" + CONSUME_BATCH);

from("file://target/charsetUTF8?initialDelay=0&delay=10&delete=true&charset=UTF-8")
.convertBodyTo(String.class)
.to("mock:charsetUTF8");

from("file://target/charsetISO?initialDelay=0&delay=10&delete=true&charset=ISO-8859-1")
.convertBodyTo(String.class)
.to("mock:charsetISO");

from("file://target/idempotent?idempotent=true&move=done/${file:name}&initialDelay=0&delay=10")
.convertBodyTo(String.class).to("mock:idempotent");

bindToRegistry("myFilter", new MyFileFilter<>());
from(("file://target/filter?initialDelay=0&delay=10&filter=#myFilter"))
.convertBodyTo(String.class).to("mock:filter");

from(("file://target/sortBy?initialDelay=0&delay=10&sortBy=reverse:file:name"))
.id(SORT_BY)
.noAutoStartup()
.convertBodyTo(String.class).to("mock:" + SORT_BY);
}

public class MyFileFilter<T> implements GenericFileFilter<T> {
@Override
public boolean accept(GenericFile<T> file) {
// we want all directories
if (file.isDirectory()) {
return true;
}
// we dont accept any files starting with skip in the name
return !file.getFileName().startsWith("skip");
}
}
}

0 comments on commit 4ded00b

Please sign in to comment.