Skip to content

File tree

6 files changed

+157
-0
lines changed

6 files changed

+157
-0
lines changed

messaging-modules/spring-apache-camel/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
<packaging>jar</packaging>
99
<name>spring-apache-camel</name>
1010
<url>http://maven.apache.org</url>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>org.apache.maven.plugins</groupId>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<configuration>
17+
<source>11</source>
18+
<target>11</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
1123

1224
<parent>
1325
<groupId>com.baeldung</groupId>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.camel.producertemplate;
2+
3+
import org.apache.camel.builder.RouteBuilder;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class CamelRoute extends RouteBuilder {
9+
10+
@Override
11+
public void configure() {
12+
from("direct:start").log("Received: ${body}")
13+
.transform(simple("Hello ${body}"));
14+
from("direct:fileRoute").to("file://output?fileName=output.txt&fileExist=Append");
15+
from("direct:beanRoute").bean(ProcessingBean.class, "process");
16+
17+
}
18+
19+
@Bean
20+
public ProcessingBean processingBean() {
21+
return new ProcessingBean();
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.camel.producertemplate;
2+
3+
public class ProcessingBean {
4+
5+
public String process(String input) {
6+
return "Bean processed " + input.toUpperCase();
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.camel.producertemplate;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ProducerTemplateApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ProducerTemplateApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.camel.producertemplate;
2+
3+
import org.apache.camel.ProducerTemplate;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class ProducerTemplateController {
11+
12+
@Autowired
13+
private ProducerTemplate producerTemplate;
14+
15+
@GetMapping("/send/simple/{message}")
16+
public String sendSimpleMessage(@PathVariable String message) {
17+
String response = producerTemplate.requestBody("direct:start", message, String.class);
18+
return response;
19+
}
20+
21+
@GetMapping("/send/file/{message}")
22+
public String sendToFile(@PathVariable String message) {
23+
producerTemplate.sendBody("direct:fileRoute", message + "\n");
24+
return "Message appended to output.txt";
25+
}
26+
27+
@GetMapping("/send/bean/{message}")
28+
public String sendToBean(@PathVariable String message) {
29+
String response = producerTemplate.requestBody("direct:beanRoute", message, String.class);
30+
return response;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.producertemplate;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.boot.test.context.SpringBootTest;
16+
import org.springframework.test.annotation.DirtiesContext;
17+
18+
import com.baeldung.camel.producertemplate.ProducerTemplateApplication;
19+
import com.baeldung.camel.producertemplate.ProducerTemplateController;
20+
21+
@SpringBootTest(classes = ProducerTemplateApplication.class)
22+
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
23+
public class ProducerTemplateIntegrationTest {
24+
25+
@Autowired
26+
private ProducerTemplateController producerTemplateController;
27+
28+
private static final String TEST_MESSAGE = "TestMessage";
29+
private static final Path OUTPUT_FILE = Paths.get("output/output.txt");
30+
31+
@BeforeEach
32+
void setUp() throws IOException {
33+
34+
if (Files.exists(OUTPUT_FILE)) {
35+
Files.delete(OUTPUT_FILE);
36+
}
37+
}
38+
39+
@Test
40+
void givenMessage_whenSendingSimpleMessage_thenReturnsProcessedMessage() {
41+
String inputMessage = TEST_MESSAGE;
42+
String response = producerTemplateController.sendSimpleMessage(inputMessage);
43+
assertNotNull(response, "Response should not be null");
44+
assertEquals("Hello " + inputMessage, response);
45+
}
46+
47+
@Test
48+
void givenMessage_whenSendingToFile_thenFileContainsMessage() throws IOException {
49+
String inputMessage = TEST_MESSAGE;
50+
String response = producerTemplateController.sendToFile(inputMessage);
51+
52+
assertEquals("Message appended to output.txt", response);
53+
assertTrue(Files.exists(OUTPUT_FILE));
54+
String fileContent = Files.readString(OUTPUT_FILE);
55+
assertTrue(fileContent.contains(inputMessage));
56+
}
57+
58+
@Test
59+
void givenMessage_whenSendingToBean_thenReturnsUppercaseMessage() {
60+
String inputMessage = TEST_MESSAGE;
61+
String response = producerTemplateController.sendToBean(inputMessage);
62+
assertNotNull(response);
63+
assertEquals("Bean processed " + inputMessage.toUpperCase(), response);
64+
}
65+
66+
}

0 commit comments

Comments
 (0)