-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #223 from loicmathieu/feat/pubsub-emulator
Feat/pubsub emulator
- Loading branch information
Showing
9 changed files
with
207 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...n-tests/main/src/main/java/io/quarkiverse/googlecloudservices/it/pubsub/TopicManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package io.quarkiverse.googlecloudservices.it.pubsub; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.stream.StreamSupport; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
import com.google.api.gax.core.CredentialsProvider; | ||
import com.google.api.gax.grpc.GrpcTransportChannel; | ||
import com.google.api.gax.rpc.FixedTransportChannelProvider; | ||
import com.google.api.gax.rpc.TransportChannelProvider; | ||
import com.google.cloud.pubsub.v1.*; | ||
import com.google.pubsub.v1.*; | ||
|
||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
|
||
@ApplicationScoped | ||
public class TopicManager { | ||
@Inject | ||
CredentialsProvider credentialsProvider; | ||
|
||
@ConfigProperty(name = "quarkus.google.cloud.project-id") | ||
String projectId; | ||
|
||
@ConfigProperty(name = "pubsub.use-emulator", defaultValue = "false") | ||
boolean useEmulator; | ||
|
||
private TopicName topicName; | ||
private Optional<TransportChannelProvider> channelProvider; | ||
|
||
@PostConstruct | ||
void init() { | ||
this.topicName = TopicName.of(projectId, "test-topic"); | ||
|
||
if (useEmulator) { | ||
ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8085").usePlaintext().build(); | ||
channelProvider = Optional.of(FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel))); | ||
} else { | ||
channelProvider = Optional.empty(); | ||
} | ||
} | ||
|
||
public Subscriber initSubscriber(MessageReceiver receiver) throws IOException { | ||
ProjectSubscriptionName subscriptionName = initTopicAndSubscription(); | ||
var builder = Subscriber.newBuilder(subscriptionName, receiver) | ||
.setCredentialsProvider(credentialsProvider); | ||
channelProvider.ifPresent(builder::setChannelProvider); | ||
return builder.build(); | ||
} | ||
|
||
public Publisher initPublisher() throws IOException { | ||
var builder = Publisher.newBuilder(topicName) | ||
.setCredentialsProvider(credentialsProvider); | ||
channelProvider.ifPresent(builder::setChannelProvider); | ||
return builder.build(); | ||
} | ||
|
||
private ProjectSubscriptionName initTopicAndSubscription() throws IOException { | ||
TopicAdminSettings topicAdminSettings = getTopicAdminSettings(); | ||
try (TopicAdminClient topicAdminClient = TopicAdminClient.create(topicAdminSettings)) { | ||
Iterable<Topic> topics = topicAdminClient.listTopics(ProjectName.of(projectId)).iterateAll(); | ||
Optional<Topic> existing = StreamSupport.stream(topics.spliterator(), false) | ||
.filter(topic -> topic.getName().equals(topicName.toString())) | ||
.findFirst(); | ||
if (existing.isEmpty()) { | ||
topicAdminClient.createTopic(topicName.toString()); | ||
} | ||
} | ||
|
||
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, "test-subscription"); | ||
SubscriptionAdminSettings subscriptionAdminSettings = getSubscriptionAdminSettings(); | ||
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(subscriptionAdminSettings)) { | ||
Iterable<Subscription> subscriptions = subscriptionAdminClient.listSubscriptions(ProjectName.of(projectId)) | ||
.iterateAll(); | ||
Optional<Subscription> existing = StreamSupport.stream(subscriptions.spliterator(), false) | ||
.filter(sub -> sub.getName().equals(subscriptionName.toString())) | ||
.findFirst(); | ||
if (existing.isEmpty()) { | ||
subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0); | ||
} | ||
} | ||
return subscriptionName; | ||
} | ||
|
||
private SubscriptionAdminSettings getSubscriptionAdminSettings() throws IOException { | ||
var builder = SubscriptionAdminSettings.newBuilder() | ||
.setCredentialsProvider(credentialsProvider); | ||
channelProvider.ifPresent(builder::setTransportChannelProvider); | ||
return builder.build(); | ||
} | ||
|
||
private TopicAdminSettings getTopicAdminSettings() throws IOException { | ||
var builder = TopicAdminSettings.newBuilder() | ||
.setCredentialsProvider(credentialsProvider); | ||
channelProvider.ifPresent(builder::setTransportChannelProvider); | ||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...on-tests/main/src/test/java/io/quarkiverse/googlecloudservices/it/PubSubResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.quarkiverse.googlecloudservices.it; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.hamcrest.core.IsEqual.equalTo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.containers.PubSubEmulatorContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.http.ContentType; | ||
|
||
@QuarkusTest | ||
public class PubSubResourceTest { | ||
private static final PubSubEmulatorContainer EMULATOR = new PubSubEmulatorContainer( | ||
DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk")); | ||
|
||
@BeforeAll | ||
public static void startGcloudContainer() { | ||
List<String> portBindings = new ArrayList<>(); | ||
portBindings.add("8085:8085"); | ||
EMULATOR.setPortBindings(portBindings); | ||
EMULATOR.start(); | ||
} | ||
|
||
@AfterAll | ||
public static void stopGcloudContainer() { | ||
EMULATOR.stop(); | ||
} | ||
|
||
@Test | ||
public void testPubSub() throws ExecutionException, InterruptedException, TimeoutException { | ||
String message = "Hello Pub/Sub"; | ||
given() | ||
.body(message) | ||
.contentType(ContentType.TEXT) | ||
.when().post("/pubsub") | ||
.then() | ||
.statusCode(204); | ||
|
||
await().atMost(3, TimeUnit.SECONDS) | ||
.untilAsserted(() -> given() | ||
.when().get("/pubsub") | ||
.then() | ||
.statusCode(200) | ||
.body(equalTo(message))); | ||
} | ||
} |
Oops, something went wrong.