Skip to content

Commit 0cec586

Browse files
authored
feat: Adding basic HTTPEndpoint configuration support in testcontainers module (#1210)
* feat: Adding basic HTTPEndpoint configuration support in testcontainers module Signed-off-by: Laurent Broudoux <[email protected]> * feat: #1209 Adding test for HTTPEndpoint in testcontainers module Signed-off-by: Laurent Broudoux <[email protected]> --------- Signed-off-by: Laurent Broudoux <[email protected]>
1 parent 58d6218 commit 0cec586

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprContainer.java

+21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import io.dapr.testcontainers.converter.ComponentYamlConverter;
1717
import io.dapr.testcontainers.converter.ConfigurationYamlConverter;
18+
import io.dapr.testcontainers.converter.HttpEndpointYamlConverter;
1819
import io.dapr.testcontainers.converter.SubscriptionYamlConverter;
1920
import io.dapr.testcontainers.converter.YamlConverter;
2021
import io.dapr.testcontainers.converter.YamlMapperFactory;
@@ -48,6 +49,7 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
4849
private static final Yaml YAML_MAPPER = YamlMapperFactory.create();
4950
private static final YamlConverter<Component> COMPONENT_CONVERTER = new ComponentYamlConverter(YAML_MAPPER);
5051
private static final YamlConverter<Subscription> SUBSCRIPTION_CONVERTER = new SubscriptionYamlConverter(YAML_MAPPER);
52+
private static final YamlConverter<HttpEndpoint> HTTPENDPOINT_CONVERTER = new HttpEndpointYamlConverter(YAML_MAPPER);
5153
private static final YamlConverter<Configuration> CONFIGURATION_CONVERTER = new ConfigurationYamlConverter(
5254
YAML_MAPPER);
5355
private static final WaitStrategy WAIT_STRATEGY = Wait.forHttp("/v1.0/healthz/outbound")
@@ -56,6 +58,7 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
5658

5759
private final Set<Component> components = new HashSet<>();
5860
private final Set<Subscription> subscriptions = new HashSet<>();
61+
private final Set<HttpEndpoint> httpEndpoints = new HashSet<>();
5962
private DaprLogLevel daprLogLevel = DaprLogLevel.INFO;
6063
private String appChannelAddress = "localhost";
6164
private String placementService = "placement";
@@ -99,6 +102,10 @@ public Set<Subscription> getSubscriptions() {
99102
return subscriptions;
100103
}
101104

105+
public Set<HttpEndpoint> getHttpEndpoints() {
106+
return httpEndpoints;
107+
}
108+
102109
public DaprContainer withAppPort(Integer port) {
103110
this.appPort = port;
104111
return this;
@@ -134,6 +141,11 @@ public DaprContainer withSubscription(Subscription subscription) {
134141
return this;
135142
}
136143

144+
public DaprContainer withHttpEndpoint(HttpEndpoint httpEndpoint) {
145+
httpEndpoints.add(httpEndpoint);
146+
return this;
147+
}
148+
137149
public DaprContainer withPlacementImage(String placementDockerImageName) {
138150
this.placementDockerImageName = placementDockerImageName;
139151
return this;
@@ -291,6 +303,15 @@ protected void configure() {
291303
withCopyToContainer(Transferable.of(subscriptionYaml), "/dapr-resources/" + subscription.getName() + ".yaml");
292304
}
293305

306+
for (HttpEndpoint endpoint : httpEndpoints) {
307+
String endpointYaml = HTTPENDPOINT_CONVERTER.convert(endpoint);
308+
309+
LOGGER.info("> HTTPEndpoint YAML: \n");
310+
LOGGER.info("\t\n" + endpointYaml + "\n");
311+
312+
withCopyToContainer(Transferable.of(endpointYaml), "/dapr-resources/" + endpoint.getName() + ".yaml");
313+
}
314+
294315
dependsOn(placementContainer);
295316
}
296317

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.dapr.testcontainers;
2+
3+
public class HttpEndpoint {
4+
private String name;
5+
private String baseUrl;
6+
7+
public HttpEndpoint(String name, String baseUrl) {
8+
this.name = name;
9+
this.baseUrl = baseUrl;
10+
}
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public String getBaseUrl() {
17+
return baseUrl;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.dapr.testcontainers.converter;
2+
3+
import io.dapr.testcontainers.HttpEndpoint;
4+
import org.yaml.snakeyaml.Yaml;
5+
6+
import java.util.LinkedHashMap;
7+
import java.util.Map;
8+
9+
public class HttpEndpointYamlConverter implements YamlConverter<HttpEndpoint> {
10+
private final Yaml mapper;
11+
12+
public HttpEndpointYamlConverter(Yaml mapper) {
13+
this.mapper = mapper;
14+
}
15+
16+
@Override
17+
public String convert(HttpEndpoint endpoint) {
18+
Map<String, Object> endpointProps = new LinkedHashMap<>();
19+
endpointProps.put("apiVersion", "dapr.io/v1alpha1");
20+
endpointProps.put("kind", "HTTPEndpoint");
21+
22+
Map<String, String> endpointMetadata = new LinkedHashMap<>();
23+
endpointMetadata.put("name", endpoint.getName());
24+
endpointProps.put("metadata", endpointMetadata);
25+
26+
Map<String, Object> endpointSpec = new LinkedHashMap<>();
27+
endpointSpec.put("baseUrl", endpoint.getBaseUrl());
28+
endpointProps.put("spec", endpointSpec);
29+
30+
return mapper.dumpAsMap(endpointProps);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.dapr.testcontainers.converter;
2+
3+
import io.dapr.testcontainers.DaprContainer;
4+
import io.dapr.testcontainers.HttpEndpoint;
5+
import org.junit.jupiter.api.Test;
6+
import org.yaml.snakeyaml.Yaml;
7+
8+
import java.util.Set;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
class HttpEndpointYamlConverterTest {
13+
private final Yaml MAPPER = YamlMapperFactory.create();
14+
15+
private final HttpEndpointYamlConverter converter = new HttpEndpointYamlConverter(MAPPER);
16+
17+
@Test
18+
void testHttpEndpointToYaml() {
19+
DaprContainer dapr = new DaprContainer("daprio/daprd")
20+
.withAppName("dapr-app")
21+
.withAppPort(8081)
22+
.withHttpEndpoint(new HttpEndpoint("my-endpoint", "http://localhost:8080"))
23+
.withAppChannelAddress("host.testcontainers.internal");
24+
25+
Set<HttpEndpoint> endpoints = dapr.getHttpEndpoints();
26+
assertEquals(1, endpoints.size());
27+
28+
HttpEndpoint endpoint = endpoints.iterator().next();
29+
String endpointYaml = converter.convert(endpoint);
30+
String expectedEndpointYaml =
31+
"apiVersion: dapr.io/v1alpha1\n"
32+
+ "kind: HTTPEndpoint\n"
33+
+ "metadata:\n"
34+
+ " name: my-endpoint\n"
35+
+ "spec:\n"
36+
+ " baseUrl: http://localhost:8080\n";
37+
38+
assertEquals(expectedEndpointYaml, endpointYaml);
39+
}
40+
}

0 commit comments

Comments
 (0)