com.google.pubsub.v1.PushConfig Java Examples

The following examples show how to use com.google.pubsub.v1.PushConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PubSubConnector.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
private void createSubscription(final PubSubConfig config) {
    final SubscriptionAdminClient subscriptionAdminClient = pubSubManager.subscriptionAdminClient(config);

    final ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(config.getProjectId(),
            config.getSubscription());

    try {
        subscriptionAdminClient.getSubscription(subscriptionName);
    } catch (final NotFoundException e) {
        final PushConfig pushConfig = PushConfig.newBuilder()
                .build();

        final ProjectTopicName topicName = ProjectTopicName.of(config.getProjectId(), config.getTopic());

        subscriptionAdminClient.createSubscription(subscriptionName, topicName, pushConfig, 0);
    }
}
 
Example #2
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new subscription on Google Cloud Pub/Sub.
 *
 * @param subscriptionName canonical subscription name, e.g., "subscriptionName", or the fully-qualified
 * subscription name in the {@code projects/<project_name>/subscriptions/<subscription_name>} format
 * @param topicName canonical topic name, e.g., "topicName", or the fully-qualified topic name in the
 * {@code projects/<project_name>/topics/<topic_name>} format
 * @param ackDeadline deadline in seconds before a message is resent, must be between 10
 * and 600 seconds. If not provided, set to default of 10 seconds
 * @param pushEndpoint the URL of the service receiving the push messages. If not
 * provided, uses message pulling by default
 * @return the created subscription
 */
public Subscription createSubscription(String subscriptionName, String topicName,
		Integer ackDeadline, String pushEndpoint) {
	Assert.hasText(subscriptionName, "No subscription name was specified.");
	Assert.hasText(topicName, "No topic name was specified.");

	int finalAckDeadline = this.defaultAckDeadline;
	if (ackDeadline != null) {
		validateAckDeadline(ackDeadline);
		finalAckDeadline = ackDeadline;
	}

	PushConfig.Builder pushConfigBuilder = PushConfig.newBuilder();
	if (pushEndpoint != null) {
		pushConfigBuilder.setPushEndpoint(pushEndpoint);
	}

	return this.subscriptionAdminClient.createSubscription(
			PubSubSubscriptionUtils.toProjectSubscriptionName(subscriptionName, this.projectId),
			PubSubTopicUtils.toProjectTopicName(topicName, this.projectId),
			pushConfigBuilder.build(),
			finalAckDeadline);
}
 
Example #3
Source File: SubscriberServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
@Test
public void listSubscriptions() {
  ListSubscriptionsRequest request =
      ListSubscriptionsRequest.newBuilder().setProject(TestHelpers.PROJECT1).build();
  ListSubscriptionsResponse response = blockingStub.listSubscriptions(request);

  assertThat(
      response.getSubscriptionsList(),
      Matchers.contains(
          Subscription.newBuilder()
              .setName(TestHelpers.PROJECT1_SUBSCRIPTION1)
              .setTopic(TestHelpers.PROJECT1_TOPIC1)
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build(),
          Subscription.newBuilder()
              .setName(TestHelpers.PROJECT1_SUBSCRIPTION2)
              .setTopic(TestHelpers.PROJECT1_TOPIC1)
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(30)
              .build()));
}
 
Example #4
Source File: SubscriberServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
@Test
public void getSubscription() {
  GetSubscriptionRequest request =
      GetSubscriptionRequest.newBuilder()
          .setSubscription(TestHelpers.PROJECT1_SUBSCRIPTION1)
          .build();
  Subscription response = blockingStub.getSubscription(request);
  assertThat(
      response,
      Matchers.equalTo(
          Subscription.newBuilder()
              .setName(TestHelpers.PROJECT1_SUBSCRIPTION1)
              .setTopic(TestHelpers.PROJECT1_TOPIC1)
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build()));
}
 
Example #5
Source File: SubscriberServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
@Test
public void createSubscription() {
  Subscription request =
      Subscription.newBuilder()
          .setTopic(TestHelpers.PROJECT1_TOPIC1)
          .setName("projects/project-1/subscriptions/new-subscriptions")
          .build();
  Subscription builtRequest =
      request
          .toBuilder()
          .setAckDeadlineSeconds(10)
          .putLabels(KAFKA_TOPIC, "kafka-topic-1")
          .setPushConfig(PushConfig.getDefaultInstance())
          .build();
  SubscriptionManager fakeSubscriptionManager =
      new FakeSubscriptionManager(
          builtRequest, mock(SubscriptionManager.class), mockKafkaClientFactory);
  when(mockSubscriptionManagerFactory.create(builtRequest)).thenReturn(fakeSubscriptionManager);

  assertThat(blockingStub.createSubscription(request), Matchers.equalTo(builtRequest));
  assertThat(fakeSubscriptionManager.isRunning(), Matchers.is(true));
  verify(mockSubscriptionManagerFactory).create(builtRequest);
}
 
