这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions messaging-modules/spring-apache-camel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
<packaging>jar</packaging>
<name>spring-apache-camel</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>com.baeldung</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.camel.producertemplate;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class CamelRoute extends RouteBuilder {

@Override
public void configure() {
from("direct:start").log("Received: ${body}")
.transform(simple("Hello ${body}"));
from("direct:fileRoute").to("file://output?fileName=output.txt&fileExist=Append");
from("direct:beanRoute").bean(ProcessingBean.class, "process");

}

@Bean
public ProcessingBean processingBean() {
return new ProcessingBean();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.camel.producertemplate;

public class ProcessingBean {

public String process(String input) {
return "Bean processed " + input.toUpperCase();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.camel.producertemplate;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProducerTemplateApplication {

public static void main(String[] args) {
SpringApplication.run(ProducerTemplateApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.camel.producertemplate;

import org.apache.camel.ProducerTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProducerTemplateController {

@Autowired
private ProducerTemplate producerTemplate;

@GetMapping("/send/simple/{message}")
public String sendSimpleMessage(@PathVariable String message) {
String response = producerTemplate.requestBody("direct:start", message, String.class);
return response;
}

@GetMapping("/send/file/{message}")
public String sendToFile(@PathVariable String message) {
producerTemplate.sendBody("direct:fileRoute", message + "\n");
return "Message appended to output.txt";
}

@GetMapping("/send/bean/{message}")
public String sendToBean(@PathVariable String message) {
String response = producerTemplate.requestBody("direct:beanRoute", message, String.class);
return response;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.baeldung.producertemplate;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;

import com.baeldung.camel.producertemplate.ProducerTemplateApplication;
import com.baeldung.camel.producertemplate.ProducerTemplateController;

@SpringBootTest(classes = ProducerTemplateApplication.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class ProducerTemplateIntegrationTest {

@Autowired
private ProducerTemplateController producerTemplateController;

private static final String TEST_MESSAGE = "TestMessage";
private static final Path OUTPUT_FILE = Paths.get("output/output.txt");

@BeforeEach
void setUp() throws IOException {

if (Files.exists(OUTPUT_FILE)) {
Files.delete(OUTPUT_FILE);
}
}

@Test
void givenMessage_whenSendingSimpleMessage_thenReturnsProcessedMessage() {
String inputMessage = TEST_MESSAGE;
String response = producerTemplateController.sendSimpleMessage(inputMessage);
assertNotNull(response, "Response should not be null");
assertEquals("Hello " + inputMessage, response);
}

@Test
void givenMessage_whenSendingToFile_thenFileContainsMessage() throws IOException {
String inputMessage = TEST_MESSAGE;
String response = producerTemplateController.sendToFile(inputMessage);

assertEquals("Message appended to output.txt", response);
assertTrue(Files.exists(OUTPUT_FILE));
String fileContent = Files.readString(OUTPUT_FILE);
assertTrue(fileContent.contains(inputMessage));
}

@Test
void givenMessage_whenSendingToBean_thenReturnsUppercaseMessage() {
String inputMessage = TEST_MESSAGE;
String response = producerTemplateController.sendToBean(inputMessage);
assertNotNull(response);
assertEquals("Bean processed " + inputMessage.toUpperCase(), response);
}

}