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: Add REGAPIC coverage #1431

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion .github/workflows/downstream-native-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ jobs:
java -version
sudo apt-get update -y
sudo apt-get install libxml2-utils
mvn test -Pnative,-showcase -Denforcer.skip=true -ntp -B
mvn test -Pnative,-showcase,-enable-integration-tests -Denforcer.skip=true -ntp -B
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ HttpRequest createHttpRequest() throws IOException {
jsonFactory.createJsonParser(requestBody).parse(tokenRequest);
jsonHttpContent =
new JsonHttpContent(jsonFactory, tokenRequest)
.setMediaType((new HttpMediaType("application/json")));
.setMediaType((new HttpMediaType("application/json; charset=utf-8")));
} else {
// Force underlying HTTP lib to set Content-Length header to avoid 411s.
// See EmptyContent.java.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.google.longrunning.ListOperationsRequest;
import com.google.protobuf.Empty;
import com.google.protobuf.Field;
import com.google.protobuf.util.JsonFormat;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -47,9 +49,13 @@
import org.mockito.Mockito;

public class HttpRequestRunnableTest {
private static final String MEDIA_TYPE = "application/json; charset=utf-8";

private static Field requestMessage;
private static Field bodyRequestMessage;
private static final String ENDPOINT = "https://www.googleapis.com/animals/v1/projects";
private static HttpRequestFormatter<Field> requestFormatter;
private static HttpRequestFormatter<Field> bodyRequestFormatter;
private static HttpResponseParser<Empty> responseParser;

@SuppressWarnings("unchecked")
Expand All @@ -64,6 +70,15 @@ public static void setUp() {
.setTypeUrl("small")
.build();

bodyRequestMessage =
Field.newBuilder()
.setName("feline ☺ → ←")
.setNumber(2)
.setDefaultValue("bird ☺ → ←")
.setJsonName("mouse ☺ → ←")
.setTypeUrl("small ☺ → ←")
.build();

requestFormatter =
ProtoMessageRequestFormatter.<Field>newBuilder()
.setPath(
Expand Down Expand Up @@ -91,6 +106,22 @@ public static void setUp() {
.setRequestBodyExtractor(request -> null)
.build();

bodyRequestFormatter =
ProtoMessageRequestFormatter.<Field>newBuilder()
.setPath(
"/name/{name=*}",
request -> {
Map<String, String> fields = new HashMap<>();
ProtoRestSerializer<Field> serializer = ProtoRestSerializer.create();
serializer.putPathParam(fields, "name", request.getName());
return fields;
})
.setQueryParamsExtractor(request -> new HashMap<>())
.setRequestBodyExtractor(
request ->
ProtoRestSerializer.create().toBody("*", request.toBuilder().build(), true))
.build();

responseParser = Mockito.mock(HttpResponseParser.class);
}

Expand Down Expand Up @@ -177,4 +208,40 @@ public void testRequestUrlUnnormalizedPatch() throws IOException {
Truth.assertThat(httpRequest.getRequestMethod()).isEqualTo("POST");
Truth.assertThat(httpRequest.getHeaders().get("X-HTTP-Method-Override")).isEqualTo("PATCH");
}

/*
We use a separate RequestFormatter as formatting the body requests sets the charset to be UTF-8.
The other tests above do not have a set a body request and sent as EmptyContent which has a null Type/ CharSet
*/
@Test
public void testUnicodeValuesInBody() throws IOException {
ApiMethodDescriptor<Field, Empty> methodDescriptor =
ApiMethodDescriptor.<Field, Empty>newBuilder()
.setFullMethodName("house.cat.get")
.setHttpMethod("PUT")
.setRequestFormatter(bodyRequestFormatter)
.setResponseParser(responseParser)
.build();

HttpRequestRunnable<Field, Empty> httpRequestRunnable =
new HttpRequestRunnable<>(
bodyRequestMessage,
methodDescriptor,
"www.googleapis.com/animals/v1/projects",
HttpJsonCallOptions.newBuilder().build(),
new MockHttpTransport(),
HttpJsonMetadata.newBuilder().build(),
(result) -> {});

HttpRequest httpRequest = httpRequestRunnable.createHttpRequest();
Truth.assertThat(httpRequest.getContent().getType()).isEqualTo(MEDIA_TYPE);
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
httpRequest.getContent().writeTo(byteArrayOutputStream);
String output = byteArrayOutputStream.toString();
Field.Builder expectedBuilder = Field.newBuilder();
JsonFormat.parser().merge(output, expectedBuilder);
Field expected = expectedBuilder.build();
Truth.assertThat(expected).isEqualTo(bodyRequestMessage);
}
}
}
42 changes: 40 additions & 2 deletions showcase/gapic-showcase/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>gapic-showcase</artifactId>
Expand All @@ -20,6 +20,7 @@

<properties>
<fmt.skip>true</fmt.skip>
<gapic.showcase.version>0.25.0</gapic.showcase.version>
</properties>

<profiles>
Expand Down Expand Up @@ -79,6 +80,33 @@
</plugins>
</build>
</profile>
<profile>
<id>enable-integration-tests</id>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.6.8</version>
<executions>
<execution>
<id>download-compliance-suite</id>
<phase>test</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>
https://raw.githubusercontent.com/googleapis/gapic-showcase/v${gapic.showcase.version}/server/services/compliance_suite.json
</url>
<outputDirectory>src/test/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<build>
Expand Down Expand Up @@ -155,12 +183,22 @@
<artifactId>gax-httpjson</artifactId>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.api.grpc</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.google.showcase.v1beta1.it;

import static com.google.common.truth.Truth.assertThat;

import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ProtocolStringList;
import com.google.protobuf.util.JsonFormat;
import com.google.showcase.v1beta1.ComplianceClient;
import com.google.showcase.v1beta1.ComplianceGroup;
import com.google.showcase.v1beta1.ComplianceSettings;
import com.google.showcase.v1beta1.ComplianceSuite;
import com.google.showcase.v1beta1.EchoSettings;
import com.google.showcase.v1beta1.RepeatRequest;
import com.google.showcase.v1beta1.RepeatResponse;
import java.io.FileReader;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Map;
import java.util.function.Function;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class ITCompliance {

private static ComplianceSuite complianceSuite;
private static ComplianceClient complianceClient;
private static Map<String, Function<RepeatRequest, RepeatResponse>> complianceValidRpcSet;

@BeforeClass
public static void createClient() throws IOException, GeneralSecurityException {
ComplianceSuite.Builder builder = ComplianceSuite.newBuilder();
JsonFormat.parser().merge(new FileReader("src/test/resources/compliance_suite.json"), builder);
complianceSuite = builder.build();

ComplianceSettings httpjsonComplianceSettings =
ComplianceSettings.newHttpJsonBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultHttpJsonTransportProviderBuilder()
.setHttpTransport(
new NetHttpTransport.Builder().doNotValidateCertificate().build())
.setEndpoint("http://localhost:7469")
.build())
.build();
complianceClient = ComplianceClient.create(httpjsonComplianceSettings);

complianceValidRpcSet =
ImmutableMap.of(
"Compliance.RepeatDataBody",
x -> complianceClient.repeatDataBody(x),
"Compliance.RepeatDataBodyInfo",
x -> complianceClient.repeatDataBodyInfo(x),
"Compliance.RepeatDataQuery",
x -> complianceClient.repeatDataQuery(x),
"Compliance.RepeatDataSimplePath",
x -> complianceClient.repeatDataSimplePath(x),
"Compliance.RepeatDataBodyPut",
x -> complianceClient.repeatDataBodyPut(x),
"Compliance.RepeatDataBodyPatch",
x -> complianceClient.repeatDataBodyPatch(x),
// TODO(lawrence): Figure out what is causing this test failure
// "Compliance.RepeatDataPathResource",
// x -> complianceClient.repeatDataPathResource(x),
"Compliance.RepeatDataPathTrailingResource",
x -> complianceClient.repeatDataPathTrailingResource(x));
}

@AfterClass
public static void destroyClient() {
complianceClient.close();
}

/*
Note: For PATCH requests, Gax HttpJson sets the HttpVerb as POST and sets the
`x-http-method-override` method as PATCH.
*/
@Test
public void testCompliance() {
for (ComplianceGroup compliancegroup : complianceSuite.getGroupList()) {
ProtocolStringList protocolStringList = compliancegroup.getRpcsList();
for (RepeatRequest repeatRequest : compliancegroup.getRequestsList()) {
for (String rpcName : protocolStringList) {
if (complianceValidRpcSet.containsKey(rpcName)) {
System.out.printf(
"Testing group: `%s`- RPC Name: `%s` with Request Name: `%s`\n",
compliancegroup.getName(), rpcName, repeatRequest.getName());
RepeatResponse response = complianceValidRpcSet.get(rpcName).apply(repeatRequest);
assertThat(response.getRequest().getInfo()).isEqualTo(repeatRequest.getInfo());
}
}
}
}
}
}
Loading