Skip to content

Configure Test Containers PubSub emulator #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Copy maven settings
run: |
wget https://raw.githubusercontent.com/entur/ror-maven-settings/master/.m2/settings.xml -O .github/workflows/settings.xml
- uses: actions/setup-java@v4
with:
java-version: 17
Expand All @@ -35,7 +32,7 @@ jobs:
${{ runner.os }}-maven-
${{ runner.os }}-
- name: Run maven build
run: mvn verify -s .github/workflows/settings.xml -PprettierCheck -Dprettier.nodePath=node -Dprettier.npmPath=npm
run: mvn verify -PprettierCheck -Dprettier.nodePath=node -Dprettier.npmPath=npm
- uses: actions/upload-artifact@v4.6.0
with:
path: target/*-SNAPSHOT.jar
Expand All @@ -45,8 +42,7 @@ jobs:
SONAR_PROJECT_NAME: ${{ github.event.repository.name }}
SONAR_PROJECT_KEY: entur_${{ github.event.repository.name }}
run: |
mvn -s .github/workflows/settings.xml \
org.jacoco:jacoco-maven-plugin:prepare-agent verify \
mvn org.jacoco:jacoco-maven-plugin:prepare-agent verify \
org.jacoco:jacoco-maven-plugin:report sonar:sonar \
-P prettierSkip \
-Dmaven.main.skip \
Expand Down
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</scm>
<properties>
<java.version>17</java.version>
<entur.google.pubsub.emulator.download.skip>false</entur.google.pubsub.emulator.download.skip>
<entur.google.pubsub.emulator.download.skip>true</entur.google.pubsub.emulator.download.skip>
<camel.version>4.4.5</camel.version>
<entur.helpers.version>4.9</entur.helpers.version>
<netex-validator-java.version>9.1.3</netex-validator-java.version>
Expand Down Expand Up @@ -238,6 +238,15 @@
</dependency>

<!-- testing -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>gcloud</artifactId>
</dependency>
<dependency>
<groupId>org.entur</groupId>
<artifactId>netex-validator-java</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,35 @@
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.apache.camel.test.spring.junit5.UseAdviceWith;
import org.entur.pubsub.base.EnturGooglePubSubAdmin;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.rutebanken.helper.storage.repository.InMemoryBlobStoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PubSubEmulatorContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

@CamelSpringBootTest
@UseAdviceWith
@ActiveProfiles(
{
"test",
"default",
"in-memory-blobstore",
"google-pubsub-emulator",
"google-pubsub-autocreate",
}
{ "test", "default", "in-memory-blobstore", "google-pubsub-autocreate" }
)
@Testcontainers
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public abstract class AntuRouteBuilderIntegrationTestBase {

private static PubSubEmulatorContainer pubsubEmulator;

@Autowired
private EnturGooglePubSubAdmin enturGooglePubSubAdmin;

@Value("${blobstore.gcs.antu.exchange.container.name}")
private String antuExchangeContainerName;

Expand Down Expand Up @@ -89,8 +97,41 @@ void initInMemoryBlobStoreRepositories() {
antuInMemoryBlobStoreRepository.setContainerName(antuContainerName);
}

@BeforeAll
public static void init() {
pubsubEmulator =
new PubSubEmulatorContainer(
DockerImageName.parse(
"gcr.io/google.com/cloudsdktool/cloud-sdk:emulators"
)
);
pubsubEmulator.start();
}

@AfterAll
public static void tearDown() {
pubsubEmulator.stop();
}

@AfterEach
public void teardown() {
enturGooglePubSubAdmin.deleteAllSubscriptions();
}

@AfterEach
void stopContext() {
context.stop();
}

@DynamicPropertySource
static void emulatorProperties(DynamicPropertyRegistry registry) {
registry.add(
"spring.cloud.gcp.pubsub.emulator-host",
pubsubEmulator::getEmulatorEndpoint
);
registry.add(
"camel.component.google-pubsub.endpoint",
pubsubEmulator::getEmulatorEndpoint
);
}
}
3 changes: 1 addition & 2 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ blobstore.gcs.project.id=test
# PubSub
antu.pubsub.project.id=test
logging.level.io.grpc.internal.ManagedChannelOrphanWrapper=OFF
camel.component.google-pubsub.endpoint=localhost:8095

# Spring Cloud GCP is used in unit tests only for spawning a PubSub emulator and creating Topics and Subscriptions
spring.cloud.gcp.project-id=${antu.pubsub.project.id}
spring.cloud.gcp.pubsub.emulatorHost=localhost:8095
camel.component.google-pubsub.authenticate=false
camel.component.google-pubsub.synchronous-pull-retryable-codes=DEADLINE_EXCEEDED

Expand Down