Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2025 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;

import java.util.Collections;
import java.util.List;

public class AppHttpPipeline implements ConfigurationSettings {
private List<ListEntry> handlers;

/**
* Creates an AppHttpPipeline.
*
* @param handlers List of handlers for the AppHttpPipeline
*/
public AppHttpPipeline(List<ListEntry> handlers) {
if (handlers != null) {
this.handlers = Collections.unmodifiableList(handlers);
}
}

public List<ListEntry> getHandlers() {
return handlers;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,28 @@
public class Configuration {
private final String name;
private final TracingConfigurationSettings tracing;
private final AppHttpPipeline appHttpPipeline;

//@TODO: add httpPipeline
//@TODO: add secrets
//@TODO: add components
//@TODO: add accessControl
// @TODO: add secrets https://github.com/dapr/java-sdk/issues/1280
// @TODO: add metrics https://github.com/dapr/java-sdk/issues/1281
// @TODO: add logging https://github.com/dapr/java-sdk/issues/1282
// @TODO: add middleware httpPipeline https://github.com/dapr/java-sdk/issues/1283
// @TODO: add nameResolution https://github.com/dapr/java-sdk/issues/1284
// @TODO: add disallow components https://github.com/dapr/java-sdk/issues/1285
// @TODO: add mtls https://github.com/dapr/java-sdk/issues/1286

/**
* Creates a new configuration.
* @param name Configuration name.
* @param tracing TracingConfigParameters tracing configuration parameters.
*
* @param name Configuration name.
* @param tracing TracingConfigParameters tracing configuration
* parameters.
* @param appHttpPipeline AppHttpPipeline middleware configuration.
*/
public Configuration(String name, TracingConfigurationSettings tracing) {
public Configuration(String name, TracingConfigurationSettings tracing, AppHttpPipeline appHttpPipeline) {
this.name = name;
this.tracing = tracing;
this.appHttpPipeline = appHttpPipeline;
}

public String getName() {
Expand All @@ -42,4 +50,8 @@ public String getName() {
public TracingConfigurationSettings getTracing() {
return tracing;
}

public AppHttpPipeline getAppHttpPipeline() {
return appHttpPipeline;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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;

public class ListEntry {
private String name;
private String type;

public ListEntry(String name, String type) {
this.name = name;
this.type = type;
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public void setName(String name) {
this.name = name;
}

Check warning on line 35 in testcontainers-dapr/src/main/java/io/dapr/testcontainers/ListEntry.java

View check run for this annotation

Codecov / codecov/patch

testcontainers-dapr/src/main/java/io/dapr/testcontainers/ListEntry.java#L34-L35

Added lines #L34 - L35 were not covered by tests

public void setType(String type) {
this.type = type;
}

Check warning on line 39 in testcontainers-dapr/src/main/java/io/dapr/testcontainers/ListEntry.java

View check run for this annotation

Codecov / codecov/patch

testcontainers-dapr/src/main/java/io/dapr/testcontainers/ListEntry.java#L38-L39

Added lines #L38 - L39 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.dapr.testcontainers.converter;

import io.dapr.testcontainers.AppHttpPipeline;
import io.dapr.testcontainers.Configuration;
import io.dapr.testcontainers.ListEntry;
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.List;
import java.util.Map;

public class ConfigurationYamlConverter implements YamlConverter<Configuration> {
Expand Down Expand Up @@ -58,6 +61,17 @@ public String convert(Configuration configuration) {
}

configurationSpec.put("tracing", tracingMap);

}

AppHttpPipeline appHttpPipeline = configuration.getAppHttpPipeline();
if (appHttpPipeline != null) {

Map<String, Object> appHttpPipelineMap = new LinkedHashMap<>();
List<ListEntry> handlers = appHttpPipeline.getHandlers();
appHttpPipelineMap.put("handlers", handlers);
configurationSpec.put("appHttpPipeline", appHttpPipelineMap);

}

configurationProps.put("spec", configurationSpec);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.dapr.testcontainers.converter;

import io.dapr.testcontainers.ListEntry;
import io.dapr.testcontainers.MetadataEntry;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
Expand All @@ -21,6 +22,7 @@ public static Yaml create() {
options.setPrettyFlow(true);
Representer representer = new Representer(options);
representer.addClassTag(MetadataEntry.class, Tag.MAP);
representer.addClassTag(ListEntry.class, Tag.MAP);
return new Yaml(representer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public void testComponentToYaml() {
assertFalse(kvstore.getMetadata().isEmpty());

String componentYaml = converter.convert(kvstore);
String expectedComponentYaml =
"apiVersion: dapr.io/v1alpha1\n"
String expectedComponentYaml = "apiVersion: dapr.io/v1alpha1\n"
+ "kind: Component\n"
+ "metadata:\n"
+ " name: statestore\n"
Expand All @@ -48,4 +47,46 @@ public void testComponentToYaml() {

assertEquals(expectedComponentYaml, componentYaml);
}

@Test
public void testComponentWithInLineStringToYaml() {
DaprContainer dapr = new DaprContainer("daprio/daprd")
.withAppName("dapr-app")
.withAppPort(8081)
.withComponent(new Component(
"alias",
"middleware.http.routeralias",
"v1",
Map.of("routes", "{\n" +
" \"/mall/activity/info\": \"/v1.0/invoke/srv.default/method/mall/activity/info\",\n" +
" \"/hello/activity/{id}/info\": \"/v1.0/invoke/srv.default/method/hello/activity/info\",\n" + //
" \"/hello/activity/{id}/user\": \"/v1.0/invoke/srv.default/method/hello/activity/user\"\n" + //
"}")))
.withAppChannelAddress("host.testcontainers.internal");

Set<Component> 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: alias\n"
+ "spec:\n"
+ " type: middleware.http.routeralias\n"
+ " version: v1\n"
+ " metadata:\n"
+ " - name: routes\n"
+ " value: |-\n"
+ " {\n"
+ " \"/mall/activity/info\": \"/v1.0/invoke/srv.default/method/mall/activity/info\",\n"
+ " \"/hello/activity/{id}/info\": \"/v1.0/invoke/srv.default/method/hello/activity/info\",\n"
+ " \"/hello/activity/{id}/user\": \"/v1.0/invoke/srv.default/method/hello/activity/user\"\n"
+ " }\n";

assertEquals(expectedComponentYaml, componentYaml);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/*
* Copyright 2025 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.converter;

import io.dapr.testcontainers.AppHttpPipeline;
import io.dapr.testcontainers.Configuration;
import io.dapr.testcontainers.DaprContainer;
import io.dapr.testcontainers.ListEntry;
import io.dapr.testcontainers.OtelTracingConfigurationSettings;
import io.dapr.testcontainers.TracingConfigurationSettings;
import org.junit.jupiter.api.Test;
Expand All @@ -10,6 +25,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.ArrayList;
import java.util.List;

class ConfigurationYamlConverterTest {
private final Yaml MAPPER = YamlMapperFactory.create();
private final ConfigurationYamlConverter converter = new ConfigurationYamlConverter(MAPPER);
Expand All @@ -28,10 +46,16 @@ public void testConfigurationToYaml() {
null
);


List<ListEntry> handlers = new ArrayList<>();
handlers.add(new ListEntry("alias", "middleware.http.routeralias"));

AppHttpPipeline appHttpPipeline = new AppHttpPipeline(handlers);

DaprContainer dapr = new DaprContainer("daprio/daprd")
.withAppName("dapr-app")
.withAppPort(8081)
.withConfiguration(new Configuration("my-config", tracing))
.withConfiguration(new Configuration("my-config", tracing, appHttpPipeline))
.withAppChannelAddress("host.testcontainers.internal");

Configuration configuration = dapr.getConfiguration();
Expand All @@ -50,7 +74,11 @@ public void testConfigurationToYaml() {
+ " otel:\n"
+ " endpointAddress: localhost:4317\n"
+ " isSecure: false\n"
+ " protocol: grpc\n";
+ " protocol: grpc\n"
+ " appHttpPipeline:\n"
+ " handlers:\n"
+ " - name: alias\n"
+ " type: middleware.http.routeralias\n";

assertEquals(expectedConfigurationYaml, configurationYaml);
}
Expand Down
Loading