Skip to content

Commit

Permalink
fix: Support PubSub's _deleted-topic_ pattern (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraleung authored May 26, 2021
1 parent 842d7a3 commit 7d8c62d
Show file tree
Hide file tree
Showing 43 changed files with 17,187 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
run: bazel --batch test $(bazel query "//src/test/..." | grep "Test$") --noshow_progress

- name: Integration Tests
run: bazel --batch test //test/integration:asset //test/integration:credentials //test/integration:iam //test/integration:kms //test/integration:logging //test/integration:redis //test/integration:library --noshow_progress
run: bazel --batch test //test/integration:asset //test/integration:credentials //test/integration:iam //test/integration:kms //test/integration:logging //test/integration:pubsub //test/integration:redis //test/integration:library --noshow_progress

- uses: actions/upload-artifact@v2
if: ${{ failure() }}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.api.generator.gapic.composer.resourcename;

import com.google.api.generator.gapic.utils.ResourceNameConstants;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
Expand Down Expand Up @@ -42,6 +43,12 @@ public static List<List<String>> parseTokenHierarchy(List<String> patterns) {

// Process variables.
for (String rawPatternToken : rawPatternTokens) {
// PubSub exception case.
if (rawPatternToken.equals(ResourceNameConstants.DELETED_TOPIC_LITERAL)) {
hierarchy.add(rawPatternToken);
continue;
}

if (!rawPatternToken.startsWith(LEFT_BRACE) || !rawPatternToken.endsWith(RIGHT_BRACE)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static String toLowerCamelCase(String s) {
return s;
}

s = s.replace('-', '_');

if (s.indexOf(UNDERSCORE) >= 0) {
s = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, s);
}
Expand All @@ -39,6 +41,8 @@ public static String toUpperCamelCase(String s) {
return s;
}

s = s.replace('-', '_');

if (s.indexOf(UNDERSCORE) >= 0) {
s = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, s);
}
Expand Down
40 changes: 40 additions & 0 deletions test/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ INTEGRATION_TEST_LIBRARIES = [
"credentials", # Check that the capital name edge case is handled.
"iam", # Mixin-only special-case API can build on its own.
"kms", # Mixins, with an override in the proto file.
"pubsub", # Special=case handling for "_deleted-topic_" resource name patterns.
"logging", # Java package remapping in gapic.yaml.
"redis", # Has a gapic.yaml.
"library", # No gRPC service config.
Expand All @@ -36,6 +37,7 @@ API_GAPIC_TARGETS = {
"credentials": "@com_google_googleapis//google/iam/credentials/v1:credentials_java_gapic",
"iam": ":iam_java_gapic", # Googleapis' LRO does not have a Java Gapic.
"kms": ":kms_java_gapic", # Local target because mixins are not rolled out yet.
"pubsub": ":pubsub_java_gapic",
"logging": "@com_google_googleapis//google/logging/v2:logging_java_gapic",
"redis": "@com_google_googleapis//google/cloud/redis/v1beta1:redis_java_gapic",
"library": "@com_google_googleapis//google/example/library/v1:library_java_gapic",
Expand Down Expand Up @@ -274,3 +276,41 @@ java_gapic_assembly_gradle_pkg(
"@com_google_googleapis//google/cloud/location:location_java_proto",
],
)

# PubSub
# TODO: Remove some of these targets when PubSub has been migrated in googleapis.
java_gapic_library(
name = "pubsub_java_gapic",
srcs = ["@com_google_googleapis//google/pubsub/v1:pubsub_proto_with_info"],
gapic_yaml = "@com_google_googleapis//google/pubsub/v1:pubsub_gapic.yaml",
grpc_service_config = "@com_google_googleapis//google/pubsub/v1:pubsub_grpc_service_config.json",
# For the IAM mixin.
service_yaml = "pubsub_v1.yaml",
test_deps = [
"@com_google_googleapis//google/pubsub/v1:pubsub_java_grpc",
"@com_google_googleapis//google/iam/v1:iam_java_grpc",
],
deps = [
"@com_google_googleapis//google/iam/v1:iam_java_proto",
"@com_google_googleapis//google/pubsub/v1:pubsub_java_proto",
],
)

java_gapic_test(
name = "pubsub_java_gapic_test_suite",
test_classes = [
"com.google.cloud.pubsub.v1.SubscriptionAdminClientTest",
"com.google.cloud.pubsub.v1.TopicAdminClientTest",
],
runtime_deps = [":pubsub_java_gapic_test"],
)

java_gapic_assembly_gradle_pkg(
name = "google-cloud-pubsub-v1-java",
deps = [
":pubsub_java_gapic",
"@com_google_googleapis//google/pubsub/v1:pubsub_java_grpc",
"@com_google_googleapis//google/pubsub/v1:pubsub_java_proto",
"@com_google_googleapis//google/pubsub/v1:pubsub_proto",
],
)
9 changes: 9 additions & 0 deletions test/integration/goldens/pubsub/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "goldens_files",
srcs = glob([
"*.java",
"gapic_metadata.json",
]),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.pubsub.v1;

import com.google.api.core.BetaApi;
import com.google.api.gax.grpc.testing.MockGrpcService;
import com.google.protobuf.AbstractMessage;
import io.grpc.ServerServiceDefinition;
import java.util.List;
import javax.annotation.Generated;

@BetaApi
@Generated("by gapic-generator-java")
public class MockIAMPolicy implements MockGrpcService {
private final MockIAMPolicyImpl serviceImpl;

public MockIAMPolicy() {
serviceImpl = new MockIAMPolicyImpl();
}

@Override
public List<AbstractMessage> getRequests() {
return serviceImpl.getRequests();
}

@Override
public void addResponse(AbstractMessage response) {
serviceImpl.addResponse(response);
}

@Override
public void addException(Exception exception) {
serviceImpl.addException(exception);
}

@Override
public ServerServiceDefinition getServiceDefinition() {
return serviceImpl.bindService();
}

@Override
public void reset() {
serviceImpl.reset();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.pubsub.v1;

import com.google.api.core.BetaApi;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.IAMPolicyGrpc.IAMPolicyImplBase;
import com.google.iam.v1.Policy;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
import com.google.protobuf.AbstractMessage;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import javax.annotation.Generated;

@BetaApi
@Generated("by gapic-generator-java")
public class MockIAMPolicyImpl extends IAMPolicyImplBase {
private List<AbstractMessage> requests;
private Queue<Object> responses;

public MockIAMPolicyImpl() {
requests = new ArrayList<>();
responses = new LinkedList<>();
}

public List<AbstractMessage> getRequests() {
return requests;
}

public void addResponse(AbstractMessage response) {
responses.add(response);
}

public void setResponses(List<AbstractMessage> responses) {
this.responses = new LinkedList<Object>(responses);
}

public void addException(Exception exception) {
responses.add(exception);
}

public void reset() {
requests = new ArrayList<>();
responses = new LinkedList<>();
}

@Override
public void setIamPolicy(SetIamPolicyRequest request, StreamObserver<Policy> responseObserver) {
Object response = responses.poll();
if (response instanceof Policy) {
requests.add(request);
responseObserver.onNext(((Policy) response));
responseObserver.onCompleted();
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method SetIamPolicy, expected %s or %s",
response == null ? "null" : response.getClass().getName(),
Policy.class.getName(),
Exception.class.getName())));
}
}

@Override
public void getIamPolicy(GetIamPolicyRequest request, StreamObserver<Policy> responseObserver) {
Object response = responses.poll();
if (response instanceof Policy) {
requests.add(request);
responseObserver.onNext(((Policy) response));
responseObserver.onCompleted();
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method GetIamPolicy, expected %s or %s",
response == null ? "null" : response.getClass().getName(),
Policy.class.getName(),
Exception.class.getName())));
}
}