Example #6
Source File: ConfigurationManagerTest.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteSubscription() throws ConfigurationNotFoundException {
  configurationManager.deleteSubscription("projects/project-2/subscriptions/subscription-3");
  assertThat(
      configurationManager.getSubscriptions("projects/project-2"),
      Matchers.contains(
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-2/subscriptions/subscription-1")
              .setTopic("projects/project-2/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-2/subscriptions/subscription-2")
              .setTopic("projects/project-2/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setAckDeadlineSeconds(10)
              .setPushConfig(PushConfig.getDefaultInstance())
              .build()));
}
 
Example #7
Source File: PubsubHelper.java    From flink with Apache License 2.0 6 votes vote down vote up
public void createSubscription(String subscriptionProject, String subscription, String topicProject, String topic) throws IOException {
	ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.newBuilder()
		.setProject(subscriptionProject)
		.setSubscription(subscription)
		.build();

	deleteSubscription(subscriptionName);

	SubscriptionAdminClient adminClient = getSubscriptionAdminClient();

	ProjectTopicName topicName = ProjectTopicName.of(topicProject, topic);

	PushConfig pushConfig = PushConfig.getDefaultInstance();

	LOG.info("CreateSubscription {}", subscriptionName);
	getSubscriptionAdminClient().createSubscription(subscriptionName, topicName, pushConfig, 1);
}
 
Example #8
Source File: ConfigurationManagerTest.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Test
public void getSubscriptionsForTopic() {
  assertThat(
      configurationManager.getSubscriptionsForTopic("projects/project-1/topics/topic-1"),
      Matchers.contains(
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-1")
              .setTopic("projects/project-1/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setAckDeadlineSeconds(10)
              .setPushConfig(PushConfig.getDefaultInstance())
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-2")
              .setTopic("projects/project-1/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build()));
  assertThat(
      configurationManager.getSubscriptionsForTopic("projects/project-2/topics/topic-1"),
      Matchers.contains(
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-2/subscriptions/subscription-1")
              .setTopic("projects/project-2/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-2/subscriptions/subscription-2")
              .setTopic("projects/project-2/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build()));
}
 
Example #9
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
/** Updates the managed Server when a new Subscription is created. */
public final com.google.pubsub.v1.Subscription createSubscription(
    com.google.pubsub.v1.Subscription subscription)
    throws ConfigurationAlreadyExistsException, ConfigurationNotFoundException {
  ProjectTopicName projectTopicName = ProjectTopicName.parse(subscription.getTopic());
  ProjectSubscriptionName projectSubscriptionName =
      ProjectSubscriptionName.parse(subscription.getName());

  if (getSubscriptionByName(subscription.getName()).isPresent()) {
    throw new ConfigurationAlreadyExistsException(
        "Subscription " + projectSubscriptionName.toString() + " already exists");
  }
  com.google.pubsub.v1.Topic topic =
      getTopicByName(projectTopicName.toString())
          .orElseThrow(
              () ->
                  new ConfigurationNotFoundException(
                      "Topic " + projectTopicName.toString() + " does not exist"));
  com.google.pubsub.v1.Subscription.Builder builder =
      subscription.toBuilder().putLabels(KAFKA_TOPIC, topic.getLabelsOrThrow(KAFKA_TOPIC));
  if (subscription.getAckDeadlineSeconds() == 0) {
    builder.setAckDeadlineSeconds(10);
  }
  builder.setPushConfig(PushConfig.getDefaultInstance()).build();
  com.google.pubsub.v1.Subscription built = builder.build();
  subscriptionsByProject.put(
      ProjectName.of(projectSubscriptionName.getProject()).toString(), built);
  pubSubRepository.save(getPubSub());
  return built;
}
 
Example #10
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Inject
public ConfigurationManager(Server serverConfiguration, PubSubRepository pubSubRepository) {
  Preconditions.checkNotNull(serverConfiguration);
  Preconditions.checkNotNull(pubSubRepository);

  this.serverConfiguration = serverConfiguration;
  this.pubSubRepository = pubSubRepository;
  topicsByProject.clear();
  subscriptionsByProject.clear();
  for (Project p : pubSubRepository.load().getProjectsList()) {
    for (Topic t : p.getTopicsList()) {
      String projectName = ProjectName.format(p.getName());
      String topicName = ProjectTopicName.format(p.getName(), t.getName());
      String kafkaTopic = Optional.ofNullable(t.getKafkaTopic()).orElse(t.getName());
      topicsByProject.put(
          projectName,
          com.google.pubsub.v1.Topic.newBuilder()
              .setName(topicName)
              .putLabels(KAFKA_TOPIC, kafkaTopic)
              .build());

      for (Subscription s : t.getSubscriptionsList()) {
        subscriptionsByProject.put(
            projectName,
            com.google.pubsub.v1.Subscription.newBuilder()
                .setTopic(topicName)
                .setName(ProjectSubscriptionName.format(p.getName(), s.getName()))
                .setAckDeadlineSeconds(s.getAckDeadlineSeconds())
                .setPushConfig(PushConfig.getDefaultInstance())
                .putLabels(KAFKA_TOPIC, kafkaTopic)
                .build());
      }
    }
  }
}
 
Example #11
Source File: SubscriberServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Test
public void listSubscriptions_withPagination() {
  ListSubscriptionsRequest request =
      ListSubscriptionsRequest.newBuilder()
          .setProject(TestHelpers.PROJECT1)
          .setPageSize(1)
          .build();
  ListSubscriptionsResponse response = blockingStub.listSubscriptions(request);

  assertThat(
      response.getSubscriptionsList(),
      Matchers.contains(
          Subscription.newBuilder()
              .setName(TestHelpers.PROJECT1_SUBSCRIPTION1)
              .setTopic(TestHelpers.PROJECT1_TOPIC1)
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build()));
  assertThat(response.getNextPageToken(), Matchers.not(Matchers.isEmptyOrNullString()));

  request = request.toBuilder().setPageToken(response.getNextPageToken()).setPageSize(0).build();
  response = blockingStub.listSubscriptions(request);
  assertThat(
      response.getSubscriptionsList(),
      Matchers.contains(
          Subscription.newBuilder()
              .setName(TestHelpers.PROJECT1_SUBSCRIPTION2)
              .setTopic(TestHelpers.PROJECT1_TOPIC1)
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(30)
              .build()));
  assertThat(response.getNextPageToken(), Matchers.isEmptyOrNullString());
}
 
Example #12
Source File: PubsubIntegrationTest.java    From gcp-ingestion with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Create a Pub/Sub topic and subscription.
 *
 * @throws IOException if Pub/Sub is unavailable
 */
@Before
public void initializePubsubResources() throws IOException {
  projectId = ServiceOptions.getDefaultProjectId();
  topicId = "test-topic-" + UUID.randomUUID().toString();
  subscriptionId = "test-subscription-" + UUID.randomUUID().toString();
  topicName = ProjectTopicName.of(projectId, topicId);
  subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);

  TopicAdminClient.create().createTopic(topicName);
  SubscriptionAdminClient.create().createSubscription(subscriptionName, topicName,
      PushConfig.getDefaultInstance(), 0);
}
 
Example #13
Source File: PubSubSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void prepare() throws IOException {
	assumeThat(
			"PUB/SUB-sample integration tests are disabled. Please use '-Dit.pubsub=true' "
					+ "to enable them. ",
			System.getProperty("it.pubsub"), is("true"));

	projectName = ProjectName.of(ServiceOptions.getDefaultProjectId()).getProject();
	topicAdminClient = TopicAdminClient.create();
	subscriptionAdminClient = SubscriptionAdminClient.create();

	topicAdminClient.createTopic(ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC));
	topicAdminClient.createTopic(ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC2));

	subscriptionAdminClient.createSubscription(
			ProjectSubscriptionName.of(projectName, SAMPLE_TEST_SUBSCRIPTION1),
			ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC),
			PushConfig.getDefaultInstance(),
			10);

	subscriptionAdminClient.createSubscription(
			ProjectSubscriptionName.of(projectName, SAMPLE_TEST_SUBSCRIPTION2),
			ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC2),
			PushConfig.getDefaultInstance(),
			10);
	subscriptionAdminClient.createSubscription(
			ProjectSubscriptionName.of(projectName, SAMPLE_TEST_SUBSCRIPTION3),
			ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC2),
			PushConfig.getDefaultInstance(),
			10);
}
 
Example #14
Source File: Connection.java    From heroic with Apache License 2.0 5 votes vote down vote up
void createSubscription() throws IOException {
    log.info("Creating subscription {} for topic {}", subscriptionName, topicName);
    SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(
        SubscriptionAdminSettings.newBuilder()
          .setTransportChannelProvider(channelProvider)
          .setCredentialsProvider(credentialsProvider)
          .build()
    );
    try {
        subscriptionAdminClient.createSubscription(
            subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
    } catch (AlreadyExistsException e) {
        log.info("Subscription already exists");
    }
}
 
Example #15
Source File: Subscriptions.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static Subscription createOccurrenceSubscription(String subId, String projectId) 
    throws IOException, StatusRuntimeException, InterruptedException {
  // This topic id will automatically receive messages when Occurrences are added or modified
  String topicId = "container-analysis-occurrences-v1";
  ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
  ProjectSubscriptionName subName = ProjectSubscriptionName.of(projectId, subId);

  SubscriptionAdminClient client = SubscriptionAdminClient.create();
  PushConfig config = PushConfig.getDefaultInstance();
  Subscription sub = client.createSubscription(subName, topicName, config, 0);
  return sub;
}
 
Example #16
Source File: InspectTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws Exception {
  // Create a new topic
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.createTopic(topicName);
  }

  // Create a new subscription
  try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
    subscriptionAdminClient
        .createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
  }
}
 
Example #17
Source File: RiskAnalysisTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws Exception {
  // Create a new topic
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.createTopic(topicName);
  }
  // Create a new subscription
  try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
    subscriptionAdminClient
        .createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
  }
}
 
