Skip to content

Commit

Permalink
Merge pull request #1541 from michalvavrik/backports/3.2.9-second
Browse files Browse the repository at this point in the history
[3.2] Backport gRPC interceptor test coverage and tagging tests with JIRAs
  • Loading branch information
michalvavrik authored Nov 23, 2023
2 parents 21ab26a + ecf2442 commit dfeaf55
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public void constraintsExist() throws JsonProcessingException {
}

@Test
@Tag("https://github.com/quarkusio/quarkus/pull/36664")
@Tag("QUARKUS-3672")
public void interceptedTest() {
// make server to generate a response so interceptors might intercept it
// ignore response, we will read interceptors result later
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.quarkus.ts.http.advanced;

import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.quarkus.grpc.GlobalInterceptor;

class GrpcClientInterceptors {
public static Metadata.Key<String> CLIENT_METHOD = Metadata.Key.of("client-method-target", ASCII_STRING_MARSHALLER);
public static Metadata.Key<String> CLIENT_CLASS = Metadata.Key.of("client-class-target", ASCII_STRING_MARSHALLER);

@GlobalInterceptor
@ApplicationScoped
static class ClassTarget extends Base {
}

static class MethodTarget extends Base {
}

static class Producer {
@GlobalInterceptor
@Produces
MethodTarget methodTarget() {
return new MethodTarget();
}
}

abstract static class Base implements ClientInterceptor {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions options,
Channel next) {
String interceptedTarget = getClass().getName();
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, options)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
if (interceptedTarget.contains("MethodTarget")) {
headers.put(CLIENT_METHOD, interceptedTarget);
} else if (interceptedTarget.contains("ClassTarget")) {
headers.put(CLIENT_CLASS, interceptedTarget);
}
super.start(responseListener, headers);
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.ts.http.advanced;

import io.grpc.stub.StreamObserver;
import io.quarkus.example.HelloReply;
import io.quarkus.example.InterceptedMessageGrpc;
import io.quarkus.example.InterceptedRequest;
import io.quarkus.grpc.GrpcService;

@GrpcService
public class GrpcInterceptorsService extends InterceptedMessageGrpc.InterceptedMessageImplBase {

@Override
public void showInterceptedMessage(InterceptedRequest request, StreamObserver<HelloReply> responseObserver) {
String serverMethod = GrpcServerInterceptors.SERVER_METHOD.get();
String serverClass = GrpcServerInterceptors.SERVER_CLASS.get();
HelloReply reply = HelloReply.newBuilder()
.setMessage("Intercepted client side method passed by server method interceptor is: " + serverMethod
+ "\nIntercepted client side class passed by server class interceptor is: " + serverClass)
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import io.quarkus.example.GreeterGrpc;
import io.quarkus.example.HelloReply;
import io.quarkus.example.HelloRequest;
import io.quarkus.example.InterceptedMessageGrpc;
import io.quarkus.example.InterceptedRequest;
import io.quarkus.grpc.GrpcClient;

@Path("/grpc")
Expand All @@ -18,11 +22,22 @@ public class GrpcResource {
@GrpcClient("hello")
GreeterGrpc.GreeterBlockingStub client;

@Inject
@GrpcClient("hello")
InterceptedMessageGrpc.InterceptedMessageBlockingStub interceptorsClient;

@GET
@Path("/{name}")
@Produces(MediaType.TEXT_PLAIN)
public String hello(@PathParam("name") String name) {
return client.sayHello(HelloRequest.newBuilder().setName(name).build()).getMessage();
}

@GET
@Path("global/interceptors")
public Response globalInterceptors() {
HelloReply helloReply = interceptorsClient.showInterceptedMessage(InterceptedRequest.newBuilder().build());
return Response.ok(helloReply.getMessage()).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkus.ts.http.advanced;

import static io.quarkus.ts.http.advanced.GrpcClientInterceptors.CLIENT_CLASS;
import static io.quarkus.ts.http.advanced.GrpcClientInterceptors.CLIENT_METHOD;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

import io.grpc.Context;
import io.grpc.Contexts;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.quarkus.grpc.GlobalInterceptor;

class GrpcServerInterceptors {

public static Context.Key<String> SERVER_METHOD = Context.key("server-method-target");
public static Context.Key<String> SERVER_CLASS = Context.key("server-class-target");

@GlobalInterceptor
@ApplicationScoped
static class ClassTarget extends Base {
}

static class MethodTarget extends Base {
}

static class Producer {
@GlobalInterceptor
@Produces
MethodTarget methodTarget() {
return new MethodTarget();
}
}

abstract static class Base implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata metadata,
ServerCallHandler<ReqT, RespT> next) {
Context ctx = null;
String interceptedTarget = getClass().getName();
// Determine where was grpc call intercepted and put client side intercepted data to Context.
if (interceptedTarget.contains("MethodTarget")) {
ctx = Context.current().withValue(SERVER_METHOD, metadata.get(CLIENT_METHOD));
ctx.attach();
} else if (interceptedTarget.contains("ClassTarget")) {
ctx = Context.current().withValue(SERVER_CLASS, metadata.get(CLIENT_CLASS));
ctx.attach();
} else {
throw new RuntimeException("Unexpected intercepted class or method by GlobalInterceptor.");
}
return Contexts.interceptCall(ctx, call, metadata, next);
}
}
}
10 changes: 9 additions & 1 deletion http/http-advanced/src/main/proto/helloworld.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// Intercepted message service
service InterceptedMessage {
rpc ShowInterceptedMessage (InterceptedRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
Expand All @@ -27,4 +32,7 @@ message HelloRequest {
// The response message containing the greetings
message HelloReply {
string message = 1;
}
}

message InterceptedRequest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ public void testGrpc() {
getApp().given().when().get("/api/grpc/trinity").then().statusCode(HttpStatus.SC_OK).body(is("Hello trinity"));
}

@Test
@Tag("QUARKUS-3742")
@DisplayName("GRPC client and server global interceptors test")
public void testGrpcGlobalInterceptors() {
getApp().given().when().get("/api/grpc/global/interceptors").then().statusCode(HttpStatus.SC_OK)
.body(containsString("ClientInterceptors$ClassTarget"),
containsString("ClientInterceptors$MethodTarget"));
}

@Test
@DisplayName("Http/2 Server test")
public void http2Server() throws InterruptedException, URISyntaxException {
Expand Down Expand Up @@ -246,7 +255,7 @@ public void keepRequestScopeValuesAfterEventPropagation() {
}

@Test
@Tag("https://github.com/quarkusio/quarkus/issues/36402")
@Tag("QUARKUS-3685")
public void sseConnectionTest() {
String response = getApp().given().get("/api/sse/client").thenReturn().body().asString();

Expand All @@ -256,7 +265,7 @@ public void sseConnectionTest() {
}

@Test
@Tag("https://github.com/quarkusio/quarkus/pull/36664")
@Tag("QUARKUS-3672")
public void interceptedTest() {
// make server to generate a response so interceptors might intercept it
// ignore response, we will read interceptors result later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@Tag("QUARKUS-3674")
@QuarkusTest
public class TestSecurityDisabledTest extends AbstractSecurityCallbackTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.security.TestSecurity;

@Tag("QUARKUS-3674")
@QuarkusTest
public class TestSecurityEnabledTest extends AbstractSecurityCallbackTest {
@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import static org.hamcrest.CoreMatchers.containsString;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.scenarios.QuarkusScenario;

@Tag("QUARKUS-3680")
@QuarkusScenario
public class TokenPropagationFilterIT extends BaseOidcIT {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import static org.hamcrest.CoreMatchers.containsString;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.scenarios.QuarkusScenario;

@Tag("QUARKUS-3680")
@QuarkusScenario
public class TokenPropagationFilterIT extends BaseOidcIT {

Expand Down

0 comments on commit dfeaf55

Please sign in to comment.