diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/Configuration.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/Configuration.java new file mode 100644 index 000000000..e884551ee --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/Configuration.java @@ -0,0 +1,45 @@ +/* + * Copyright 2024 The Dapr Authors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and +limitations under the License. +*/ + +package io.dapr.testcontainers; + +/** + * Represents a Dapr component. + */ +public class Configuration { + private final String name; + private final TracingConfigurationSettings tracing; + + //@TODO: add httpPipeline + //@TODO: add secrets + //@TODO: add components + //@TODO: add accessControl + + /** + * Creates a new configuration. + * @param name Configuration name. + * @param tracing TracingConfigParameters tracing configuration parameters. + */ + public Configuration(String name, TracingConfigurationSettings tracing) { + this.name = name; + this.tracing = tracing; + } + + public String getName() { + return name; + } + + public TracingConfigurationSettings getTracing() { + return tracing; + } +} diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/ConfigurationSettings.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/ConfigurationSettings.java new file mode 100644 index 000000000..d9cf29224 --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/ConfigurationSettings.java @@ -0,0 +1,6 @@ +package io.dapr.testcontainers; + +// This is a marker interface, so we could get +// a list of all the configuration settings implementations +public interface ConfigurationSettings { +} diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprContainer.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprContainer.java index 4ddbd7d6b..d96cc2552 100644 --- a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprContainer.java +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprContainer.java @@ -13,49 +13,54 @@ package io.dapr.testcontainers; +import io.dapr.testcontainers.converter.ComponentYamlConverter; +import io.dapr.testcontainers.converter.ConfigurationYamlConverter; +import io.dapr.testcontainers.converter.SubscriptionYamlConverter; +import io.dapr.testcontainers.converter.YamlConverter; +import io.dapr.testcontainers.converter.YamlMapperFactory; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.Network; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.WaitStrategy; import org.testcontainers.images.builder.Transferable; import org.testcontainers.utility.DockerImageName; -import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.nodes.Tag; -import org.yaml.snakeyaml.representer.Representer; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; public class DaprContainer extends GenericContainer { - private static final int DAPRD_DEFAULT_HTTP_PORT = 3500; private static final int DAPRD_DEFAULT_GRPC_PORT = 50001; + private static final DaprProtocol DAPR_PROTOCOL = DaprProtocol.HTTP; + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("daprio/daprd"); + private static final Yaml YAML_MAPPER = YamlMapperFactory.create(); + private static final YamlConverter COMPONENT_CONVERTER = new ComponentYamlConverter(YAML_MAPPER); + private static final YamlConverter SUBSCRIPTION_CONVERTER = new SubscriptionYamlConverter(YAML_MAPPER); + private static final YamlConverter CONFIGURATION_CONVERTER = new ConfigurationYamlConverter( + YAML_MAPPER); private static final WaitStrategy WAIT_STRATEGY = Wait.forHttp("/v1.0/healthz/outbound") .forPort(DAPRD_DEFAULT_HTTP_PORT) .forStatusCodeMatching(statusCode -> statusCode >= 200 && statusCode <= 399); private final Set components = new HashSet<>(); private final Set subscriptions = new HashSet<>(); - private DaprProtocol protocol = DaprProtocol.HTTP; - private String appName; - private Integer appPort = null; private DaprLogLevel daprLogLevel = DaprLogLevel.INFO; private String appChannelAddress = "localhost"; private String placementService = "placement"; - private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("daprio/daprd"); - private static final Yaml yaml = getYamlMapper(); - private DaprPlacementContainer placementContainer; private String placementDockerImageName = "daprio/placement"; + + private Configuration configuration; + private DaprPlacementContainer placementContainer; + private String appName; + private Integer appPort; private boolean shouldReusePlacement; /** @@ -78,6 +83,10 @@ public DaprContainer(String image) { this(DockerImageName.parse(image)); } + public Configuration getConfiguration() { + return configuration; + } + public Set getComponents() { return components; } @@ -91,6 +100,16 @@ public DaprContainer withAppPort(Integer port) { return this; } + public DaprContainer withAppChannelAddress(String appChannelAddress) { + this.appChannelAddress = appChannelAddress; + return this; + } + + public DaprContainer withConfiguration(Configuration configuration) { + this.configuration = configuration; + return this; + } + public DaprContainer withPlacementService(String placementService) { this.placementService = placementService; return this; @@ -111,6 +130,21 @@ public DaprContainer withSubscription(Subscription subscription) { return this; } + public DaprContainer withPlacementImage(String placementDockerImageName) { + this.placementDockerImageName = placementDockerImageName; + return this; + } + + public DaprContainer withReusablePlacement(boolean reuse) { + this.shouldReusePlacement = reuse; + return this; + } + + public DaprContainer withPlacementContainer(DaprPlacementContainer placementContainer) { + this.placementContainer = placementContainer; + return this; + } + public DaprContainer withComponent(Component component) { components.add(component); return this; @@ -123,7 +157,7 @@ public DaprContainer withComponent(Component component) { */ public DaprContainer withComponent(Path path) { try { - Map component = this.yaml.loadAs(Files.newInputStream(path), Map.class); + Map component = this.YAML_MAPPER.loadAs(Files.newInputStream(path), Map.class); String type = (String) component.get("type"); Map metadata = (Map) component.get("metadata"); @@ -165,60 +199,6 @@ public String getGrpcEndpoint() { return ":" + getMappedPort(DAPRD_DEFAULT_GRPC_PORT); } - public DaprContainer withAppChannelAddress(String appChannelAddress) { - this.appChannelAddress = appChannelAddress; - return this; - } - - /** - * Get a map of Dapr component details. - * @param component A Dapr Component. - * @return Map of component details. - */ - public Map componentToMap(Component component) { - Map componentProps = new HashMap<>(); - componentProps.put("apiVersion", "dapr.io/v1alpha1"); - componentProps.put("kind", "Component"); - - Map componentMetadata = new LinkedHashMap<>(); - componentMetadata.put("name", component.getName()); - componentProps.put("metadata", componentMetadata); - - Map componentSpec = new HashMap<>(); - componentSpec.put("type", component.getType()); - componentSpec.put("version", component.getVersion()); - - if (!component.getMetadata().isEmpty()) { - componentSpec.put("metadata", component.getMetadata()); - } - - componentProps.put("spec", componentSpec); - return Collections.unmodifiableMap(componentProps); - } - - /** - * Get a map of Dapr subscription details. - * @param subscription A Dapr Subscription. - * @return Map of subscription details. - */ - public Map subscriptionToMap(Subscription subscription) { - Map subscriptionProps = new HashMap<>(); - subscriptionProps.put("apiVersion", "dapr.io/v1alpha1"); - subscriptionProps.put("kind", "Subscription"); - - Map subscriptionMetadata = new LinkedHashMap<>(); - subscriptionMetadata.put("name", subscription.getName()); - subscriptionProps.put("metadata", subscriptionMetadata); - - Map subscriptionSpec = new HashMap<>(); - subscriptionSpec.put("pubsubname", subscription.getPubsubName()); - subscriptionSpec.put("topic", subscription.getTopic()); - subscriptionSpec.put("route", subscription.getRoute()); - - subscriptionProps.put("spec", subscriptionSpec); - return Collections.unmodifiableMap(subscriptionProps); - } - @Override protected void configure() { super.configure(); @@ -241,7 +221,7 @@ protected void configure() { cmds.add(appName); cmds.add("--dapr-listen-addresses=0.0.0.0"); cmds.add("--app-protocol"); - cmds.add(protocol.getName()); + cmds.add(DAPR_PROTOCOL.getName()); cmds.add("-placement-host-address"); cmds.add(placementService + ":50005"); @@ -257,10 +237,15 @@ protected void configure() { cmds.add("--log-level"); cmds.add(daprLogLevel.toString()); - cmds.add("-components-path"); + cmds.add("--resources-path"); cmds.add("/dapr-resources"); withCommand(cmds.toArray(new String[]{})); + if (configuration != null) { + String configurationYaml = CONFIGURATION_CONVERTER.convert(configuration); + withCopyToContainer(Transferable.of(configurationYaml), "/dapr-resources/" + configuration.getName() + ".yaml"); + } + if (components.isEmpty()) { components.add(new Component("kvstore", "state.in-memory", "v1", Collections.emptyMap())); components.add(new Component("pubsub", "pubsub.in-memory", "v1", Collections.emptyMap())); @@ -271,28 +256,18 @@ protected void configure() { } for (Component component : components) { - String componentYaml = componentToYaml(component); + String componentYaml = COMPONENT_CONVERTER.convert(component); withCopyToContainer(Transferable.of(componentYaml), "/dapr-resources/" + component.getName() + ".yaml"); } for (Subscription subscription : subscriptions) { - String subscriptionYaml = subscriptionToYaml(subscription); + String subscriptionYaml = SUBSCRIPTION_CONVERTER.convert(subscription); withCopyToContainer(Transferable.of(subscriptionYaml), "/dapr-resources/" + subscription.getName() + ".yaml"); } dependsOn(placementContainer); } - public String subscriptionToYaml(Subscription subscription) { - Map subscriptionMap = subscriptionToMap(subscription); - return yaml.dumpAsMap(subscriptionMap); - } - - public String componentToYaml(Component component) { - Map componentMap = componentToMap(component); - return yaml.dumpAsMap(componentMap); - } - public String getAppName() { return appName; } @@ -313,21 +288,6 @@ public static DockerImageName getDefaultImageName() { return DEFAULT_IMAGE_NAME; } - public DaprContainer withPlacementImage(String placementDockerImageName) { - this.placementDockerImageName = placementDockerImageName; - return this; - } - - public DaprContainer withReusablePlacement(boolean reuse) { - this.shouldReusePlacement = reuse; - return this; - } - - public DaprContainer withPlacementContainer(DaprPlacementContainer placementContainer) { - this.placementContainer = placementContainer; - return this; - } - // Required by spotbugs plugin @Override public boolean equals(Object o) { @@ -338,13 +298,4 @@ public boolean equals(Object o) { public int hashCode() { return super.hashCode(); } - - private static Yaml getYamlMapper() { - DumperOptions options = new DumperOptions(); - options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - options.setPrettyFlow(true); - Representer representer = new Representer(options); - representer.addClassTag(MetadataEntry.class, Tag.MAP); - return new Yaml(representer); - } } diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/OtelTracingConfigurationSettings.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/OtelTracingConfigurationSettings.java new file mode 100644 index 000000000..38d862c91 --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/OtelTracingConfigurationSettings.java @@ -0,0 +1,34 @@ +package io.dapr.testcontainers; + +/** + * Configuration settings for Otel tracing. + */ +public class OtelTracingConfigurationSettings implements ConfigurationSettings { + private final String endpointAddress; + private final Boolean isSecure; + private final String protocol; + + /** + * Creates a new configuration. + * @param endpointAddress tracing endpoint address + * @param isSecure if the endpoint is secure + * @param protocol tracing protocol + */ + public OtelTracingConfigurationSettings(String endpointAddress, Boolean isSecure, String protocol) { + this.endpointAddress = endpointAddress; + this.isSecure = isSecure; + this.protocol = protocol; + } + + public String getEndpointAddress() { + return endpointAddress; + } + + public Boolean getSecure() { + return isSecure; + } + + public String getProtocol() { + return protocol; + } +} diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/TracingConfigurationSettings.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/TracingConfigurationSettings.java new file mode 100644 index 000000000..7150848a6 --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/TracingConfigurationSettings.java @@ -0,0 +1,59 @@ +/* + * Copyright 2024 The Dapr Authors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and +limitations under the License. +*/ + +package io.dapr.testcontainers; + +/** + * Represents a Dapr tracing configuration parameters . + */ +public class TracingConfigurationSettings implements ConfigurationSettings { + private final String samplingRate; + private final Boolean stdout; + private final OtelTracingConfigurationSettings otel; + private final ZipkinTracingConfigurationSettings zipkin; + + /** + * Creates a new configuration. + * @param samplingRate tracing sampling rate + * @param stdout if it should send traces to the system standard output + * @param otel if using OpenTelemetry + * @param zipkin if using Zipkin + */ + public TracingConfigurationSettings( + String samplingRate, + Boolean stdout, + OtelTracingConfigurationSettings otel, + ZipkinTracingConfigurationSettings zipkin + ) { + this.samplingRate = samplingRate; + this.stdout = stdout; + this.otel = otel; + this.zipkin = zipkin; + } + + public String getSamplingRate() { + return this.samplingRate; + } + + public Boolean getStdout() { + return this.stdout; + } + + public OtelTracingConfigurationSettings getOtel() { + return otel; + } + + public ZipkinTracingConfigurationSettings getZipkin() { + return zipkin; + } +} diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/ZipkinTracingConfigurationSettings.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/ZipkinTracingConfigurationSettings.java new file mode 100644 index 000000000..be95cb34d --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/ZipkinTracingConfigurationSettings.java @@ -0,0 +1,16 @@ +package io.dapr.testcontainers; + +/** + * Configuration settings for Zipkin tracing. + */ +public class ZipkinTracingConfigurationSettings implements ConfigurationSettings { + private final String endpointAddress; + + public ZipkinTracingConfigurationSettings(String endpointAddress) { + this.endpointAddress = endpointAddress; + } + + public String getEndpointAddress() { + return endpointAddress; + } +} diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/ComponentYamlConverter.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/ComponentYamlConverter.java new file mode 100644 index 000000000..586de01e4 --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/ComponentYamlConverter.java @@ -0,0 +1,38 @@ +package io.dapr.testcontainers.converter; + +import io.dapr.testcontainers.Component; +import org.yaml.snakeyaml.Yaml; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class ComponentYamlConverter implements YamlConverter { + private final Yaml mapper; + + public ComponentYamlConverter(Yaml mapper) { + this.mapper = mapper; + } + + @Override + public String convert(Component component) { + Map componentProps = new LinkedHashMap<>(); + componentProps.put("apiVersion", "dapr.io/v1alpha1"); + componentProps.put("kind", "Component"); + + Map componentMetadata = new LinkedHashMap<>(); + componentMetadata.put("name", component.getName()); + componentProps.put("metadata", componentMetadata); + + Map componentSpec = new LinkedHashMap<>(); + componentSpec.put("type", component.getType()); + componentSpec.put("version", component.getVersion()); + + if (!component.getMetadata().isEmpty()) { + componentSpec.put("metadata", component.getMetadata()); + } + + componentProps.put("spec", componentSpec); + + return mapper.dumpAsMap(componentProps); + } +} diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/ConfigurationYamlConverter.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/ConfigurationYamlConverter.java new file mode 100644 index 000000000..d7f87cefe --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/ConfigurationYamlConverter.java @@ -0,0 +1,67 @@ +package io.dapr.testcontainers.converter; + +import io.dapr.testcontainers.Configuration; +import io.dapr.testcontainers.OtelTracingConfigurationSettings; +import io.dapr.testcontainers.TracingConfigurationSettings; +import io.dapr.testcontainers.ZipkinTracingConfigurationSettings; +import org.yaml.snakeyaml.Yaml; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class ConfigurationYamlConverter implements YamlConverter { + private final Yaml mapper; + + public ConfigurationYamlConverter(Yaml mapper) { + this.mapper = mapper; + } + + @Override + public String convert(Configuration configuration) { + Map configurationProps = new LinkedHashMap<>(); + configurationProps.put("apiVersion", "dapr.io/v1alpha1"); + configurationProps.put("kind", "Configuration"); + + Map configurationMetadata = new LinkedHashMap<>(); + configurationMetadata.put("name", configuration.getName()); + configurationProps.put("metadata", configurationMetadata); + + Map configurationSpec = new LinkedHashMap<>(); + TracingConfigurationSettings tracing = configuration.getTracing(); + + if (tracing != null) { + Map tracingMap = new LinkedHashMap<>(); + + tracingMap.put("samplingRate", configuration.getTracing().getSamplingRate()); + tracingMap.put("stdout", configuration.getTracing().getStdout()); + + OtelTracingConfigurationSettings otel = tracing.getOtel(); + + if (otel != null) { + Map otelMap = new LinkedHashMap<>(); + + otelMap.put("endpointAddress", otel.getEndpointAddress()); + otelMap.put("isSecure", otel.getSecure()); + otelMap.put("protocol", otel.getProtocol()); + + tracingMap.put("otel", otelMap); + } + + ZipkinTracingConfigurationSettings zipkin = tracing.getZipkin(); + + if (zipkin != null) { + Map zipkinMap = new LinkedHashMap<>(); + + zipkinMap.put("endpointAddress", zipkin.getEndpointAddress()); + + tracingMap.put("zipkin", zipkinMap); + } + + configurationSpec.put("tracing", tracingMap); + } + + configurationProps.put("spec", configurationSpec); + + return mapper.dumpAsMap(configurationProps); + } +} diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/SubscriptionYamlConverter.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/SubscriptionYamlConverter.java new file mode 100644 index 000000000..89f119ba0 --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/SubscriptionYamlConverter.java @@ -0,0 +1,35 @@ +package io.dapr.testcontainers.converter; + +import io.dapr.testcontainers.Subscription; +import org.yaml.snakeyaml.Yaml; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class SubscriptionYamlConverter implements YamlConverter { + private final Yaml mapper; + + public SubscriptionYamlConverter(Yaml mapper) { + this.mapper = mapper; + } + + @Override + public String convert(Subscription subscription) { + Map subscriptionProps = new LinkedHashMap<>(); + subscriptionProps.put("apiVersion", "dapr.io/v1alpha1"); + subscriptionProps.put("kind", "Subscription"); + + Map subscriptionMetadata = new LinkedHashMap<>(); + subscriptionMetadata.put("name", subscription.getName()); + subscriptionProps.put("metadata", subscriptionMetadata); + + Map subscriptionSpec = new LinkedHashMap<>(); + subscriptionSpec.put("pubsubname", subscription.getPubsubName()); + subscriptionSpec.put("topic", subscription.getTopic()); + subscriptionSpec.put("route", subscription.getRoute()); + + subscriptionProps.put("spec", subscriptionSpec); + + return mapper.dumpAsMap(subscriptionProps); + } +} diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/YamlConverter.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/YamlConverter.java new file mode 100644 index 000000000..2326edc6b --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/YamlConverter.java @@ -0,0 +1,5 @@ +package io.dapr.testcontainers.converter; + +public interface YamlConverter { + String convert(T value); +} diff --git a/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/YamlMapperFactory.java b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/YamlMapperFactory.java new file mode 100644 index 000000000..1c04ee165 --- /dev/null +++ b/testcontainers-dapr/src/main/java/io/dapr/testcontainers/converter/YamlMapperFactory.java @@ -0,0 +1,26 @@ +package io.dapr.testcontainers.converter; + +import io.dapr.testcontainers.MetadataEntry; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.nodes.Tag; +import org.yaml.snakeyaml.representer.Representer; + +/** + * Factory for creating a YAML mapper. + */ +public class YamlMapperFactory { + + /** + * Creates a YAML mapper. + * @return YAML mapper. + */ + public static Yaml create() { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setPrettyFlow(true); + Representer representer = new Representer(options); + representer.addClassTag(MetadataEntry.class, Tag.MAP); + return new Yaml(representer); + } +} diff --git a/testcontainers-dapr/src/test/java/io/dapr/testcontainers/DaprComponentTest.java b/testcontainers-dapr/src/test/java/io/dapr/testcontainers/DaprComponentTest.java index 2a5db5967..b0d338722 100644 --- a/testcontainers-dapr/src/test/java/io/dapr/testcontainers/DaprComponentTest.java +++ b/testcontainers-dapr/src/test/java/io/dapr/testcontainers/DaprComponentTest.java @@ -13,12 +13,14 @@ package io.dapr.testcontainers; +import io.dapr.testcontainers.converter.ComponentYamlConverter; +import io.dapr.testcontainers.converter.YamlMapperFactory; import org.junit.jupiter.api.Test; +import org.yaml.snakeyaml.Yaml; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Collections; import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -26,38 +28,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; public class DaprComponentTest { - - @Test - public void componentStateStoreSerializationTest() { - DaprContainer dapr = new DaprContainer("daprio/daprd") - .withAppName("dapr-app") - .withAppPort(8081) - .withComponent(new Component( - "statestore", - "state.in-memory", - "v1", - Collections.singletonMap("actorStateStore", "true"))) - .withAppChannelAddress("host.testcontainers.internal"); - - Set components = dapr.getComponents(); - assertEquals(1, components.size()); - - Component kvstore = components.iterator().next(); - assertFalse(kvstore.getMetadata().isEmpty()); - - String componentYaml = dapr.componentToYaml(kvstore); - String expectedComponentYaml = "metadata:\n" + " name: statestore\n" - + "apiVersion: dapr.io/v1alpha1\n" - + "kind: Component\n" - + "spec:\n" - + " metadata:\n" - + " - name: actorStateStore\n" - + " value: 'true'\n" - + " type: state.in-memory\n" - + " version: v1\n"; - - assertEquals(expectedComponentYaml, componentYaml); - } + private final Yaml MAPPER = YamlMapperFactory.create(); + private final ComponentYamlConverter converter = new ComponentYamlConverter(MAPPER); @Test public void containerConfigurationTest() { @@ -73,28 +45,6 @@ public void containerConfigurationTest() { assertThrows(IllegalStateException.class, dapr::getGrpcPort); } - @Test - public void subscriptionSerializationTest() { - DaprContainer dapr = new DaprContainer("daprio/daprd") - .withAppName("dapr-app") - .withAppPort(8081) - .withSubscription(new Subscription("my-subscription", "pubsub", "topic", "/events")) - .withAppChannelAddress("host.testcontainers.internal"); - - Set subscriptions = dapr.getSubscriptions(); - assertEquals(1, subscriptions.size()); - - String subscriptionYaml = dapr.subscriptionToYaml(subscriptions.iterator().next()); - String expectedSubscriptionYaml = "metadata:\n" + " name: my-subscription\n" - + "apiVersion: dapr.io/v1alpha1\n" - + "kind: Subscription\n" - + "spec:\n" - + " route: /events\n" - + " pubsubname: pubsub\n" - + " topic: topic\n"; - assertEquals(expectedSubscriptionYaml, subscriptionYaml); - } - @Test public void withComponentFromPath() { URL stateStoreYaml = this.getClass().getClassLoader().getResource("dapr-resources/statestore.yaml"); @@ -111,12 +61,15 @@ public void withComponentFromPath() { Component kvstore = components.iterator().next(); assertFalse(kvstore.getMetadata().isEmpty()); - String componentYaml = dapr.componentToYaml(kvstore); - String expectedComponentYaml = "metadata:\n" - + " name: statestore\n" - + "apiVersion: dapr.io/v1alpha1\n" + String componentYaml = converter.convert(kvstore); + String expectedComponentYaml = + "apiVersion: dapr.io/v1alpha1\n" + "kind: Component\n" + + "metadata:\n" + + " name: statestore\n" + "spec:\n" + + " type: null\n" + + " version: v1\n" + " metadata:\n" + " - name: name\n" + " value: keyPrefix\n" @@ -129,9 +82,7 @@ public void withComponentFromPath() { + " - name: name\n" + " value: redisPassword\n" + " - name: value\n" - + " value: ''\n" - + " type: null\n" - + " version: v1\n"; + + " value: ''\n"; assertEquals(expectedComponentYaml, componentYaml); } diff --git a/testcontainers-dapr/src/test/java/io/dapr/testcontainers/converter/ComponentYamlConverterTest.java b/testcontainers-dapr/src/test/java/io/dapr/testcontainers/converter/ComponentYamlConverterTest.java new file mode 100644 index 000000000..38ced68d9 --- /dev/null +++ b/testcontainers-dapr/src/test/java/io/dapr/testcontainers/converter/ComponentYamlConverterTest.java @@ -0,0 +1,51 @@ +package io.dapr.testcontainers.converter; + +import io.dapr.testcontainers.Component; +import io.dapr.testcontainers.DaprContainer; +import org.junit.jupiter.api.Test; +import org.yaml.snakeyaml.Yaml; + +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +class ComponentYamlConverterTest { + private final Yaml MAPPER = YamlMapperFactory.create(); + private final ComponentYamlConverter converter = new ComponentYamlConverter(MAPPER); + + @Test + public void testComponentToYaml() { + DaprContainer dapr = new DaprContainer("daprio/daprd") + .withAppName("dapr-app") + .withAppPort(8081) + .withComponent(new Component( + "statestore", + "state.in-memory", + "v1", + Map.of("actorStateStore", "true"))) + .withAppChannelAddress("host.testcontainers.internal"); + + Set components = dapr.getComponents(); + assertEquals(1, components.size()); + + Component kvstore = components.iterator().next(); + assertFalse(kvstore.getMetadata().isEmpty()); + + String componentYaml = converter.convert(kvstore); + String expectedComponentYaml = + "apiVersion: dapr.io/v1alpha1\n" + + "kind: Component\n" + + "metadata:\n" + + " name: statestore\n" + + "spec:\n" + + " type: state.in-memory\n" + + " version: v1\n" + + " metadata:\n" + + " - name: actorStateStore\n" + + " value: 'true'\n"; + + assertEquals(expectedComponentYaml, componentYaml); + } +} diff --git a/testcontainers-dapr/src/test/java/io/dapr/testcontainers/converter/ConfigurationYamlConverterTest.java b/testcontainers-dapr/src/test/java/io/dapr/testcontainers/converter/ConfigurationYamlConverterTest.java new file mode 100644 index 000000000..cbe081d01 --- /dev/null +++ b/testcontainers-dapr/src/test/java/io/dapr/testcontainers/converter/ConfigurationYamlConverterTest.java @@ -0,0 +1,57 @@ +package io.dapr.testcontainers.converter; + +import io.dapr.testcontainers.Configuration; +import io.dapr.testcontainers.DaprContainer; +import io.dapr.testcontainers.OtelTracingConfigurationSettings; +import io.dapr.testcontainers.TracingConfigurationSettings; +import org.junit.jupiter.api.Test; +import org.yaml.snakeyaml.Yaml; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class ConfigurationYamlConverterTest { + private final Yaml MAPPER = YamlMapperFactory.create(); + private final ConfigurationYamlConverter converter = new ConfigurationYamlConverter(MAPPER); + + @Test + public void testConfigurationToYaml() { + OtelTracingConfigurationSettings otel = new OtelTracingConfigurationSettings( + "localhost:4317", + false, + "grpc" + ); + TracingConfigurationSettings tracing = new TracingConfigurationSettings( + "1", + true, + otel, + null + ); + + DaprContainer dapr = new DaprContainer("daprio/daprd") + .withAppName("dapr-app") + .withAppPort(8081) + .withConfiguration(new Configuration("my-config", tracing)) + .withAppChannelAddress("host.testcontainers.internal"); + + Configuration configuration = dapr.getConfiguration(); + assertNotNull(configuration); + + String configurationYaml = converter.convert(configuration); + String expectedConfigurationYaml = + "apiVersion: dapr.io/v1alpha1\n" + + "kind: Configuration\n" + + "metadata:\n" + + " name: my-config\n" + + "spec:\n" + + " tracing:\n" + + " samplingRate: '1'\n" + + " stdout: true\n" + + " otel:\n" + + " endpointAddress: localhost:4317\n" + + " isSecure: false\n" + + " protocol: grpc\n"; + + assertEquals(expectedConfigurationYaml, configurationYaml); + } +} diff --git a/testcontainers-dapr/src/test/java/io/dapr/testcontainers/converter/SubscriptionYamlConverterTest.java b/testcontainers-dapr/src/test/java/io/dapr/testcontainers/converter/SubscriptionYamlConverterTest.java new file mode 100644 index 000000000..a17984e60 --- /dev/null +++ b/testcontainers-dapr/src/test/java/io/dapr/testcontainers/converter/SubscriptionYamlConverterTest.java @@ -0,0 +1,41 @@ +package io.dapr.testcontainers.converter; + +import io.dapr.testcontainers.DaprContainer; +import io.dapr.testcontainers.Subscription; +import org.junit.jupiter.api.Test; +import org.yaml.snakeyaml.Yaml; + +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; + +class SubscriptionYamlConverterTest { + private final Yaml MAPPER = YamlMapperFactory.create(); + private final SubscriptionYamlConverter converter = new SubscriptionYamlConverter(MAPPER); + + @Test + public void testSubscriptionToYaml() { + DaprContainer dapr = new DaprContainer("daprio/daprd") + .withAppName("dapr-app") + .withAppPort(8081) + .withSubscription(new Subscription("my-subscription", "pubsub", "topic", "/events")) + .withAppChannelAddress("host.testcontainers.internal"); + + Set subscriptions = dapr.getSubscriptions(); + assertEquals(1, subscriptions.size()); + + Subscription subscription = subscriptions.iterator().next(); + String subscriptionYaml = converter.convert(subscription); + String expectedSubscriptionYaml = + "apiVersion: dapr.io/v1alpha1\n" + + "kind: Subscription\n" + + "metadata:\n" + + " name: my-subscription\n" + + "spec:\n" + + " pubsubname: pubsub\n" + + " topic: topic\n" + + " route: /events\n"; + + assertEquals(expectedSubscriptionYaml, subscriptionYaml); + } +}