Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring code in plugin-tester-java(scala) #288

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/main/paradox/server/details.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ private void runTest(TestCases testCase, Settings settings) throws Exception {
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;
}

case JWT_TOKEN_CREDS:
{
FileInputStream credentialsStream =
new FileInputStream(new File(settings.getServiceAccountKeyFile()));
new FileInputStream(settings.getServiceAccountKeyFile());
clientTester.jwtTokenCreds(credentialsStream);
break;
}
Expand All @@ -174,7 +174,7 @@ private void runTest(TestCases testCase, Settings settings) throws Exception {
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;
}
Expand All @@ -184,7 +184,7 @@ private void runTest(TestCases testCase, Settings settings) throws Exception {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public static void main(String[] args) {
.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 =
Expand All @@ -74,9 +72,7 @@ public static void main(String[] args) {
.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()));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,22 @@
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();

Expand Down Expand Up @@ -73,7 +70,7 @@ private static void singleRequestReply(GreeterService client) throws Exception {

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));
Expand All @@ -98,7 +95,7 @@ private static void streamingRequestReply(GreeterService client, Materializer ma
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public static void main(String[] args) throws Exception {

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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public CompletionStage<HelloReply> sayHello(HelloRequest in) {
@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();
Expand All @@ -64,10 +64,7 @@ public Source<HelloReply, NotUsed> itKeepsReplying(HelloRequest in) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@
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();

Expand Down Expand Up @@ -68,7 +65,7 @@ private static void singleRequestReply(GreeterServiceClient client) throws Excep

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 =
Expand Down Expand Up @@ -96,7 +93,7 @@ private static void streamingRequestReply(GreeterServiceClient client, Materiali
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public static void main(String[] args) throws Exception {

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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public static void main(String[] args) throws Exception {

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
}
Expand All @@ -48,7 +46,7 @@ public static CompletionStage<ServerBinding> run(ActorSystem sys) throws Excepti
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
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;
}

Expand All @@ -46,7 +46,7 @@ public CompletionStage<HelloReply> sayHello(HelloRequest in, Metadata metadata)
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 =
Expand All @@ -65,10 +65,7 @@ public Source<HelloReply, NotUsed> itKeepsReplying(HelloRequest in, Metadata met
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
Expand All @@ -88,7 +85,6 @@ private boolean isAuthenticated(Metadata metadata) {
}

private String authTaggedName(HelloRequest in, Metadata metadata) {
boolean authenticated = isAuthenticated(metadata);
return String.format(
"%s (%sauthenticated)", in.getName(), isAuthenticated(metadata) ? "" : "not ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Loading