Skip to content

Commit

Permalink
Merge branch 'alpha/g10' of github.com:googleapis/gapic-generator-jav…
Browse files Browse the repository at this point in the history
…a into alpha/g11
  • Loading branch information
miraleung committed Oct 29, 2020
2 parents 08bb6a9 + f652052 commit 34fd4d6
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 52 deletions.
1 change: 0 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
startup --batch

build --protocopt=--include_source_info
build --protocopt=--experimental_allow_proto3_optional
13 changes: 13 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,16 @@ below are temporary and better ones will be coming.
```sh
bazel run //src/test/java/com/google/api/generator/engine:JavaCodeGeneratorTest_update
```

- Run a single integration test for API like `Redis`, it generates Java source code using
the Java microgenerator and compares them with the goldens files in `test/integration/goldens/redis`.

```sh
bazel test //test/integration:redis
```

- Update goldens files based on code generation in integration test, for example `Redis`. It generates Java source code using the Java microgenerator and overwrites the goldens files in `test/integration/goldens/redis` based on code generation.

```sh
bazel run //test/integration:redis_update
```
83 changes: 83 additions & 0 deletions rules_bazel/java/integration_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,86 @@ def integration_test(name, target, data):
test_library = "%s_test" % target,
srcs = data,
)

def _overwrite_golden_impl(ctx):
# Extract the Java source files from the generated 3 srcjars from API bazel target,
# and put them in the temporary folder `codegen_tmp`, zip as `goldens_output_zip`.
# Overwrite the goldens folder e.g `test/integration/goldens/redis` with the
# code generation in `goldens_output_zip`.

gapic_library = ctx.attr.gapic_library
resource_name_library = ctx.attr.resource_name_library
test_library = ctx.attr.test_library
srcs = ctx.files.srcs
# Convert the name of bazel rules e.g. `redis_update` to `redis`
# because we will need to overwrite the goldens files in `redis` folder.
api_name = "_".join(ctx.attr.name.split("_")[:-1])
goldens_output_zip = ctx.outputs.goldens_output_zip

script = """
mkdir codegen_tmp
unzip -j {input} -d codegen_tmp
unzip -j {input_resource_name} -d codegen_tmp
unzip -j {input_test} -d codegen_tmp
cd codegen_tmp
# Remove unneeded non-Java files, like MANIFEST
rm -rf $(find . -type f ! -name "*.java")
zip -r ../{goldens_output_zip} .
""".format(
goldens_output_zip = goldens_output_zip.path,
input = gapic_library[JavaInfo].source_jars[0].path,
input_resource_name = resource_name_library[JavaInfo].source_jars[0].path,
input_test = test_library[JavaInfo].source_jars[0].path,
)

ctx.actions.run_shell(
inputs = srcs + [
gapic_library[JavaInfo].source_jars[0],
resource_name_library[JavaInfo].source_jars[0],
test_library[JavaInfo].source_jars[0],
],
outputs = [goldens_output_zip],
command = script,
)

# Overwrite the goldens.
golden_update_script_content = """
cd ${{BUILD_WORKSPACE_DIRECTORY}}
unzip -ao {goldens_output_zip} -d test/integration/goldens/{api_name}
""".format(
goldens_output_zip = goldens_output_zip.path,
api_name = api_name,
)
ctx.actions.write(
output = ctx.outputs.golden_update_script,
content = golden_update_script_content,
is_executable = True,
)
return [DefaultInfo(executable = ctx.outputs.golden_update_script)]

overwrite_golden = rule(
attrs = {
"gapic_library": attr.label(),
"resource_name_library": attr.label(),
"test_library": attr.label(),
"srcs": attr.label_list(
allow_files = True,
mandatory = True,
),
},
outputs = {
"goldens_output_zip": "%{name}.zip",
"golden_update_script": "%{name}.sh",
},
executable = True,
implementation = _overwrite_golden_impl,
)

def golden_update(name, target, data):
overwrite_golden(
name = name,
gapic_library = target,
resource_name_library = "%s_resource_name" % target,
test_library = "%s_test" % target,
srcs = data,
)
13 changes: 13 additions & 0 deletions test/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load(
)
load(
"//:rules_bazel/java/integration_test.bzl",
"golden_update",
"integration_test",
)

Expand All @@ -27,6 +28,18 @@ integration_test(
target = ":asset_java_gapic",
)

golden_update(
name = "redis_update",
data = ["//test/integration/goldens/redis:goldens_files"],
target = ":redis_java_gapic",
)

golden_update(
name = "asset_update",
data = ["//test/integration/goldens/asset:goldens_files"],
target = ":asset_java_gapic",
)

####################################################
# API Library Rules
####################################################
Expand Down
45 changes: 24 additions & 21 deletions test/integration/goldens/asset/AssetServiceClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.google.cloud.asset.v1;

import static com.google.cloud.asset.v1.AssetServiceClient.SearchAllIamPoliciesPagedResponse;
import static com.google.cloud.asset.v1.AssetServiceClient.SearchAllResourcesPagedResponse;

