-
Notifications
You must be signed in to change notification settings - Fork 3
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 #314 from entur/improve/testcontainers-for-pubsub-…
…emulator Improve/testcontainers for pubsub emulator
- Loading branch information
Showing
14 changed files
with
216 additions
and
111 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
129 changes: 129 additions & 0 deletions
129
...t-test/java/no/entur/uttu/ext/entur/export/messaging/EnturPubSubMessagingServiceTest.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,129 @@ | ||
/* | ||
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by | ||
* the European Commission - subsequent versions of the EUPL (the "Licence"); | ||
* You may not use this work except in compliance with the Licence. | ||
* You may obtain a copy of the Licence at: | ||
* | ||
* https://joinup.ec.europa.eu/software/page/eupl | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the Licence is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Licence for the specific language governing permissions and | ||
* limitations under the Licence. | ||
* | ||
*/ | ||
|
||
package no.entur.uttu.ext.entur.export.messaging; | ||
|
||
import com.google.api.gax.core.CredentialsProvider; | ||
import com.google.api.gax.core.NoCredentialsProvider; | ||
import com.google.cloud.spring.pubsub.core.PubSubTemplate; | ||
import com.google.cloud.spring.pubsub.core.subscriber.PubSubSubscriberTemplate; | ||
import com.google.pubsub.v1.PubsubMessage; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import no.entur.uttu.UttuIntegrationTest; | ||
import no.entur.uttu.export.messaging.spi.MessagingService; | ||
import org.entur.pubsub.base.EnturGooglePubSubAdmin; | ||
import org.junit.*; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
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; | ||
|
||
@Testcontainers | ||
@ActiveProfiles({ "test", "entur-pubsub-messaging-service" }) | ||
public class EnturPubSubMessagingServiceTest extends UttuIntegrationTest { | ||
|
||
public static final String TEST_CODESPACE = "rut"; | ||
|
||
public static final String TEST_EXPORT_FILE_NAME = "netex.zip"; | ||
private static PubSubEmulatorContainer pubsubEmulator; | ||
|
||
@Autowired | ||
private MessagingService messagingService; | ||
|
||
@Autowired | ||
private PubSubTemplate pubSubTemplate; | ||
|
||
@Autowired | ||
PubSubSubscriberTemplate subscriberTemplate; | ||
|
||
@Autowired | ||
private EnturGooglePubSubAdmin enturGooglePubSubAdmin; | ||
|
||
@Value("${export.notify.queue.name:FlexibleLinesExportQueue}") | ||
private String queueName; | ||
|
||
@DynamicPropertySource | ||
static void emulatorProperties(DynamicPropertyRegistry registry) { | ||
registry.add( | ||
"spring.cloud.gcp.pubsub.emulator-host", | ||
pubsubEmulator::getEmulatorEndpoint | ||
); | ||
registry.add("spring.cloud.gcp.pubsub.enabled", () -> true); | ||
registry.add("spring.cloud.gcp.project-id", () -> "uttu-gcp-test-project"); | ||
registry.add("spring.cloud.gcp.pubsub.project-id", () -> "uttu-gcp-test-project"); | ||
registry.add("export.notify.enabled", () -> true); | ||
registry.add("export.notify.queue.name", () -> "FlexibleLinesExportQueue"); | ||
} | ||
|
||
@BeforeClass | ||
public static void init() { | ||
pubsubEmulator = | ||
new PubSubEmulatorContainer( | ||
DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:emulators") | ||
); | ||
pubsubEmulator.start(); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() { | ||
pubsubEmulator.stop(); | ||
} | ||
|
||
@Before | ||
public void setup() { | ||
enturGooglePubSubAdmin.createSubscriptionIfMissing(queueName); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
enturGooglePubSubAdmin.deleteAllSubscriptions(); | ||
} | ||
|
||
// By default, autoconfiguration will initialize application default credentials. | ||
// For testing purposes, don't use any credentials. Bootstrap w/ NoCredentialsProvider. | ||
@TestConfiguration | ||
static class PubSubEmulatorConfiguration { | ||
|
||
@Bean | ||
CredentialsProvider googleCredentials() { | ||
return NoCredentialsProvider.create(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNotifyExport() { | ||
messagingService.notifyExport(TEST_CODESPACE, TEST_EXPORT_FILE_NAME); | ||
|
||
List<PubsubMessage> messages = pubSubTemplate.pullAndAck(queueName, 1, false); | ||
Assert.assertEquals(1, messages.size()); | ||
PubsubMessage pubsubMessage = messages.get(0); | ||
String codespace = pubsubMessage | ||
.getAttributesMap() | ||
.get(EnturPubSubMessagingService.HEADER_CHOUETTE_REFERENTIAL); | ||
Assert.assertEquals("rb_" + TEST_CODESPACE, codespace); | ||
Assert.assertEquals( | ||
TEST_EXPORT_FILE_NAME, | ||
pubsubMessage.getData().toString(StandardCharsets.UTF_8) | ||
); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/ext/java/no/entur/uttu/ext/entur/config/EnturConfiguration.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,11 @@ | ||
package no.entur.uttu.ext.entur.config; | ||
|
||
import org.entur.pubsub.base.config.GooglePubSubConfig; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Configuration | ||
@Profile("entur-pubsub-messaging-service") | ||
@Import(GooglePubSubConfig.class) | ||
public class EnturConfiguration {} |
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
5 changes: 0 additions & 5 deletions
5
src/main/java/no/entur/uttu/export/messaging/MessagingService.java
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
src/main/java/no/entur/uttu/export/messaging/NoopMessagingService.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,21 @@ | ||
package no.entur.uttu.export.messaging; | ||
|
||
import no.entur.uttu.export.messaging.spi.MessagingService; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* The default implementation is a noop operation | ||
*/ | ||
@Component | ||
@ConditionalOnMissingBean( | ||
value = MessagingService.class, | ||
ignored = NoopMessagingService.class | ||
) | ||
public class NoopMessagingService implements MessagingService { | ||
|
||
@Override | ||
public void notifyExport(String codespace, String filename) { | ||
// intentionally empty | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/no/entur/uttu/export/messaging/spi/MessagingService.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,13 @@ | ||
package no.entur.uttu.export.messaging.spi; | ||
|
||
/** | ||
* Represents a service which can send a notification to an external system about an export | ||
*/ | ||
public interface MessagingService { | ||
/** | ||
* Notify about export | ||
* @param codespace The codespace of the provider | ||
* @param filename The filename for this export | ||
*/ | ||
void notifyExport(String codespace, String filename); | ||
} |
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
66 changes: 0 additions & 66 deletions
66
src/test/java/no/entur/uttu/export/messaging/MessagingServiceTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.