Skip to content

Commit 647cc67

Browse files
JonCookJonathan Cook
andauthored
BAEL-4736 - Convert JSONArray to List of Object using camel-jackson (eugenp#10316)
* BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson Co-authored-by: Jonathan Cook <[email protected]>
1 parent d592d5f commit 647cc67

File tree

7 files changed

+193
-0
lines changed

7 files changed

+193
-0
lines changed

spring-apache-camel/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@
4747
<artifactId>camel-spring-javaconfig</artifactId>
4848
<version>${env.camel.version}</version>
4949
</dependency>
50+
<dependency>
51+
<groupId>org.apache.camel</groupId>
52+
<artifactId>camel-jackson</artifactId>
53+
<version>${env.camel.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.apache.camel</groupId>
57+
<artifactId>camel-test</artifactId>
58+
<version>${env.camel.version}</version>
59+
<scope>test</scope>
60+
</dependency>
5061
</dependencies>
5162

5263
<properties>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.camel.jackson;
2+
3+
public class Fruit {
4+
5+
private String name;
6+
private int id;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.camel.jackson;
2+
3+
import java.util.List;
4+
5+
public class FruitList {
6+
7+
private List<Fruit> fruits;
8+
9+
public List<Fruit> getFruits() {
10+
return fruits;
11+
}
12+
13+
public void setFruits(List<Fruit> fruits) {
14+
this.fruits = fruits;
15+
}
16+
17+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.camel.jackson;
2+
3+
import java.io.IOException;
4+
import java.net.URISyntaxException;
5+
import java.net.URL;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
import java.util.List;
9+
10+
import org.apache.camel.builder.RouteBuilder;
11+
import org.apache.camel.component.jackson.ListJacksonDataFormat;
12+
import org.apache.camel.component.mock.MockEndpoint;
13+
import org.apache.camel.test.junit4.CamelTestSupport;
14+
import org.junit.Test;
15+
16+
public class FruitArrayJacksonUnmarshalUnitTest extends CamelTestSupport {
17+
18+
@Test
19+
public void givenJsonFruitArray_whenUnmarshalled_thenSuccess() throws Exception {
20+
MockEndpoint mock = getMockEndpoint("mock:marshalledObject");
21+
mock.expectedMessageCount(1);
22+
mock.message(0).body().isInstanceOf(List.class);
23+
24+
String json = readJsonFromFile("/json/fruit-array.json");
25+
template.sendBody("direct:jsonInput", json);
26+
assertMockEndpointsSatisfied();
27+
28+
@SuppressWarnings("unchecked")
29+
List<Fruit> fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(List.class);
30+
assertNotNull("Fruit lists should not be null", fruitList);
31+
32+
assertEquals("There should be two fruits", 2, fruitList.size());
33+
34+
Fruit fruit = fruitList.get(0);
35+
assertEquals("Fruit name", "Banana", fruit.getName());
36+
assertEquals("Fruit id", 100, fruit.getId());
37+
38+
fruit = fruitList.get(1);
39+
assertEquals("Fruit name", "Apple", fruit.getName());
40+
assertEquals("Fruit id", 101, fruit.getId());
41+
}
42+
43+
@Override
44+
protected RouteBuilder createRouteBuilder() throws Exception {
45+
return new RouteBuilder() {
46+
@Override
47+
public void configure() throws Exception {
48+
49+
from("direct:jsonInput").unmarshal(new ListJacksonDataFormat(Fruit.class))
50+
.to("mock:marshalledObject");
51+
}
52+
};
53+
}
54+
55+
private String readJsonFromFile(String path) throws URISyntaxException, IOException {
56+
URL resource = FruitArrayJacksonUnmarshalUnitTest.class.getResource(path);
57+
return new String(Files.readAllBytes(Paths.get(resource.toURI())));
58+
}
59+
60+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.camel.jackson;
2+
3+
import java.io.IOException;
4+
import java.net.URISyntaxException;
5+
import java.net.URL;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
import java.util.List;
9+
10+
import org.apache.camel.builder.RouteBuilder;
11+
import org.apache.camel.component.jackson.JacksonDataFormat;
12+
import org.apache.camel.component.mock.MockEndpoint;
13+
import org.apache.camel.test.junit4.CamelTestSupport;
14+
import org.junit.Test;
15+
16+
public class FruitListJacksonUnmarshalUnitTest extends CamelTestSupport {
17+
18+
@Test
19+
public void givenJsonFruitList_whenUnmarshalled_thenSuccess() throws Exception {
20+
MockEndpoint mock = getMockEndpoint("mock:marshalledObject");
21+
mock.expectedMessageCount(1);
22+
mock.message(0).body().isInstanceOf(FruitList.class);
23+
24+
String json = readJsonFromFile("/json/fruit-list.json");
25+
template.sendBody("direct:jsonInput", json);
26+
assertMockEndpointsSatisfied();
27+
28+
FruitList fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(FruitList.class);
29+
assertNotNull("Fruit lists should not be null", fruitList);
30+
31+
List<Fruit> fruits = fruitList.getFruits();
32+
assertEquals("There should be two fruits", 2, fruits.size());
33+
34+
Fruit fruit = fruits.get(0);
35+
assertEquals("Fruit name", "Banana", fruit.getName());
36+
assertEquals("Fruit id", 100, fruit.getId());
37+
38+
fruit = fruits.get(1);
39+
assertEquals("Fruit name", "Apple", fruit.getName());
40+
assertEquals("Fruit id", 101, fruit.getId());
41+
}
42+
43+
@Override
44+
protected RouteBuilder createRouteBuilder() throws Exception {
45+
return new RouteBuilder() {
46+
@Override
47+
public void configure() throws Exception {
48+
from("direct:jsonInput").unmarshal(new JacksonDataFormat(FruitList.class))
49+
.to("mock:marshalledObject");
50+
}
51+
};
52+
}
53+
54+
private String readJsonFromFile(String path) throws URISyntaxException, IOException {
55+
URL resource = FruitListJacksonUnmarshalUnitTest.class.getResource(path);
56+
return new String(Files.readAllBytes(Paths.get(resource.toURI())));
57+
}
58+
59+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"id": 100,
4+
"name": "Banana"
5+
},
6+
{
7+
"id": 101,
8+
"name": "Apple"
9+
}
10+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"fruits": [
3+
{
4+
"id": 100,
5+
"name": "Banana"
6+
},
7+
{
8+
"id": 101,
9+
"name": "Apple"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)