@Override
public void testIamPermissions(
TestIamPermissionsRequest request,
StreamObserver<TestIamPermissionsResponse> responseObserver) {
Object response = responses.poll();
if (response instanceof TestIamPermissionsResponse) {
requests.add(request);
responseObserver.onNext(((TestIamPermissionsResponse) response));
responseObserver.onCompleted();
} else if (response instanceof Exception) {
responseObserver.onError(((Exception) response));
} else {
responseObserver.onError(
new IllegalArgumentException(
String.format(
"Unrecognized response type %s for method TestIamPermissions, expected %s or %s",
response == null ? "null" : response.getClass().getName(),
TestIamPermissionsResponse.class.getName(),
Exception.class.getName())));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.pubsub.v1;

import com.google.api.core.BetaApi;
import com.google.api.gax.grpc.testing.MockGrpcService;
import com.google.protobuf.AbstractMessage;
import io.grpc.ServerServiceDefinition;
import java.util.List;
import javax.annotation.Generated;

@BetaApi
@Generated("by gapic-generator-java")
public class MockPublisher implements MockGrpcService {
private final MockPublisherImpl serviceImpl;

public MockPublisher() {
serviceImpl = new MockPublisherImpl();
}

@Override
public List<AbstractMessage> getRequests() {
return serviceImpl.getRequests();
}

@Override
public void addResponse(AbstractMessage response) {
serviceImpl.addResponse(response);
}

@Override
public void addException(Exception exception) {
serviceImpl.addException(exception);
}

@Override
public ServerServiceDefinition getServiceDefinition() {
return serviceImpl.bindService();
}

@Override
public void reset() {
serviceImpl.reset();
}
}
Loading

0 comments on commit 7d8c62d

Please sign in to comment.