Example #18
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public void createSubscription(String subscriptionProject, String subscription, String topicProject, String topic) throws IOException {
	ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.newBuilder()
		.setProject(subscriptionProject)
		.setSubscription(subscription)
		.build();

	deleteSubscription(subscriptionName);

	ProjectTopicName topicName = ProjectTopicName.of(topicProject, topic);

	PushConfig pushConfig = PushConfig.getDefaultInstance();

	LOG.info("CreateSubscription {}", subscriptionName);
	getSubscriptionAdminClient().createSubscription(subscriptionName, topicName, pushConfig, 1).isInitialized();
}
 
Example #19
Source File: ConfigurationManagerTest.java    From kafka-pubsub-emulator with Apache License 2.0 4 votes vote down vote up
@Test
public void createSubscription()
    throws ConfigurationNotFoundException, ConfigurationAlreadyExistsException {
  com.google.pubsub.v1.Subscription newSubscription =
      com.google.pubsub.v1.Subscription.newBuilder()
          .setName("projects/project-1/subscriptions/new-subscription")
          .setTopic("projects/project-1/topics/topic-1")
          .build();
  configurationManager.createSubscription(newSubscription);
  assertThat(
      configurationManager.getSubscriptions("projects/project-1"),
      Matchers.contains(
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/new-subscription")
              .setTopic("projects/project-1/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-1")
              .setTopic("projects/project-1/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-2")
              .setTopic("projects/project-1/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-3")
              .setTopic("projects/project-1/topics/topic-2")
              .putLabels(KAFKA_TOPIC, "kafka-topic-2")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(30)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-4")
              .setTopic("projects/project-1/topics/topic-2")
              .putLabels(KAFKA_TOPIC, "kafka-topic-2")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(45)
              .build()));
}
 
