Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2899c40

Browse files
authored
[ggj][test][build] fix: fix test src packaging, update integration goldens (#435)
* fix: support non-name fields with res-refs in resname def parsing * fix: add workaround for missing default_host and oauth_scopes annotation * fix: clarify LRO parsing error messages * feat: support deeply-nested types in AST and proto message parsing * fix: prevent resname tokens from matching subcomponents * fix: use TypeParser for proto message parsing * fix: use generic types in field instantiation in ServiceClientTest * fix: prevent descension into map types in nested message parsing * fix: merge master * fix: use both map generics in ServiceClientTest codegen * fix: dir structure of generated files * test: add asset API gradle pkg rules * fix: remove unused dep * test: add logging integration target and goldens, consolidate rules * fix: fix asset_java_gapic build * fix: fix test src packaging, update integration goldens * [ggj][resname][codegen] fix: pass all tokens to instansiate in resname 1-pattern case (#437) * fix: pass all tokens to instansiate in resname 1-pattern case * fix: update goldens * fix: update goldens * [ggj][engx] build: add all integration tests to pre-commit hook (#438) * fix: pass all tokens to instansiate in resname 1-pattern case * fix: update goldens * fix: update goldens * build: add all integration tests to pre-commit hook
1 parent 215b053 commit 2899c40

23 files changed

+4116
-59
lines changed

.githooks/pre-commit

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ then
101101
fi
102102
fi
103103

104-
# Check tests.
104+
# Check unit tests.
105105
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ]
106106
then
107-
echo_status "Checking tests..."
107+
echo_status "Checking unit tests..."
108108
bazel --batch test --disk_cache="$BAZEL_CACHE_DIR" //src/test/...
109109
TEST_STATUS=$?
110110
if [ $TEST_STATUS != 0 ]
@@ -114,6 +114,19 @@ then
114114
fi
115115
fi
116116

117+
# Check integration tests.
118+
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ]
119+
then
120+
echo_status "Checking integration tests..."
121+
bazel --batch test --disk_cache="$BAZEL_CACHE_DIR" //test/integration/...
122+
TEST_STATUS=$?
123+
if [ $TEST_STATUS != 0 ]
124+
then
125+
echo_error "Tests failed." "Please fix them and try again."
126+
exit 1
127+
fi
128+
fi
129+
117130
# Check and fix Bazel format.
118131
if [ $NUM_BAZEL_FILES_CHANGED -gt 0 ]
119132
then

rules_java_gapic/java_gapic.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _java_gapic_postprocess_srcjar_impl(ctx):
3636
zip -r $WORKING_DIR/{output_srcjar_name}.srcjar ./
3737
cd $WORKING_DIR/{output_dir_path}/proto/src/main/java
3838
zip -r $WORKING_DIR/{output_srcjar_name}-resource-name.srcjar ./
39-
cd $WORKING_DIR/{output_dir_path}/proto/src/test/java
39+
cd $WORKING_DIR/{output_dir_path}/src/test/java
4040
zip -r $WORKING_DIR/{output_srcjar_name}-tests.srcjar ./
4141
cd $WORKING_DIR
4242
mv {output_srcjar_name}.srcjar {output_main}

