|
| 1 | +package com.playtika.testcontainers.wiremock; |
| 2 | + |
| 3 | +import com.playtika.testcontainer.common.spring.DockerPresenceBootstrapConfiguration; |
| 4 | +import com.playtika.testcontainer.common.utils.ContainerUtils; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 7 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
| 8 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 9 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.core.annotation.Order; |
| 13 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 14 | +import org.springframework.core.env.MapPropertySource; |
| 15 | +import org.testcontainers.containers.GenericContainer; |
| 16 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 17 | +import org.testcontainers.containers.wait.strategy.WaitStrategy; |
| 18 | + |
| 19 | +import java.util.LinkedHashMap; |
| 20 | + |
| 21 | +import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart; |
| 22 | +import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +@Order(HIGHEST_PRECEDENCE) |
| 26 | +@Configuration |
| 27 | +@ConditionalOnExpression("${embedded.containers.enabled:true}") |
| 28 | +@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class) |
| 29 | +@ConditionalOnProperty(name = "embedded.wiremock.enabled", havingValue = "true", matchIfMissing = true) |
| 30 | +@EnableConfigurationProperties(WiremockProperties.class) |
| 31 | +public class EmbeddedWiremockBootstrapConfiguration { |
| 32 | + |
| 33 | + static final String BEAN_NAME_EMBEDDED_WIREMOCK = "embeddedWiremock"; |
| 34 | + private static final String WIREMOCK_NETWORK_ALIAS = "wiremock.testcontainer.docker"; |
| 35 | + private static final WaitStrategy DEFAULT_WAITER = Wait.forHttp("/__admin/mappings") |
| 36 | + .withMethod("GET") |
| 37 | + .forStatusCode(200); |
| 38 | + |
| 39 | + @Bean(value = BEAN_NAME_EMBEDDED_WIREMOCK, destroyMethod = "stop") |
| 40 | + public GenericContainer<?> wiremockContainer(ConfigurableEnvironment environment, |
| 41 | + WiremockProperties properties) { |
| 42 | + GenericContainer<?> wiremock = |
| 43 | + new GenericContainer<>(ContainerUtils.getDockerImageName(properties)) |
| 44 | + .waitingFor(DEFAULT_WAITER) |
| 45 | + .withCommand("--port " + properties.getPort()) |
| 46 | + .withExposedPorts(properties.getPort()) |
| 47 | + .withNetworkAliases(WIREMOCK_NETWORK_ALIAS); |
| 48 | + |
| 49 | + wiremock = configureCommonsAndStart(wiremock, properties, log); |
| 50 | + registerWiremockEnvironment(wiremock, environment, properties); |
| 51 | + return wiremock; |
| 52 | + } |
| 53 | + |
| 54 | + private void registerWiremockEnvironment(GenericContainer<?> container, ConfigurableEnvironment environment, WiremockProperties properties) { |
| 55 | + Integer mappedPort = container.getMappedPort(properties.getPort()); |
| 56 | + String host = container.getHost(); |
| 57 | + |
| 58 | + LinkedHashMap<String, Object> map = new LinkedHashMap<>(); |
| 59 | + map.put("embedded.wiremock.port", mappedPort); |
| 60 | + map.put("embedded.wiremock.host", host); |
| 61 | + map.put("embedded.wiremock.networkAlias", WIREMOCK_NETWORK_ALIAS); |
| 62 | + map.put("embedded.wiremock.internalPort", properties.getPort()); |
| 63 | + |
| 64 | + log.info("Started wiremock. Connection Details: {}", map); |
| 65 | + |
| 66 | + MapPropertySource propertySource = new MapPropertySource("embeddedWiremockInfo", map); |
| 67 | + environment.getPropertySources().addFirst(propertySource); |
| 68 | + } |
| 69 | +} |
0 commit comments