Skip to content

Commit

Permalink
fix: use generic types in field instantiation in ServiceClientTest
Browse files Browse the repository at this point in the history
  • Loading branch information
miraleung committed Oct 30, 2020
1 parent 930edb2 commit 74dbafb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ static Expr createDefaultValue(
}

static Expr createDefaultValue(Field f) {
return createDefaultValue(f, false);
}

static Expr createDefaultValue(Field f, boolean useExplicitInitTypeInGenerics) {
if (f.isRepeated()) {
TypeNode newType =
TypeNode.withReference(
ConcreteReference.withClazz(f.isMap() ? HashMap.class : ArrayList.class));
ConcreteReference.Builder refBuilder =
ConcreteReference.builder().setClazz(f.isMap() ? HashMap.class : ArrayList.class);
if (useExplicitInitTypeInGenerics) {
refBuilder = refBuilder.setGenerics(f.type().reference().generics().get(0));
}

TypeNode newType = TypeNode.withReference(refBuilder.build());
return NewObjectExpr.builder().setType(newType).setIsGeneric(true).build();
}

Expand Down Expand Up @@ -238,7 +246,7 @@ static Expr createSimpleMessageBuilderExpr(
.setReturnType(TypeNode.STRING)
.build();
} else {
defaultExpr = createDefaultValue(field);
defaultExpr = createDefaultValue(field, true);
}
builderExpr =
MethodInvocationExpr.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ public void createSimpleMessage_containsRepeatedField() {
message, typeStringsToResourceNames, messageTypes);
expr.accept(writerVisitor);
assertEquals(
"PagedExpandResponse.newBuilder().addAllResponses(new"
+ " ArrayList<>()).setNextPageToken(\"next_page_token-1530815211\").build()",
"PagedExpandResponse.newBuilder().addAllResponses(new ArrayList<EchoResponse>())"
+ ".setNextPageToken(\"next_page_token-1530815211\").build()",
writerVisitor.write());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class SubscriberClientTest {
.setPushConfig(PushConfig.newBuilder().build())
.setAckDeadlineSeconds(2135351438)
.setRetainAckedMessages(true)
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.setEnableMessageOrdering(true)
.setExpirationPolicy(ExpirationPolicy.newBuilder().build())
.setFilter("filter-1274492040")
Expand Down Expand Up @@ -140,7 +140,7 @@ public class SubscriberClientTest {
.setPushConfig(PushConfig.newBuilder().build())
.setAckDeadlineSeconds(2135351438)
.setRetainAckedMessages(true)
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.setEnableMessageOrdering(true)
.setExpirationPolicy(ExpirationPolicy.newBuilder().build())
.setFilter("filter-1274492040")
Expand Down Expand Up @@ -199,7 +199,7 @@ public class SubscriberClientTest {
.setPushConfig(PushConfig.newBuilder().build())
.setAckDeadlineSeconds(2135351438)
.setRetainAckedMessages(true)
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.setEnableMessageOrdering(true)
.setExpirationPolicy(ExpirationPolicy.newBuilder().build())
.setFilter("filter-1274492040")
Expand Down Expand Up @@ -258,7 +258,7 @@ public class SubscriberClientTest {
.setPushConfig(PushConfig.newBuilder().build())
.setAckDeadlineSeconds(2135351438)
.setRetainAckedMessages(true)
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.setEnableMessageOrdering(true)
.setExpirationPolicy(ExpirationPolicy.newBuilder().build())
.setFilter("filter-1274492040")
Expand Down Expand Up @@ -317,7 +317,7 @@ public class SubscriberClientTest {
.setPushConfig(PushConfig.newBuilder().build())
.setAckDeadlineSeconds(2135351438)
.setRetainAckedMessages(true)
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.setEnableMessageOrdering(true)
.setExpirationPolicy(ExpirationPolicy.newBuilder().build())
.setFilter("filter-1274492040")
Expand Down Expand Up @@ -366,7 +366,7 @@ public class SubscriberClientTest {
.setPushConfig(PushConfig.newBuilder().build())
.setAckDeadlineSeconds(2135351438)
.setRetainAckedMessages(true)
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.setEnableMessageOrdering(true)
.setExpirationPolicy(ExpirationPolicy.newBuilder().build())
.setFilter("filter-1274492040")
Expand Down Expand Up @@ -415,7 +415,7 @@ public class SubscriberClientTest {
.setPushConfig(PushConfig.newBuilder().build())
.setAckDeadlineSeconds(2135351438)
.setRetainAckedMessages(true)
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.setEnableMessageOrdering(true)
.setExpirationPolicy(ExpirationPolicy.newBuilder().build())
.setFilter("filter-1274492040")
Expand Down Expand Up @@ -781,7 +781,7 @@ public class SubscriberClientTest {
@Test
public void pullTest() throws Exception {
PullResponse expectedResponse =
PullResponse.newBuilder().addAllReceivedMessages(new ArrayList<>()).build();
PullResponse.newBuilder().addAllReceivedMessages(new ArrayList<ReceivedMessage>()).build();
mockSubscriber.addResponse(expectedResponse);

SubscriptionName subscription = SubscriptionName.of("[PROJECT]", "[SUBSCRIPTION]");
Expand Down Expand Up @@ -823,7 +823,7 @@ public class SubscriberClientTest {
@Test
public void pullTest2() throws Exception {
PullResponse expectedResponse =
PullResponse.newBuilder().addAllReceivedMessages(new ArrayList<>()).build();
PullResponse.newBuilder().addAllReceivedMessages(new ArrayList<ReceivedMessage>()).build();
mockSubscriber.addResponse(expectedResponse);

String subscription = "subscription341203229";
Expand Down Expand Up @@ -865,14 +865,16 @@ public class SubscriberClientTest {
@Test
public void streamingPullTest() throws Exception {
StreamingPullResponse expectedResponse =
StreamingPullResponse.newBuilder().addAllReceivedMessages(new ArrayList<>()).build();
StreamingPullResponse.newBuilder()
.addAllReceivedMessages(new ArrayList<ReceivedMessage>())
.build();
mockSubscriber.addResponse(expectedResponse);
StreamingPullRequest request =
StreamingPullRequest.newBuilder()
.setSubscription(SubscriptionName.of("[PROJECT]", "[SUBSCRIPTION]").toString())
.addAllAckIds(new ArrayList<>())
.addAllModifyDeadlineSeconds(new ArrayList<>())
.addAllModifyDeadlineAckIds(new ArrayList<>())
.addAllAckIds(new ArrayList<String>())
.addAllModifyDeadlineSeconds(new ArrayList<Integer>())
.addAllModifyDeadlineAckIds(new ArrayList<String>())
.setStreamAckDeadlineSeconds(1875467245)
.setClientId("client_id-1904089585")
.setMaxOutstandingMessages(-1315266996)
Expand Down Expand Up @@ -901,9 +903,9 @@ public class SubscriberClientTest {
StreamingPullRequest request =
StreamingPullRequest.newBuilder()
.setSubscription(SubscriptionName.of("[PROJECT]", "[SUBSCRIPTION]").toString())
.addAllAckIds(new ArrayList<>())
.addAllModifyDeadlineSeconds(new ArrayList<>())
.addAllModifyDeadlineAckIds(new ArrayList<>())
.addAllAckIds(new ArrayList<String>())
.addAllModifyDeadlineSeconds(new ArrayList<Integer>())
.addAllModifyDeadlineAckIds(new ArrayList<String>())
.setStreamAckDeadlineSeconds(1875467245)
.setClientId("client_id-1904089585")
.setMaxOutstandingMessages(-1315266996)
Expand Down Expand Up @@ -1011,7 +1013,7 @@ public class SubscriberClientTest {
Snapshot.newBuilder()
.setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString())
.setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString())
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.build();
mockSubscriber.addResponse(expectedResponse);

Expand Down Expand Up @@ -1051,7 +1053,7 @@ public class SubscriberClientTest {
Snapshot.newBuilder()
.setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString())
.setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString())
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.build();
mockSubscriber.addResponse(expectedResponse);

Expand Down Expand Up @@ -1179,7 +1181,7 @@ public class SubscriberClientTest {
Snapshot.newBuilder()
.setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString())
.setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString())
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.build();
mockSubscriber.addResponse(expectedResponse);

Expand Down Expand Up @@ -1222,7 +1224,7 @@ public class SubscriberClientTest {
Snapshot.newBuilder()
.setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString())
.setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString())
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.build();
mockSubscriber.addResponse(expectedResponse);

Expand Down Expand Up @@ -1265,7 +1267,7 @@ public class SubscriberClientTest {
Snapshot.newBuilder()
.setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString())
.setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString())
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.build();
mockSubscriber.addResponse(expectedResponse);

Expand Down Expand Up @@ -1308,7 +1310,7 @@ public class SubscriberClientTest {
Snapshot.newBuilder()
.setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString())
.setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString())
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.build();
mockSubscriber.addResponse(expectedResponse);

Expand Down Expand Up @@ -1351,7 +1353,7 @@ public class SubscriberClientTest {
Snapshot.newBuilder()
.setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString())
.setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString())
.putAllLabels(new HashMap<>())
.putAllLabels(new HashMap<String>())
.build();
mockSubscriber.addResponse(expectedResponse);

Expand Down

0 comments on commit 74dbafb

Please sign in to comment.