Skip to content

Commit 0d77c92

Browse files
author
admitrov
committed
fix: refactor dynamic property registration and configuration for embedded services
1 parent 1ee2c5c commit 0d77c92

File tree

27 files changed

+453
-464
lines changed

27 files changed

+453
-464
lines changed

embedded-azurite/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
<groupId>com.playtika.testcontainers</groupId>
3333
<artifactId>embedded-toxiproxy</artifactId>
3434
</dependency>
35-
<dependency>
36-
<groupId>org.springframework</groupId>
37-
<artifactId>spring-test</artifactId>
38-
</dependency>
3935
</dependencies>
4036

4137
</project>

embedded-azurite/src/main/java/com/playtika/testcontainer/azurite/EmbeddedAzuriteBootstrapConfiguration.java

Lines changed: 81 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1212
import org.springframework.context.annotation.Bean;
1313
import org.springframework.context.annotation.Configuration;
14-
import org.springframework.test.context.DynamicPropertyRegistrar;
14+
import org.springframework.core.env.ConfigurableEnvironment;
15+
import org.springframework.core.env.MapPropertySource;
1516
import org.testcontainers.containers.GenericContainer;
1617
import org.testcontainers.containers.Network;
1718
import org.testcontainers.containers.ToxiproxyContainer;
1819

20+
import java.util.LinkedHashMap;
1921
import java.util.Map;
2022
import java.util.Optional;
2123

