This is an automated email from the ASF dual-hosted git repository.

engelen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko-grpc.git


The following commit(s) were added to refs/heads/main by this push:
     new 865cef14 Refactoring code in plugin-tester-java(scala) (#288)
865cef14 is described below

commit 865cef14fe9898a5eaac6c23284fe26dd760a200
Author: Laglangyue <[email protected]>
AuthorDate: Thu Apr 25 18:21:41 2024 +0800

    Refactoring code in plugin-tester-java(scala) (#288)
    
    * clean up some code
    
    * clean up some code in greeterServer
    
    * update powerGreeterService
    
    * update doc
---
 docs/src/main/paradox/server/details.md                      |  4 ++--
 .../java/io/grpc/testing/integration2/TestServiceClient.java |  8 ++++----
 .../src/main/java/example/myapp/CombinedServer.java          |  8 ++------
 .../main/java/example/myapp/helloworld/GreeterClient.java    | 11 ++++-------
 .../main/java/example/myapp/helloworld/GreeterServer.java    |  4 +---
 .../java/example/myapp/helloworld/GreeterServiceImpl.java    |  9 +++------
 .../java/example/myapp/helloworld/LiftedGreeterClient.java   | 11 ++++-------
 .../myapp/helloworld/LoggingErrorHandlingGreeterServer.java  |  4 +---
 .../java/example/myapp/helloworld/PowerGreeterServer.java    |  6 ++----
 ...ServicePowerApiImpl.java => PowerGreeterServiceImpl.java} | 12 ++++--------
 .../scala/example/myapp/helloworld/GreeterServiceSpec.scala  |  2 +-
 11 files changed, 28 insertions(+), 51 deletions(-)

diff --git a/docs/src/main/paradox/server/details.md 
b/docs/src/main/paradox/server/details.md
index 6c979919..839a185a 100644
--- a/docs/src/main/paradox/server/details.md
+++ b/docs/src/main/paradox/server/details.md
@@ -16,10 +16,10 @@ generated client stubs.
 Here's an example implementation of these server power APIs:
 
 Scala
-:  @@snip 
[GreeterServicePowerApiImpl.scala](/plugin-tester-scala/src/main/scala/example/myapp/helloworld/PowerGreeterServiceImpl.scala)
 { #full-service-impl }
+:  @@snip 
[PowerGreeterServiceImpl.scala](/plugin-tester-scala/src/main/scala/example/myapp/helloworld/PowerGreeterServiceImpl.scala)
 { #full-service-impl }
 
 Java
-:  @@snip 
[GreeterServicePowerApiImpl.java](/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServicePowerApiImpl.java)
 { #full-service-impl }
+:  @@snip 
[PowerGreeterServiceImpl.java](/plugin-tester-java/src/main/java/example/myapp/helloworld/PowerGreeterServiceImpl.java)
 { #full-service-impl }
 
 ## Status codes
 
diff --git 
a/interop-tests/src/main/java/io/grpc/testing/integration2/TestServiceClient.java
 
b/interop-tests/src/main/java/io/grpc/testing/integration2/TestServiceClient.java
index 3b0df682..9fa4d337 100644
--- 
a/interop-tests/src/main/java/io/grpc/testing/integration2/TestServiceClient.java
+++ 
b/interop-tests/src/main/java/io/grpc/testing/integration2/TestServiceClient.java
@@ -156,7 +156,7 @@ public class TestServiceClient {
           String jsonKey =
               Files.asCharSource(new 
File(settings.getServiceAccountKeyFile()), UTF_8).read();
           FileInputStream credentialsStream =
-              new FileInputStream(new 
File(settings.getServiceAccountKeyFile()));
+              new FileInputStream(settings.getServiceAccountKeyFile());
           clientTester.serviceAccountCreds(jsonKey, credentialsStream, 
settings.getOauthScope());
           break;
         }
@@ -164,7 +164,7 @@ public class TestServiceClient {
       case JWT_TOKEN_CREDS:
         {
           FileInputStream credentialsStream =
-              new FileInputStream(new 
File(settings.getServiceAccountKeyFile()));
+              new FileInputStream(settings.getServiceAccountKeyFile());
           clientTester.jwtTokenCreds(credentialsStream);
           break;
         }
@@ -174,7 +174,7 @@ public class TestServiceClient {
           String jsonKey =
               Files.asCharSource(new 
File(settings.getServiceAccountKeyFile()), UTF_8).read();
           FileInputStream credentialsStream =
-              new FileInputStream(new 
File(settings.getServiceAccountKeyFile()));
+              new FileInputStream(settings.getServiceAccountKeyFile());
           clientTester.oauth2AuthToken(jsonKey, credentialsStream, 
settings.getOauthScope());
           break;
         }
@@ -184,7 +184,7 @@ public class TestServiceClient {
           String jsonKey =
               Files.asCharSource(new 
File(settings.getServiceAccountKeyFile()), UTF_8).read();
           FileInputStream credentialsStream =
-              new FileInputStream(new 
File(settings.getServiceAccountKeyFile()));
+              new FileInputStream(settings.getServiceAccountKeyFile());
           clientTester.perRpcCreds(jsonKey, credentialsStream, 
settings.getOauthScope());
           break;
         }
diff --git a/plugin-tester-java/src/main/java/example/myapp/CombinedServer.java 
b/plugin-tester-java/src/main/java/example/myapp/CombinedServer.java
index 04b38df0..96d03d81 100644
--- a/plugin-tester-java/src/main/java/example/myapp/CombinedServer.java
+++ b/plugin-tester-java/src/main/java/example/myapp/CombinedServer.java
@@ -62,9 +62,7 @@ class CombinedServer {
           .newServerAt("127.0.0.1", 8090)
           .bind(serviceHandlers)
       //#concatOrNotFound
-      .thenAccept(binding -> {
-        System.out.println("gRPC server bound to: " + binding.localAddress());
-      });
+      .thenAccept(binding -> System.out.println("gRPC server bound to: " + 
binding.localAddress()));
 
       //#grpc-web
       Function<HttpRequest, CompletionStage<HttpResponse>> 
grpcWebServiceHandlers =
@@ -74,9 +72,7 @@ class CombinedServer {
         .newServerAt("127.0.0.1", 8090)
         .bind(grpcWebServiceHandlers)
       //#grpc-web
-      .thenAccept(binding -> {
-          System.out.println("gRPC-Web server bound to: " + 
binding.localAddress());
-      });
+      .thenAccept(binding -> System.out.println("gRPC-Web server bound to: " + 
binding.localAddress()));
 
   }
 }
diff --git 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterClient.java 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterClient.java
index dc865dd8..e19f2394 100644
--- 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterClient.java
+++ 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterClient.java
@@ -17,25 +17,22 @@ package example.myapp.helloworld;
 import example.myapp.helloworld.grpc.*;
 import io.grpc.StatusRuntimeException;
 import java.time.Duration;
-import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.CompletionStage;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.apache.pekko.Done;
 import org.apache.pekko.NotUsed;
 import org.apache.pekko.actor.ActorSystem;
 import org.apache.pekko.grpc.GrpcClientSettings;
+import org.apache.pekko.japi.Pair;
 import org.apache.pekko.stream.Materializer;
 import org.apache.pekko.stream.SystemMaterializer;
 import org.apache.pekko.stream.javadsl.Source;
 
 class GreeterClient {
   public static void main(String[] args) throws Exception {
-
-    String serverHost = "127.0.0.1";
-    int serverPort = 8090;
-
     ActorSystem system = ActorSystem.create("HelloWorldClient");
     Materializer materializer = SystemMaterializer.get(system).materializer();
 
@@ -73,7 +70,7 @@ class GreeterClient {
 
   private static void streamingRequest(GreeterService client) throws Exception 
{
     List<HelloRequest> requests =
-        Arrays.asList("Alice", "Bob", "Peter").stream()
+        Stream.of("Alice", "Bob", "Peter")
             .map(name -> HelloRequest.newBuilder().setName(name).build())
             .collect(Collectors.toList());
     CompletionStage<HelloReply> reply = 
client.itKeepsTalking(Source.from(requests));
@@ -98,7 +95,7 @@ class GreeterClient {
     Source<HelloRequest, NotUsed> requestStream =
         Source.tick(interval, interval, "tick")
             .zipWithIndex()
-            .map(pair -> pair.second())
+            .map(Pair::second)
             .map(i -> HelloRequest.newBuilder().setName("Alice-" + i).build())
             .take(10)
             .mapMaterializedValue(m -> NotUsed.getInstance());
diff --git 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServer.java 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServer.java
index dffa16cd..9542b3e7 100644
--- 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServer.java
+++ 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServer.java
@@ -37,9 +37,7 @@ class GreeterServer {
 
     run(sys)
         .thenAccept(
-            binding -> {
-              System.out.println("gRPC server bound to: " + 
binding.localAddress());
-            });
+            binding -> System.out.println("gRPC server bound to: " + 
binding.localAddress()));
 
     // ActorSystem threads will keep the app alive until `system.terminate()` 
is called
   }
diff --git 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServiceImpl.java
 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServiceImpl.java
index b8fab657..adb6370b 100644
--- 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServiceImpl.java
+++ 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServiceImpl.java
@@ -46,12 +46,12 @@ public class GreeterServiceImpl implements GreeterService {
   @Override
   public CompletionStage<HelloReply> itKeepsTalking(Source<HelloRequest, 
NotUsed> in) {
     System.out.println("sayHello to in stream...");
-    return in.runWith(Sink.seq(), mat)
+    return in.runWith(Sink.<HelloRequest>seq(), mat)
         .thenApply(
             elements -> {
               String elementsStr =
                   elements.stream()
-                      .map(elem -> elem.getName())
+                      .map(HelloRequest::getName)
                       .collect(Collectors.toList())
                       .toString();
               return HelloReply.newBuilder().setMessage("Hello, " + 
elementsStr).build();
@@ -64,10 +64,7 @@ public class GreeterServiceImpl implements GreeterService {
     List<Character> characters =
         ("Hello, " + in.getName()).chars().mapToObj(c -> (char) 
c).collect(Collectors.toList());
     return Source.from(characters)
-        .map(
-            character -> {
-              return 
HelloReply.newBuilder().setMessage(String.valueOf(character)).build();
-            });
+        .map(character -> 
HelloReply.newBuilder().setMessage(String.valueOf(character)).build());
   }
 
   @Override
diff --git 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/LiftedGreeterClient.java
 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/LiftedGreeterClient.java
index 2702d3a4..bac878bc 100644
--- 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/LiftedGreeterClient.java
+++ 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/LiftedGreeterClient.java
@@ -16,25 +16,22 @@ package example.myapp.helloworld;
 import example.myapp.helloworld.grpc.*;
 import io.grpc.StatusRuntimeException;
 import java.time.Duration;
-import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.CompletionStage;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.apache.pekko.Done;
 import org.apache.pekko.NotUsed;
 import org.apache.pekko.actor.ActorSystem;
 import org.apache.pekko.grpc.GrpcClientSettings;
+import org.apache.pekko.japi.Pair;
 import org.apache.pekko.stream.Materializer;
 import org.apache.pekko.stream.SystemMaterializer;
 import org.apache.pekko.stream.javadsl.Source;
 
 class LiftedGreeterClient {
   public static void main(String[] args) throws Exception {
-
-    String serverHost = "127.0.0.1";
-    int serverPort = 8090;
-
     ActorSystem system = ActorSystem.create("HelloWorldClient");
     Materializer materializer = SystemMaterializer.get(system).materializer();
 
@@ -68,7 +65,7 @@ class LiftedGreeterClient {
 
   private static void streamingRequest(GreeterServiceClient client) throws 
Exception {
     List<HelloRequest> requests =
-        Arrays.asList("Alice", "Bob", "Peter").stream()
+        Stream.of("Alice", "Bob", "Peter")
             .map(name -> HelloRequest.newBuilder().setName(name).build())
             .collect(Collectors.toList());
     CompletionStage<HelloReply> reply =
@@ -96,7 +93,7 @@ class LiftedGreeterClient {
     Source<HelloRequest, NotUsed> requestStream =
         Source.tick(interval, interval, "tick")
             .zipWithIndex()
-            .map(pair -> pair.second())
+            .map(Pair::second)
             .map(i -> HelloRequest.newBuilder().setName("Alice-" + i).build())
             .take(10)
             .mapMaterializedValue(m -> NotUsed.getInstance());
diff --git 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/LoggingErrorHandlingGreeterServer.java
 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/LoggingErrorHandlingGreeterServer.java
index 8ccf02de..2b914c76 100644
--- 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/LoggingErrorHandlingGreeterServer.java
+++ 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/LoggingErrorHandlingGreeterServer.java
@@ -50,9 +50,7 @@ public class LoggingErrorHandlingGreeterServer {
 
     run(sys)
         .thenAccept(
-            binding -> {
-              System.out.println("gRPC server bound to: " + 
binding.localAddress());
-            });
+            binding -> System.out.println("gRPC server bound to: " + 
binding.localAddress()));
 
     // ActorSystem threads will keep the app alive until `system.terminate()` 
is called
   }
diff --git 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/PowerGreeterServer.java
 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/PowerGreeterServer.java
index a588f5fe..6570bec0 100644
--- 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/PowerGreeterServer.java
+++ 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/PowerGreeterServer.java
@@ -37,9 +37,7 @@ class PowerGreeterServer {
 
     run(sys)
         .thenAccept(
-            binding -> {
-              System.out.println("gRPC server bound to: " + 
binding.localAddress());
-            });
+            binding -> System.out.println("gRPC server bound to: " + 
binding.localAddress()));
 
     // ActorSystem threads will keep the app alive until `system.terminate()` 
is called
   }
@@ -48,7 +46,7 @@ class PowerGreeterServer {
     Materializer mat = SystemMaterializer.get(sys).materializer();
 
     // Instantiate implementation
-    GreeterServicePowerApi impl = new GreeterServicePowerApiImpl(mat);
+    GreeterServicePowerApi impl = new PowerGreeterServiceImpl(mat);
 
     return Http.get(sys)
         .newServerAt("127.0.0.1", 8091)
diff --git 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServicePowerApiImpl.java
 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/PowerGreeterServiceImpl.java
similarity index 89%
rename from 
plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServicePowerApiImpl.java
rename to 
plugin-tester-java/src/main/java/example/myapp/helloworld/PowerGreeterServiceImpl.java
index d358de63..c29e2210 100644
--- 
a/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServicePowerApiImpl.java
+++ 
b/plugin-tester-java/src/main/java/example/myapp/helloworld/PowerGreeterServiceImpl.java
@@ -27,10 +27,10 @@ import org.apache.pekko.stream.Materializer;
 import org.apache.pekko.stream.javadsl.Sink;
 import org.apache.pekko.stream.javadsl.Source;
 
-public class GreeterServicePowerApiImpl implements GreeterServicePowerApi {
+public class PowerGreeterServiceImpl implements GreeterServicePowerApi {
   private final Materializer mat;
 
-  public GreeterServicePowerApiImpl(Materializer mat) {
+  public PowerGreeterServiceImpl(Materializer mat) {
     this.mat = mat;
   }
 
@@ -46,7 +46,7 @@ public class GreeterServicePowerApiImpl implements 
GreeterServicePowerApi {
   public CompletionStage<HelloReply> itKeepsTalking(
       Source<HelloRequest, NotUsed> in, Metadata metadata) {
     System.out.println("sayHello to in stream...");
-    return in.runWith(Sink.seq(), mat)
+    return in.runWith(Sink.<HelloRequest>seq(), mat)
         .thenApply(
             elements -> {
               String elementsStr =
@@ -65,10 +65,7 @@ public class GreeterServicePowerApiImpl implements 
GreeterServicePowerApi {
     List<Character> characters =
         ("Hello, " + greetee).chars().mapToObj(c -> (char) 
c).collect(Collectors.toList());
     return Source.from(characters)
-        .map(
-            character -> {
-              return 
HelloReply.newBuilder().setMessage(String.valueOf(character)).build();
-            });
+        .map(character -> 
HelloReply.newBuilder().setMessage(String.valueOf(character)).build());
   }
 
   @Override
@@ -88,7 +85,6 @@ public class GreeterServicePowerApiImpl implements 
GreeterServicePowerApi {
   }
 
   private String authTaggedName(HelloRequest in, Metadata metadata) {
-    boolean authenticated = isAuthenticated(metadata);
     return String.format(
         "%s (%sauthenticated)", in.getName(), isAuthenticated(metadata) ? "" : 
"not ");
   }
diff --git 
a/plugin-tester-scala/src/test/scala/example/myapp/helloworld/GreeterServiceSpec.scala
 
b/plugin-tester-scala/src/test/scala/example/myapp/helloworld/GreeterServiceSpec.scala
index 3cdc4af4..0f9f1933 100644
--- 
a/plugin-tester-scala/src/test/scala/example/myapp/helloworld/GreeterServiceSpec.scala
+++ 
b/plugin-tester-scala/src/test/scala/example/myapp/helloworld/GreeterServiceSpec.scala
@@ -31,7 +31,7 @@ import scala.concurrent.{ Await, ExecutionContext }
 import scala.concurrent.duration._
 
 @RunWith(classOf[JUnitRunner])
-class GreeterSpec extends Matchers with AnyWordSpecLike with BeforeAndAfterAll 
with ScalaFutures {
+class GreeterServiceSpec extends Matchers with AnyWordSpecLike with 
BeforeAndAfterAll with ScalaFutures {
 
   implicit val patience: PatienceConfig = PatienceConfig(10.seconds, Span(100, 
org.scalatest.time.Millis))
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to