Skip to content

Commit

Permalink
[#3028] Increase test coverage of a binding mode of camel-rest component
Browse files Browse the repository at this point in the history
  • Loading branch information
Vratislav Hais committed Aug 30, 2021
1 parent 5f5cd36 commit 78acc9b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

public Person() {
}

public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

@XmlAttribute
private String firstName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,26 @@ public String restProducer(@QueryParam("port") int port) {
return producerTemplate.requestBodyAndHeaders(
"rest:get:/rest/template/{messageStart}/{messageEnd}?host=localhost:" + port, null, headers, String.class);
}

@Path("/producer/binding/mode/json")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Person restProducerBindingModeJson(@QueryParam("port") int port) {
String query = "rest:get:/rest/binding/json/producer" +
"?bindingMode=json" +
"&outType=org.apache.camel.quarkus.component.rest.it.Person" +
"&host=localhost:" + port;
return producerTemplate.requestBody(query, null, Person.class);
}

@Path("/producer/binding/mode/xml")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Person restProducerBindingModeXml(@QueryParam("port") int port) {
String query = "rest:get:/rest/binding/xml/producer" +
"?bindingMode=xml" +
"&outType=org.apache.camel.quarkus.component.rest.it.Person" +
"&host=localhost:" + port;
return producerTemplate.requestBody(query, null, Person.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class RestRoutes extends RouteBuilder {

@Override
public void configure() {
final String personJson = "{\"firstName\": \"John\", \"lastName\": \"Doe\", \"age\": 64}";
final String personXml = "<person firstName=\"John\" lastName=\"Doe\" age=\"64\"/>";
restConfiguration()
.enableCORS(true)
.corsAllowCredentials(true)
Expand Down Expand Up @@ -68,6 +70,11 @@ public void configure() {
.setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
.endRest()

.get("/binding/json/producer")
.route()
.setBody(constant(personJson))
.endRest()

.post("/pojo/binding/xml")
.bindingMode(RestBindingMode.xml)
.type(Person.class)
Expand All @@ -77,6 +84,11 @@ public void configure() {
.setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
.endRest()

.get("/binding/xml/producer")
.route()
.setBody(constant(personXml))
.endRest()

.post("/log")
.route()
.log("Hello ${body}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.matchesPattern;
import static org.junit.jupiter.api.Assertions.assertEquals;

@QuarkusTest
class RestTest {
private static final Person person = new Person("John", "Doe", 64);

@Test
public void inspectConfiguration() {
Expand Down Expand Up @@ -107,11 +109,6 @@ public void requestValidation() {

@Test
public void jsonBinding() {
Person person = new Person();
person.setFirstName("John");
person.setLastName("Doe");
person.setAge(64);

String result = String.format(
"Name: %s %s, Age: %d",
person.getFirstName(),
Expand All @@ -128,12 +125,20 @@ public void jsonBinding() {
}

@Test
public void xmlBinding() {
Person person = new Person();
person.setFirstName("John");
person.setLastName("Doe");
person.setAge(64);
public void jsonBindingProducer() {
Person respondPerson = RestAssured.given()
.queryParam("port", RestAssured.port)
.get("/rest/producer/binding/mode/json")
.then()
.statusCode(200)
.extract()
.body()
.as(Person.class);
assertEquals(respondPerson, person);
}

@Test
public void xmlBinding() {
String result = String.format(
"Name: %s %s, Age: %d",
person.getFirstName(),
Expand All @@ -149,6 +154,19 @@ public void xmlBinding() {
.body(equalTo(result));
}

@Test
public void xmlBindingProducer() {
Person respondPerson = RestAssured.given()
.queryParam("port", RestAssured.port)
.get("/rest/producer/binding/mode/xml")
.then()
.statusCode(200)
.extract()
.body()
.as(Person.class);
assertEquals(respondPerson, person);
}

@Test
public void testRestProducer() {
RestAssured.given()
Expand Down

0 comments on commit 78acc9b

Please sign in to comment.