Skip to content

Commit 235c400

Browse files
committed
Add FORM post tests
1 parent 2010f37 commit 235c400

File tree

5 files changed

+82
-13
lines changed

5 files changed

+82
-13
lines changed

examples/example-helidon/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55

66
<parent>
7-
<artifactId>web-reactor</artifactId>
8-
<groupId>io.dinject</groupId>
9-
<version>1.0</version>
7+
<groupId>org.avaje</groupId>
8+
<artifactId>java8-oss</artifactId>
9+
<version>2.1</version>
1010
</parent>
1111

1212
<modelVersion>4.0.0</modelVersion>

examples/example-javalin/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
<parent>
1212
<groupId>org.avaje</groupId>
13-
<artifactId>java8-parent</artifactId>
14-
<version>1.3</version>
13+
<artifactId>java8-oss</artifactId>
14+
<version>2.1</version>
1515
</parent>
1616

1717
<properties>

examples/example-javalin/src/main/java/org/example/myapp/web/HelloController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ void saveForm2(String name, String email, String url) {
103103
System.out.println("name " + name + " email:" + email + " url:" + url);
104104
}
105105

106+
@Post("saveform3")
107+
@Form
108+
HelloDto saveForm3(HelloForm helloForm) {
109+
return new HelloDto(52, helloForm.name, helloForm.email);
110+
}
111+
106112
@Hidden
107113
@Get
108114
List<HelloDto> getAll() {

examples/example-javalin/src/main/java/org/example/myapp/web/HelloDto.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public HelloDto(int id, String name, String otherParam) {
1818
this.otherParam = otherParam;
1919
}
2020

21+
/**
22+
* Jackson constructor.
23+
*/
24+
public HelloDto() {
25+
}
26+
2127
public UUID getGid() {
2228
return gid;
2329
}

examples/example-javalin/src/test/java/org/example/myapp/HelloControllerTest.java

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.example.myapp;
22

3+
import io.restassured.common.mapper.TypeRef;
4+
import io.restassured.http.ContentType;
35
import io.restassured.response.Response;
46
import org.example.myapp.web.HelloDto;
57
import org.junit.jupiter.api.Test;
68

9+
import java.util.List;
10+
711
import static io.restassured.RestAssured.get;
812
import static io.restassured.RestAssured.given;
913
import static org.assertj.core.api.Assertions.assertThat;
@@ -19,14 +23,21 @@ void hello() {
1923
assertThat(response.statusCode()).isEqualTo(200);
2024
}
2125

22-
// @Test
23-
// void hello2() {
24-
// given().get(baseUrl + "/foo/hello")
25-
// .then()
26-
// .statusCode(200)
27-
// .body(equalTo("Hello from Foo"));
28-
// }
29-
//
26+
@SuppressWarnings("unchecked")
27+
@Test
28+
void hello2() {
29+
30+
TypeRef listDto = new TypeRef<List<HelloDto>>() { };
31+
final List<HelloDto> beans =
32+
(List<HelloDto>)given().get(baseUrl + "/hello")
33+
.then()
34+
.statusCode(200)
35+
.extract()
36+
.as(listDto);
37+
38+
assertThat(beans).hasSize(2);
39+
}
40+
3041
@Test
3142
void postIt() {
3243
HelloDto dto = new HelloDto(12, "rob", "other");
@@ -36,4 +47,50 @@ void postIt() {
3647
.body("age", equalTo(45))
3748
.body("name", startsWith("testName=hello"));
3849
}
50+
51+
@Test
52+
void postForm_controller_using_formbean() {
53+
54+
given().urlEncodingEnabled(true)
55+
.param("name", "Bazz")
56+
.param("email", "user@foo.com")
57+
.param("url", "http://foo.com")
58+
.param("startDate", "2020-12-03")
59+
.header("Accept", ContentType.JSON.getAcceptHeader())
60+
.post(baseUrl + "/hello/saveform")
61+
.then()
62+
.statusCode(201);
63+
64+
}
65+
66+
@Test
67+
void postForm2_controllerUsingParams() {
68+
69+
given().urlEncodingEnabled(true)
70+
.param("name", "Bazz")
71+
.param("email", "user@foo.com")
72+
.param("url", "http://foo.com")
73+
.param("startDate", "2020-12-03")
74+
.header("Accept", ContentType.JSON.getAcceptHeader())
75+
.post(baseUrl + "/hello/saveform2")
76+
.then()
77+
.statusCode(201);
78+
}
79+
80+
@Test
81+
void postForm3_controllerFormBean_responseJsonDto() {
82+
83+
given().urlEncodingEnabled(true)
84+
.param("name", "Bax")
85+
.param("email", "Bax@foo.com")
86+
.param("url", "http://foo.com")
87+
.param("startDate", "2020-12-03")
88+
.header("Accept", ContentType.JSON.getAcceptHeader())
89+
.post(baseUrl + "/hello/saveform3")
90+
.then()
91+
.body("name", equalTo("Bax"))
92+
.body("otherParam", equalTo("Bax@foo.com"))
93+
.body("id", equalTo(52))
94+
.statusCode(201);
95+
}
3996
}

0 commit comments

Comments
 (0)