Skip to content
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

chore: Migrate Junit 4 to Junit 5 for showcase #2757

Merged
merged 20 commits into from
May 20, 2024
Merged
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
20 changes: 18 additions & 2 deletions showcase/gapic-showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,30 @@

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.4.2</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.google.showcase.v1beta1.it;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.api.gax.httpjson.*;
import com.google.api.gax.rpc.ApiClientHeaderProvider;
Expand All @@ -31,15 +31,15 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

// TODO: add testing on error responses once feat is implemented in showcase.
// https://github.com/googleapis/gapic-showcase/pull/1456
// TODO: watch for showcase gRPC trailer changes suggested in
// https://github.com/googleapis/gapic-showcase/pull/1509#issuecomment-2089147103
public class ITApiVersionHeaders {
class ITApiVersionHeaders {
private static final String HTTP_RESPONSE_HEADER_STRING =
"x-showcase-request-" + ApiClientHeaderProvider.API_VERSION_HEADER_KEY;
private static final Metadata.Key<String> API_VERSION_HEADER_KEY =
Expand All @@ -51,7 +51,6 @@ public class ITApiVersionHeaders {
private static final String EXPECTED_EXCEPTION_MESSAGE =
"Header provider can't override the header: "
+ ApiClientHeaderProvider.API_VERSION_HEADER_KEY;
private static final int DEFAULT_AWAIT_TERMINATION_SEC = 10;

// Implement a client interceptor to retrieve the trailing metadata from response.
private static class GrpcCapturingClientInterceptor implements ClientInterceptor {
Expand Down Expand Up @@ -149,17 +148,17 @@ public void onClose(int statusCode, HttpJsonMetadata trailers) {
}
}

private HttpJsonCapturingClientInterceptor httpJsonInterceptor;
private GrpcCapturingClientInterceptor grpcInterceptor;
private HttpJsonCapturingClientInterceptor httpJsonComplianceInterceptor;
private GrpcCapturingClientInterceptor grpcComplianceInterceptor;
private EchoClient grpcClient;
private EchoClient httpJsonClient;
private ComplianceClient grpcComplianceClient;
private ComplianceClient httpJsonComplianceClient;

@Before
public void createClients() throws Exception {
private static HttpJsonCapturingClientInterceptor httpJsonInterceptor;
private static GrpcCapturingClientInterceptor grpcInterceptor;
private static HttpJsonCapturingClientInterceptor httpJsonComplianceInterceptor;
private static GrpcCapturingClientInterceptor grpcComplianceInterceptor;
private static EchoClient grpcClient;
private static EchoClient httpJsonClient;
private static ComplianceClient grpcComplianceClient;
private static ComplianceClient httpJsonComplianceClient;

@BeforeAll
static void createClients() throws Exception {
// Create gRPC Interceptor and Client
grpcInterceptor = new GrpcCapturingClientInterceptor();
grpcClient = TestClientInitializer.createGrpcEchoClient(ImmutableList.of(grpcInterceptor));
Expand All @@ -183,28 +182,31 @@ public void createClients() throws Exception {
ImmutableList.of(httpJsonComplianceInterceptor));
}

@After
public void destroyClient() throws InterruptedException {
@AfterAll
static void destroyClient() throws InterruptedException {
grpcClient.close();
httpJsonClient.close();
grpcComplianceClient.close();
httpJsonComplianceClient.close();

grpcClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
httpJsonClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
grpcComplianceClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
httpJsonComplianceClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
httpJsonClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
grpcComplianceClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
httpJsonComplianceClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

@Test
public void testGrpc_matchesApiVersion() {
void testGrpc_matchesApiVersion() {
grpcClient.echo(EchoRequest.newBuilder().build());
String headerValue = grpcInterceptor.metadata.get(API_VERSION_HEADER_KEY);
assertThat(headerValue).isEqualTo(EXPECTED_ECHO_API_VERSION);
}

@Test
public void testHttpJson_matchesHeaderName() {
void testHttpJson_matchesHeaderName() {
httpJsonClient.echo(EchoRequest.newBuilder().build());
ArrayList headerValues =
(ArrayList) httpJsonInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
Expand All @@ -213,15 +215,15 @@ public void testHttpJson_matchesHeaderName() {
}

@Test
public void testGrpc_noApiVersion() {
void testGrpc_noApiVersion() {
RepeatRequest request =
RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build();
grpcComplianceClient.repeatDataSimplePath(request);
assertThat(API_VERSION_HEADER_KEY).isNotIn(grpcComplianceInterceptor.metadata.keys());
}

@Test
public void testHttpJson_noApiVersion() {
void testHttpJson_noApiVersion() {
RepeatRequest request =
RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build();
httpJsonComplianceClient.repeatDataSimplePath(request);
Expand All @@ -230,7 +232,7 @@ public void testHttpJson_noApiVersion() {
}

@Test
public void testGrpcEcho_userApiVersionThrowsException() throws IOException {
void testGrpcEcho_userApiVersionThrowsException() throws IOException {
StubSettings stubSettings =
grpcClient
.getSettings()
Expand All @@ -249,7 +251,7 @@ public void testGrpcEcho_userApiVersionThrowsException() throws IOException {
}

@Test
public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException {
void testHttpJsonEcho_userApiVersionThrowsException() throws IOException {
StubSettings stubSettings =
httpJsonClient
.getSettings()
Expand All @@ -268,7 +270,7 @@ public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException
}

@Test
public void testGrpcCompliance_userApiVersionSetSuccess() throws IOException {
void testGrpcCompliance_userApiVersionSetSuccess() throws IOException {
StubSettings stubSettingsWithApiVersionHeader =
grpcComplianceClient
.getSettings()
Expand All @@ -293,7 +295,7 @@ public void testGrpcCompliance_userApiVersionSetSuccess() throws IOException {
}

@Test
public void testHttpJsonCompliance_userApiVersionSetSuccess() throws IOException {
void testHttpJsonCompliance_userApiVersionSetSuccess() throws IOException {
StubSettings httpJsonStubSettingsWithApiVersionHeader =
httpJsonComplianceClient
.getSettings()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.google.showcase.v1beta1.it;

import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.api.gax.httpjson.ApiMethodDescriptor;
import com.google.api.gax.httpjson.ForwardingHttpJsonClientCall;
Expand Down Expand Up @@ -47,12 +47,12 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.threeten.bp.Duration;

public class ITAutoPopulatedFields {
class ITAutoPopulatedFields {

private static class HttpJsonInterceptor implements HttpJsonClientInterceptor {
private Consumer<Object> onRequestIntercepted;
Expand Down Expand Up @@ -120,8 +120,8 @@ public void sendMessage(ReqT message) {
private EchoClient httpJsonClient;
private EchoClient httpJsonClientWithRetries;

@Before
public void createClients() throws Exception {
@BeforeEach
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
void createClients() throws Exception {
RetrySettings defaultRetrySettings =
RetrySettings.newBuilder()
.setInitialRpcTimeout(Duration.ofMillis(5000L))
Expand Down Expand Up @@ -156,15 +156,22 @@ public void createClients() throws Exception {
defaultRetrySettings, retryableCodes, ImmutableList.of(httpJsonInterceptor));
}

@After
public void destroyClient() {
@AfterEach
void destroyClient() throws InterruptedException {
grpcClientWithoutRetries.close();
grpcClientWithRetries.close();
httpJsonClient.close();

grpcClientWithoutRetries.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
grpcClientWithRetries.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
httpJsonClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

@Test
public void testGrpc_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
void testGrpc_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
List<String> capturedRequestIds = new ArrayList<>();
grpcRequestInterceptor.setOnRequestIntercepted(
request -> {
Expand All @@ -181,7 +188,7 @@ public void testGrpc_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
}

@Test
public void testGrpc_shouldNotAutoPopulateRequestIdIfSetInRequest() {
void testGrpc_shouldNotAutoPopulateRequestIdIfSetInRequest() {
List<String> capturedRequestIds = new ArrayList<>();
grpcRequestInterceptor.setOnRequestIntercepted(
request -> {
Expand All @@ -197,7 +204,7 @@ public void testGrpc_shouldNotAutoPopulateRequestIdIfSetInRequest() {
}

@Test
public void testHttpJson_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
void testHttpJson_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
List<String> capturedRequestIds = new ArrayList<>();
httpJsonInterceptor.setOnRequestIntercepted(
request -> {
Expand All @@ -214,7 +221,7 @@ public void testHttpJson_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
}

@Test
public void testHttpJson_shouldNotAutoPopulateRequestIdIfSetInRequest() {
void testHttpJson_shouldNotAutoPopulateRequestIdIfSetInRequest() {
String UUIDsent = UUID.randomUUID().toString();
List<String> capturedRequestIds = new ArrayList<>();
httpJsonInterceptor.setOnRequestIntercepted(
Expand All @@ -230,7 +237,7 @@ public void testHttpJson_shouldNotAutoPopulateRequestIdIfSetInRequest() {
}

@Test
public void testGRPC_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() throws Exception {
void testGRPC_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() throws Exception {
List<String> capturedRequestIds = new ArrayList<>();
grpcRequestInterceptor.setOnRequestIntercepted(
request -> {
Expand Down Expand Up @@ -264,7 +271,7 @@ public void testGRPC_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() thr
}

@Test
public void testGRPC_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
void testGRPC_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
List<String> capturedRequestIds = new ArrayList<>();
grpcRequestInterceptor.setOnRequestIntercepted(
request -> {
Expand Down Expand Up @@ -302,8 +309,7 @@ public void testGRPC_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() thro
}

@Test
public void testHttpJson_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried()
throws Exception {
void testHttpJson_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() throws Exception {
List<String> capturedRequestIds = new ArrayList<>();
httpJsonInterceptor.setOnRequestIntercepted(
request -> {
Expand Down Expand Up @@ -336,7 +342,7 @@ public void testHttpJson_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried()
}

@Test
public void testHttpJson_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
void testHttpJson_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
List<String> capturedRequestIds = new ArrayList<>();
httpJsonInterceptor.setOnRequestIntercepted(
request -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class ITBidiStreaming {
class ITBidiStreaming {

private static EchoClient grpcClient;

@BeforeClass
public static void createClients() throws Exception {
@BeforeAll
static void createClients() throws Exception {
// Create gRPC Echo Client
grpcClient = TestClientInitializer.createGrpcEchoClient();
}

@AfterClass
public static void destroyClients() throws Exception {
@AfterAll
static void destroyClients() throws Exception {
grpcClient.close();
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}
Expand All @@ -57,7 +57,7 @@ public static void destroyClients() throws Exception {
// three requests, respond twice for every request etc. If that happens, the response content may
// not be exactly the same as request content.
@Test
public void testGrpc_splitCall_shouldListensToResponse() throws Exception {
void testGrpc_splitCall_shouldListensToResponse() throws Exception {
// given
List<String> expected = Arrays.asList("The rain in Spain stays mainly on the plain".split(" "));
TestResponseObserver responseObserver = new TestResponseObserver();
Expand Down
Loading
Loading