Skip to content
Open
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
5 changes: 5 additions & 0 deletions testsuite/integration/subsystem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
<groupId>org.wildfly.grpc</groupId>
<artifactId>wildfly-grpc-subsystem</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
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);
}
}
}
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";

Copy link
Copy Markdown
Collaborator

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.helloworld

HelloWorldParent defines

protected static final String TARGET = "localhost:9555";
protected static final String TARGET_HOST = "localhost";
protected static final int TARGET_PORT = 9555;

which is used in

  • PlaintextHelloWorldTest

    channel = ManagedChannelBuilder.forTarget(TARGET).usePlaintext().build();
  • OnewaySecureHelloWorldTest

    channel = Grpc.newChannelBuilderForAddress(TARGET_HOST, TARGET_PORT, creds).build();
  • TwowaySecureHelloWorldTest

    channel = Grpc.newChannelBuilderForAddress(TARGET_HOST, TARGET_PORT, creds).build();

Package org.wildfly.feature.pack.grpc.test.stream

StreamingTestParent defines

protected static final String TARGET = "localhost:9555";
protected static final String TARGET_HOST = "localhost";
protected static final int TARGET_PORT = 9555;

which is used in

  • PlaintextStreamingTest

    channel = ManagedChannelBuilder.forTarget(TARGET).usePlaintext().build();
  • OnewaySecureStreamingTest

    channel = Grpc.newChannelBuilderForAddress(TARGET_HOST, TARGET_PORT, creds).build();
  • TwowaySecureStreamingTest

  • channel = Grpc.newChannelBuilderForAddress(TARGET_HOST, TARGET_PORT, creds).build();

This 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.


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());
}
}
Loading