Skip to content

Commit 65b0a80

Browse files
committed
- build: refactor tests for tRPC and JSON-RPC
1 parent a6d0788 commit 65b0a80

16 files changed

+402
-70
lines changed

modules/jooby-apt/src/main/java/io/jooby/internal/apt/MvcRoute.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,6 @@ private List<String> generateRpcParameter(
10081008
return statements;
10091009
}
10101010

1011-
private void controllerVar(boolean kt, List<String> buffer) {
1012-
controllerVar(kt, buffer, 2);
1013-
}
1014-
10151011
private void controllerVar(boolean kt, List<String> buffer, int indent) {
10161012
buffer.add(statement(indent(indent), var(kt), "c = this.factory.apply(ctx)", semicolon(kt)));
10171013
}
@@ -1091,7 +1087,7 @@ public String getMethodName() {
10911087
}
10921088

10931089
public String getJsonRpcMethodName() {
1094-
var annotation = AnnotationSupport.findAnnotationByName(method, "io.jooby.jsonrpc.JsonRpc");
1090+
var annotation = AnnotationSupport.findAnnotationByName(method, "io.jooby.annotation.JsonRpc");
10951091
if (annotation != null) {
10961092
var val =
10971093
AnnotationSupport.findAnnotationValue(annotation, VALUE).stream().findFirst().orElse("");

modules/jooby-apt/src/test/java/tests/i3863/C3863.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

modules/jooby-apt/src/test/java/tests/i3863/Issue3863.java

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,71 @@
77

88
import static org.assertj.core.api.Assertions.assertThat;
99

10+
import org.junit.Assert;
1011
import org.junit.jupiter.api.Test;
1112

1213
import io.jooby.apt.ProcessorRunner;
1314

1415
public class Issue3863 {
1516
@Test
16-
public void shouldGenerateTrpcService() throws Exception {
17-
new ProcessorRunner(new C3863())
17+
public void trpcOnTopLevelDoesNothingByItSelf() throws Exception {
18+
new ProcessorRunner(new TopLevelTrpcDoesNothingByItSelf()).withSourceCode(Assert::assertNull);
19+
}
20+
21+
@Test
22+
public void trpcQuery() throws Exception {
23+
new ProcessorRunner(new SpecificTrpcAnnotation())
24+
.withSourceCode(
25+
source -> {
26+
assertThat(source)
27+
.contains("app.get(\"/trpc/users.getUserById\", this::trpcGetUserById);")
28+
.contains("app.post(\"/trpc/users.createUser\", this::trpcCreateUser);");
29+
});
30+
}
31+
32+
@Test
33+
public void mixedAnnotation() throws Exception {
34+
new ProcessorRunner(new MixedTrpcAnnotation())
35+
.withSourceCode(
36+
source -> {
37+
assertThat(source)
38+
// tRPC
39+
.contains("app.get(\"/trpc/users.getUserById\", this::trpcGetUserById);")
40+
.contains("app.post(\"/trpc/users.createUser\", this::trpcCreateUser);")
41+
// REST
42+
.contains("app.get(\"/api/users/{id}\", this::getUserById);")
43+
.contains("app.post(\"/api/users\", this::createUser);");
44+
});
45+
}
46+
47+
@Test
48+
public void mixedMutation() throws Exception {
49+
new ProcessorRunner(new MixedMutation())
50+
.withSourceCode(
51+
source -> {
52+
assertThat(source)
53+
// tRPC
54+
.contains("app.post(\"/trpc/users.createUser\", this::trpcCreateUser);")
55+
.contains("app.post(\"/trpc/users.updateUser\", this::trpcUpdateUser);")
56+
.contains("app.post(\"/trpc/users.patchUser\", this::trpcPatchUser);")
57+
.contains("app.post(\"/trpc/users.deleteUser\", this::trpcDeleteUser);")
58+
// REST
59+
.contains("app.post(\"/\", this::createUser);")
60+
.contains("app.put(\"/\", this::updateUser);")
61+
.contains("app.patch(\"/\", this::patchUser);")
62+
.contains("app.delete(\"/\", this::deleteUser);");
63+
});
64+
}
65+
66+
@Test
67+
public void overloadTrpc() throws Exception {
68+
new ProcessorRunner(new OverloadTrpc())
1869
.withSourceCode(
1970
source -> {
2071
assertThat(source)
21-
.contains("app.get(\"/trpc/users.ping\", this::trpcPingInteger);")
72+
// tRPC
2273
.contains("app.get(\"/trpc/users.ping\", this::trpcPing);")
23-
.contains(
24-
"public io.jooby.trpc.TrpcResponse<java.util.List<tests.i3863.U3863>>"
25-
+ " trpcMultipleSimpleArgs(io.jooby.Context ctx) throws Exception {");
74+
.contains("app.get(\"/trpc/users.ping\", this::trpcPingInteger);");
2675
});
2776
}
2877
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3863;
7+
8+
import io.jooby.annotation.*;
9+
10+
@Trpc("users")
11+
public class MixedMutation {
12+
13+
@Trpc
14+
@POST
15+
public U3863 createUser(U3863 payload) {
16+
return null;
17+
}
18+
19+
@Trpc
20+
@PUT
21+
public U3863 updateUser(U3863 payload) {
22+
return null;
23+
}
24+
25+
@Trpc
26+
@PATCH
27+
public U3863 patchUser(U3863 payload) {
28+
return null;
29+
}
30+
31+
@Trpc
32+
@DELETE
33+
public void deleteUser(U3863 payload) {}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3863;
7+
8+
import io.jooby.Context;
9+
import io.jooby.annotation.*;
10+
11+
@Trpc("users")
12+
@Path("/api/users")
13+
public class MixedTrpcAnnotation {
14+
15+
@Trpc
16+
@GET("/{id}")
17+
public U3863 getUserById(Context ctx, @PathParam long id) {
18+
return null;
19+
}
20+
21+
@Trpc
22+
@POST
23+
public U3863 createUser(U3863 payload) {
24+
return null;
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3863;
7+
8+
import io.jooby.annotation.Trpc;
9+
10+
@Trpc("users")
11+
public class OverloadTrpc {
12+
13+
@Trpc.Query
14+
public String ping() {
15+
return null;
16+
}
17+
18+
@Trpc.Query
19+
public String ping(Integer since) {
20+
return null;
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3863;
7+
8+
import io.jooby.Context;
9+
import io.jooby.annotation.Trpc;
10+
11+
@Trpc("users")
12+
public class SpecificTrpcAnnotation {
13+
14+
@Trpc.Query
15+
public U3863 getUserById(Context ctx, long id) {
16+
return null;
17+
}
18+
19+
@Trpc.Mutation
20+
public U3863 createUser(U3863 payload) {
21+
return null;
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3863;
7+
8+
import io.jooby.Context;
9+
import io.jooby.annotation.Trpc;
10+
11+
@Trpc("users")
12+
public class TopLevelTrpcDoesNothingByItSelf {
13+
14+
public String ping(Integer year) {
15+
return null;
16+
}
17+
18+
public U3863 findUser(Context ctx, long id) {
19+
return null;
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3864;
7+
8+
import io.jooby.annotation.JsonRpc;
9+
10+
@JsonRpc("movies")
11+
public class CustomNaming {
12+
13+
@JsonRpc("getById")
14+
public String rpcMethod1() {
15+
return "1";
16+
}
17+
18+
@JsonRpc("create")
19+
public String rpcMethod2() {
20+
return rpcMethod3();
21+
}
22+
23+
private String rpcMethod3() {
24+
return "1";
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3864;
7+
8+
import io.jooby.annotation.JsonRpc;
9+
import jakarta.inject.Inject;
10+
11+
@JsonRpc
12+
public class DIService {
13+
14+
@Inject
15+
public DIService(CustomNaming value) {}
16+
17+
public String rpcMethod1() {
18+
return "1";
19+
}
20+
}

0 commit comments

Comments
 (0)