Skip to content

Commit 9715428

Browse files
committed
feat: #261 Adding GooglePubSub support
Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com>
1 parent 59f4891 commit 9715428

File tree

6 files changed

+377
-3
lines changed

6 files changed

+377
-3
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@
158158
<scope>test</scope>
159159
</dependency>
160160

161+
<dependency>
162+
<groupId>org.testcontainers</groupId>
163+
<artifactId>gcloud</artifactId>
164+
<version>${testcontainers.version}</version>
165+
<scope>test</scope>
166+
</dependency>
167+
<dependency>
168+
<groupId>com.google.cloud</groupId>
169+
<artifactId>google-cloud-pubsub</artifactId>
170+
<version>1.134.1</version>
171+
<scope>test</scope>
172+
</dependency>
173+
161174
<dependency>
162175
<groupId>com.github.dasniko</groupId>
163176
<artifactId>testcontainers-keycloak</artifactId>

src/main/java/io/github/microcks/testcontainers/MicrocksAsyncMinionContainer.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.github.microcks.testcontainers.connection.AmazonServiceConnection;
1919
import io.github.microcks.testcontainers.connection.GenericConnection;
20+
import io.github.microcks.testcontainers.connection.GooglePubSubConnection;
2021
import io.github.microcks.testcontainers.connection.KafkaConnection;
2122

2223
import org.testcontainers.containers.GenericContainer;
@@ -187,6 +188,23 @@ public MicrocksAsyncMinionContainer withAmazonSNSConnection(AmazonServiceConnect
187188
return this;
188189
}
189190

191+
/**
192+
* Connect the MicrocksAsyncMinionContainer to a Google Pub/Sub service to allow Pub/Sub messages mocking.
193+
* @param connection Connection details to a Google Pub/Sub service.
194+
* @return self
195+
*/
196+
public MicrocksAsyncMinionContainer withGooglePubSubConnection(GooglePubSubConnection connection) {
197+
if (!extraProtocols.contains(",GOOGLEPUBSUB")) {
198+
extraProtocols += ",GOOGLEPUBSUB";
199+
}
200+
withEnv(ASYNC_PROTOCOLS_ENV_VAR, extraProtocols);
201+
withEnv("GOOGLEPUBSUB_PROJECT", connection.getProjectId());
202+
if (connection.getEmulatorHost() != null) {
203+
withEnv("PUBSUB_EMULATOR_HOST", connection.getEmulatorHost());
204+
}
205+
return this;
206+
}
207+
190208
/**
191209
* Get the Http endpoint where Microcks can be accessed (you'd have to append '/api' to access APIs)
192210
* @return The Http endpoint for talking to container.
@@ -289,6 +307,24 @@ public String getAmazonSNSMockTopic(String service, String version, String opera
289307
return getAmazonServiceMockDestination(service, version, operationName);
290308
}
291309

310+
/**
311+
* Get the exposed mock topic for a Google Pub/Sub Service.
312+
* @param service The name of Service/API
313+
* @param version The version of Service/API
314+
* @param operationName The name of operation to get the topic for
315+
* @return A usable topic to interact with Microcks mocks.
316+
*/
317+
public String getGooglePubSubMockTopic(String service, String version, String operationName) {
318+
// operationName may start with SUBSCRIBE or PUBLISH.
319+
if (operationName.contains(" ")) {
320+
operationName = operationName.split(" ")[1];
321+
}
322+
return String.format(DESTINATION_PATTERN,
323+
service.replace(" ", "").replace("-", ""),
324+
version.replace(" ", ""),
325+
operationName.replace("/", "-"));
326+
}
327+
292328
private String getAmazonServiceMockDestination(String service, String version, String operationName) {
293329
// operationName may start with SUBSCRIBE or PUBLISH.
294330
if (operationName.contains(" ")) {

src/main/java/io/github/microcks/testcontainers/MicrocksContainersEnsemble.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.github.microcks.testcontainers.connection.AmazonServiceConnection;
1919
import io.github.microcks.testcontainers.connection.GenericConnection;
20+
import io.github.microcks.testcontainers.connection.GooglePubSubConnection;
2021
import io.github.microcks.testcontainers.connection.KafkaConnection;
2122
import io.github.microcks.testcontainers.model.Secret;
2223

@@ -266,6 +267,17 @@ public MicrocksContainersEnsemble withAmazonSNSConnection(AmazonServiceConnectio
266267
return this;
267268
}
268269

270+
/**
271+
* Once the Async Feature is enabled, connects to a Google Pub/Sub service.
272+
* @param connection Connection details to a Google Pub/Sub service.
273+
* @return self
274+
*/
275+
public MicrocksContainersEnsemble withGooglePubSubConnection(GooglePubSubConnection connection) {
276+
ensureAsyncFeatureIsEnabled();
277+
this.asyncMinion.withGooglePubSubConnection(connection);
278+
return this;
279+
}
280+
269281
/**
270282
* Set host accessibility on ensemble containers.
271283
* @param hostAccessible Host accessibility flag
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.microcks.testcontainers.connection;
17+
18+
/**
19+
* A simple bean representing a Google Pub/Sub connection settings.
20+
* @author laurent
21+
*/
22+
public class GooglePubSubConnection {
23+
24+
private final String projectId;
25+
private final String emulatorHost;
26+
27+
/**
28+
* Create a GooglePubSubConnection.
29+
* @param projectId The GCP Project ID
30+
* @param emulatorHost The Pub/Sub emulator host (including port)
31+
*/
32+
public GooglePubSubConnection(String projectId, String emulatorHost) {
33+
this.projectId = projectId;
34+
this.emulatorHost = emulatorHost;
35+
}
36+
37+
public String getProjectId() {
38+
return projectId;
39+
}
40+
41+
public String getEmulatorHost() {
42+
return emulatorHost;
43+
}
44+
}

0 commit comments

Comments
 (0)