Skip to content

Commit 243408a

Browse files
committed
Merge branch 'master' of github.com:googleapis/gapic-generator-java into alpha/g4
2 parents ccdba28 + 11bde65 commit 243408a

File tree

11 files changed

+404
-52
lines changed

11 files changed

+404
-52
lines changed

.bazelrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
startup --batch
22

3-
build --protocopt=--include_source_info
43
build --protocopt=--experimental_allow_proto3_optional

DEVELOPMENT.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,16 @@ below are temporary and better ones will be coming.
105105
```sh
106106
bazel run //src/test/java/com/google/api/generator/engine:JavaCodeGeneratorTest_update
107107
```
108+
109+
- Run a single integration test for API like `Redis`, it generates Java source code using
110+
the Java microgenerator and compares them with the goldens files in `test/integration/goldens/redis`.
111+
112+
```sh
113+
bazel test //test/integration:redis
114+
```
115+
116+
- 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.
117+
118+
```sh
119+
bazel run //test/integration:redis_update
120+
```

rules_bazel/java/integration_test.bzl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,86 @@ def integration_test(name, target, data):
9797
test_library = "%s_test" % target,
9898
srcs = data,
9999
)
100+
101+
def _overwrite_golden_impl(ctx):
102+
# Extract the Java source files from the generated 3 srcjars from API bazel target,
103+
# and put them in the temporary folder `codegen_tmp`, zip as `goldens_output_zip`.
104+
# Overwrite the goldens folder e.g `test/integration/goldens/redis` with the
105+
# code generation in `goldens_output_zip`.
106+
107+
gapic_library = ctx.attr.gapic_library
108+
resource_name_library = ctx.attr.resource_name_library
109+
test_library = ctx.attr.test_library
110+
srcs = ctx.files.srcs
111+
# Convert the name of bazel rules e.g. `redis_update` to `redis`
112+
# because we will need to overwrite the goldens files in `redis` folder.
113+
api_name = "_".join(ctx.attr.name.split("_")[:-1])
114+
goldens_output_zip = ctx.outputs.goldens_output_zip
115+
116+
script = """
117+
mkdir codegen_tmp
118+
unzip -j {input} -d codegen_tmp
119+
unzip -j {input_resource_name} -d codegen_tmp
120+
unzip -j {input_test} -d codegen_tmp
121+
cd codegen_tmp
122+
# Remove unneeded non-Java files, like MANIFEST
123+
rm -rf $(find . -type f ! -name "*.java")
124+
zip -r ../{goldens_output_zip} .
125+
""".format(
126+
goldens_output_zip = goldens_output_zip.path,
127+
input = gapic_library[JavaInfo].source_jars[0].path,
128+
input_resource_name = resource_name_library[JavaInfo].source_jars[0].path,
129+
input_test = test_library[JavaInfo].source_jars[0].path,
130+
)
131+
132+
ctx.actions.run_shell(
133+
inputs = srcs + [
134+
gapic_library[JavaInfo].source_jars[0],
135+
resource_name_library[JavaInfo].source_jars[0],
136+
test_library[JavaInfo].source_jars[0],
137+
],
138+
outputs = [goldens_output_zip],
139+
command = script,
140+
)
141+
142+
# Overwrite the goldens.
143+
golden_update_script_content = """
144+
cd ${{BUILD_WORKSPACE_DIRECTORY}}
145+
unzip -ao {goldens_output_zip} -d test/integration/goldens/{api_name}
146+
""".format(
147+
goldens_output_zip = goldens_output_zip.path,
148+
api_name = api_name,
149+
)
150+
ctx.actions.write(
151+
output = ctx.outputs.golden_update_script,
152+
content = golden_update_script_content,
153+
is_executable = True,
154+
)
155+
return [DefaultInfo(executable = ctx.outputs.golden_update_script)]
156+
157+
overwrite_golden = rule(
158+
attrs = {
159+
"gapic_library": attr.label(),
160+
"resource_name_library": attr.label(),
161+
"test_library": attr.label(),
162+
"srcs": attr.label_list(
163+
allow_files = True,
164+
mandatory = True,
165+
),
166+
},
167+
outputs = {
168+
"goldens_output_zip": "%{name}.zip",
169+
"golden_update_script": "%{name}.sh",
170+
},
171+
executable = True,
172+
implementation = _overwrite_golden_impl,
173+
)
174+
175+
def golden_update(name, target, data):
176+
overwrite_golden(
177+
name = name,
178+
gapic_library = target,
179+
resource_name_library = "%s_resource_name" % target,
180+
test_library = "%s_test" % target,
181+
srcs = data,
182+
)

