#173 Added new pattern, tests
This commit is contained in:
		
							
								
								
									
										20
									
								
								eip-splitter/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								eip-splitter/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| --- | ||||
| layout: pattern | ||||
| title: EIP Splitter | ||||
| folder: eip-splitter | ||||
| permalink: /patterns/eip-splitter/ | ||||
| categories: Enterprise integration | ||||
| tags: | ||||
|  - Java | ||||
|  - Difficulty-Intermittent | ||||
|  - Enterprise integration | ||||
| --- | ||||
|  | ||||
| ## Intent | ||||
|  | ||||
|  | ||||
| ## Applicability | ||||
|  | ||||
|  | ||||
| ## Credits | ||||
|  | ||||
							
								
								
									
										68
									
								
								eip-splitter/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								eip-splitter/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <!-- | ||||
|     The MIT License | ||||
|     Copyright (c) 2014-2016 Ilkka Seppälä | ||||
|     Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|     of this software and associated documentation files (the "Software"), to deal | ||||
|     in the Software without restriction, including without limitation the rights | ||||
|     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|     copies of the Software, and to permit persons to whom the Software is | ||||
|     furnished to do so, subject to the following conditions: | ||||
|     The above copyright notice and this permission notice shall be included in | ||||
|     all copies or substantial portions of the Software. | ||||
|     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|     THE SOFTWARE. | ||||
| --> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
|     <artifactId>eip-splitter</artifactId> | ||||
|     <parent> | ||||
|         <groupId>com.iluwatar</groupId> | ||||
|         <artifactId>java-design-patterns</artifactId> | ||||
|         <version>1.18.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
|  | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-web</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <dependency> | ||||
|             <groupId>org.apache.camel</groupId> | ||||
|             <artifactId>camel-core</artifactId> | ||||
|             <version>${camel.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.apache.camel</groupId> | ||||
|             <artifactId>camel-csv</artifactId> | ||||
|             <version>${camel.version}</version> | ||||
|         </dependency> | ||||
|  | ||||
|         <dependency> | ||||
|             <groupId>org.apache.camel</groupId> | ||||
|             <artifactId>camel-spring-boot</artifactId> | ||||
|             <version>${camel.version}</version> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- Testing --> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-test</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <dependency> | ||||
|             <groupId>org.apache.camel</groupId> | ||||
|             <artifactId>camel-test-spring</artifactId> | ||||
|             <version>${camel.version}</version> | ||||
|         </dependency> | ||||
|  | ||||
|     </dependencies> | ||||
| </project> | ||||
| @@ -0,0 +1,45 @@ | ||||
| package com.iluwatar.eip.splitter; | ||||
|  | ||||
| import org.apache.camel.CamelContext; | ||||
| import org.apache.camel.builder.RouteBuilder; | ||||
| import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| import org.springframework.context.ConfigurableApplicationContext; | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * <p> | ||||
|  * </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) throws Exception { | ||||
|     // Run Spring Boot application and obtain ApplicationContext | ||||
|     ConfigurableApplicationContext context = SpringApplication.run(App.class, args); | ||||
|  | ||||
|     // Get CamelContext from ApplicationContext | ||||
|     CamelContext camelContext = (CamelContext) context.getBean("camelContext"); | ||||
|  | ||||
|     // Add a new routes that will handle endpoints form SplitterRoute class. | ||||
|     camelContext.addRoutes(new RouteBuilder() { | ||||
|  | ||||
|       @Override | ||||
|       public void configure() throws Exception { | ||||
|         from("{{endpoint}}").log("ENDPOINT: ${body}"); | ||||
|       } | ||||
|  | ||||
|     }); | ||||
|  | ||||
|     // Add producer that will send test message to an entry point in WireTapRoute | ||||
|     String[] stringArray = {"Test item #1", "Test item #2", "Test item #3"}; | ||||
|     camelContext.createProducerTemplate().sendBody("{{entry}}", stringArray); | ||||
|  | ||||
|     SpringApplication.exit(context); | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,26 @@ | ||||
| package com.iluwatar.eip.splitter.routes; | ||||
|  | ||||
| import org.apache.camel.builder.RouteBuilder; | ||||
| import org.springframework.stereotype.Component; | ||||
|  | ||||
| /** | ||||
|  * Sample splitter route definition. | ||||
|  * | ||||
|  * <p> | ||||
|  * </p> | ||||
|  * | ||||
|  * In this example input/output endpoints names are stored in <i>application.properties</i> file. | ||||
|  */ | ||||
| @Component | ||||
| public class SplitterRoute extends RouteBuilder { | ||||
|  | ||||
|   /** | ||||
|    * Configures the route | ||||
|    * @throws Exception in case of exception during configuration | ||||
|    */ | ||||
|   @Override | ||||
|   public void configure() throws Exception { | ||||
|     // Main route | ||||
|     from("{{entry}}").split().body().to("{{endpoint}}"); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										2
									
								
								eip-splitter/src/main/resources/application.properties
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								eip-splitter/src/main/resources/application.properties
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| entry=direct:entry | ||||
| endpoint=direct:endpoint | ||||
| @@ -0,0 +1,15 @@ | ||||
| package com.iluwatar.eip.splitter; | ||||
|  | ||||
| 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,53 @@ | ||||
| package com.iluwatar.eip.splitter.routes; | ||||
|  | ||||
| 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>SplitterRoute</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 = SplitterRouteTest.class) | ||||
| @ActiveProfiles("test") | ||||
| @EnableAutoConfiguration | ||||
| @ComponentScan | ||||
| public class SplitterRouteTest { | ||||
|  | ||||
|   @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(new String[] {"TEST1", "TEST2", "TEST3"}); | ||||
|  | ||||
|     // Endpoint should have three different messages in the end order of the messages is not important | ||||
|     endpoint.expectedMessageCount(3); | ||||
|     endpoint.assertIsSatisfied(); | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,2 @@ | ||||
| entry=direct:entry | ||||
| endpoint=mock:endpoint | ||||
		Reference in New Issue
	
	Block a user