Merge pull request #14802 from hmdrzsharifi/BAEL-6916

Bael 6916: Update Camel dependency in article https://www.baeldung.com/apache-camel-intro#maven-dependencies
This commit is contained in:
Liam Williams 2023-09-30 15:15:18 +01:00 committed by GitHub
commit 3a129c0bd1
8 changed files with 156 additions and 71 deletions

View File

@ -17,30 +17,39 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.apache.camel</groupId> <groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-core</artifactId> <artifactId>camel-spring-boot-starter</artifactId>
<version>${env.camel.version}</version> <version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>${camel.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.camel</groupId> <groupId>org.apache.camel</groupId>
<artifactId>camel-spring-javaconfig</artifactId> <artifactId>camel-test-spring-junit5</artifactId>
<version>${env.camel.version}</version> <version>${camel.version}</version>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.camel</groupId> <groupId>org.junit.platform</groupId>
<artifactId>camel-jackson</artifactId> <artifactId>junit-platform-launcher</artifactId>
<version>${env.camel.version}</version> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.camel</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>camel-test</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<version>${env.camel.version}</version> </dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<properties> <properties>
<env.camel.version>3.14.7</env.camel.version> <camel.version>3.21.0</camel.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,11 @@
package com.baeldung.camel.apache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -5,14 +5,17 @@ import java.util.Date;
import org.apache.camel.Exchange; import org.apache.camel.Exchange;
import org.apache.camel.Processor; import org.apache.camel.Processor;
import org.springframework.stereotype.Component;
@Component
public class FileProcessor implements Processor { public class FileProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception { public void process(Exchange exchange) throws Exception {
String originalFileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME, String.class); String originalFileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
Date date = new Date(); Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String changedFileName = dateFormat.format(date) + originalFileName; String changedFileName = dateFormat.format(date) + originalFileName;
exchange.getIn().setHeader(Exchange.FILE_NAME, changedFileName); exchange.getIn().setHeader(Exchange.FILE_NAME, changedFileName);
} }

View File

@ -1,7 +1,9 @@
package com.baeldung.camel.apache.file; package com.baeldung.camel.apache.file;
import org.apache.camel.builder.RouteBuilder; import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class FileRouter extends RouteBuilder { public class FileRouter extends RouteBuilder {
private static final String SOURCE_FOLDER = "src/test/source-folder"; private static final String SOURCE_FOLDER = "src/test/source-folder";

View File

@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="fileRouter" class="com.baeldung.camel.apache.file.FileRouter" /> <context:component-scan base-package="com.baeldung.camel.apache"/>
<bean id="fileProcessor" class="com.baeldung.camel.apache.file.FileProcessor" /> <context:annotation-config />
<camelContext xmlns="http://camel.apache.org/schema/spring"> <camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="fileRouter" /> <routeBuilder ref="fileRouter" />

View File

@ -1,31 +1,65 @@
package com.apache.baeldung.camel.jackson; package com.apache.baeldung.camel.jackson;
import com.baeldung.camel.apache.Application;
import com.baeldung.camel.apache.jackson.Fruit;
import org.apache.camel.Configuration;
import org.apache.camel.EndpointInject;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.ListJacksonDataFormat;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.test.annotation.DirtiesContext;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List; import java.util.List;
import org.apache.camel.builder.RouteBuilder; import static org.springframework.test.util.AssertionErrors.assertEquals;
import org.apache.camel.component.jackson.ListJacksonDataFormat; import static org.springframework.test.util.AssertionErrors.assertNotNull;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import com.baeldung.camel.apache.jackson.Fruit; @CamelSpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@SpringBootTest(classes = {Application.class, FruitArrayJacksonUnmarshalUnitTest.TestConfig.class})
public class FruitArrayJacksonUnmarshalUnitTest {
public class FruitArrayJacksonUnmarshalUnitTest extends CamelTestSupport { @Autowired
private ProducerTemplate template;
@EndpointInject("mock:marshalledObject")
private MockEndpoint mock;
@Configuration
static class TestConfig {
@Bean
RoutesBuilder route() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:jsonInput").unmarshal(new ListJacksonDataFormat(Fruit.class))
.to("mock:marshalledObject");
}
};
}
}
@Test @Test
public void givenJsonFruitArray_whenUnmarshalled_thenSuccess() throws Exception { public void givenJsonFruitArray_whenUnmarshalled_thenSuccess() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:marshalledObject"); mock.setExpectedMessageCount(1);
mock.expectedMessageCount(1);
mock.message(0).body().isInstanceOf(List.class); mock.message(0).body().isInstanceOf(List.class);
String json = readJsonFromFile("/json/fruit-array.json"); String json = readJsonFromFile("/json/fruit-array.json");
template.sendBody("direct:jsonInput", json); template.sendBody("direct:jsonInput", json);
assertMockEndpointsSatisfied(); mock.assertIsSatisfied();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Fruit> fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(List.class); List<Fruit> fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(List.class);
@ -42,21 +76,9 @@ public class FruitArrayJacksonUnmarshalUnitTest extends CamelTestSupport {
assertEquals("Fruit id", 101, fruit.getId()); assertEquals("Fruit id", 101, fruit.getId());
} }
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:jsonInput").unmarshal(new ListJacksonDataFormat(Fruit.class))
.to("mock:marshalledObject");
}
};
}
private String readJsonFromFile(String path) throws URISyntaxException, IOException { private String readJsonFromFile(String path) throws URISyntaxException, IOException {
URL resource = FruitArrayJacksonUnmarshalUnitTest.class.getResource(path); URL resource = FruitArrayJacksonUnmarshalUnitTest.class.getResource(path);
return new String(Files.readAllBytes(Paths.get(resource.toURI()))); return new String(Files.readAllBytes(Paths.get(resource.toURI())), StandardCharsets.UTF_8);
} }
} }

