Best Testcontainers-java code snippet using org.testcontainers.containers.PubSubEmulatorContainerTest.createTopic
Source:PubSubEmulatorContainerTest.java
...42 TransportChannelProvider channelProvider =43 FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));44 NoCredentialsProvider credentialsProvider = NoCredentialsProvider.create();45 String topicId = "my-topic-id";46 createTopic(topicId, channelProvider, credentialsProvider);47 String subscriptionId = "my-subscription-id";48 createSubscription(subscriptionId, topicId, channelProvider, credentialsProvider);49 Publisher publisher = Publisher.newBuilder(TopicName.of(PROJECT_ID, topicId))50 .setChannelProvider(channelProvider)51 .setCredentialsProvider(credentialsProvider)52 .build();53 PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("test message")).build();54 publisher.publish(message);55 SubscriberStubSettings subscriberStubSettings =56 SubscriberStubSettings.newBuilder()57 .setTransportChannelProvider(channelProvider)58 .setCredentialsProvider(credentialsProvider)59 .build();60 try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) {61 PullRequest pullRequest = PullRequest.newBuilder()62 .setMaxMessages(1)63 .setSubscription(ProjectSubscriptionName.format(PROJECT_ID, subscriptionId))64 .build();65 PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);66 assertThat(pullResponse.getReceivedMessagesList()).hasSize(1);67 assertThat(pullResponse.getReceivedMessages(0).getMessage().getData().toStringUtf8()).isEqualTo("test message");68 }69 } finally {70 channel.shutdown();71 }72 }73 // }74 // createTopic {75 private void createTopic(String topicId, TransportChannelProvider channelProvider, NoCredentialsProvider credentialsProvider) throws IOException {76 TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()77 .setTransportChannelProvider(channelProvider)78 .setCredentialsProvider(credentialsProvider)79 .build();80 try (TopicAdminClient topicAdminClient = TopicAdminClient.create(topicAdminSettings)) {81 TopicName topicName = TopicName.of(PROJECT_ID, topicId);82 topicAdminClient.createTopic(topicName);83 }84 }85 // }86 // createSubscription {87 private void createSubscription(String subscriptionId, String topicId, TransportChannelProvider channelProvider, NoCredentialsProvider credentialsProvider) throws IOException {88 SubscriptionAdminSettings subscriptionAdminSettings = SubscriptionAdminSettings.newBuilder()89 .setTransportChannelProvider(channelProvider)90 .setCredentialsProvider(credentialsProvider)91 .build();92 SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(subscriptionAdminSettings);93 ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);94 subscriptionAdminClient.createSubscription(subscriptionName, TopicName.of(PROJECT_ID, topicId), PushConfig.getDefaultInstance(), 10);95 }96 // }...
createTopic
Using AI Code Generation
1import (2const (3func TestPubSub(t *testing.T) {4 ctx := context.Background()5 pubSubEmulator, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{6 ContainerRequest: testcontainers.ContainerRequest{7 ExposedPorts: []string{"8085/tcp"},8 Cmd: []string{"gcloud", "beta", "emulators", "pubsub", "start", "--project", projectID, "--host-port=
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!