use of java.util.List in project camel by apache.
the class AggregateGroupedExchangeBatchSizeTest method testGrouped.
@SuppressWarnings("unchecked")
public void testGrouped() throws Exception {
MockEndpoint result = getMockEndpoint("mock:result");
// we expect 1 or 2 messages since we group all we get in using the same correlation key
result.expectedMinimumMessageCount(1);
// then we sent all the message at once
template.sendBody("direct:start", "100");
template.sendBody("direct:start", "150");
template.sendBody("direct:start", "130");
template.sendBody("direct:start", "200");
assertMockEndpointsSatisfied();
Exchange out = result.getExchanges().get(0);
List<Exchange> grouped = out.getIn().getBody(List.class);
assertTrue("Should be either 2 or 4, was " + grouped.size(), grouped.size() == 2 || grouped.size() == 4);
assertEquals("100", grouped.get(0).getIn().getBody(String.class));
assertEquals("150", grouped.get(1).getIn().getBody(String.class));
// wait a bit for the remainder to come in
Thread.sleep(1000);
if (result.getReceivedCounter() == 2) {
out = result.getExchanges().get(1);
grouped = out.getIn().getBody(List.class);
assertEquals(2, grouped.size());
assertEquals("130", grouped.get(0).getIn().getBody(String.class));
assertEquals("200", grouped.get(1).getIn().getBody(String.class));
}
// END SNIPPET: e2
}
use of java.util.List in project camel by apache.
the class AggregateGroupedExchangeSizePredicateTest method testGroupedSize.
@SuppressWarnings("unchecked")
public void testGroupedSize() throws Exception {
MockEndpoint result = getMockEndpoint("mock:result");
// we expect 2 messages since we group by size (3 and 2)
result.expectedMessageCount(2);
template.sendBodyAndHeader("direct:start", "100", "groupSize", 3);
template.sendBodyAndHeader("direct:start", "150", "groupSize", 3);
template.sendBodyAndHeader("direct:start", "130", "groupSize", 3);
template.sendBodyAndHeader("direct:start", "200", "groupSize", 2);
template.sendBodyAndHeader("direct:start", "190", "groupSize", 2);
assertMockEndpointsSatisfied();
Exchange out = result.getExchanges().get(0);
List<Exchange> grouped = out.getIn().getBody(List.class);
assertEquals(3, grouped.size());
assertEquals("100", grouped.get(0).getIn().getBody(String.class));
assertEquals("150", grouped.get(1).getIn().getBody(String.class));
assertEquals("130", grouped.get(2).getIn().getBody(String.class));
out = result.getExchanges().get(1);
grouped = out.getIn().getBody(List.class);
assertEquals(2, grouped.size());
assertEquals("200", grouped.get(0).getIn().getBody(String.class));
assertEquals("190", grouped.get(1).getIn().getBody(String.class));
}
use of java.util.List in project camel by apache.
the class AlbertoAggregatorTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
AggregationStrategy surnameAggregator = new AggregationStrategy() {
@SuppressWarnings("unchecked")
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
debugIn("Surname Aggregator", oldExchange, newExchange);
Exchange answer = newExchange;
if (oldExchange != null) {
List<String> brothers = oldExchange.getIn().getBody(List.class);
brothers.add(newExchange.getIn().getBody(String.class));
answer = oldExchange;
} else {
List<String> brothers = new ArrayList<String>();
brothers.add(newExchange.getIn().getBody(String.class));
newExchange.getIn().setBody(brothers);
}
debugOut("Surname Aggregator", answer);
return answer;
}
};
@SuppressWarnings("unchecked")
AggregationStrategy brothersAggregator = new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
debugIn("Brothers Aggregator", oldExchange, newExchange);
Exchange answer = newExchange;
if (oldExchange != null) {
Map<String, List<?>> brothers = oldExchange.getIn().getBody(Map.class);
brothers.put(newExchange.getIn().getHeader(SURNAME_HEADER, String.class), newExchange.getIn().getBody(List.class));
answer = oldExchange;
} else {
Map<String, List<?>> brothers = new HashMap<String, List<?>>();
brothers.put(newExchange.getIn().getHeader(SURNAME_HEADER, String.class), newExchange.getIn().getBody(List.class));
newExchange.getIn().setBody(brothers);
}
debugOut("Brothers Aggregator", answer);
return answer;
}
};
private void debugIn(String stringId, Exchange oldExchange, Exchange newExchange) {
if (oldExchange != null) {
log.debug(stringId + " old headers in: " + oldExchange.getIn().getHeaders());
log.debug(stringId + " old body in: " + oldExchange.getIn().getBody());
}
log.debug(stringId + " new headers in: " + newExchange.getIn().getHeaders());
log.debug(stringId + " new body in: " + newExchange.getIn().getBody());
}
private void debugOut(String stringId, Exchange exchange) {
log.debug(stringId + " old headers out: " + exchange.getIn().getHeaders());
log.debug(stringId + " old body out: " + exchange.getIn().getBody());
}
@Override
public void configure() throws Exception {
from("direct:start").split(bodyAs(String.class).tokenize(",")).process(// header
new Processor() {
public void process(Exchange exchange) throws Exception {
String[] parts = exchange.getIn().getBody(String.class).split(" ");
exchange.getIn().setBody(parts[0]);
exchange.getIn().setHeader(SURNAME_HEADER, parts[1]);
}
}).to("direct:joinSurnames");
from("direct:joinSurnames").aggregate(header(SURNAME_HEADER), surnameAggregator).completionTimeout(2000L).setHeader(TYPE_HEADER, constant(BROTHERS_TYPE)).to("direct:joinBrothers");
// Join all brothers lists and remove surname and type headers
AggregateDefinition agg = from("direct:joinBrothers").aggregate(header(TYPE_HEADER), brothersAggregator);
agg.setCompletionTimeout(2000L);
agg.removeHeader(SURNAME_HEADER).removeHeader(TYPE_HEADER).to("mock:result");
}
};
}
use of java.util.List in project camel by apache.
the class AlbertoAggregatorTest method testAggregator.
public void testAggregator() throws Exception {
String allNames = "Harpo Marx,Fiodor Karamazov,Chico Marx,Ivan Karamazov,Groucho Marx,Alexei Karamazov,Dimitri Karamazov";
List<String> marxBrothers = new ArrayList<String>();
marxBrothers.add("Harpo");
marxBrothers.add("Chico");
marxBrothers.add("Groucho");
List<String> karamazovBrothers = new ArrayList<String>();
karamazovBrothers.add("Fiodor");
karamazovBrothers.add("Ivan");
karamazovBrothers.add("Alexei");
karamazovBrothers.add("Dimitri");
Map<String, List<String>> allBrothers = new HashMap<String, List<String>>();
allBrothers.put("Marx", marxBrothers);
allBrothers.put("Karamazov", karamazovBrothers);
MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
resultEndpoint.expectedMessageCount(1);
resultEndpoint.expectedBodiesReceived(allBrothers);
template.sendBody("direct:start", allNames);
assertMockEndpointsSatisfied();
}
use of java.util.List in project camel by apache.
the class AtomPollingConsumerTest method testNoSplitEntries.
@Test
public void testNoSplitEntries() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.assertIsSatisfied();
Exchange exchange = mock.getExchanges().get(0);
Message in = exchange.getIn();
assertNotNull(in);
assertTrue(in.getBody() instanceof List);
assertTrue(in.getHeader(AtomConstants.ATOM_FEED) instanceof Feed);
Feed feed = in.getHeader(AtomConstants.ATOM_FEED, Feed.class);
assertEquals("James Strachan", feed.getAuthor().getName());
List<?> entries = in.getBody(List.class);
assertEquals(7, entries.size());
}
Aggregations