Skip to content

Commit a40aa72

Browse files
jmesnilclaude
andcommitted
test: add integration tests for JSON serialization in gRPC messages
Verify that JsonFormat (from protobuf-java-util, backed by Gson) is accessible from a WildFly deployment, as enabled by the optional com.google.code.gson dependency added to the com.google.protobuf module. - JsonGreeterServiceImpl: service impl that uses JsonFormat to round-trip the request through JSON serialization before building the response - JsonSerializationTest: Arquillian integration test that deploys the service and verifies the call succeeds over a plaintext connection - Add protobuf-java-util as provided dependency to the testsuite pom (it is available in WildFly at runtime; needed only for compilation) Closes #510 Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 5df93a9 commit a40aa72

3 files changed

Lines changed: 109 additions & 0 deletions

File tree

‎testsuite/integration/subsystem/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@
7676
<groupId>org.wildfly.grpc</groupId>
7777
<artifactId>wildfly-grpc-subsystem</artifactId>
7878
</dependency>
79+
<dependency>
80+
<groupId>com.google.protobuf</groupId>
81+
<artifactId>protobuf-java-util</artifactId>
82+
<scope>provided</scope>
83+
</dependency>
7984
</dependencies>
8085

8186
<build>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright The WildFly Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package org.wildfly.feature.pack.grpc.test.json;
6+
7+
import com.google.protobuf.util.JsonFormat;
8+
9+
import io.grpc.stub.StreamObserver;
10+
import messages.HelloReply;
11+
import messages.HelloRequest;
12+
13+
import org.wildfly.feature.pack.grpc.test.helloworld.GreeterGrpc;
14+
15+
/**
16+
* A gRPC service implementation that uses {@link JsonFormat} to serialize and deserialize
17+
* the request message. This verifies that JSON serialization (backed by Gson) is accessible
18+
* from a WildFly deployment.
19+
*/
20+
public class JsonGreeterServiceImpl extends GreeterGrpc.GreeterImplBase {
21+
22+
@Override
23+
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
24+
try {
25+
// Serialize request to JSON and parse it back to verify JsonFormat works
26+
String json = JsonFormat.printer().print(request);
27+
HelloRequest.Builder builder = HelloRequest.newBuilder();
28+
JsonFormat.parser().merge(json, builder);
29+
HelloRequest parsedRequest = builder.build();
30+
31+
String name = parsedRequest.getName();
32+
responseObserver.onNext(HelloReply.newBuilder().setMessage("Hello " + name).build());
33+
responseObserver.onCompleted();
34+
} catch (Exception e) {
35+
responseObserver.onError(e);
36+
}
37+
}
38+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright The WildFly Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package org.wildfly.feature.pack.grpc.test.json;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
import org.jboss.arquillian.container.test.api.Deployment;
10+
import org.jboss.arquillian.container.test.api.RunAsClient;
11+
import org.jboss.arquillian.junit.Arquillian;
12+
import org.jboss.shrinkwrap.api.Archive;
13+
import org.jboss.shrinkwrap.api.ShrinkWrap;
14+
import org.jboss.shrinkwrap.api.spec.WebArchive;
15+
import org.junit.AfterClass;
16+
import org.junit.Assert;
17+
import org.junit.BeforeClass;
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.wildfly.feature.pack.grpc.test.helloworld.GreeterGrpc;
21+
22+
import io.grpc.ManagedChannel;
23+
import io.grpc.ManagedChannelBuilder;
24+
import messages.HelloReply;
25+
import messages.HelloRequest;
26+
27+
/**
28+
* Verifies that JSON serialization (via {@code com.google.protobuf.util.JsonFormat}) works
29+
* inside a WildFly deployment, as enabled by the Gson dependency added to the
30+
* {@code com.google.protobuf} WildFly module.
31+
*/
32+
@RunWith(Arquillian.class)
33+
@RunAsClient
34+
public class JsonSerializationTest {
35+
36+
private static final String TARGET = "localhost:9555";
37+
38+
private static ManagedChannel channel;
39+
private static GreeterGrpc.GreeterBlockingStub blockingStub;
40+
41+
@Deployment
42+
public static Archive<?> createTestArchive() {
43+
WebArchive war = ShrinkWrap.create(WebArchive.class, "JsonSerializationTest.war");
44+
war.addClasses(JsonGreeterServiceImpl.class, GreeterGrpc.class);
45+
war.addPackage(HelloRequest.class.getPackage());
46+
return war;
47+
}
48+
49+
@BeforeClass
50+
public static void beforeClass() {
51+
channel = ManagedChannelBuilder.forTarget(TARGET).usePlaintext().build();
52+
blockingStub = GreeterGrpc.newBlockingStub(channel);
53+
}
54+
55+
@AfterClass
56+
public static void afterClass() throws Exception {
57+
channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
58+
}
59+
60+
@Test
61+
public void helloWithJsonSerialization() {
62+
HelloRequest request = HelloRequest.newBuilder().setName("Bob").build();
63+
HelloReply reply = blockingStub.sayHello(request);
64+
Assert.assertEquals("Hello Bob", reply.getMessage());
65+
}
66+
}

0 commit comments

Comments
 (0)