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 21, 2024
1 parent 1dbc131 commit a02dc40
Show file tree
Hide file tree
Showing 16 changed files with 1,381 additions and 0 deletions.
60 changes: 60 additions & 0 deletions microprofile/grpc/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,65 @@
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-tls</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-grpc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
<scope>test</scope>
</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>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</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));
}
}
Loading

0 comments on commit a02dc40

Please sign in to comment.