Skip to content

Commit

Permalink
fix: [REGAPIC] Fix snake_case nested resource names in generated unit…
Browse files Browse the repository at this point in the history
… tests (#990)
  • Loading branch information
vam-google authored May 12, 2022
1 parent f7ceab9 commit b8e3ae4
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ public static Expr createValue(
String nestedFieldName = field.name();
Map<String, String> nestedValuePatterns = new HashMap<>();
for (Map.Entry<String, String> entry : valuePatterns.entrySet()) {
if (entry.getKey().startsWith(nestedFieldName + '.')) {
String lowerCamelNestedFieldName = JavaStyle.toLowerCamelCase(nestedFieldName);
if (entry.getKey().startsWith(lowerCamelNestedFieldName + '.')) {
nestedValuePatterns.put(
entry.getKey().substring(nestedFieldName.length() + 1), entry.getValue());
entry.getKey().substring(lowerCamelNestedFieldName.length() + 1), entry.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public class EchoClient implements BackgroundResource {
* .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setSeverity(Severity.forNumber(0))
* .setFoobar(Foobar.newBuilder().build())
* .setFooBar(Foobar.newBuilder().build())
* .build();
* EchoResponse response = echoClient.echo(request);
* }
Expand All @@ -386,7 +386,7 @@ public class EchoClient implements BackgroundResource {
* .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setSeverity(Severity.forNumber(0))
* .setFoobar(Foobar.newBuilder().build())
* .setFooBar(Foobar.newBuilder().build())
* .build();
* ApiFuture<EchoResponse> future = echoClient.echoCallable().futureCall(request);
* // Do something.
Expand Down Expand Up @@ -772,7 +772,7 @@ public class EchoClient implements BackgroundResource {
* .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setSeverity(Severity.forNumber(0))
* .setFoobar(Foobar.newBuilder().build())
* .setFooBar(Foobar.newBuilder().build())
* .build();
* Object response = echoClient.collideName(request);
* }
Expand All @@ -798,7 +798,7 @@ public class EchoClient implements BackgroundResource {
* .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setSeverity(Severity.forNumber(0))
* .setFoobar(Foobar.newBuilder().build())
* .setFooBar(Foobar.newBuilder().build())
* .build();
* ApiFuture<Object> future = echoClient.collideNameCallable().futureCall(request);
* // Do something.
Expand All @@ -810,6 +810,57 @@ public class EchoClient implements BackgroundResource {
return stub.collideNameCallable();
}

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Sample code:
*
* <pre>{@code
* // This snippet has been automatically generated for illustrative purposes only.
* // It may require modifications to work in your environment.
* try (EchoClient echoClient = EchoClient.create()) {
* EchoRequest request =
* EchoRequest.newBuilder()
* .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setSeverity(Severity.forNumber(0))
* .setFooBar(Foobar.newBuilder().build())
* .build();
* Object response = echoClient.nestedBinding(request);
* }
* }</pre>
*
* @param request The request object containing all of the parameters for the API call.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final Object nestedBinding(EchoRequest request) {
return nestedBindingCallable().call(request);
}

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Sample code:
*
* <pre>{@code
* // This snippet has been automatically generated for illustrative purposes only.
* // It may require modifications to work in your environment.
* try (EchoClient echoClient = EchoClient.create()) {
* EchoRequest request =
* EchoRequest.newBuilder()
* .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
* .setSeverity(Severity.forNumber(0))
* .setFooBar(Foobar.newBuilder().build())
* .build();
* ApiFuture<Object> future = echoClient.nestedBindingCallable().futureCall(request);
* // Do something.
* Object response = future.get();
* }
* }</pre>
*/
public final UnaryCallable<EchoRequest, Object> nestedBindingCallable() {
return stub.nestedBindingCallable();
}

@Override
public final void close() {
stub.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public class EchoClientHttpJsonTest {
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFoobar(Foobar.newBuilder().build())
.setFooBar(Foobar.newBuilder().build())
.build();
client.echo(request);
Assert.fail("No exception raised");
Expand Down Expand Up @@ -702,7 +702,7 @@ public class EchoClientHttpJsonTest {
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFoobar(Foobar.newBuilder().build())
.setFooBar(Foobar.newBuilder().build())
.build();

Object actualResponse = client.collideName(request);
Expand Down Expand Up @@ -736,12 +736,73 @@ public class EchoClientHttpJsonTest {
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFoobar(Foobar.newBuilder().build())
.setFooBar(Foobar.newBuilder().build())
.build();
client.collideName(request);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}

@Test
public void nestedBindingTest() throws Exception {
Object expectedResponse = Object.newBuilder().setContent("content951530617").build();
mockService.addResponse(expectedResponse);

EchoRequest request =
EchoRequest.newBuilder()
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFooBar(
Foobar.newBuilder()
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setInfo("info3237038")
.build())
.build();

Object actualResponse = client.nestedBinding(request);
Assert.assertEquals(expectedResponse, actualResponse);

List<String> actualRequests = mockService.getRequestPaths();
Assert.assertEquals(1, actualRequests.size());

String apiClientHeaderKey =
mockService
.getRequestHeaders()
.get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
.iterator()
.next();
Assert.assertTrue(
GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
.matcher(apiClientHeaderKey)
.matches());
}

@Test
public void nestedBindingExceptionTest() throws Exception {
ApiException exception =
ApiExceptionFactory.createException(
new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
mockService.addException(exception);

try {
EchoRequest request =
EchoRequest.newBuilder()
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFooBar(
Foobar.newBuilder()
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setInfo("info3237038")
.build())
.build();
client.nestedBinding(request);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public class EchoClientTest {
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFoobar(Foobar.newBuilder().build())
.setFooBar(Foobar.newBuilder().build())
.build();
client.echo(request);
Assert.fail("No exception raised");
Expand Down Expand Up @@ -679,7 +679,7 @@ public class EchoClientTest {
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFoobar(Foobar.newBuilder().build())
.setFooBar(Foobar.newBuilder().build())
.build();

Object actualResponse = client.collideName(request);
Expand All @@ -694,7 +694,7 @@ public class EchoClientTest {
Assert.assertEquals(request.getContent(), actualRequest.getContent());
Assert.assertEquals(request.getError(), actualRequest.getError());
Assert.assertEquals(request.getSeverity(), actualRequest.getSeverity());
Assert.assertEquals(request.getFoobar(), actualRequest.getFoobar());
Assert.assertEquals(request.getFooBar(), actualRequest.getFooBar());
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
Expand All @@ -712,12 +712,64 @@ public class EchoClientTest {
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFoobar(Foobar.newBuilder().build())
.setFooBar(Foobar.newBuilder().build())
.build();
client.collideName(request);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}

@Test
public void nestedBindingTest() throws Exception {
Object expectedResponse = Object.newBuilder().setContent("content951530617").build();
mockEcho.addResponse(expectedResponse);

EchoRequest request =
EchoRequest.newBuilder()
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFooBar(Foobar.newBuilder().build())
.build();

Object actualResponse = client.nestedBinding(request);
Assert.assertEquals(expectedResponse, actualResponse);

List<AbstractMessage> actualRequests = mockEcho.getRequests();
Assert.assertEquals(1, actualRequests.size());
EchoRequest actualRequest = ((EchoRequest) actualRequests.get(0));

Assert.assertEquals(request.getName(), actualRequest.getName());
Assert.assertEquals(request.getParent(), actualRequest.getParent());
Assert.assertEquals(request.getContent(), actualRequest.getContent());
Assert.assertEquals(request.getError(), actualRequest.getError());
Assert.assertEquals(request.getSeverity(), actualRequest.getSeverity());
Assert.assertEquals(request.getFooBar(), actualRequest.getFooBar());
Assert.assertTrue(
channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

@Test
public void nestedBindingExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockEcho.addException(exception);

try {
EchoRequest request =
EchoRequest.newBuilder()
.setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
.setSeverity(Severity.forNumber(0))
.setFooBar(Foobar.newBuilder().build())
.build();
client.nestedBinding(request);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public class EchoSettings extends ClientSettings<EchoSettings> {
return ((EchoStubSettings) getStubSettings()).collideNameSettings();
}

/** Returns the object with the settings used for calls to nestedBinding. */
public UnaryCallSettings<EchoRequest, Object> nestedBindingSettings() {
return ((EchoStubSettings) getStubSettings()).nestedBindingSettings();
}

public static final EchoSettings create(EchoStubSettings stub) throws IOException {
return new EchoSettings.Builder(stub.toBuilder()).build();
}
Expand Down Expand Up @@ -262,6 +267,11 @@ public class EchoSettings extends ClientSettings<EchoSettings> {
return getStubSettingsBuilder().collideNameSettings();
}

/** Returns the builder for the settings used for calls to nestedBinding. */
public UnaryCallSettings.Builder<EchoRequest, Object> nestedBindingSettings() {
return getStubSettingsBuilder().nestedBindingSettings();
}

@Override
public EchoSettings build() throws IOException {
return new EchoSettings(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public class EchoSettings extends ClientSettings<EchoSettings> {
return ((EchoStubSettings) getStubSettings()).collideNameSettings();
}

/** Returns the object with the settings used for calls to nestedBinding. */
public UnaryCallSettings<EchoRequest, Object> nestedBindingSettings() {
return ((EchoStubSettings) getStubSettings()).nestedBindingSettings();
}

public static final EchoSettings create(EchoStubSettings stub) throws IOException {
return new EchoSettings.Builder(stub.toBuilder()).build();
}
Expand Down Expand Up @@ -262,6 +267,11 @@ public class EchoSettings extends ClientSettings<EchoSettings> {
return getStubSettingsBuilder().collideNameSettings();
}

/** Returns the builder for the settings used for calls to nestedBinding. */
public UnaryCallSettings.Builder<EchoRequest, Object> nestedBindingSettings() {
return getStubSettingsBuilder().nestedBindingSettings();
}

@Override
public EchoSettings build() throws IOException {
return new EchoSettings(this);
Expand Down
Loading

0 comments on commit b8e3ae4

Please sign in to comment.