@@ -34,65 +36,68 @@ public class EmbeddedAzuriteBootstrapConfiguration {
3436

3537
@Bean
3638
@ConditionalOnToxiProxyEnabled(module = "azurite")
37-
ToxiproxyContainer.ContainerProxy azuriteQueueContainerProxy(ToxiproxyContainer toxiproxyContainer,
38-
@Qualifier(AZURITE_BEAN_NAME) GenericContainer<?> azurite,
39-
AzuriteProperties properties) {
40-
return toxiproxyContainer.getProxy(azurite, properties.getQueueStoragePort());
41-
}
39+
ToxiproxyContainer.ContainerProxy azuriteBlobContainerProxy(ToxiproxyContainer toxiproxyContainer,
40+
@Qualifier(AZURITE_BEAN_NAME) GenericContainer<?> azurite,
41+
AzuriteProperties properties,
42+
ConfigurableEnvironment environment) {
43+
ToxiproxyContainer.ContainerProxy proxy = toxiproxyContainer.getProxy(azurite, properties.getBlobStoragePort());
4244

43-
@Bean
44-
@ConditionalOnToxiProxyEnabled(module = "azurite")
45-
public DynamicPropertyRegistrar azuriteBlobToxiProxyDynamicPropertyRegistrar(ToxiproxyContainer.ContainerProxy proxy) {
46-
return registry -> {
47-
registry.add("embedded.azurite.toxiproxy.host", proxy::getContainerIpAddress);
48-
registry.add("embedded.azurite.toxiproxy.blobStoragePort", proxy::getProxyPort);
49-
registry.add("embedded.azurite.toxiproxy.proxyName", proxy::getName);
50-
log.info("Started Azurite ToxiProxy connection details {}", Map.of(
51-
"embedded.azurite.toxiproxy.host", proxy.getContainerIpAddress(),
52-
"embedded.azurite.toxiproxy.blobStoragePort", proxy.getProxyPort(),
53-
"embedded.azurite.toxiproxy.proxyName", proxy.getName()
54-
));
55-
};
45+
Map<String, Object> map = new LinkedHashMap<>();
46+
map.put("embedded.azurite.toxiproxy.host", proxy.getContainerIpAddress());
47+
map.put("embedded.azurite.toxiproxy.blobStoragePort", proxy.getProxyPort());
48+
map.put("embedded.azurite.toxiproxy.proxyName", proxy.getName());
49+
50+
MapPropertySource propertySource = new MapPropertySource("embeddedAzuriteBlobToxiproxyInfo", map);
51+
environment.getPropertySources().addFirst(propertySource);
52+
log.info("Started Azurite ToxiProxy connection details {}", map);
53+
54+
return proxy;
5655
}
5756

5857
@Bean
5958
@ConditionalOnToxiProxyEnabled(module = "azurite")
60-
public DynamicPropertyRegistrar azuriteQueueToxiProxyDynamicPropertyRegistrar(ToxiproxyContainer toxiproxyContainer,
61-
@Qualifier(AZURITE_BEAN_NAME) GenericContainer<?> azurite,
62-
AzuriteProperties properties) {
63-
return registry -> {
64-
ToxiproxyContainer.ContainerProxy proxy = toxiproxyContainer.getProxy(azurite, properties.getQueueStoragePort());
65-
registry.add("embedded.azurite.toxiproxy.host", proxy::getContainerIpAddress);
66-
registry.add("embedded.azurite.toxiproxy.queueStoragePor", proxy::getProxyPort);
67-
registry.add("embedded.azurite.toxiproxy.proxyName", proxy::getName);
68-
log.info("Started Azurite ToxiProxy connection details {}", Map.of(
69-
"embedded.azurite.toxiproxy.host", proxy.getContainerIpAddress(),
70-
"embedded.azurite.toxiproxy.queueStoragePor", proxy.getProxyPort(),
71-
"embedded.azurite.toxiproxy.proxyName", proxy.getName()
72-
));
73-
};
59+
ToxiproxyContainer.ContainerProxy azuriteQueueContainerProxy(ToxiproxyContainer toxiproxyContainer,
60+
@Qualifier(AZURITE_BEAN_NAME) GenericContainer<?> azurite,
61+
AzuriteProperties properties,
62+
ConfigurableEnvironment environment) {
63+
ToxiproxyContainer.ContainerProxy proxy = toxiproxyContainer.getProxy(azurite, properties.getQueueStoragePort());
64+
65+
Map<String, Object> map = new LinkedHashMap<>();
66+
map.put("embedded.azurite.toxiproxy.host", proxy.getContainerIpAddress());
67+
map.put("embedded.azurite.toxiproxy.queueStoragePor", proxy.getProxyPort());
68+
map.put("embedded.azurite.toxiproxy.proxyName", proxy.getName());
69+
70+
MapPropertySource propertySource = new MapPropertySource("embeddedAzuriteQueueToxiproxyInfo", map);
71+
environment.getPropertySources().addFirst(propertySource);
72+
log.info("Started Azurite ToxiProxy connection details {}", map);
73+
74+
return proxy;
7475
}
7576

7677
@Bean
7778
@ConditionalOnToxiProxyEnabled(module = "azurite")
78-
public DynamicPropertyRegistrar azuriteTableToxiProxyDynamicPropertyRegistrar(ToxiproxyContainer toxiproxyContainer,
79-
@Qualifier(AZURITE_BEAN_NAME) GenericContainer<?> azurite,
80-
AzuriteProperties properties) {
81-
return registry -> {
82-
ToxiproxyContainer.ContainerProxy proxy = toxiproxyContainer.getProxy(azurite, properties.getTableStoragePort());
83-
registry.add("embedded.azurite.toxiproxy.host", proxy::getContainerIpAddress);
84-
registry.add("embedded.azurite.toxiproxy.tableStoragePort", proxy::getProxyPort);
85-
registry.add("embedded.azurite.toxiproxy.proxyName", proxy::getName);
86-
log.info("Started Azurite ToxiProxy connection details {}", Map.of(
87-
"embedded.azurite.toxiproxy.host", proxy.getContainerIpAddress(),
88-
"embedded.azurite.toxiproxy.tableStoragePort", proxy.getProxyPort(),
89-
"embedded.azurite.toxiproxy.proxyName", proxy.getName()
90-
));
91-
};
79+
ToxiproxyContainer.ContainerProxy azuriteTableContainerProxy(ToxiproxyContainer toxiproxyContainer,
80+
@Qualifier(AZURITE_BEAN_NAME) GenericContainer<?> azurite,
81+
AzuriteProperties properties,
82+
ConfigurableEnvironment environment) {
83+
ToxiproxyContainer.ContainerProxy proxy = toxiproxyContainer.getProxy(azurite, properties.getTableStoragePort());
84+
85+
Map<String, Object> map = new LinkedHashMap<>();
86+
map.put("embedded.azurite.toxiproxy.host", proxy.getContainerIpAddress());
87+
map.put("embedded.azurite.toxiproxy.tableStoragePort", proxy.getProxyPort());
88+
map.put("embedded.azurite.toxiproxy.proxyName", proxy.getName());
89+
90+
MapPropertySource propertySource = new MapPropertySource("embeddedAzuriteTableToxiproxyInfo", map);
91+
environment.getPropertySources().addFirst(propertySource);
92+
log.info("Started Azurite ToxiProxy connection details {}", map);
93+
94+
return proxy;
9295
}
9396

9497
@Bean(name = AZURITE_BEAN_NAME, destroyMethod = "stop")
95-
public GenericContainer<?> azurite(AzuriteProperties properties, Optional<Network> network) {
98+
public GenericContainer<?> azurite(ConfigurableEnvironment environment,
99+
AzuriteProperties properties,
100+
Optional<Network> network) {
96101
GenericContainer<?> azuriteContainer = new GenericContainer<>(ContainerUtils.getDockerImageName(properties))
97102
.withExposedPorts(properties.getBlobStoragePort(), properties.getQueueStoragePort(), properties.getTableStoragePort())
98103
.withNetworkAliases(AZURITE_BLOB_NETWORK_ALIAS)
@@ -105,41 +110,39 @@ public GenericContainer<?> azurite(AzuriteProperties properties, Optional<Networ
105110
"--tableHost", "0.0.0.0",
106111
"--tablePort", String.valueOf(properties.getTableStoragePort()),
107112
"--skipApiVersionCheck");
113+
108114
network.ifPresent(azuriteContainer::withNetwork);
115+
109116
configureCommonsAndStart(azuriteContainer, properties, log);
117+
registerEnvironment(azuriteContainer, environment, properties);
110118
return azuriteContainer;
111119
}
112120

113-
@Bean
114-
public DynamicPropertyRegistrar azuriteDynamicPropertyRegistrar(@Qualifier(AZURITE_BEAN_NAME) GenericContainer<?> azurite, AzuriteProperties properties) {
115-
return registry -> {
116-
Integer mappedBlobStoragePort = azurite.getMappedPort(properties.getBlobStoragePort());
117-
Integer mappedQueueStoragePort = azurite.getMappedPort(properties.getQueueStoragePort());
118-
Integer mappedTableStoragePort = azurite.getMappedPort(properties.getTableStoragePort());
119-
String host = azurite.getHost();
120-
registry.add("embedded.azurite.host", () -> host);
121-
registry.add("embedded.azurite.blobStoragePort", () -> mappedBlobStoragePort);
122-
registry.add("embedded.azurite.queueStoragePor", () -> mappedQueueStoragePort);
123-
registry.add("embedded.azurite.tableStoragePort", () -> mappedTableStoragePort);
124-
registry.add("embedded.azurite.account-name", () -> AzuriteProperties.ACCOUNT_NAME);
125-
registry.add("embedded.azurite.account-key", () -> AzuriteProperties.ACCOUNT_KEY);
126-
registry.add("embedded.azurite.blob-endpoint", () -> "http://" + host + ":" + mappedBlobStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME);
127-
registry.add("embedded.azurite.queue-endpoint", () -> "http://" + host + ":" + mappedQueueStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME);
128-
registry.add("embedded.azurite.table-endpoint", () -> "http://" + host + ":" + mappedTableStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME);
129-
registry.add("embedded.azurite.networkAlias", () -> AZURITE_BLOB_NETWORK_ALIAS);
130-
log.info("Started Azurite. Connection details: {}", Map.of(
131-
"embedded.azurite.host", host,
132-
"embedded.azurite.blobStoragePort", mappedBlobStoragePort,
133-
"embedded.azurite.queueStoragePor", mappedQueueStoragePort,
134-
"embedded.azurite.tableStoragePort", mappedTableStoragePort,
135-
"embedded.azurite.account-name", AzuriteProperties.ACCOUNT_NAME,
136-
"embedded.azurite.account-key", AzuriteProperties.ACCOUNT_KEY,
137-
"embedded.azurite.blob-endpoint", "http://" + host + ":" + mappedBlobStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME,
138-
"embedded.azurite.queue-endpoint", "http://" + host + ":" + mappedQueueStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME,
139-
"embedded.azurite.table-endpoint", "http://" + host + ":" + mappedTableStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME,
140-
"embedded.azurite.networkAlias", AZURITE_BLOB_NETWORK_ALIAS
141-
));
142-
};
121+
private void registerEnvironment(GenericContainer<?> azurite,
122+
ConfigurableEnvironment environment,
123+
AzuriteProperties properties) {
124+
125+
Integer mappedBlobStoragePort = azurite.getMappedPort(properties.getBlobStoragePort());
126+
Integer mappedQueueStoragePort = azurite.getMappedPort(properties.getQueueStoragePort());
127+
Integer mappedTableStoragePort = azurite.getMappedPort(properties.getTableStoragePort());
128+
String host = azurite.getHost();
129+
130+
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
131+
map.put("embedded.azurite.host", host);
132+
map.put("embedded.azurite.blobStoragePort", mappedBlobStoragePort);
133+
map.put("embedded.azurite.queueStoragePor", mappedQueueStoragePort);
134+
map.put("embedded.azurite.tableStoragePort", mappedTableStoragePort);
135+
map.put("embedded.azurite.account-name", AzuriteProperties.ACCOUNT_NAME);
136+
map.put("embedded.azurite.account-key", AzuriteProperties.ACCOUNT_KEY);
137+
map.put("embedded.azurite.blob-endpoint", "http://" + host + ":" + mappedBlobStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME);
138+
map.put("embedded.azurite.queue-endpoint", "http://" + host + ":" + mappedQueueStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME);
139+
map.put("embedded.azurite.table-endpoint", "http://" + host + ":" + mappedTableStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME);
140+
map.put("embedded.azurite.networkAlias", AZURITE_BLOB_NETWORK_ALIAS);
141+
142+
log.info("Started Azurite. Connection details: {}", map);
143+
144+
MapPropertySource propertySource = new MapPropertySource("embeddedAzuriteInfo", map);
145+
environment.getPropertySources().addFirst(propertySource);
143146
}
144147

145148
}

embedded-couchbase/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,5 @@
5757
<artifactId>awaitility</artifactId>
5858
<scope>test</scope>
5959
</dependency>
60-
<dependency>
61-
<groupId>org.springframework</groupId>
62-
<artifactId>spring-test</artifactId>
63-
</dependency>
6460
</dependencies>
6561
</project>

0 commit comments

Comments
 (0)