X Tutup
Skip to content

Commit f80d903

Browse files
author
adkm
committed
Wire Tap EIP implementation and tests
1 parent 0d4a8db commit f80d903

File tree

7 files changed

+193
-1
lines changed

7 files changed

+193
-1
lines changed

eip-wire-tap/pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
The MIT License
4+
Copyright (c) 2014-2016 Ilkka Seppälä
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
20+
-->
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
<artifactId>eip-wire-tap</artifactId>
26+
<parent>
27+
<groupId>com.iluwatar</groupId>
28+
<artifactId>java-design-patterns</artifactId>
29+
<version>1.18.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.apache.camel</groupId>
40+
<artifactId>camel-core</artifactId>
41+
<version>${camel.version}</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.apache.camel</groupId>
46+
<artifactId>camel-spring-boot</artifactId>
47+
<version>${camel.version}</version>
48+
</dependency>
49+
50+
<!-- Testing -->
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-starter-test</artifactId>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.apache.camel</groupId>
58+
<artifactId>camel-test-spring</artifactId>
59+
<version>${camel.version}</version>
60+
</dependency>
61+
62+
</dependencies>
63+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar.eip.wiretap;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* In most integration cases there is a need to monitor the messages flowing through the system. It is usually achieved
8+
* by intercepting the message and redirecting it to a different location like console, filesystem or the database.
9+
* It is important that such functionality should not modify the original message and influence the processing path.
10+
*
11+
* <p>
12+
* Wire Tap allows you to route messages to a separate location while they are being forwarded to the ultimate
13+
* destination. It basically consumes messages of the input channel and publishes the unmodified message to both
14+
* output channels.
15+
* </p>
16+
*/
17+
@SpringBootApplication
18+
public class App {
19+
20+
/**
21+
* Program entry point. It starts Spring Boot application and using Apache Camel it auto-configures routes.
22+
*
23+
* @param args command line args
24+
*/
25+
public static void main(String[] args) {
26+
SpringApplication.run(App.class, args);
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.iluwatar.eip.wiretap.routes;
2+
3+
import org.apache.camel.builder.RouteBuilder;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* Sample wire tap route definition.
8+
*
9+
* <p>
10+
* It consumes messages out of the <i>direct:entry</i> entry point and forwards them to <i>direct:endpoint</i>.
11+
* Wire Tap intercepts the message and sends it to <i>direct:wireTap</i>, which in turn forwards it to
12+
* <i>direct:wireTapEndpoint</i>.
13+
* </p>
14+
*
15+
* In this example input/output endpoints names are stored in <i>application.properties</i> file.
16+
*/
17+
@Component
18+
public class WireTapRoute extends RouteBuilder {
19+
20+
/**
21+
* Configures the route
22+
* @throws Exception in case of exception during configuration
23+
*/
24+
@Override
25+
public void configure() throws Exception {
26+
// Main route
27+
from("{{entry}}").wireTap("direct:wireTap").to("{{endpoint}}");
28+
29+
// Wire tap route
30+
from("direct:wireTap").to("{{wireTapEndpoint}}");
31+
}
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
entry=direct:entry
2+
endpoint=direct:endpoint
3+
wireTapEndpoint=direct:wireTapEndpoint
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.iluwatar.eip.wiretap;
2+
3+
import org.apache.camel.EndpointInject;
4+
import org.apache.camel.Message;
5+
import org.apache.camel.ProducerTemplate;
6+
import org.apache.camel.component.mock.MockEndpoint;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
10+
import org.springframework.boot.test.SpringApplicationConfiguration;
11+
import org.springframework.context.annotation.ComponentScan;
12+
import org.springframework.test.annotation.DirtiesContext;
13+
import org.springframework.test.context.ActiveProfiles;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
/**
19+
* Test class for <i>WireTapRoute</i>.
20+
* <p>
21+
* In order for it to work we have to mock endpoints we want to read/write to. To mock those we need to substitute
22+
* original endpoint names to mocks.
23+
* </p>
24+
*/
25+
@RunWith(SpringJUnit4ClassRunner.class)
26+
@SpringApplicationConfiguration(classes = WireTapRouteTest.class)
27+
@ActiveProfiles("test")
28+
@EnableAutoConfiguration
29+
@ComponentScan
30+
public class WireTapRouteTest {
31+
32+
@EndpointInject(uri = "{{entry}}")
33+
private ProducerTemplate entry;
34+
35+
@EndpointInject(uri = "{{endpoint}}")
36+
private MockEndpoint endpoint;
37+
38+
@EndpointInject(uri = "{{wireTapEndpoint}}")
39+
private MockEndpoint wireTapEndpoint;
40+
41+
/**
42+
* Test if both endpoints receive exactly one message containing the same, unchanged body.
43+
* @throws Exception in case of en exception during the test
44+
*/
45+
@Test
46+
@DirtiesContext
47+
public void testWireTap() throws Exception {
48+
entry.sendBody("TEST");
49+
50+
endpoint.expectedMessageCount(1);
51+
wireTapEndpoint.expectedMessageCount(1);
52+
53+
endpoint.assertIsSatisfied();
54+
wireTapEndpoint.assertIsSatisfied();
55+
56+
Message endpointIn = endpoint.getExchanges().get(0).getIn();
57+
Message wireTapEndpointIn = wireTapEndpoint.getExchanges().get(0).getIn();
58+
59+
assertEquals("TEST", endpointIn.getBody());
60+
assertEquals("TEST", wireTapEndpointIn.getBody());
61+
}
62+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
entry=direct:entry
2+
endpoint=mock:endpoint
3+
wireTapEndpoint=mock:wireTapEndpoint

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@
145145
<module>cqrs</module>
146146
<module>event-sourcing</module>
147147
<module>data-transfer-object</module>
148-
<module>throttling</module>
148+
<module>throttling</module>
149149
<module>partial-response</module>
150+
<module>eip-wire-tap</module>
150151
</modules>
151152

152153
<dependencyManagement>

0 commit comments

Comments
 (0)
X Tutup