import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.GaxGrpcProperties;
import com.google.api.gax.grpc.testing.LocalChannelProvider;
Expand Down Expand Up @@ -83,7 +86,7 @@ public void tearDown() throws Exception {
}

@Test
public void exportAssetsTest() {
public void exportAssetsTest() throws Exception {
ExportAssetsResponse expectedResponse =
ExportAssetsResponse.newBuilder()
.setOutputConfig(OutputConfig.newBuilder().build())
Expand All @@ -99,7 +102,7 @@ public void exportAssetsTest() {

ExportAssetsRequest request =
ExportAssetsRequest.newBuilder()
.setParent(ProjectName.of("[PROJECT]").toString())
.setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
.addAllAssetTypes(new ArrayList<>())
.setOutputConfig(OutputConfig.newBuilder().build())
.build();
Expand Down Expand Up @@ -130,7 +133,7 @@ public void exportAssetsExceptionTest() throws Exception {
try {
ExportAssetsRequest request =
ExportAssetsRequest.newBuilder()
.setParent(ProjectName.of("[PROJECT]").toString())
.setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
.addAllAssetTypes(new ArrayList<>())
.setOutputConfig(OutputConfig.newBuilder().build())
.build();
Expand All @@ -144,14 +147,14 @@ public void exportAssetsExceptionTest() throws Exception {
}

@Test
public void batchGetAssetsHistoryTest() {
public void batchGetAssetsHistoryTest() throws Exception {
BatchGetAssetsHistoryResponse expectedResponse =
BatchGetAssetsHistoryResponse.newBuilder().addAllAssets(new ArrayList<>()).build();
mockAssetService.addResponse(expectedResponse);

BatchGetAssetsHistoryRequest request =
BatchGetAssetsHistoryRequest.newBuilder()
.setParent(ProjectName.of("[PROJECT]").toString())
.setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
.addAllAssetNames(new ArrayList<>())
.setReadTimeWindow(TimeWindow.newBuilder().build())
.build();
Expand Down Expand Up @@ -182,7 +185,7 @@ public void batchGetAssetsHistoryExceptionTest() throws Exception {
try {
BatchGetAssetsHistoryRequest request =
BatchGetAssetsHistoryRequest.newBuilder()
.setParent(ProjectName.of("[PROJECT]").toString())
.setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
.addAllAssetNames(new ArrayList<>())
.setReadTimeWindow(TimeWindow.newBuilder().build())
.build();
Expand All @@ -194,7 +197,7 @@ public void batchGetAssetsHistoryExceptionTest() throws Exception {
}

@Test
public void createFeedTest() {
public void createFeedTest() throws Exception {
Feed expectedResponse =
Feed.newBuilder()
.setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
Expand Down Expand Up @@ -235,7 +238,7 @@ public void createFeedExceptionTest() throws Exception {
}

@Test
public void getFeedTest() {
public void getFeedTest() throws Exception {
Feed expectedResponse =
Feed.newBuilder()
.setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
Expand Down Expand Up @@ -276,7 +279,7 @@ public void getFeedExceptionTest() throws Exception {
}

@Test
public void getFeedTest2() {
public void getFeedTest2() throws Exception {
Feed expectedResponse =
Feed.newBuilder()
.setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
Expand Down Expand Up @@ -317,7 +320,7 @@ public void getFeedExceptionTest2() throws Exception {
}

@Test
public void listFeedsTest() {
public void listFeedsTest() throws Exception {
ListFeedsResponse expectedResponse =
ListFeedsResponse.newBuilder().addAllFeeds(new ArrayList<>()).build();
mockAssetService.addResponse(expectedResponse);
Expand Down Expand Up @@ -353,7 +356,7 @@ public void listFeedsExceptionTest() throws Exception {
}

@Test
public void updateFeedTest() {
public void updateFeedTest() throws Exception {
Feed expectedResponse =
Feed.newBuilder()
.setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
Expand Down Expand Up @@ -394,7 +397,7 @@ public void updateFeedExceptionTest() throws Exception {
}

@Test
public void deleteFeedTest() {
public void deleteFeedTest() throws Exception {
Empty expectedResponse = Empty.newBuilder().build();
mockAssetService.addResponse(expectedResponse);

Expand Down Expand Up @@ -429,7 +432,7 @@ public void deleteFeedExceptionTest() throws Exception {
}

@Test
public void deleteFeedTest2() {
public void deleteFeedTest2() throws Exception {
Empty expectedResponse = Empty.newBuilder().build();
mockAssetService.addResponse(expectedResponse);

Expand Down Expand Up @@ -464,26 +467,26 @@ public void deleteFeedExceptionTest2() throws Exception {
}

@Test
public void searchAllResourcesTest() {
public void searchAllResourcesTest() throws Exception {
ResourceSearchResult responsesElement = ResourceSearchResult.newBuilder().build();
SearchAllResourcesResponse expectedResponse =
SearchAllResourcesResponse.newBuilder()
.setNextPageToken("")
.addAllResponses(Arrays.asList(responsesElement))
.addAllResults(Arrays.asList(responsesElement))
.build();
mockAssetService.addResponse(expectedResponse);

String scope = "scope109264468";
String query = "query107944136";
List<String> assetTypes = new ArrayList<>();

SearchAllResourcesResponse pagedListResponse =
SearchAllResourcesPagedResponse pagedListResponse =
client.searchAllResources(scope, query, assetTypes);

List<ResourceSearchResult> resources = Lists.newArrayList(pagedListResponse.iterateAll());

Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getResponsesList().get(0), resources.get(0));
Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0));

List<AbstractMessage> actualRequests = mockAssetService.getRequests();
Assert.assertEquals(1, actualRequests.size());
Expand Down Expand Up @@ -515,24 +518,24 @@ public void searchAllResourcesExceptionTest() throws Exception {
}

@Test
public void searchAllIamPoliciesTest() {
public void searchAllIamPoliciesTest() throws Exception {
IamPolicySearchResult responsesElement = IamPolicySearchResult.newBuilder().build();
SearchAllIamPoliciesResponse expectedResponse =
SearchAllIamPoliciesResponse.newBuilder()
.setNextPageToken("")
.addAllResponses(Arrays.asList(responsesElement))
.addAllResults(Arrays.asList(responsesElement))
.build();
mockAssetService.addResponse(expectedResponse);

String scope = "scope109264468";
String query = "query107944136";

SearchAllIamPoliciesResponse pagedListResponse = client.searchAllIamPolicies(scope, query);
SearchAllIamPoliciesPagedResponse pagedListResponse = client.searchAllIamPolicies(scope, query);

List<IamPolicySearchResult> resources = Lists.newArrayList(pagedListResponse.iterateAll());

Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getResponsesList().get(0), resources.get(0));
Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0));

List<AbstractMessage> actualRequests = mockAssetService.getRequests();
Assert.assertEquals(1, actualRequests.size());
Expand Down
18 changes: 9 additions & 9 deletions test/integration/goldens/asset/FeedName.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ public static FeedName of(String project, String feed) {
}

@BetaApi("The static create methods are not stable yet and may be changed in the future.")
public static FeedName ofProjectFeedBuilder(String project, String feed) {
public static FeedName ofProjectFeedName(String project, String feed) {
return newBuilder().setProject(project).setFeed(feed).build();
}

@BetaApi("The static create methods are not stable yet and may be changed in the future.")
public static FeedName ofFolderFeedBuilder(String folder, String feed) {
public static FeedName ofFolderFeedName(String folder, String feed) {
return newFolderFeedBuilder().setFolder(folder).setFeed(feed).build();
}

@BetaApi("The static create methods are not stable yet and may be changed in the future.")
public static FeedName ofOrganizationFeedBuilder(String organization, String feed) {
public static FeedName ofOrganizationFeedName(String organization, String feed) {
return newOrganizationFeedBuilder().setOrganization(organization).setFeed(feed).build();
}

Expand All @@ -132,17 +132,17 @@ public static String format(String project, String feed) {
}

@BetaApi("The static format methods are not stable yet and may be changed in the future.")
public static String formatProjectFeedBuilder(String project, String feed) {
public static String formatProjectFeedName(String project, String feed) {
return newBuilder().setProject(project).setFeed(feed).build().toString();
}

@BetaApi("The static format methods are not stable yet and may be changed in the future.")
public static String formatFolderFeedBuilder(String folder, String feed) {
public static String formatFolderFeedName(String folder, String feed) {
return newFolderFeedBuilder().setFolder(folder).setFeed(feed).build().toString();
}

@BetaApi("The static format methods are not stable yet and may be changed in the future.")
public static String formatOrganizationFeedBuilder(String organization, String feed) {
public static String formatOrganizationFeedName(String organization, String feed) {
return newOrganizationFeedBuilder()
.setOrganization(organization)
.setFeed(feed)
Expand All @@ -156,13 +156,13 @@ public static FeedName parse(String formattedString) {
}
if (PROJECT_FEED.matches(formattedString)) {
Map<String, String> matchMap = PROJECT_FEED.match(formattedString);
return ofProjectFeedBuilder(matchMap.get("project"), matchMap.get("feed"));
return ofProjectFeedName(matchMap.get("project"), matchMap.get("feed"));
} else if (FOLDER_FEED.matches(formattedString)) {
Map<String, String> matchMap = FOLDER_FEED.match(formattedString);
return ofFolderFeedBuilder(matchMap.get("folder"), matchMap.get("feed"));
return ofFolderFeedName(matchMap.get("folder"), matchMap.get("feed"));
} else if (ORGANIZATION_FEED.matches(formattedString)) {
Map<String, String> matchMap = ORGANIZATION_FEED.match(formattedString);
return ofOrganizationFeedBuilder(matchMap.get("organization"), matchMap.get("feed"));
return ofOrganizationFeedName(matchMap.get("organization"), matchMap.get("feed"));
}
throw new ValidationException("FeedName.parse: formattedString not in valid format");
}
Expand Down
Loading

0 comments on commit 34fd4d6

Please sign in to comment.