-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test(showcase): Add gRPC and HttpJson Showcase ITs to verify Error Details #13928
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
base: main
Are you sure you want to change the base?
Changes from all commits
f7af718
3d2c286
dbe16db
f7a9e69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * 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 | ||
| * | ||
| * https://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 com.google.showcase.v1beta1.it; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import com.google.api.client.http.HttpResponseException; | ||
| import com.google.api.gax.rpc.ApiException; | ||
| import com.google.api.gax.rpc.ErrorDetails; | ||
| import com.google.api.gax.rpc.StatusCode; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParser; | ||
| import com.google.protobuf.TypeRegistry; | ||
| import com.google.protobuf.util.JsonFormat; | ||
| import com.google.rpc.BadRequest; | ||
| import com.google.rpc.DebugInfo; | ||
| import com.google.rpc.ErrorInfo; | ||
| import com.google.rpc.Help; | ||
| import com.google.rpc.LocalizedMessage; | ||
| import com.google.rpc.PreconditionFailure; | ||
| import com.google.rpc.QuotaFailure; | ||
| import com.google.rpc.RequestInfo; | ||
| import com.google.rpc.ResourceInfo; | ||
| import com.google.rpc.RetryInfo; | ||
| import com.google.rpc.Status; | ||
| import com.google.showcase.v1beta1.EchoClient; | ||
| import com.google.showcase.v1beta1.EchoResponse; | ||
| import com.google.showcase.v1beta1.FailEchoWithDetailsRequest; | ||
| import com.google.showcase.v1beta1.PoetryError; | ||
| import com.google.showcase.v1beta1.it.util.TestClientInitializer; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Integration tests for verifying that client libraries correctly propagate and deserialize | ||
| * standard and custom error details from {@link ApiException} over gRPC and HTTP/JSON transports. | ||
| */ | ||
| class ITErrorDetails { | ||
|
|
||
| private static EchoClient grpcClient; | ||
| private static EchoClient httpjsonClient; | ||
|
|
||
| @BeforeAll | ||
| static void createClients() throws Exception { | ||
| grpcClient = TestClientInitializer.createGrpcEchoClient(); | ||
| httpjsonClient = TestClientInitializer.createHttpJsonEchoClient(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void destroyClients() throws InterruptedException { | ||
| grpcClient.close(); | ||
| httpjsonClient.close(); | ||
|
|
||
| grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); | ||
| httpjsonClient.awaitTermination( | ||
| TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); | ||
| } | ||
|
Comment on lines
+67
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If static void destroyClients() throws InterruptedException {
try {
try {
grpcClient.close();
} finally {
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}
} finally {
try {
httpjsonClient.close();
} finally {
httpjsonClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}
}
}References
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other existing integration tests uses this sequential pattern (such as ITIam.java, ITPagination.java, ITServerSideStreaming.java, etc.), we can leave this as-is to maintain consistency and readability. |
||
|
|
||
| /** | ||
| * Helper method to verify the content of error details. Transport-neutral validation of standard | ||
| * and custom error packets. | ||
| */ | ||
| private void verifyErrorDetailsContent(ErrorDetails errorDetails, String expectedPoem) { | ||
| // Verify standard error details are present and populated | ||
| // We are assuming that the mock server's hardcoded return values will stay the same | ||
| // https://github.com/googleapis/gapic-showcase/blob/b6c247f153369044d599969f6929ecdeb066c4c6/server/services/echo_service.go | ||
| ErrorInfo errorInfo = errorDetails.getErrorInfo(); | ||
| assertThat(errorInfo).isNotNull(); | ||
| assertThat(errorInfo.getReason()).isEqualTo("some ErrorInfo reason"); | ||
|
|
||
| RetryInfo retryInfo = errorDetails.getRetryInfo(); | ||
| assertThat(retryInfo).isNotNull(); | ||
| assertThat(retryInfo.getRetryDelay().getSeconds()).isEqualTo(11); | ||
|
|
||
| DebugInfo debugInfo = errorDetails.getDebugInfo(); | ||
| assertThat(debugInfo).isNotNull(); | ||
| assertThat(debugInfo.getDetail()).isEqualTo("a DebugInfo detail"); | ||
|
|
||
| QuotaFailure quotaFailure = errorDetails.getQuotaFailure(); | ||
| assertThat(quotaFailure).isNotNull(); | ||
| assertThat(quotaFailure.getViolations(0).getDescription()) | ||
| .isEqualTo("First QuotaFailure description"); | ||
|
|
||
| PreconditionFailure preconditionFailure = errorDetails.getPreconditionFailure(); | ||
| assertThat(preconditionFailure).isNotNull(); | ||
| assertThat(preconditionFailure.getViolations(0).getDescription()) | ||
| .isEqualTo("First PreconditionFailure description"); | ||
|
|
||
| BadRequest badRequest = errorDetails.getBadRequest(); | ||
| assertThat(badRequest).isNotNull(); | ||
| assertThat(badRequest.getFieldViolations(0).getDescription()) | ||
| .isEqualTo("First BadRequest description"); | ||
|
|
||
| RequestInfo requestInfo = errorDetails.getRequestInfo(); | ||
| assertThat(requestInfo).isNotNull(); | ||
| assertThat(requestInfo.getRequestId()).isEqualTo("RequestInfo: showcase-request-id"); | ||
| assertThat(requestInfo.getServingData()).isEqualTo("RequestInfo: showcase serving data"); | ||
|
|
||
| ResourceInfo resourceInfo = errorDetails.getResourceInfo(); | ||
| assertThat(resourceInfo).isNotNull(); | ||
| assertThat(resourceInfo.getResourceType()).isEqualTo("ResourceInfo: showcase resource"); | ||
|
|
||
| Help help = errorDetails.getHelp(); | ||
| assertThat(help).isNotNull(); | ||
| assertThat(help.getLinks(0).getDescription()).isEqualTo("Help: first showcase help link"); | ||
|
|
||
| LocalizedMessage localizedMessage = errorDetails.getLocalizedMessage(); | ||
| assertThat(localizedMessage).isNotNull(); | ||
| assertThat(localizedMessage.getLocale()).isEqualTo("fr-CH"); | ||
| assertThat(localizedMessage.getMessage()) | ||
| .isEqualTo("This LocalizedMessage should be treated specially"); | ||
|
|
||
| // Verify custom PoetryError can be unpacked and matches expected poem | ||
| PoetryError poetryError = errorDetails.getMessage(PoetryError.class); | ||
| assertThat(poetryError).isNotNull(); | ||
| assertThat(poetryError.getPoem()).isEqualTo(expectedPoem); | ||
|
|
||
| // Verify mismatched type returns null safely (mismatch unpacking) | ||
| EchoResponse mismatchedDetail = errorDetails.getMessage(EchoResponse.class); | ||
| assertThat(mismatchedDetail).isNull(); | ||
|
Comment on lines
+135
to
+137
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might be better as a unit test inside ErrorDetailsTest and not as an IT. |
||
| } | ||
|
|
||
| // Verifies error details are correctly propagated and unpacked over standard gRPC protocol | ||
| @Test | ||
| void testGrpc_failEchoWithDetails() { | ||
| FailEchoWithDetailsRequest request = FailEchoWithDetailsRequest.newBuilder().build(); | ||
| ApiException exception = | ||
| assertThrows(ApiException.class, () -> grpcClient.failEchoWithDetails(request)); | ||
|
|
||
| assertThat(exception.getStatusCode().getCode()).isEqualTo(StatusCode.Code.ABORTED); | ||
| assertThat(exception.getErrorDetails()).isNotNull(); | ||
|
|
||
| // Reuse Transport-neutral Validation | ||
| verifyErrorDetailsContent(exception.getErrorDetails(), "roses are red"); | ||
| } | ||
|
|
||
| // Verifies custom Error Details messages reflect user-defined inputs via gRPC | ||
| @Test | ||
| void testGrpc_failEchoWithDetails_customMessage() { | ||
| String customMessage = "this is a custom message to echo back"; | ||
| FailEchoWithDetailsRequest request = | ||
| FailEchoWithDetailsRequest.newBuilder().setMessage(customMessage).build(); | ||
| ApiException exception = | ||
| assertThrows(ApiException.class, () -> grpcClient.failEchoWithDetails(request)); | ||
|
|
||
| assertThat(exception.getStatusCode().getCode()).isEqualTo(StatusCode.Code.ABORTED); | ||
| assertThat(exception.getErrorDetails()).isNotNull(); | ||
|
|
||
| // Reuse Transport-neutral Validation | ||
| verifyErrorDetailsContent(exception.getErrorDetails(), customMessage); | ||
| } | ||
|
|
||
| // Verifies error details are accessible in raw form over REST/HTTP and validates manually-parsed | ||
| // decompression | ||
| @Test | ||
| void testHttpJson_failEchoWithDetails() throws Exception { | ||
| FailEchoWithDetailsRequest request = FailEchoWithDetailsRequest.newBuilder().build(); | ||
| ApiException exception = | ||
| assertThrows(ApiException.class, () -> httpjsonClient.failEchoWithDetails(request)); | ||
| assertThat(exception.getStatusCode().getCode()).isEqualTo(StatusCode.Code.ABORTED); | ||
|
|
||
| // GAX HTTP/JSON parser limitation: Because the response contains a custom/unregistered type | ||
| // (PoetryError) in the Any details list, HttpJsonErrorParser fails to parse the status payload, | ||
| // resulting in empty/null ErrorDetails. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty/null ErrorInfo? |
||
| ErrorDetails errorDetails = exception.getErrorDetails(); | ||
| if (errorDetails != null) { | ||
| assertThat(errorDetails.getErrorInfo()).isNull(); | ||
| } | ||
|
nnicolee marked this conversation as resolved.
|
||
|
|
||
| // Workaround REST limitation: Parse the raw HTTP JSON error response manually using a custom | ||
| // TypeRegistry that registers standard types plus the showcase-specific PoetryError type. | ||
|
Comment on lines
+187
to
+188
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. small comment to note: This mimics the behavior in |
||
| assertThat(exception.getCause()).isInstanceOf(HttpResponseException.class); | ||
| HttpResponseException httpException = (HttpResponseException) exception.getCause(); | ||
| String errorJson = httpException.getContent(); | ||
| assertThat(errorJson).isNotNull(); | ||
|
|
||
| TypeRegistry typeRegistry = | ||
| TypeRegistry.newBuilder() | ||
| .add(ErrorInfo.getDescriptor()) | ||
| .add(RetryInfo.getDescriptor()) | ||
| .add(DebugInfo.getDescriptor()) | ||
| .add(QuotaFailure.getDescriptor()) | ||
| .add(PreconditionFailure.getDescriptor()) | ||
| .add(BadRequest.getDescriptor()) | ||
| .add(RequestInfo.getDescriptor()) | ||
| .add(ResourceInfo.getDescriptor()) | ||
| .add(Help.getDescriptor()) | ||
| .add(LocalizedMessage.getDescriptor()) | ||
| .add(PoetryError.getDescriptor()) | ||
| .build(); | ||
| JsonFormat.Parser jsonParser = | ||
| JsonFormat.parser().ignoringUnknownFields().usingTypeRegistry(typeRegistry); | ||
|
|
||
| // Parse the AIP-193 "error" JSON object into a status builder | ||
| JsonObject root = JsonParser.parseString(errorJson).getAsJsonObject(); | ||
| JsonObject errorObj = root.getAsJsonObject("error"); | ||
| Status.Builder statusBuilder = Status.newBuilder(); | ||
| jsonParser.merge(errorObj.toString(), statusBuilder); | ||
| Status status = statusBuilder.build(); | ||
|
|
||
| // Verify we can successfully unpack the details from our custom status instance | ||
| ErrorDetails parsedDetails = | ||
| ErrorDetails.builder().setRawErrorMessages(status.getDetailsList()).build(); | ||
|
|
||
| // Reuse Transport-neutral Validation! | ||
| verifyErrorDetailsContent(parsedDetails, "roses are red"); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.