diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index 08d4181c8fd3..cb78e3f9c728 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -49,7 +49,6 @@ import io.gapi.gax.grpc.ServiceApiSettings; import io.gapi.gax.internal.ApiUtils; import io.gapi.gax.protobuf.PathTemplate; -import io.grpc.Channel; import io.grpc.ManagedChannel; import java.io.IOException; @@ -151,14 +150,14 @@ public Iterable extractResources(ListTopicSubscriptionsResponse payload) * A PathTemplate representing the fully-qualified path to represent * a project resource. */ - public static final PathTemplate PROJECT_PATH_TEMPLATE = + private static final PathTemplate PROJECT_PATH_TEMPLATE = PathTemplate.create("/projects/{project}"); /** * A PathTemplate representing the fully-qualified path to represent * a topic resource. */ - public static final PathTemplate TOPIC_PATH_TEMPLATE = + private static final PathTemplate TOPIC_PATH_TEMPLATE = PathTemplate.create("/projects/{project}/topics/{topic}"); // ======== @@ -220,13 +219,28 @@ public static final String createTopicPath(String project, String topic) { "project", project,"topic", topic); } + /** + * Extracts the project from the given fully-qualified path which + * represents a project resource. + */ + public static final String extractProjectFromProjectPath(String projectPath) { + return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); + } - // ======== - // Getters - // ======== + /** + * Extracts the project from the given fully-qualified path which + * represents a topic resource. + */ + public static final String extractProjectFromTopicPath(String topicPath) { + return TOPIC_PATH_TEMPLATE.parse(topicPath).get("project"); + } - public Channel getChannel() { - return channel; + /** + * Extracts the topic from the given fully-qualified path which + * represents a topic resource. + */ + public static final String extractTopicFromTopicPath(String topicPath) { + return TOPIC_PATH_TEMPLATE.parse(topicPath).get("topic"); } diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index 6ec9195d25a2..1c9daaf82384 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -50,7 +50,6 @@ import io.gapi.gax.grpc.ServiceApiSettings; import io.gapi.gax.internal.ApiUtils; import io.gapi.gax.protobuf.PathTemplate; -import io.grpc.Channel; import io.grpc.ManagedChannel; import java.io.IOException; @@ -132,14 +131,14 @@ public Iterable extractResources(ListSubscriptionsResponse payload * A PathTemplate representing the fully-qualified path to represent * a project resource. */ - public static final PathTemplate PROJECT_PATH_TEMPLATE = + private static final PathTemplate PROJECT_PATH_TEMPLATE = PathTemplate.create("/projects/{project}"); /** * A PathTemplate representing the fully-qualified path to represent * a subscription resource. */ - public static final PathTemplate SUBSCRIPTION_PATH_TEMPLATE = + private static final PathTemplate SUBSCRIPTION_PATH_TEMPLATE = PathTemplate.create("/projects/{project}/subscriptions/{subscription}"); // ======== @@ -168,7 +167,11 @@ public static SubscriberApi create(ServiceApiSettings settings) throws IOExcepti return new SubscriberApi(settings); } - private SubscriberApi(ServiceApiSettings settings) throws IOException { + /** + * Constructs an instance of SubscriberApi, using the given settings. This is protected so that it + * easy to make a subclass, but otherwise, the static factory methods should be preferred. + */ + protected SubscriberApi(ServiceApiSettings settings) throws IOException { ServiceApiSettings internalSettings = ApiUtils.settingsWithChannels(settings, SERVICE_ADDRESS, DEFAULT_SERVICE_PORT, ALL_SCOPES); this.settings = internalSettings; @@ -197,13 +200,28 @@ public static final String createSubscriptionPath(String project, String subscri "project", project,"subscription", subscription); } + /** + * Extracts the project from the given fully-qualified path which + * represents a project resource. + */ + public static final String extractProjectFromProjectPath(String projectPath) { + return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); + } - // ======== - // Getters - // ======== + /** + * Extracts the project from the given fully-qualified path which + * represents a subscription resource. + */ + public static final String extractProjectFromSubscriptionPath(String subscriptionPath) { + return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("project"); + } - public Channel getChannel() { - return channel; + /** + * Extracts the subscription from the given fully-qualified path which + * represents a subscription resource. + */ + public static final String extractSubscriptionFromSubscriptionPath(String subscriptionPath) { + return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("subscription"); } diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/testing/LocalPublisherImpl.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/testing/LocalPublisherImpl.java index b9c5f9d513bd..6ec1c008f6d0 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/testing/LocalPublisherImpl.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/testing/LocalPublisherImpl.java @@ -77,8 +77,8 @@ public void getTopic(GetTopicRequest request, StreamObserver responseObse public void listTopics(ListTopicsRequest request, StreamObserver responseObserver) { List responseTopics = new ArrayList<>(); for (String topicName : topics.keySet()) { - String projectOfTopic = PublisherApi.TOPIC_PATH_TEMPLATE.parse(topicName).get("project"); - String projectOfRequest = PublisherApi.PROJECT_PATH_TEMPLATE.parse(request.getProject()).get("project"); + String projectOfTopic = PublisherApi.extractProjectFromTopicPath(topicName); + String projectOfRequest = PublisherApi.extractProjectFromProjectPath(request.getProject()); if (projectOfTopic.equals(projectOfRequest)) { Topic topicObj = Topic.newBuilder().setName(topicName).build(); responseTopics.add(topicObj); diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/testing/LocalPubsubHelper.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/testing/LocalPubsubHelper.java index 13fbf5208047..033330ad6b83 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/testing/LocalPubsubHelper.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/testing/LocalPubsubHelper.java @@ -19,19 +19,22 @@ * A class that runs an in-memory Publisher instance for use in tests. */ public class LocalPubsubHelper { - private static final SocketAddress address = new LocalAddress("in-process-1"); - private static Server server; - private static LocalPublisherImpl publisherImpl; + private static int FLOW_CONTROL_WINDOW = 65 * 1024; + + private final SocketAddress address; + private final Server server; + private final LocalPublisherImpl publisherImpl; /** * Constructs a new LocalPubsubHelper. The method start() must * be called before it is used. */ - public LocalPubsubHelper() { + public LocalPubsubHelper(String addressString) { + address = new LocalAddress(addressString); publisherImpl = new LocalPublisherImpl(); NettyServerBuilder builder = NettyServerBuilder .forAddress(address) - .flowControlWindow(65 * 1024) + .flowControlWindow(FLOW_CONTROL_WINDOW) .channelType(LocalServerChannel.class); builder.addService(PublisherGrpc.bindService(publisherImpl)); server = builder.build(); diff --git a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java index 96bdd9106497..19939e0876b6 100644 --- a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java +++ b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java @@ -38,7 +38,7 @@ public class PublisherApiTest { @BeforeClass public static void startStaticServer() { - pubsubHelper = new LocalPubsubHelper().start(); + pubsubHelper = new LocalPubsubHelper("in-process-1").start(); } @AfterClass