Repository navigation
test: add integration tests for JSON serialization in gRPC messages #515
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
Open
jmesnil
wants to merge
1
commit into
wildfly-extras:main
Choose a base branch
from
jmesnil:json-serialization-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...bsystem/src/test/java/org/wildfly/feature/pack/grpc/test/json/JsonGreeterServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright The WildFly Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.wildfly.feature.pack.grpc.test.json; | ||
|
|
||
| import com.google.protobuf.util.JsonFormat; | ||
|
|
||
| import io.grpc.stub.StreamObserver; | ||
| import messages.HelloReply; | ||
| import messages.HelloRequest; | ||
|
|
||
| import org.wildfly.feature.pack.grpc.test.helloworld.GreeterGrpc; | ||
|
|
||
| /** | ||
| * A gRPC service implementation that uses {@link JsonFormat} to serialize and deserialize | ||
| * the request message. This verifies that JSON serialization (backed by Gson) is accessible | ||
| * from a WildFly deployment. | ||
| */ | ||
| public class JsonGreeterServiceImpl extends GreeterGrpc.GreeterImplBase { | ||
|
|
||
| @Override | ||
| public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) { | ||
| try { | ||
| // Serialize request to JSON and parse it back to verify JsonFormat works | ||
| String json = JsonFormat.printer().print(request); | ||
| HelloRequest.Builder builder = HelloRequest.newBuilder(); | ||
| JsonFormat.parser().merge(json, builder); | ||
| HelloRequest parsedRequest = builder.build(); | ||
|
|
||
| String name = parsedRequest.getName(); | ||
| responseObserver.onNext(HelloReply.newBuilder().setMessage("Hello " + name).build()); | ||
| responseObserver.onCompleted(); | ||
| } catch (Exception e) { | ||
| responseObserver.onError(e); | ||
| } | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
...ubsystem/src/test/java/org/wildfly/feature/pack/grpc/test/json/JsonSerializationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright The WildFly Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.wildfly.feature.pack.grpc.test.json; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.jboss.arquillian.container.test.api.Deployment; | ||
| import org.jboss.arquillian.container.test.api.RunAsClient; | ||
| import org.jboss.arquillian.junit.Arquillian; | ||
| import org.jboss.shrinkwrap.api.Archive; | ||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Assert; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.wildfly.feature.pack.grpc.test.helloworld.GreeterGrpc; | ||
|
|
||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.ManagedChannelBuilder; | ||
| import messages.HelloReply; | ||
| import messages.HelloRequest; | ||
|
|
||
| /** | ||
| * Verifies that JSON serialization (via {@code com.google.protobuf.util.JsonFormat}) works | ||
| * inside a WildFly deployment, as enabled by the Gson dependency added to the | ||
| * {@code com.google.protobuf} WildFly module. | ||
| */ | ||
| @RunWith(Arquillian.class) | ||
| @RunAsClient | ||
| public class JsonSerializationTest { | ||
|
|
||
| private static final String TARGET = "localhost:9555"; | ||
|
|
||
| private static ManagedChannel channel; | ||
| private static GreeterGrpc.GreeterBlockingStub blockingStub; | ||
|
|
||
| @Deployment | ||
| public static Archive<?> createTestArchive() { | ||
| WebArchive war = ShrinkWrap.create(WebArchive.class, "JsonSerializationTest.war"); | ||
| war.addClasses(JsonGreeterServiceImpl.class, GreeterGrpc.class); | ||
| war.addPackage(HelloRequest.class.getPackage()); | ||
| return war; | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void beforeClass() { | ||
| channel = ManagedChannelBuilder.forTarget(TARGET).usePlaintext().build(); | ||
| blockingStub = GreeterGrpc.newBlockingStub(channel); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void afterClass() throws Exception { | ||
| channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); | ||
| } | ||
|
|
||
| @Test | ||
| public void helloWithJsonSerialization() { | ||
| HelloRequest request = HelloRequest.newBuilder().setName("Bob").build(); | ||
| HelloReply reply = blockingStub.sayHello(request); | ||
| Assert.assertEquals("Hello Bob", reply.getMessage()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a repeating pattern in the tests to define the target host and port and to create channels:
Package
org.wildfly.feature.pack.grpc.test.helloworldHelloWorldParentdefineswhich is used in
PlaintextHelloWorldTestOnewaySecureHelloWorldTestTwowaySecureHelloWorldTestPackage
org.wildfly.feature.pack.grpc.test.streamStreamingTestParentdefineswhich is used in
PlaintextStreamingTestOnewaySecureStreamingTestTwowaySecureStreamingTestThis is not much repeating code atm, but it might make sense to have at least the constants for the target host and port in one place.