Best Testcontainers-java code snippet using org.testcontainers.containers.PubSubEmulatorContainerTest.createSubscription
Source:PubSubEmulatorContainerTest.java
...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 // }97}...
createSubscription
Using AI Code Generation
1 PubSubEmulatorContainer pubSubEmulatorContainer = new PubSubEmulatorContainer();2 pubSubEmulatorContainer.start();3 pubSubEmulatorContainer.createSubscription("test-topic", "test-subscription");4 pubSubEmulatorContainer.stop();5 pubSubEmulatorContainer.start();6 pubSubEmulatorContainer.stop();7 pubSubEmulatorContainer.start();8 pubSubEmulatorContainer.stop();9 pubSubEmulatorContainer.start();10 pubSubEmulatorContainer.stop();11 pubSubEmulatorContainer.start();
createSubscription
Using AI Code Generation
1pubsubContainer.createTopic(topicId);2pubsubContainer.createSubscription(subscriptionId, topicId);3pubsubContainer.publish(topicId, "message");4List<PubsubMessage> messages = pubsubContainer.pull(subscriptionId, 1, true);5assertThat(messages).hasSize(1);6assertThat(messages.get(0).getData().toStringUtf8()).isEqualTo("message");7pubsubContainer.deleteSubscription(subscriptionId);8pubsubContainer.deleteTopic(topicId);9pubsubContainer.stop();
createSubscription
Using AI Code Generation
1import org.junit.jupiter.api.Test;2import org.testcontainers.containers.PubSubEmulatorContainer;3import org.testcontainers.utility.DockerImageName;4import java.util.concurrent.TimeUnit;5import static org.junit.jupiter.api.Assertions.assertTrue;6public class PubSubEmulatorContainerTest {7 private static final String TOPIC = "my-topic";8 public void testCreateSubscription() throws Exception {9 PubSubEmulatorContainer pubSubEmulatorContainer = new PubSubEmulatorContainer(DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:alpine"));10 pubSubEmulatorContainer.start();11 pubSubEmulatorContainer.createSubscription("my-subscription", TOPIC);12 assertTrue(pubSubEmulatorContainer.getSubscriptions(TOPIC).contains("my-subscription"));13 pubSubEmulatorContainer.stop();14 }15}16 at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:520)17 at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:318)18 at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)19 at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:316)20 at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:298)21 at org.testcontainers.containers.PubSubEmulatorContainer.start(PubSubEmulatorContainer.java:64)22 at org.testcontainers.containers.PubSubEmulatorContainerTest.testCreateSubscription(PubSubEmulatorContainerTest.java:23)23 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)24 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)25 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)26 at java.base/java.lang.reflect.Method.invoke(Method.java:566)27 at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)28 at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)29 at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(
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!!