Wire Tap EIP implementation and tests

This commit is contained in:
adkm
2017-10-04 17:04:32 +02:00
parent 0d4a8db114
commit f80d903cff
7 changed files with 193 additions and 1 deletions

View File

@ -0,0 +1,28 @@
package com.iluwatar.eip.wiretap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* In most integration cases there is a need to monitor the messages flowing through the system. It is usually achieved
* by intercepting the message and redirecting it to a different location like console, filesystem or the database.
* It is important that such functionality should not modify the original message and influence the processing path.
*
* <p>
* Wire Tap allows you to route messages to a separate location while they are being forwarded to the ultimate
* destination. It basically consumes messages of the input channel and publishes the unmodified message to both
* output channels.
* </p>
*/
@SpringBootApplication
public class App {
/**
* Program entry point. It starts Spring Boot application and using Apache Camel it auto-configures routes.
*
* @param args command line args
*/
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

View File

@ -0,0 +1,32 @@
package com.iluwatar.eip.wiretap.routes;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
/**
* Sample wire tap route definition.
*
* <p>
* It consumes messages out of the <i>direct:entry</i> entry point and forwards them to <i>direct:endpoint</i>.
* Wire Tap intercepts the message and sends it to <i>direct:wireTap</i>, which in turn forwards it to
* <i>direct:wireTapEndpoint</i>.
* </p>
*
* In this example input/output endpoints names are stored in <i>application.properties</i> file.
*/
@Component
public class WireTapRoute extends RouteBuilder {
/**
* Configures the route
* @throws Exception in case of exception during configuration
*/
@Override
public void configure() throws Exception {
// Main route
from("{{entry}}").wireTap("direct:wireTap").to("{{endpoint}}");
// Wire tap route
from("direct:wireTap").to("{{wireTapEndpoint}}");
}
}

View File

@ -0,0 +1,3 @@
entry=direct:entry
endpoint=direct:endpoint
wireTapEndpoint=direct:wireTapEndpoint

View File

@ -0,0 +1,62 @@
package com.iluwatar.eip.wiretap;
import org.apache.camel.EndpointInject;
import org.apache.camel.Message;
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>WireTapRoute</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 = WireTapRouteTest.class)
@ActiveProfiles("test")
@EnableAutoConfiguration
@ComponentScan
public class WireTapRouteTest {
@EndpointInject(uri = "{{entry}}")
private ProducerTemplate entry;
@EndpointInject(uri = "{{endpoint}}")
private MockEndpoint endpoint;
@EndpointInject(uri = "{{wireTapEndpoint}}")
private MockEndpoint wireTapEndpoint;
/**
* Test if both endpoints receive exactly one message containing the same, unchanged body.
* @throws Exception in case of en exception during the test
*/
@Test
@DirtiesContext
public void testWireTap() throws Exception {
entry.sendBody("TEST");
endpoint.expectedMessageCount(1);
wireTapEndpoint.expectedMessageCount(1);
endpoint.assertIsSatisfied();
wireTapEndpoint.assertIsSatisfied();
Message endpointIn = endpoint.getExchanges().get(0).getIn();
Message wireTapEndpointIn = wireTapEndpoint.getExchanges().get(0).getIn();
assertEquals("TEST", endpointIn.getBody());
assertEquals("TEST", wireTapEndpointIn.getBody());
}
}

View File

@ -0,0 +1,3 @@
entry=direct:entry
endpoint=mock:endpoint
wireTapEndpoint=mock:wireTapEndpoint