Skip to content

Commit df73a01

Browse files
committed
Makes gRPC examples consistent in the way the use generated code.
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
1 parent c7f10da commit df73a01

File tree

6 files changed

+52
-49
lines changed

6 files changed

+52
-49
lines changed

examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringService.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.stream.Collectors;
2020
import java.util.stream.Stream;
2121

22+
import io.helidon.examples.microprofile.grpc.Strings.StringMessage;
2223
import io.helidon.grpc.api.Grpc;
2324
import io.helidon.grpc.core.CollectingObserver;
2425

@@ -50,8 +51,8 @@ public Descriptors.FileDescriptor proto() {
5051
* @return string message
5152
*/
5253
@Grpc.Unary("Upper")
53-
public Strings.StringMessage upper(Strings.StringMessage request) {
54-
return newMessage(request.getText().toUpperCase());
54+
public StringMessage upper(StringMessage request) {
55+
return newStringMessage(request.getText().toUpperCase());
5556
}
5657

5758
/**
@@ -61,8 +62,8 @@ public Strings.StringMessage upper(Strings.StringMessage request) {
6162
* @return string message
6263
*/
6364
@Grpc.Unary("Lower")
64-
public Strings.StringMessage lower(Strings.StringMessage request) {
65-
return newMessage(request.getText().toLowerCase());
65+
public StringMessage lower(StringMessage request) {
66+
return newStringMessage(request.getText().toLowerCase());
6667
}
6768

6869
/**
@@ -72,9 +73,9 @@ public Strings.StringMessage lower(Strings.StringMessage request) {
7273
* @return stream of string messages
7374
*/
7475
@Grpc.ServerStreaming("Split")
75-
public Stream<Strings.StringMessage> split(Strings.StringMessage request) {
76+
public Stream<StringMessage> split(StringMessage request) {
7677
String[] parts = request.getText().split(" ");
77-
return Stream.of(parts).map(this::newMessage);
78+
return Stream.of(parts).map(this::newStringMessage);
7879
}
7980

8081
/**
@@ -84,16 +85,16 @@ public Stream<Strings.StringMessage> split(Strings.StringMessage request) {
8485
* @return single message as a stream
8586
*/
8687
@Grpc.ClientStreaming("Join")
87-
public StreamObserver<Strings.StringMessage> join(StreamObserver<Strings.StringMessage> observer) {
88+
public StreamObserver<StringMessage> join(StreamObserver<StringMessage> observer) {
8889
return CollectingObserver.create(
8990
Collectors.joining(" "),
9091
observer,
91-
Strings.StringMessage::getText,
92-
this::newMessage);
92+
StringMessage::getText,
93+
this::newStringMessage);
9394
}
9495

95-
private Strings.StringMessage newMessage(String text) {
96-
return Strings.StringMessage.newBuilder().setText(text).build();
96+
private StringMessage newStringMessage(String text) {
97+
return StringMessage.newBuilder().setText(text).build();
9798
}
9899
}
99100

examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringServiceClient.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2024, 2025 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.stream.Stream;
2020

21+
import io.helidon.examples.microprofile.grpc.Strings.StringMessage;
2122
import io.helidon.grpc.api.Grpc;
2223

2324
import io.grpc.stub.StreamObserver;
@@ -36,7 +37,7 @@ public interface StringServiceClient {
3637
* @return string message
3738
*/
3839
@Grpc.Unary("Upper")
39-
Strings.StringMessage upper(Strings.StringMessage request);
40+
StringMessage upper(StringMessage request);
4041

4142
/**
4243
* Lowercase a string.
@@ -45,7 +46,7 @@ public interface StringServiceClient {
4546
* @return string message
4647
*/
4748
@Grpc.Unary("Lower")
48-
Strings.StringMessage lower(Strings.StringMessage request);
49+
StringMessage lower(StringMessage request);
4950

5051
/**
5152
* Split a string using space delimiters.
@@ -54,7 +55,7 @@ public interface StringServiceClient {
5455
* @return stream of string messages
5556
*/
5657
@Grpc.ServerStreaming("Split")
57-
Stream<Strings.StringMessage> split(Strings.StringMessage request);
58+
Stream<StringMessage> split(StringMessage request);
5859

5960
/**
6061
* Join a stream of messages using spaces.
@@ -63,6 +64,6 @@ public interface StringServiceClient {
6364
* @return single message as a stream
6465
*/
6566
@Grpc.ClientStreaming("Join")
66-
StreamObserver<Strings.StringMessage> join(StreamObserver<Strings.StringMessage> observer);
67+
StreamObserver<StringMessage> join(StreamObserver<StringMessage> observer);
6768
}
6869

examples/microprofile/grpc/src/main/resources/application.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ server:
3030
resource-path: "server.p12"
3131
features:
3232
grpc-reflection:
33-
enabled: true
33+
enabled: false
3434

3535
grpc:
3636
client:
3737
channels:
3838
- name: "string-channel"
39-
port: 0
4039
tls:
4140
trust:
4241
keystore:

examples/microprofile/grpc/src/test/java/io/helidon/examples/microprofile/grpc/StringServiceTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2024, 2025 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import io.helidon.grpc.api.Grpc;
2525
import io.helidon.microprofile.grpc.client.GrpcConfigurablePort;
2626
import io.helidon.microprofile.testing.junit5.HelidonTest;
27+
import io.helidon.examples.microprofile.grpc.Strings.StringMessage;
2728

2829
import io.grpc.stub.StreamObserver;
2930
import jakarta.inject.Inject;
@@ -55,33 +56,33 @@ void updatePort() {
5556

5657
@Test
5758
void testUnaryUpper() {
58-
Strings.StringMessage res = client.upper(newMessage("hello"));
59+
StringMessage res = client.upper(newStringMessage("hello"));
5960
assertThat(res.getText(), is("HELLO"));
6061
}
6162

6263
@Test
6364
void testUnaryLower() {
64-
Strings.StringMessage res = client.lower(newMessage("HELLO"));
65+
StringMessage res = client.lower(newStringMessage("HELLO"));
6566
assertThat(res.getText(), is("hello"));
6667
}
6768

6869
@Test
6970
void testServerStreamingSplit() {
70-
Stream<Strings.StringMessage> stream = client.split(newMessage("hello world"));
71-
List<Strings.StringMessage> value = stream.toList();
71+
Stream<StringMessage> stream = client.split(newStringMessage("hello world"));
72+
List<StringMessage> value = stream.toList();
7273
assertThat(value, hasSize(2));
73-
assertThat(value, contains(newMessage("hello"), newMessage("world")));
74+
assertThat(value, contains(newStringMessage("hello"), newStringMessage("world")));
7475
}
7576

7677
@Test
7778
void testClientStreamingJoin() throws InterruptedException {
78-
ListObserver<Strings.StringMessage> response = new ListObserver<>();
79-
StreamObserver<Strings.StringMessage> request = client.join(response);
80-
request.onNext(newMessage("hello"));
81-
request.onNext(newMessage("world"));
79+
ListObserver<StringMessage> response = new ListObserver<>();
80+
StreamObserver<StringMessage> request = client.join(response);
81+
request.onNext(newStringMessage("hello"));
82+
request.onNext(newStringMessage("world"));
8283
request.onCompleted();
83-
List<Strings.StringMessage> value = response.value();
84-
assertThat(value.getFirst(), is(newMessage("hello world")));
84+
List<StringMessage> value = response.value();
85+
assertThat(value.getFirst(), is(newStringMessage("hello world")));
8586
}
8687

8788
/**
@@ -90,8 +91,8 @@ void testClientStreamingJoin() throws InterruptedException {
9091
* @param data the string
9192
* @return the string message
9293
*/
93-
Strings.StringMessage newMessage(String data) {
94-
return Strings.StringMessage.newBuilder().setText(data).build();
94+
StringMessage newStringMessage(String data) {
95+
return StringMessage.newBuilder().setText(data).build();
9596
}
9697

9798
/**

examples/webserver/grpc/src/main/java/io/helidon/examples/webserver/grpc/StringService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2024, 2025 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,15 +62,15 @@ private void lower(StringMessage request, StreamObserver<StringMessage> observer
6262

6363
private void split(StringMessage request, StreamObserver<StringMessage> observer) {
6464
String[] parts = request.getText().split(" ");
65-
stream(observer, Stream.of(parts).map(this::response));
65+
stream(observer, Stream.of(parts).map(this::newStringMessage));
6666
}
6767

6868
private StreamObserver<StringMessage> join(StreamObserver<StringMessage> observer) {
6969
return CollectingObserver.create(
7070
Collectors.joining(" "),
7171
observer,
7272
StringMessage::getText,
73-
this::response);
73+
this::newStringMessage);
7474
}
7575

7676
private StreamObserver<Strings.StringMessage> echo(StreamObserver<Strings.StringMessage> observer) {
@@ -92,7 +92,7 @@ public void onCompleted() {
9292
};
9393
}
9494

95-
private StringMessage response(String text) {
95+
private StringMessage newStringMessage(String text) {
9696
return StringMessage.newBuilder().setText(text).build();
9797
}
9898
}

examples/webserver/grpc/src/test/java/io/helidon/examples/webserver/grpc/StringServiceTest.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2024, 2025 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
import io.helidon.webserver.grpc.GrpcRouting;
3636
import io.helidon.webserver.testing.junit5.ServerTest;
3737
import io.helidon.webserver.testing.junit5.SetUpRoute;
38+
import io.helidon.examples.webserver.grpc.Strings.StringMessage;
3839

3940
import io.grpc.Channel;
4041
import io.grpc.stub.StreamObserver;
@@ -77,23 +78,23 @@ static void routing(Router.RouterBuilder<?> router) {
7778
void testUnaryUpper() {
7879
GrpcClient grpcClient = webClient.client(GrpcClient.PROTOCOL);
7980
StringServiceGrpc.StringServiceBlockingStub service = StringServiceGrpc.newBlockingStub(grpcClient.channel());
80-
Strings.StringMessage res = service.upper(newStringMessage("hello"));
81+
StringMessage res = service.upper(newStringMessage("hello"));
8182
assertThat(res.getText(), is("HELLO"));
8283
}
8384

8485
@Test
8586
void testUnaryLower() {
8687
GrpcClient grpcClient = webClient.client(GrpcClient.PROTOCOL);
8788
StringServiceGrpc.StringServiceBlockingStub service = StringServiceGrpc.newBlockingStub(grpcClient.channel());
88-
Strings.StringMessage res = service.lower(newStringMessage("HELLO"));
89+
StringMessage res = service.lower(newStringMessage("HELLO"));
8990
assertThat(res.getText(), is("hello"));
9091
}
9192

9293
@Test
9394
void testServerStreamingSplit() {
9495
GrpcClient grpcClient = webClient.client(GrpcClient.PROTOCOL);
9596
StringServiceGrpc.StringServiceBlockingStub service = StringServiceGrpc.newBlockingStub(grpcClient.channel());
96-
Iterator<Strings.StringMessage> res = service.split(newStringMessage("hello world"));
97+
Iterator<StringMessage> res = service.split(newStringMessage("hello world"));
9798
assertThat(res.next().getText(), is("hello"));
9899
assertThat(res.next().getText(), is("world"));
99100
assertThat(res.hasNext(), is(false));
@@ -103,25 +104,25 @@ void testServerStreamingSplit() {
103104
void testClientStreamingJoin() throws ExecutionException, InterruptedException, TimeoutException {
104105
GrpcClient grpcClient = webClient.client(GrpcClient.PROTOCOL);
105106
StringServiceGrpc.StringServiceStub service = StringServiceGrpc.newStub(grpcClient.channel());
106-
CompletableFuture<Strings.StringMessage> future = new CompletableFuture<>();
107-
StreamObserver<Strings.StringMessage> req = service.join(singleStreamObserver(future));
107+
CompletableFuture<StringMessage> future = new CompletableFuture<>();
108+
StreamObserver<StringMessage> req = service.join(singleStreamObserver(future));
108109
req.onNext(newStringMessage("hello"));
109110
req.onNext(newStringMessage("world"));
110111
req.onCompleted();
111-
Strings.StringMessage res = future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
112+
StringMessage res = future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
112113
assertThat(res.getText(), is("hello world"));
113114
}
114115

115116
@Test
116117
void testBidirectionalEcho() throws ExecutionException, InterruptedException, TimeoutException {
117118
GrpcClient grpcClient = webClient.client(GrpcClient.PROTOCOL);
118119
StringServiceGrpc.StringServiceStub service = StringServiceGrpc.newStub(grpcClient.channel());
119-
CompletableFuture<Iterator<Strings.StringMessage>> future = new CompletableFuture<>();
120-
StreamObserver<Strings.StringMessage> req = service.echo(multiStreamObserver(future));
120+
CompletableFuture<Iterator<StringMessage>> future = new CompletableFuture<>();
121+
StreamObserver<StringMessage> req = service.echo(multiStreamObserver(future));
121122
req.onNext(newStringMessage("hello"));
122123
req.onNext(newStringMessage("world"));
123124
req.onCompleted();
124-
Iterator<Strings.StringMessage> res = future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
125+
Iterator<StringMessage> res = future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
125126
assertThat(res.next().getText(), is("hello"));
126127
assertThat(res.next().getText(), is("world"));
127128
assertThat(res.hasNext(), is(false));
@@ -132,7 +133,7 @@ void testUnaryUpperInterceptor() {
132133
GrpcClient grpcClient = webClient.client(GrpcClient.PROTOCOL);
133134
Channel channel = grpcClient.channel(new StringServiceInterceptor());
134135
StringServiceGrpc.StringServiceBlockingStub service = StringServiceGrpc.newBlockingStub(channel);
135-
Strings.StringMessage res = service.upper(newStringMessage("hello"));
136+
StringMessage res = service.upper(newStringMessage("hello"));
136137
assertThat(res.getText(), is("[[HELLO]]"));
137138
}
138139

@@ -149,8 +150,8 @@ void testHealthHttp() {
149150
}
150151
}
151152

152-
static Strings.StringMessage newStringMessage(String data) {
153-
return Strings.StringMessage.newBuilder().setText(data).build();
153+
static StringMessage newStringMessage(String data) {
154+
return StringMessage.newBuilder().setText(data).build();
154155
}
155156

156157
static <ReqT> StreamObserver<ReqT> singleStreamObserver(CompletableFuture<ReqT> future) {

0 commit comments

Comments
 (0)