Skip to content

Commit 7a0510c

Browse files
committed
No effective change - add more tests for javalin
1 parent 9ad8926 commit 7a0510c

File tree

8 files changed

+182
-20
lines changed

8 files changed

+182
-20
lines changed

tests/test-javalin/src/main/java/org/example/myapp/Main.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import io.javalin.http.staticfiles.Location;
1010
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
1111
import io.swagger.v3.oas.annotations.info.Info;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
1214

1315
import java.util.LinkedHashMap;
1416
import java.util.List;
@@ -18,6 +20,8 @@
1820
@OpenAPIDefinition(info = @Info(title = "Example service", description = "Example Javalin controllers with Java and Maven"))
1921
public class Main {
2022

23+
private static final Logger log = LoggerFactory.getLogger(Main.class);
24+
2125
public static void main(String[] args) {
2226
start(8082);
2327
}
@@ -28,13 +32,12 @@ public static Javalin start(int port) {
2832
config.showJavalinBanner = false;
2933
config.logIfServerNotStarted = false;
3034
config.addStaticFiles("public", Location.CLASSPATH);
35+
config.accessManager((handler, ctx, permittedRoles) -> {
36+
log.debug("allow access ...");
37+
handler.handle(ctx);
38+
});
3139
});
3240

33-
// app.accessManager((handler, ctx, permittedRoles) -> {
34-
// //System.out.println("allow access ...");
35-
// handler.handle(ctx);
36-
// });
37-
3841
app.exception(ValidationException.class, (exception, ctx) -> {
3942

4043
Map<String,Object> map = new LinkedHashMap<>();

tests/test-javalin/src/main/java/org/example/myapp/service/BazRepo.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
import java.util.List;
99

1010
@Singleton
11-
public class BazRepo implements Repository<Baz,Long> {
11+
public class BazRepo implements Repository<Baz, Long> {
1212

1313
@Override
1414
public Baz findById(Long id) {
15-
return new Baz();
15+
Baz baz = new Baz();
16+
baz.id = id;
17+
baz.name = "Baz" + id;
18+
//baz.startDate = LocalDate.of(2020, 1, 1);
19+
return baz;
1620
}
1721

1822
@Override

tests/test-javalin/src/main/java/org/example/myapp/web/BarController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public class BarController implements BarInterface {
1010

1111
@Override
1212
public Bar getById(long id) {
13-
return new Bar();
13+
Bar bar = new Bar();
14+
bar.id = id;
15+
bar.name = "Rob" + id;
16+
return bar;
1417
}
1518

1619
@Override

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
import java.time.LocalDate;
1818
import java.util.ArrayList;
1919
import java.util.List;
20-
import java.util.Objects;
2120

22-
import static java.util.Objects.nonNull;
2321
import static java.util.Objects.requireNonNull;
2422
import static org.example.myapp.web.AppRoles.ADMIN;
2523

@@ -56,7 +54,6 @@ String getPlainMessage() {
5654
* @return The Hello DTO given the id and name.
5755
* @deprecated Please migrate away
5856
*/
59-
// @Deprecated
6057
@Get("/:id/:date")
6158
HelloDto hello(int id, LocalDate date, String otherParam) {
6259
return new HelloDto(id, date.toString(), otherParam);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.example.myapp;
2+
3+
import org.example.myapp.web.Bar;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static io.restassured.RestAssured.given;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.hamcrest.Matchers.equalTo;
9+
10+
class BarControllerTest extends BaseWebTest {
11+
12+
@Test
13+
void getBars() {
14+
given()
15+
.get(baseUrl + "/bars")
16+
.then()
17+
.statusCode(200)
18+
.body(equalTo("Hello"));
19+
}
20+
21+
@Test
22+
void getById() {
23+
24+
Bar bar = given()
25+
.get(baseUrl + "/bars/53")
26+
.then()
27+
.statusCode(200)
28+
.extract()
29+
.as(Bar.class);
30+
31+
assertThat(bar.id).isEqualTo(53L);
32+
assertThat(bar.name).isEqualTo("Rob53");
33+
}
34+
35+
@Test
36+
void findByCode() {
37+
given()
38+
.get(baseUrl + "/bars/find/mycode")
39+
.then()
40+
.statusCode(200);
41+
}
42+
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.example.myapp;
2+
3+
import org.example.myapp.web.Baz;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static io.restassured.RestAssured.given;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class BazControllerTest extends BaseWebTest {
10+
11+
@Test
12+
void findAll() {
13+
given()
14+
.get(baseUrl + "/baz")
15+
.then()
16+
.statusCode(200);
17+
}
18+
19+
@Test
20+
void findById() {
21+
22+
Baz baz = given()
23+
.get(baseUrl + "/baz/53")
24+
.then()
25+
.statusCode(200)
26+
.extract()
27+
.as(Baz.class);
28+
29+
assertThat(baz.id).isEqualTo(53L);
30+
assertThat(baz.name).isEqualTo("Baz53");
31+
}
32+
33+
@Test
34+
void searchByName() {
35+
given()
36+
.get(baseUrl + "/baz/findbyname/mycode")
37+
.then()
38+
.statusCode(200);
39+
}
40+
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.example.myapp;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
public class ErrorResponse {
7+
8+
private String message;
9+
10+
private Map<String,String> errors = new LinkedHashMap<>();
11+
12+
public String getMessage() {
13+
return message;
14+
}
15+
16+
public void setMessage(String message) {
17+
this.message = message;
18+
}
19+
20+
public Map<String, String> getErrors() {
21+
return errors;
22+
}
23+
24+
public void setErrors(Map<String, String> errors) {
25+
this.errors = errors;
26+
}
27+
}

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

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import org.junit.jupiter.api.Test;
88

99
import java.util.List;
10+
import java.util.Map;
1011

1112
import static io.restassured.RestAssured.get;
1213
import static io.restassured.RestAssured.given;
1314
import static org.assertj.core.api.Assertions.assertThat;
1415
import static org.hamcrest.Matchers.equalTo;
15-
import static org.hamcrest.Matchers.startsWith;
16+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1617

1718
class HelloControllerTest extends BaseWebTest {
1819

@@ -23,13 +24,12 @@ void hello() {
2324
assertThat(response.statusCode()).isEqualTo(200);
2425
}
2526

26-
@SuppressWarnings("unchecked")
2727
@Test
2828
void hello2() {
2929

30-
TypeRef listDto = new TypeRef<List<HelloDto>>() { };
31-
final List<HelloDto> beans =
32-
(List<HelloDto>)given().get(baseUrl + "/hello")
30+
TypeRef<List<HelloDto>> listDto = new TypeRef<List<HelloDto>>() { };
31+
final List<HelloDto> beans = given()
32+
.get(baseUrl + "/hello")
3333
.then()
3434
.statusCode(200)
3535
.extract()
@@ -38,14 +38,39 @@ void hello2() {
3838
assertThat(beans).hasSize(2);
3939
}
4040

41+
@Test
42+
void getWithPathParamAndQueryParam() {
43+
44+
final HelloDto bean = given()
45+
.get(baseUrl + "/hello/43/2020-03-05?otherParam=other")
46+
.then()
47+
.statusCode(200)
48+
.extract()
49+
.as(HelloDto.class);
50+
51+
assertThat(bean.id).isEqualTo(43L);
52+
assertThat(bean.name).isEqualTo("2020-03-05");
53+
assertThat(bean.otherParam).isEqualTo("other");
54+
}
55+
4156
@Test
4257
void postIt() {
4358
HelloDto dto = new HelloDto(12, "rob", "other");
4459

45-
given().body(dto).post(baseUrl + "/savebean/bar")
60+
given().body(dto).post(baseUrl + "/hello")
61+
.then()
62+
.body("id", equalTo(12))
63+
.body("name", equalTo("posted"))
64+
.body("otherParam", equalTo("other"));
65+
}
66+
67+
@Test
68+
void saveBean() {
69+
HelloDto dto = new HelloDto(12, "rob", "other");
70+
71+
given().body(dto).post(baseUrl + "/hello/savebean/foo")
4672
.then()
47-
.body("age", equalTo(45))
48-
.body("name", startsWith("testName=hello"));
73+
.statusCode(201);
4974
}
5075

5176
@Test
@@ -60,7 +85,6 @@ void postForm_controller_using_formbean() {
6085
.post(baseUrl + "/hello/saveform")
6186
.then()
6287
.statusCode(201);
63-
6488
}
6589

6690
@Test
@@ -94,6 +118,26 @@ void postForm3_controllerFormBean_responseJsonDto() {
94118
.statusCode(201);
95119
}
96120

121+
@Test
122+
void postForm_validation_expect_badRequest() {
123+
124+
final ErrorResponse res = given().urlEncodingEnabled(true)
125+
.param("email", "[email protected]")
126+
.param("url", "notAValidUrl")
127+
.header("Accept", ContentType.JSON.getAcceptHeader())
128+
.post(baseUrl + "/hello/saveform")
129+
.then()
130+
.statusCode(422)
131+
.extract()
132+
.as(ErrorResponse.class);
133+
134+
assertNotNull(res);
135+
assertThat(res.getMessage()).contains("failed validation");
136+
final Map<String, String> errors = res.getErrors();
137+
assertThat(errors.get("url")).isEqualTo("must be a valid URL");
138+
assertThat(errors.get("name")).isEqualTo("must not be null");
139+
}
140+
97141
@Test
98142
void delete() {
99143
given().delete(baseUrl + "/hello/52")

0 commit comments

Comments
 (0)