View File

@ -1,32 +1,64 @@
package com.apache.baeldung.camel.jackson; package com.apache.baeldung.camel.jackson;
import com.baeldung.camel.apache.Application;
import com.baeldung.camel.apache.jackson.Fruit;
import com.baeldung.camel.apache.jackson.FruitList;
import org.apache.camel.Configuration;
import org.apache.camel.EndpointInject;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List; import java.util.List;
import org.apache.camel.builder.RouteBuilder; import static org.springframework.test.util.AssertionErrors.assertEquals;
import org.apache.camel.component.jackson.JacksonDataFormat; import static org.springframework.test.util.AssertionErrors.assertNotNull;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import com.baeldung.camel.apache.jackson.Fruit; @CamelSpringBootTest
import com.baeldung.camel.apache.jackson.FruitList; @SpringBootTest(classes = {Application.class, FruitListJacksonUnmarshalUnitTest.TestConfig.class})
public class FruitListJacksonUnmarshalUnitTest {
public class FruitListJacksonUnmarshalUnitTest extends CamelTestSupport { @Autowired
private ProducerTemplate template;
@EndpointInject("mock:marshalledObject")
private MockEndpoint mock;
@Configuration
static class TestConfig {
@Bean
RoutesBuilder route() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:jsonInput").unmarshal(new JacksonDataFormat(FruitList.class))
.to("mock:marshalledObject");
}
};
}
}
@Test @Test
public void givenJsonFruitList_whenUnmarshalled_thenSuccess() throws Exception { public void givenJsonFruitList_whenUnmarshalled_thenSuccess() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:marshalledObject"); mock.setExpectedMessageCount(1);
mock.expectedMessageCount(1);
mock.message(0).body().isInstanceOf(FruitList.class); mock.message(0).body().isInstanceOf(FruitList.class);
String json = readJsonFromFile("/json/fruit-list.json"); String json = readJsonFromFile("/json/fruit-list.json");
template.sendBody("direct:jsonInput", json); template.sendBody("direct:jsonInput", json);
assertMockEndpointsSatisfied(); mock.assertIsSatisfied();
FruitList fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(FruitList.class); FruitList fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(FruitList.class);
assertNotNull("Fruit lists should not be null", fruitList); assertNotNull("Fruit lists should not be null", fruitList);
@ -43,20 +75,9 @@ public class FruitListJacksonUnmarshalUnitTest extends CamelTestSupport {
assertEquals("Fruit id", 101, fruit.getId()); assertEquals("Fruit id", 101, fruit.getId());
} }
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:jsonInput").unmarshal(new JacksonDataFormat(FruitList.class))
.to("mock:marshalledObject");
}
};
}
private String readJsonFromFile(String path) throws URISyntaxException, IOException { private String readJsonFromFile(String path) throws URISyntaxException, IOException {
URL resource = FruitListJacksonUnmarshalUnitTest.class.getResource(path); URL resource = FruitListJacksonUnmarshalUnitTest.class.getResource(path);
return new String(Files.readAllBytes(Paths.get(resource.toURI()))); return new String(Files.readAllBytes(Paths.get(resource.toURI())), StandardCharsets.UTF_8);
} }
} }

View File

@ -1,16 +1,21 @@
package com.apache.camel.file.processor; package com.apache.camel.file.processor;
import java.io.File; import com.baeldung.camel.apache.file.FileProcessor;
import org.apache.camel.CamelContext; import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder; import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.awaitility.Awaitility;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baeldung.camel.apache.file.FileProcessor; import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
public class FileProcessorIntegrationTest { public class FileProcessorIntegrationTest {
@ -52,7 +57,7 @@ public class FileProcessorIntegrationTest {
} }
@Test @Test
public void moveFolderContentJavaDSLTest() throws Exception { public void givenJavaDSLRoute_whenCamelStart_thenMoveFolderContent() throws Exception {
final CamelContext camelContext = new DefaultCamelContext(); final CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() { camelContext.addRoutes(new RouteBuilder() {
@Override @Override
@ -61,15 +66,26 @@ public class FileProcessorIntegrationTest {
} }
}); });
camelContext.start(); camelContext.start();
Thread.sleep(DURATION_MILIS); verifyFolderContent();
camelContext.stop(); camelContext.stop();
} }
@Test @Test
public void moveFolderContentSpringDSLTest() throws InterruptedException { public void givenSpringDSLRoute_whenCamelStart_thenMoveFolderContent() throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-test.xml"); ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-test.xml");
Thread.sleep(DURATION_MILIS); verifyFolderContent();
applicationContext.close(); applicationContext.close();
}
private void verifyFolderContent() {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
File destinationFile1 = new File(DESTINATION_FOLDER + "/" + dateFormat.format(date) + "File1.txt");
File destinationFile2 = new File(DESTINATION_FOLDER + "/" + dateFormat.format(date) + "File2.txt");
Awaitility.await().atMost(DURATION_MILIS, TimeUnit.MILLISECONDS).untilAsserted(() -> {
assertThat(destinationFile1.exists()).isTrue();
assertThat(destinationFile2.exists()).isTrue();
});
} }
} }