test/integration/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ load(
66
load(
77
"//:rules_bazel/java/integration_test.bzl",
88
"integration_test",
9+
"golden_update",
910
)
1011

1112
package(default_visibility = ["//visibility:public"])
@@ -26,6 +27,18 @@ integration_test(
2627
data = ["//test/integration/goldens/asset:goldens_files"],
2728
)
2829

30+
golden_update(
31+
name = "redis_update",
32+
target = ":redis_java_gapic",
33+
data = ["//test/integration/goldens/redis:goldens_files"],
34+
)
35+
36+
golden_update(
37+
name = "asset_update",
38+
target = ":asset_java_gapic",
39+
data = ["//test/integration/goldens/asset:goldens_files"],
40+
)
41+
2942
####################################################
3043
# API Library Rules
3144
####################################################
@@ -87,3 +100,4 @@ java_gapic_library(
87100
"@com_google_googleapis//google/logging/v2:logging_java_proto",
88101
],
89102
)
103+

test/integration/goldens/asset/AssetServiceClientTest.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.google.cloud.asset.v1;
1818

19+
import static com.google.cloud.asset.v1.AssetServiceClient.SearchAllIamPoliciesPagedResponse;
20+
import static com.google.cloud.asset.v1.AssetServiceClient.SearchAllResourcesPagedResponse;
21+
1922
import com.google.api.gax.core.NoCredentialsProvider;
2023
import com.google.api.gax.grpc.GaxGrpcProperties;
2124
import com.google.api.gax.grpc.testing.LocalChannelProvider;
@@ -83,7 +86,7 @@ public void tearDown() throws Exception {
8386
}
8487