src/main/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposer.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,21 +1090,25 @@ private static MethodDefinition createToStringMethod(
10901090
List<List<String>> tokenHierarchies) {
10911091
boolean hasVariants = tokenHierarchies.size() > 1;
10921092
if (!hasVariants) {
1093-
String token = getTokenSet(tokenHierarchies).stream().collect(Collectors.toList()).get(0);
1094-
String javaTokenVarName = JavaStyle.toLowerCamelCase(token);
1095-
Preconditions.checkNotNull(
1096-
patternTokenVarExprs.get(token),
1097-
String.format(
1098-
"No expression found for token %s amongst values %s",
1099-
javaTokenVarName, patternTokenVarExprs.toString()));
1093+
1094+
List<Expr> instantiateArgExprs = new ArrayList<>();
1095+
List<String> tokens = getTokenSet(tokenHierarchies).stream().collect(Collectors.toList());
1096+
for (int i = 0; i < tokens.size(); i++) {
1097+
String token = tokens.get(i);
1098+
Preconditions.checkNotNull(
1099+
patternTokenVarExprs.get(token),
1100+
String.format(
1101+
"No expression found for token %s amongst values %s",
1102+
token, patternTokenVarExprs.toString()));
1103+
instantiateArgExprs.add(ValueExpr.withValue(StringObjectValue.withValue(token)));
1104+
instantiateArgExprs.add(patternTokenVarExprs.get(token));
1105+
}
11001106

11011107
MethodInvocationExpr returnInstantiateExpr =
11021108
MethodInvocationExpr.builder()
11031109
.setExprReferenceExpr(templateFinalVarExprs.get(0))
11041110
.setMethodName("instantiate")
1105-
.setArguments(
1106-
ValueExpr.withValue(StringObjectValue.withValue(token)),
1107-
patternTokenVarExprs.get(token))
1111+
.setArguments(instantiateArgExprs)
11081112
.setReturnType(TypeNode.STRING)
11091113
.build();
11101114
return MethodDefinition.builder()

src/test/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposerTest.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@
2525
import com.google.api.generator.gapic.protoparser.Parser;
2626
import com.google.api.generator.test.framework.Assert;
2727
import com.google.api.generator.test.framework.Utils;
28+
import com.google.logging.v2.LogEntryProto;
29+
import com.google.logging.v2.LoggingConfigProto;
30+
import com.google.logging.v2.LoggingMetricsProto;
31+
import com.google.logging.v2.LoggingProto;
2832
import com.google.protobuf.Descriptors.FileDescriptor;
2933
import com.google.protobuf.Descriptors.ServiceDescriptor;
3034
import com.google.showcase.v1beta1.EchoOuterClass;
3135
import com.google.showcase.v1beta1.TestingOuterClass;
36+
import google.cloud.CommonResources;
3237
import java.nio.file.Path;
3338
import java.nio.file.Paths;
3439
import java.util.Arrays;
40+
import java.util.HashMap;
3541
import java.util.HashSet;
3642
import java.util.List;
3743
import java.util.Map;
@@ -95,7 +101,6 @@ public void generateResourceNameClass_echoFoobarMultiplePatterns() {
95101
ResourceName foobarResname = resourceNames.get("showcase.googleapis.com/Foobar");
96102
assertThat(outputResourceNames).contains(foobarResname);
97103

98-
Service echoProtoService = services.get(0);
99104
GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(foobarResname);
100105

101106
JavaWriterVisitor visitor = new JavaWriterVisitor();
@@ -105,6 +110,55 @@ public void generateResourceNameClass_echoFoobarMultiplePatterns() {
105110
Assert.assertCodeEquals(goldenFilePath, visitor.write());
106111
}
107112

113+
@Test
114+
public void generateResourceNameClass_loggingOnePatternMultipleVariables() {
115+
FileDescriptor serviceFileDescriptor = LoggingConfigProto.getDescriptor();
116+
ServiceDescriptor serviceDescriptor = serviceFileDescriptor.getServices().get(0);
117+
assertEquals(serviceDescriptor.getName(), "ConfigServiceV2");
118+
119+
List<FileDescriptor> protoFiles =
120+
Arrays.asList(
121+
serviceFileDescriptor,
122+
LoggingProto.getDescriptor(),
123+
LogEntryProto.getDescriptor(),
124+
LoggingConfigProto.getDescriptor(),
125+
LoggingMetricsProto.getDescriptor());
126+
127+
Map<String, ResourceName> resourceNames = new HashMap<>();
128+
Map<String, Message> messageTypes = new HashMap<>();
129+
for (FileDescriptor fileDescriptor : protoFiles) {
130+
resourceNames.putAll(Parser.parseResourceNames(fileDescriptor));
131+
messageTypes.putAll(Parser.parseMessages(fileDescriptor));
132+
}
133+
134+
// Additional resource names.
135+
FileDescriptor commonResourcesFileDescriptor = CommonResources.getDescriptor();
136+
resourceNames.putAll(Parser.parseResourceNames(commonResourcesFileDescriptor));
137+
138+
Set<ResourceName> outputResourceNames = new HashSet<>();
139+
List<Service> services =
140+
Parser.parseService(
141+
serviceFileDescriptor,
142+
messageTypes,
143+
resourceNames,
144+
Optional.empty(),
145+
outputResourceNames);
146+
147+
ResourceName billingAccountLocationResname =
148+
resourceNames.get("logging.googleapis.com/BillingAccountLocation");
149+
assertThat(outputResourceNames).contains(billingAccountLocationResname);
150+
151+
GapicClass clazz =
152+
ResourceNameHelperClassComposer.instance().generate(billingAccountLocationResname);
153+
154+
JavaWriterVisitor visitor = new JavaWriterVisitor();
155+
clazz.classDefinition().accept(visitor);
156+
Utils.saveCodegenToFile(this.getClass(), "BillingAccountLocationName.golden", visitor.write());
157+
Path goldenFilePath =
158+
Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "BillingAccountLocationName.golden");
159+
Assert.assertCodeEquals(goldenFilePath, visitor.write());
160+
}
161+
108162
@Test
109163
public void generateResourceNameClass_testingSessionOnePattern() {
110164
FileDescriptor testingFileDescriptor = TestingOuterClass.getDescriptor();
@@ -125,7 +179,6 @@ public void generateResourceNameClass_testingSessionOnePattern() {
125179
ResourceName sessionResname = resourceNames.get("showcase.googleapis.com/Session");
126180
assertThat(outputResourceNames).contains(sessionResname);
127181

128-
Service testingProtoService = services.get(0);
129182
GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(sessionResname);
130183

131184
JavaWriterVisitor visitor = new JavaWriterVisitor();
@@ -134,5 +187,4 @@ public void generateResourceNameClass_testingSessionOnePattern() {
134187
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "SessionName.golden");
135188
Assert.assertCodeEquals(goldenFilePath, visitor.write());
136189
}
137-
// TODO(miraleung): Add more tests for a single pattern.
138190
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package com.google.logging.v2;
2+
3+
import com.google.api.pathtemplate.PathTemplate;
4+
import com.google.api.resourcenames.ResourceName;
5+
import com.google.common.base.Preconditions;
6+
import com.google.common.collect.ImmutableMap;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Objects;
11+
import javax.annotation.Generated;
12+
13+
// AUTO-GENERATED DOCUMENTATION AND CLASS.
14+
@Generated("by gapic-generator-java")
15+
public class BillingAccountLocationName implements ResourceName {
16+
private static final PathTemplate BILLING_ACCOUNT_LOCATION =
17+
PathTemplate.createWithoutUrlEncoding(
18+
"billingAccounts/{billing_account}/locations/{location}");
19+
private volatile Map<String, String> fieldValuesMap;
20+
private final String billingAccount;
21+
private final String location;
22+
23+
private BillingAccountLocationName(Builder builder) {
24+
billingAccount = Preconditions.checkNotNull(builder.getBillingAccount());
25+
location = Preconditions.checkNotNull(builder.getLocation());
26+
}
27+
28+
public String getBillingAccount() {
29+
return billingAccount;
30+
}
31+
32+
public String getLocation() {
33+
return location;
34+
}
35+
36+
public static Builder newBuilder() {
37+
return new Builder();
38+
}
39+
40+
public Builder toBuilder() {
41+
return new Builder(this);
42+
}
43+
44+
public static BillingAccountLocationName of(String billingAccount, String location) {
45+
return newBuilder().setBillingAccount(billingAccount).setLocation(location).build();
46+
}
47+
48+
public static String format(String billingAccount, String location) {
49+
return newBuilder().setBillingAccount(billingAccount).setLocation(location).build().toString();
50+
}
51+
52+
public static BillingAccountLocationName parse(String formattedString) {
53+
if (formattedString.isEmpty()) {
54+
return null;
55+
}
56+
Map<String, String> matchMap =
57+
BILLING_ACCOUNT_LOCATION.validatedMatch(
58+
formattedString,
59+
"BillingAccountLocationName.parse: formattedString not in valid format");
60+
return of(matchMap.get("billing_account"), matchMap.get("location"));
61+
}
62+
63+
public static List<BillingAccountLocationName> parseList(List<String> formattedStrings) {
64+
List<BillingAccountLocationName> list = new ArrayList<>(formattedStrings.size());
65+
for (String formattedString : formattedStrings) {
66+
list.add(parse(formattedString));
67+
}
68+
return list;
69+
}
70+
71+
public static List<String> toStringList(List<BillingAccountLocationName> values) {
72+
List<String> list = new ArrayList<>(values.size());
73+
for (BillingAccountLocationName value : values) {
74+
if (Objects.isNull(value)) {
75+
list.add("");
76+
} else {
77+
list.add(value.toString());
78+
}
79+
}
80+
return list;
81+
}
82+
83+
public static boolean isParsableFrom(String formattedString) {
84+
return BILLING_ACCOUNT_LOCATION.matches(formattedString);
85+
}
86+
87+
@Override
88+
public Map<String, String> getFieldValuesMap() {
89+
if (Objects.isNull(fieldValuesMap)) {
90+
synchronized (this) {
91+
if (Objects.isNull(fieldValuesMap)) {
92+
ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
93+
if (!Objects.isNull(billingAccount)) {
94+
fieldMapBuilder.put("billing_account", billingAccount);
95+
}
96+
if (!Objects.isNull(location)) {
97+
fieldMapBuilder.put("location", location);
98+
}
99+
fieldValuesMap = fieldMapBuilder.build();
100+
}
101+
}
102+
}
103+
return fieldValuesMap;
104+
}
105+
106+
public String getFieldValue(String fieldName) {
107+
return getFieldValuesMap().get(fieldName);
108+
}
109+
110+
@Override
111+
public String toString() {
112+
return BILLING_ACCOUNT_LOCATION.instantiate(
113+
"billing_account", billingAccount, "location", location);
114+
}
115+
116+
@Override
117+
public boolean equals(Object o) {
118+
if (o == this) {
119+
return true;
120+
}
121+
if (o != null || getClass() == o.getClass()) {
122+
BillingAccountLocationName that = ((BillingAccountLocationName) o);
123+
return Objects.equals(this.billingAccount, that.billingAccount)
124+
&& Objects.equals(this.location, that.location);
125+
}
126+
return false;
127+
}
128+
129+
@Override
130+
public int hashCode() {
131+
int h = 1;
132+
h *= 1000003;
133+
h ^= Objects.hashCode(billingAccount);
134+
h *= 1000003;
135+
h ^= Objects.hashCode(location);
136+
return h;
137+
}
138+
139+
/** Builder for billingAccounts/{billing_account}/locations/{location}. */
140+
public static class Builder {
141+
private String billingAccount;
142+
private String location;
143+
144+
private Builder() {}
145+
146+
public String getBillingAccount() {
147+
return billingAccount;
148+
}
149+
150+
public String getLocation() {
151+
return location;
152+
}
153+
154+
public Builder setBillingAccount(String billingAccount) {
155+
this.billingAccount = billingAccount;
156+
return this;
157+
}
158+
159+
public Builder setLocation(String location) {
160+
this.location = location;
161+
return this;
162+
}
163+
164+
private Builder(BillingAccountLocationName billingAccountLocationName) {
165+
billingAccount = billingAccountLocationName.billingAccount;
166+
location = billingAccountLocationName.location;
167+
}
168+
169+
public BillingAccountLocationName build() {
170+
return new BillingAccountLocationName(this);
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)