Skip to content

Commit

Permalink
Adds new tests and resources.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Jun 20, 2024
1 parent 1dbc131 commit 88eaa48
Show file tree
Hide file tree
Showing 13 changed files with 551 additions and 0 deletions.
45 changes: 45 additions & 0 deletions microprofile/grpc/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,50 @@
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-tls</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!--
need to use this version to work around the same groupId/artifactId and
different package names in Jakarta
This is needed for the Generated annotation in grpc sources
-->
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${version.plugin.os}</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-compile</goal>
<goal>test-compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.microprofile.grpc.client;

import io.helidon.microprofile.grpc.client.test.Echo;
import io.helidon.microprofile.grpc.client.test.EchoServiceGrpc;

import io.grpc.MethodDescriptor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsEmptyIterable.emptyIterable;

public class ClientMethodDescriptorTest {

private MethodDescriptor.Builder<?, ?> grpcDescriptor;

@BeforeEach
public void setup() {
grpcDescriptor = EchoServiceGrpc.getServiceDescriptor()
.getMethods()
.stream()
.filter(md -> md.getFullMethodName().equals("EchoService/Echo"))
.findFirst()
.orElseThrow(() -> new AssertionError("Could not find echo method"))
.toBuilder();
}

@Test
public void shouldCreateMethodDescriptorFromGrpcDescriptor() {
ClientMethodDescriptor descriptor = ClientMethodDescriptor.create("FooService",
"foo",
grpcDescriptor);

assertThat(descriptor, is(notNullValue()));
assertThat(descriptor.name(), is("foo"));
assertThat(descriptor.interceptors(), is(emptyIterable()));

MethodDescriptor<?, ?> expected = grpcDescriptor.build();
MethodDescriptor<?, ?> methodDescriptor = descriptor.descriptor();
assertThat(methodDescriptor.getFullMethodName(), is("FooService/foo"));
assertThat(methodDescriptor.getType(), is(expected.getType()));
assertThat(methodDescriptor.getRequestMarshaller(), is(expected.getRequestMarshaller()));
assertThat(methodDescriptor.getResponseMarshaller(), is(expected.getResponseMarshaller()));
}

@Test
public void shouldCreateBidirectionalMethod() {
ClientMethodDescriptor descriptor = ClientMethodDescriptor.bidirectional("FooService", "foo")
.defaultMarshallerSupplier(new JavaMarshaller.Supplier())
.build();
assertThat(descriptor, is(notNullValue()));
assertThat(descriptor.name(), is("foo"));
MethodDescriptor<?, ?> methodDescriptor = descriptor.descriptor();
assertThat(methodDescriptor.getFullMethodName(), is("FooService/foo"));
assertThat(methodDescriptor.getType(), is(MethodDescriptor.MethodType.BIDI_STREAMING));
}

@Test
public void shouldCreateClientStreamingMethod() {
ClientMethodDescriptor descriptor = ClientMethodDescriptor.clientStreaming("FooService", "foo")
.defaultMarshallerSupplier(new JavaMarshaller.Supplier())
.build();
assertThat(descriptor, is(notNullValue()));
assertThat(descriptor.name(), is("foo"));
MethodDescriptor<?, ?> methodDescriptor = descriptor.descriptor();
assertThat(methodDescriptor.getFullMethodName(), is("FooService/foo"));
assertThat(methodDescriptor.getType(), is(MethodDescriptor.MethodType.CLIENT_STREAMING));
}

@Test
public void shouldCreateServerStreamingMethod() {
ClientMethodDescriptor descriptor = ClientMethodDescriptor.serverStreaming("FooService", "foo")
.defaultMarshallerSupplier(new JavaMarshaller.Supplier())
.build();
assertThat(descriptor, is(notNullValue()));
assertThat(descriptor.name(), is("foo"));
MethodDescriptor<?, ?> methodDescriptor = descriptor.descriptor();
assertThat(methodDescriptor.getFullMethodName(), is("FooService/foo"));
assertThat(methodDescriptor.getType(), is(MethodDescriptor.MethodType.SERVER_STREAMING));
}

@Test
public void shouldCreateUnaryMethod() {
ClientMethodDescriptor descriptor = ClientMethodDescriptor.unary("FooService", "foo")
.defaultMarshallerSupplier(new JavaMarshaller.Supplier())
.build();
assertThat(descriptor, is(notNullValue()));
assertThat(descriptor.name(), is("foo"));
MethodDescriptor<?, ?> methodDescriptor = descriptor.descriptor();
assertThat(methodDescriptor.getFullMethodName(), is("FooService/foo"));
assertThat(methodDescriptor.getType(), is(MethodDescriptor.MethodType.UNARY));
}

@Test
public void shouldSetName() {
ClientMethodDescriptor.Builder builder = ClientMethodDescriptor
.unary("FooService", "foo")
.defaultMarshallerSupplier(new JavaMarshaller.Supplier());

builder.fullName("Foo/Bar");

ClientMethodDescriptor descriptor = builder.build();

assertThat(descriptor.name(), is("Bar"));
assertThat(descriptor.descriptor().getFullMethodName(), is("Foo/Bar"));
}

@Test
public void testMarshallerTypesForProtoBuilder() {
ClientMethodDescriptor descriptor = ClientMethodDescriptor
.unary("EchoService", "Echo")
.requestType(Echo.EchoRequest.class)
.responseType(Echo.EchoResponse.class)
.build();

MethodDescriptor<?, ?> methodDescriptor = descriptor.descriptor();
assertThat(methodDescriptor.getRequestMarshaller(), instanceOf(MethodDescriptor.PrototypeMarshaller.class));
assertThat(methodDescriptor.getResponseMarshaller(), instanceOf(MethodDescriptor.PrototypeMarshaller.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.microprofile.grpc.client;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import io.helidon.grpc.core.MarshallerSupplier;

import io.grpc.MethodDescriptor;

/**
* An implementation of a gRPC {@link io.grpc.MethodDescriptor.Marshaller} that
* uses Java serialization for testing.
*/
public class JavaMarshaller<T> implements MethodDescriptor.Marshaller<T> {

@Override
public InputStream stream(T obj) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(obj);
return new ByteArrayInputStream(out.toByteArray());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
@SuppressWarnings("unchecked")
public T parse(InputStream in) {
try (ObjectInputStream ois = new ObjectInputStream(in)) {
return (T) ois.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* A {@link io.helidon.grpc.core.MarshallerSupplier} implementation that supplies
* instances of {@link io.helidon.microprofile.grpc.client.JavaMarshaller}.
*/
public static class Supplier
implements MarshallerSupplier {
@Override
public <T> MethodDescriptor.Marshaller<T> get(Class<T> clazz) {
return new JavaMarshaller<>();
}
}
}
30 changes: 30 additions & 0 deletions microprofile/grpc/client/src/test/proto/echo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

syntax = "proto3";
option java_package = "io.helidon.microprofile.grpc.client.test";

service EchoService {
rpc Echo (EchoRequest) returns (EchoResponse) {}
}

message EchoRequest {
string message = 1;
}

message EchoResponse {
string message = 1;
}
31 changes: 31 additions & 0 deletions microprofile/grpc/client/src/test/proto/strings.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


syntax = "proto3";
option java_package = "io.helidon.microprofile.grpc.client.test";

service StringService {
rpc Upper (StringMessage) returns (StringMessage) {}
rpc Lower (StringMessage) returns (StringMessage) {}
rpc Split (StringMessage) returns (stream StringMessage) {}
rpc Join (stream StringMessage) returns (StringMessage) {}
rpc Echo (stream StringMessage) returns (stream StringMessage) {}
}

message StringMessage {
string text = 1;
}
34 changes: 34 additions & 0 deletions microprofile/grpc/client/src/test/resources/logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Copyright (c) 2019, 2024 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Example Logging Configuration File
# For more information see $JAVA_HOME/jre/lib/logging.properties

# Send messages to the console
handlers=io.helidon.common.HelidonConsoleHandler

# HelidonConsoleHandler uses a SimpleFormatter subclass that replaces "!thread!" with the current thread
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n

# Global logging level. Can be overridden by specific loggers
.level=INFO

# Component specific log levels
#io.helidon.webserver.level=INFO
#io.helidon.config.level=INFO
#io.helidon.security.level=INFO
#io.helidon.common.level=INFO
#io.netty.level=INFO
17 changes: 17 additions & 0 deletions microprofile/grpc/client/src/test/resources/ssl/ca.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE-----
MIICpjCCAY4CCQDsAfroMCH4tDANBgkqhkiG9w0BAQsFADAUMRIwEAYDVQQDDAls
b2NhbGhvc3QwIBcNMjAwMzMwMDk1NjU3WhgPMjI5NDAxMTIwOTU2NTdaMBQxEjAQ
BgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AKbUgPWd5YM775bazo8MmuMHmYsSacUx+01NKVjbXPz8QHJULfdrYzJEs3yJ2bpP
mOBrXDTmW2+v1TUdSRjytf9b6/+NWixovGwhGF/iXJ9dhMhwp1AwL8PViuuJPTcf
acoAbFn1yJtNjZgalevyyN+eto+UFW+qJ+jn7ncTdReke3DeHiEA+lmk1DlAsGLR
sB2gQBC2Xb8wcCltivb/WJRJG+sHja2BTIUbywmYO9UtuLsLE/Wptl+t8ybObj2G
eE/t7/aM6KTOtXb6Q/Z7YPYSrVtIa/MwkFssmcVG0ldaAszUpq5bLanH5H3AHPN5
1atNHVIYlwom2SrRazg5IisCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAWnMcVDGq
NxigGhtSFdmbnauLPd47ZEFh6iL+bG7QkT15XtpR5Q4GkxaIb1fTsqaxfj6WUG6Z
n6f9OHSuFGiQbV+h2zNVR4q+i7DcDFkbFrK0XtyUYkdHOot+I7XzURsocnZhWoKO
g+Yi0z4uBSol4olK4KNQLS39ePPrm76CqLf4i8wSUF5YEi0ExWhFgM4+19JvVyQ7
tqRM1rAWajWp696fKP47ZV2Qn39+BRh8TYKIEdStaWWUxtycC8qhnkXspIG7etcN
3kju90KNuLky6WhNOm84GVZ4w339QTSk65yzUgxTxqJMK6elqgCESTuyGS1EJV00
t2Dcg5m1p2llRQ==
-----END CERTIFICATE-----
16 changes: 16 additions & 0 deletions microprofile/grpc/client/src/test/resources/ssl/clientCert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-----BEGIN CERTIFICATE-----
MIICmTCCAYECAQEwDQYJKoZIhvcNAQEFBQAwFDESMBAGA1UEAwwJbG9jYWxob3N0
MCAXDTIwMDMzMDA5NTY1N1oYDzIyOTQwMTEyMDk1NjU3WjAPMQ0wCwYDVQQDDAR0
ZXN0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxcZyW23ksgNBBo2P
f4VZxHLj+j6jWVVC5B4Wh3dl9VdSLII3T4uWQbNCoe9vIKZRhsuXn/fbHkd89MFi
GkC02kI+d0RpWULqgnRijbEu5fCsxBYa7dpI0DHkHWjTT2qk2vAw1h+z79orRiVt
wLy4vD4Ig6sbna/1AyK89FRHeq/YSMT3b0jhcxRxWSXmKb81UJ1HjNXIW8bElRcj
Z0vMNQsFWkOWOfyN31NRkM+sqy42fKsMPysitpUz5UQzVxpkm0rom89vDO1VubQl
zGK1gBV3Ll9kkZt5+UjkW/XE9leyRg1iSsY8l5NTp0RJNqpzZkrrPYHFIFhszLNe
NvtB4QIDAQABMA0GCSqGSIb3DQEBBQUAA4IBAQBv5M6wwOroq1iCz1wyWu/RxSGS
nGn4MdFEeIKTx9urMqTiHp9bW7gVGLHQ96ApxFF30PZ58X1JrJn+eerBaaI7yOo0
FmVDlacqh5jSDvBoIzC8gFIfcDT/Q1xXmeB0KcgMdoZOhdkrX59qVenxrnihtd2I
uIWhutQB9T6VrTGiCB09gWkULsjHvKMJ1ucSG0a2K18Aqccypd3twXmllwPa/vrt
Kv7s9ZVvYibW7xXJBCV1kiZtx5TV+VO100/Ze94vvnAs7kCLZ16oGOFSj0JpCEFz
CAVAif0/huKd4bpMzwCosCPiKSGLETc6S+WTzoy0FAFmygZHEL9diXriQrMa
-----END CERTIFICATE-----
Loading

0 comments on commit 88eaa48

Please sign in to comment.