8588
@Test
86-
public void exportAssetsTest() {
89+
public void exportAssetsTest() throws Exception {
8790
ExportAssetsResponse expectedResponse =
8891
ExportAssetsResponse.newBuilder()
8992
.setOutputConfig(OutputConfig.newBuilder().build())
@@ -99,7 +102,7 @@ public void exportAssetsTest() {
99102

100103
ExportAssetsRequest request =
101104
ExportAssetsRequest.newBuilder()
102-
.setParent(ProjectName.of("[PROJECT]").toString())
105+
.setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
103106
.addAllAssetTypes(new ArrayList<>())
104107
.setOutputConfig(OutputConfig.newBuilder().build())
105108
.build();
@@ -130,7 +133,7 @@ public void exportAssetsExceptionTest() throws Exception {
130133
try {
131134
ExportAssetsRequest request =
132135
ExportAssetsRequest.newBuilder()
133-
.setParent(ProjectName.of("[PROJECT]").toString())
136+
.setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
134137
.addAllAssetTypes(new ArrayList<>())
135138
.setOutputConfig(OutputConfig.newBuilder().build())
136139
.build();
@@ -144,14 +147,14 @@ public void exportAssetsExceptionTest() throws Exception {
144147
}
145148

146149
@Test
147-
public void batchGetAssetsHistoryTest() {
150+
public void batchGetAssetsHistoryTest() throws Exception {
148151
BatchGetAssetsHistoryResponse expectedResponse =
149152
BatchGetAssetsHistoryResponse.newBuilder().addAllAssets(new ArrayList<>()).build();
150153
mockAssetService.addResponse(expectedResponse);
151154

152155
BatchGetAssetsHistoryRequest request =
153156
BatchGetAssetsHistoryRequest.newBuilder()
154-
.setParent(ProjectName.of("[PROJECT]").toString())
157+
.setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
155158
.addAllAssetNames(new ArrayList<>())
156159
.setReadTimeWindow(TimeWindow.newBuilder().build())
157160
.build();
@@ -182,7 +185,7 @@ public void batchGetAssetsHistoryExceptionTest() throws Exception {
182185
try {
183186
BatchGetAssetsHistoryRequest request =
184187
BatchGetAssetsHistoryRequest.newBuilder()
185-
.setParent(ProjectName.of("[PROJECT]").toString())
188+
.setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
186189
.addAllAssetNames(new ArrayList<>())
187190
.setReadTimeWindow(TimeWindow.newBuilder().build())
188191
.build();
@@ -194,7 +197,7 @@ public void batchGetAssetsHistoryExceptionTest() throws Exception {
194197
}
195198

196199
@Test
197-
public void createFeedTest() {
200+
public void createFeedTest() throws Exception {
198201
Feed expectedResponse =
199202
Feed.newBuilder()
200203
.setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
@@ -235,7 +238,7 @@ public void createFeedExceptionTest() throws Exception {
235238
}
236239

237240
@Test
238-
public void getFeedTest() {
241+
public void getFeedTest() throws Exception {
239242
Feed expectedResponse =
240243
Feed.newBuilder()
241244
.setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
@@ -276,7 +279,7 @@ public void getFeedExceptionTest() throws Exception {
276279
}
277280

278281
@Test
279-
public void getFeedTest2() {
282+
public void getFeedTest2() throws Exception {
280283
Feed expectedResponse =
281284
Feed.newBuilder()
282285
.setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
@@ -317,7 +320,7 @@ public void getFeedExceptionTest2() throws Exception {
317320
}
318321

319322
@Test
320-
public void listFeedsTest() {
323+
public void listFeedsTest() throws Exception {
321324
ListFeedsResponse expectedResponse =
322325
ListFeedsResponse.newBuilder().addAllFeeds(new ArrayList<>()).build();
323326
mockAssetService.addResponse(expectedResponse);
@@ -353,7 +356,7 @@ public void listFeedsExceptionTest() throws Exception {
353356
}
354357

355358
@Test
356-
public void updateFeedTest() {
359+
public void updateFeedTest() throws Exception {
357360
Feed expectedResponse =
358361
Feed.newBuilder()
359362
.setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
@@ -394,7 +397,7 @@ public void updateFeedExceptionTest() throws Exception {
394397
}
395398

396399
@Test
397-
public void deleteFeedTest() {
400+
public void deleteFeedTest() throws Exception {
398401
Empty expectedResponse = Empty.newBuilder().build();
399402
mockAssetService.addResponse(expectedResponse);
400403

@@ -429,7 +432,7 @@ public void deleteFeedExceptionTest() throws Exception {
429432
}
430433

431434
@Test
432-
public void deleteFeedTest2() {
435+
public void deleteFeedTest2() throws Exception {
433436
Empty expectedResponse = Empty.newBuilder().build();
434437
mockAssetService.addResponse(expectedResponse);
435438

@@ -464,26 +467,26 @@ public void deleteFeedExceptionTest2() throws Exception {
464467
}
465468

466469
@Test
467-
public void searchAllResourcesTest() {
470+
public void searchAllResourcesTest() throws Exception {
468471
ResourceSearchResult responsesElement = ResourceSearchResult.newBuilder().build();
469472
SearchAllResourcesResponse expectedResponse =
470473
SearchAllResourcesResponse.newBuilder()
471474
.setNextPageToken("")
472-
.addAllResponses(Arrays.asList(responsesElement))
475+
.addAllResults(Arrays.asList(responsesElement))
473476
.build();
474477
mockAssetService.addResponse(expectedResponse);
475478

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

480-
SearchAllResourcesResponse pagedListResponse =
483+
SearchAllResourcesPagedResponse pagedListResponse =
481484
client.searchAllResources(scope, query, assetTypes);
482485

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

485488
Assert.assertEquals(1, resources.size());
486-
Assert.assertEquals(expectedResponse.getResponsesList().get(0), resources.get(0));
489+
Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0));
487490

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

517520
@Test
518-
public void searchAllIamPoliciesTest() {
521+
public void searchAllIamPoliciesTest() throws Exception {
519522
IamPolicySearchResult responsesElement = IamPolicySearchResult.newBuilder().build();
520523
SearchAllIamPoliciesResponse expectedResponse =
521524
SearchAllIamPoliciesResponse.newBuilder()
522525
.setNextPageToken("")
523-
.addAllResponses(Arrays.asList(responsesElement))
526+
.addAllResults(Arrays.asList(responsesElement))
524527
.build();
525528
mockAssetService.addResponse(expectedResponse);
526529

527530
String scope = "scope109264468";
528531
String query = "query107944136";
529532

530-
SearchAllIamPoliciesResponse pagedListResponse = client.searchAllIamPolicies(scope, query);
533+
SearchAllIamPoliciesPagedResponse pagedListResponse = client.searchAllIamPolicies(scope, query);
531534

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

534537
Assert.assertEquals(1, resources.size());
535-
Assert.assertEquals(expectedResponse.getResponsesList().get(0), resources.get(0));
538+
Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0));
536539

537540
List<AbstractMessage> actualRequests = mockAssetService.getRequests();
538541
Assert.assertEquals(1, actualRequests.size());

test/integration/goldens/asset/FeedName.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ public static FeedName of(String project, String feed) {
113113
}
114114

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

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

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

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

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

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

144144
@BetaApi("The static format methods are not stable yet and may be changed in the future.")
145-
public static String formatOrganizationFeedBuilder(String organization, String feed) {
145+
public static String formatOrganizationFeedName(String organization, String feed) {
146146
return newOrganizationFeedBuilder()
147147
.setOrganization(organization)
148148
.setFeed(feed)
@@ -156,13 +156,13 @@ public static FeedName parse(String formattedString) {
156156
}
157157
if (PROJECT_FEED.matches(formattedString)) {
158158
Map<String, String> matchMap = PROJECT_FEED.match(formattedString);
159-
return ofProjectFeedBuilder(matchMap.get("project"), matchMap.get("feed"));
159+
return ofProjectFeedName(matchMap.get("project"), matchMap.get("feed"));
160160
} else if (FOLDER_FEED.matches(formattedString)) {
161161
Map<String, String> matchMap = FOLDER_FEED.match(formattedString);
162-
return ofFolderFeedBuilder(matchMap.get("folder"), matchMap.get("feed"));
162+
return ofFolderFeedName(matchMap.get("folder"), matchMap.get("feed"));
163163
} else if (ORGANIZATION_FEED.matches(formattedString)) {
164164
Map<String, String> matchMap = ORGANIZATION_FEED.match(formattedString);
165-
return ofOrganizationFeedBuilder(matchMap.get("organization"), matchMap.get("feed"));
165+
return ofOrganizationFeedName(matchMap.get("organization"), matchMap.get("feed"));
166166
}
167167
throw new ValidationException("FeedName.parse: formattedString not in valid format");
168168
}

0 commit comments

Comments
 (0)