#114 Aggregator pattern; tests; description
This commit is contained in:
@ -0,0 +1,15 @@
|
||||
package com.iluwatar.eip.aggregator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for App class
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void testMain() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.iluwatar.eip.aggregator.routes;
|
||||
|
||||
import org.apache.camel.EndpointInject;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.component.mock.MockEndpoint;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Test class for <i>AggregatorRoute</i>.
|
||||
* <p>
|
||||
* In order for it to work we have to mock endpoints we want to read/write to. To mock those we need to substitute
|
||||
* original endpoint names to mocks.
|
||||
* </p>
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = AggregatorRouteTest.class)
|
||||
@ActiveProfiles("test")
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan
|
||||
public class AggregatorRouteTest {
|
||||
|
||||
@EndpointInject(uri = "{{entry}}")
|
||||
private ProducerTemplate entry;
|
||||
|
||||
@EndpointInject(uri = "{{endpoint}}")
|
||||
private MockEndpoint endpoint;
|
||||
|
||||
/**
|
||||
* Test if endpoint receives three separate messages.
|
||||
* @throws Exception in case of en exception during the test
|
||||
*/
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testSplitter() throws Exception {
|
||||
|
||||
// Three items in one entry message
|
||||
entry.sendBody("TEST1");
|
||||
entry.sendBody("TEST2");
|
||||
entry.sendBody("TEST3");
|
||||
entry.sendBody("TEST4");
|
||||
entry.sendBody("TEST5");
|
||||
|
||||
// Endpoint should have three different messages in the end order of the messages is not important
|
||||
endpoint.expectedMessageCount(2);
|
||||
endpoint.assertIsSatisfied();
|
||||
|
||||
String body = (String) endpoint.getReceivedExchanges().get(0).getIn().getBody();
|
||||
assertEquals(3, body.split(";").length);
|
||||
|
||||
String body2 = (String) endpoint.getReceivedExchanges().get(1).getIn().getBody();
|
||||
assertEquals(2, body2.split(";").length);
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.iluwatar.eip.aggregator.routes;
|
||||
|
||||
import org.apache.camel.CamelContext;
|
||||
import org.apache.camel.Exchange;
|
||||
import org.apache.camel.impl.DefaultExchange;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests MessageAggregationStrategy
|
||||
*/
|
||||
public class MessageAggregationStrategyTest {
|
||||
|
||||
@Test
|
||||
public void testAggregate() {
|
||||
MessageAggregationStrategy mas = new MessageAggregationStrategy();
|
||||
Exchange oldExchange = new DefaultExchange((CamelContext) null);
|
||||
oldExchange.getIn().setBody("TEST1");
|
||||
|
||||
Exchange newExchange = new DefaultExchange((CamelContext) null);
|
||||
newExchange.getIn().setBody("TEST2");
|
||||
|
||||
Exchange output = mas.aggregate(oldExchange, newExchange);
|
||||
String outputBody = (String) output.getIn().getBody();
|
||||
assertEquals("TEST1;TEST2", outputBody);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAggregateOldNull() {
|
||||
MessageAggregationStrategy mas = new MessageAggregationStrategy();
|
||||
|
||||
Exchange newExchange = new DefaultExchange((CamelContext) null);
|
||||
newExchange.getIn().setBody("TEST2");
|
||||
|
||||
Exchange output = mas.aggregate(null, newExchange);
|
||||
String outputBody = (String) output.getIn().getBody();
|
||||
|
||||
assertEquals(newExchange, output);
|
||||
assertEquals("TEST2", outputBody);
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
entry=direct:entry
|
||||
endpoint=mock:endpoint
|
Reference in New Issue
Block a user