Example #20
Source File: ConfigurationManagerTest.java    From kafka-pubsub-emulator with Apache License 2.0 4 votes vote down vote up
@Test
public void getSubscriptions() {
  assertThat(
      configurationManager.getSubscriptions("projects/project-1"),
      Matchers.contains(
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-1")
              .setTopic("projects/project-1/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-2")
              .setTopic("projects/project-1/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-3")
              .setTopic("projects/project-1/topics/topic-2")
              .putLabels(KAFKA_TOPIC, "kafka-topic-2")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(30)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-1/subscriptions/subscription-4")
              .setTopic("projects/project-1/topics/topic-2")
              .putLabels(KAFKA_TOPIC, "kafka-topic-2")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(45)
              .build()));
  assertThat(
      configurationManager.getSubscriptions("projects/project-2"),
      Matchers.contains(
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-2/subscriptions/subscription-1")
              .setTopic("projects/project-2/topics/topic-1")
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .setAckDeadlineSeconds(10)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-2/subscriptions/subscription-2")
              .setTopic("projects/project-2/topics/topic-1")
              .setPushConfig(PushConfig.getDefaultInstance())
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .setAckDeadlineSeconds(10)
              .build(),
          com.google.pubsub.v1.Subscription.newBuilder()
              .setName("projects/project-2/subscriptions/subscription-3")
              .setTopic("projects/project-2/topics/topic-2")
              .setPushConfig(PushConfig.getDefaultInstance())
              .putLabels(KAFKA_TOPIC, "kafka-topic-2")
              .setAckDeadlineSeconds(30)
              .build()));
}
 
