Skip to content

Commit b105cb4

Browse files
author
Alex Davies-Moore
committed
Added tests around route generation
This commits adds a set of tests around custom context specification as part of the generator arguments.
1 parent dc63c20 commit b105cb4

File tree

8 files changed

+79
-2
lines changed

8 files changed

+79
-2
lines changed

plugin/src/main/java/com/flit/protoc/gen/server/undertow/RpcGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class RpcGenerator extends BaseGenerator {
2121
this.context = "/twirp";
2222
} else {
2323
context = context.trim();
24-
if (context.equals("")) {
24+
if (context.equals("") || context.equals("/")) {
2525
// empty route - i.e. top level "/"
26-
this.context = context;
26+
this.context = "";
2727
} else if (context.startsWith("/")) {
2828
this.context = context;
2929
} else {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.flit.protoc.gen.server.undertow;
2+
3+
import com.flit.protoc.Plugin;
4+
import com.google.protobuf.compiler.PluginProtos;
5+
import org.junit.Test;
6+
7+
import java.util.Map;
8+
import java.util.function.Function;
9+
import java.util.stream.Collectors;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertNotNull;
13+
import static org.junit.Assert.assertTrue;
14+
15+
/**
16+
* Tests the generation of a service that has core definition imported from another file
17+
*/
18+
public class ContextGeneratorTest extends BaseGeneratorTest {
19+
20+
@Test
21+
public void test_GenerateWithMissingRoot() throws Exception {
22+
test_Route("context.missing.undertow.bin", "/twirp/com.example.context.NullService");
23+
}
24+
25+
@Test
26+
public void test_GenerateWithEmptyRoot() throws Exception {
27+
test_Route("context.empty.undertow.bin", "/twirp/com.example.context.NullService");
28+
}
29+
30+
@Test
31+
public void test_GenerateWithSlashOnlyRoot() throws Exception {
32+
test_Route("context.slash.undertow.bin", "/com.example.context.NullService");
33+
}
34+
35+
@Test
36+
public void test_GenerateWithSlashRoot() throws Exception {
37+
test_Route("context.root.undertow.bin", "/root/com.example.context.NullService");
38+
}
39+
40+
@Test
41+
public void test_GenerateWithNameRoot() throws Exception {
42+
test_Route("context.name.undertow.bin", "/fibble/com.example.context.NullService");
43+
}
44+
45+
private void test_Route(String file, String route) throws Exception {
46+
PluginProtos.CodeGeneratorRequest request = load(file);
47+
48+
Plugin plugin = new Plugin(request);
49+
PluginProtos.CodeGeneratorResponse response = plugin.process();
50+
51+
assertNotNull(response);
52+
assertEquals(2, response.getFileCount());
53+
54+
Map<String, PluginProtos.CodeGeneratorResponse.File> files = response.getFileList().stream().collect(Collectors.toMap(
55+
PluginProtos.CodeGeneratorResponse.File::getName,
56+
Function.identity()
57+
));
58+
59+
assertTrue(files.containsKey("com/example/context/rpc/RpcNullServiceService.java"));
60+
assertTrue(files.containsKey("com/example/context/rpc/RpcNullServiceHandler.java"));
61+
62+
assertTrue(files.get("com/example/context/rpc/RpcNullServiceHandler.java").getContent().contains(
63+
String.format("public static final String ROUTE = \"%s\";", route)
64+
));
65+
}
66+
67+
}
Binary file not shown.
Binary file not shown.
449 Bytes
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
syntax = "proto3";
2+
3+
package com.example.context;
4+
option java_package = "com.example.context.rpc";
5+
6+
message Empty {}
7+
8+
service NullService {
9+
rpc SayNull(Empty) returns (Empty);
10+
}
448 Bytes
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)