Example #21
Source File: SubscriberServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private ConfigurationManager setupConfigurationRepository() {
  configurationManager =
      new ConfigurationManager(
          TestHelpers.SERVER_CONFIG,
          new FakePubSubRepository(
              PubSub.newBuilder()
                  .addProjects(
                      Project.newBuilder()
                          .setName("project-1")
                          .addTopics(
                              Topic.newBuilder()
                                  .setName("topic-1")
                                  .setKafkaTopic("kafka-topic-1")
                                  .addSubscriptions(
                                      com.google.cloud.partners.pubsub.kafka.config.Subscription
                                          .newBuilder()
                                          .setName("subscription-1")
                                          .setAckDeadlineSeconds(10)
                                          .build())
                                  .addSubscriptions(
                                      com.google.cloud.partners.pubsub.kafka.config.Subscription
                                          .newBuilder()
                                          .setName("subscription-2")
                                          .setAckDeadlineSeconds(30)
                                          .build())
                                  .build())
                          .build())
                  .addProjects(
                      Project.newBuilder()
                          .setName("project-2")
                          .addTopics(
                              Topic.newBuilder()
                                  .setName("topic-2")
                                  .setKafkaTopic("kafka-topic-2")
                                  .addSubscriptions(
                                      com.google.cloud.partners.pubsub.kafka.config.Subscription
                                          .newBuilder()
                                          .setName("subscription-3")
                                          .setAckDeadlineSeconds(45)
                                          .build())
                                  .build())
                          .build())
                  .build()));
  Subscription subscription1 =
      Subscription.newBuilder()
          .setName(TestHelpers.PROJECT1_SUBSCRIPTION1)
          .setTopic(TestHelpers.PROJECT1_TOPIC1)
          .putLabels(KAFKA_TOPIC, "kafka-topic-1")
          .setPushConfig(PushConfig.getDefaultInstance())
          .setAckDeadlineSeconds(10)
          .build();
  Subscription subscription2 =
      Subscription.newBuilder()
          .setName(TestHelpers.PROJECT1_SUBSCRIPTION2)
          .setTopic(TestHelpers.PROJECT1_TOPIC1)
          .putLabels(KAFKA_TOPIC, "kafka-topic-1")
          .setPushConfig(PushConfig.getDefaultInstance())
          .setAckDeadlineSeconds(30)
          .build();
  Subscription subscription3 =
      Subscription.newBuilder()
          .setName(TestHelpers.PROJECT2_SUBSCRIPTION3)
          .setTopic("projects/project-2/topics/topic-2")
          .putLabels(KAFKA_TOPIC, "kafka-topic-2")
          .setPushConfig(PushConfig.getDefaultInstance())
          .setAckDeadlineSeconds(45)
          .build();

  fakeSubscriptionManager1 =
      spy(
          new FakeSubscriptionManager(
              subscription1, mockSubscriptionManager1, mockKafkaClientFactory));
  fakeSubscriptionManager2 =
      spy(
          new FakeSubscriptionManager(
              subscription2, mockSubscriptionManager2, mockKafkaClientFactory));
  fakeSubscriptionManager3 =
      spy(
          new FakeSubscriptionManager(
              subscription3, mockSubscriptionManager3, mockKafkaClientFactory));

  when(mockSubscriptionManagerFactory.create(subscription1)).thenReturn(fakeSubscriptionManager1);
  when(mockSubscriptionManagerFactory.create(subscription2)).thenReturn(fakeSubscriptionManager2);
  when(mockSubscriptionManagerFactory.create(subscription3)).thenReturn(fakeSubscriptionManager3);

  return configurationManager;
}