From db1c344bd2a19c3e3a3216a252740c1666c427c8 Mon Sep 17 00:00:00 2001 From: enderdincer Date: Wed, 21 Aug 2024 23:32:35 +0100 Subject: [PATCH 1/2] fix: class cast exception, map to list --- .../java/io/diagrid/dapr/DaprContainer.java | 16 ++++++---------- .../java/io/diagrid/dapr/DaprComponentTest.java | 17 +++++++++++++++++ .../components/no-metadata-component.yaml | 7 +++++++ 3 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/test/resources/components/no-metadata-component.yaml diff --git a/src/main/java/io/diagrid/dapr/DaprContainer.java b/src/main/java/io/diagrid/dapr/DaprContainer.java index 11ab5f4..6d4362d 100644 --- a/src/main/java/io/diagrid/dapr/DaprContainer.java +++ b/src/main/java/io/diagrid/dapr/DaprContainer.java @@ -25,15 +25,7 @@ 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.Objects; -import java.util.Set; +import java.util.*; public class DaprContainer extends GenericContainer { @@ -259,10 +251,14 @@ public DaprContainer withComponent(Path path) { Map spec = (Map) component.get("spec"); String version = (String) spec.get("version"); List> specMetadata = - (List>) spec.getOrDefault("metadata", Collections.emptyMap()); + (List>) spec.getOrDefault("metadata", Arrays.asList(Collections.emptyMap())); ArrayList metadataEntries = new ArrayList<>(); + if (specMetadata == null) { + return withComponent(name, type, version, metadataEntries); + } + for (Map specMetadataItem : specMetadata) { for (Map.Entry metadataItem : specMetadataItem.entrySet()) { metadataEntries.add(new MetadataEntry(metadataItem.getKey(), metadataItem.getValue())); diff --git a/src/test/java/io/diagrid/dapr/DaprComponentTest.java b/src/test/java/io/diagrid/dapr/DaprComponentTest.java index 0a124a3..f780a8f 100644 --- a/src/test/java/io/diagrid/dapr/DaprComponentTest.java +++ b/src/test/java/io/diagrid/dapr/DaprComponentTest.java @@ -141,4 +141,21 @@ public void withComponentFromPath() { Assert.assertEquals(expectedComponentYaml, componentYaml); } + + @Test + public void withComponentFromPathNoMetadata() { + final URL componentYaml = this.getClass().getClassLoader().getResource("components/no-metadata-component.yaml"); + final Path path = Paths.get(componentYaml.getPath()); + + final DaprContainer dapr = new DaprContainer("daprio/daprd") + .withAppName("dapr-app") + .withAppPort(8081) + .withComponent(path) + .withAppChannelAddress("host.testcontainers.internal"); + + final Set components = dapr.getComponents(); + Assert.assertEquals(1, components.size()); + final Component component = components.iterator().next(); + Assert.assertEquals(true, component.getMetadata().isEmpty()); + } } diff --git a/src/test/resources/components/no-metadata-component.yaml b/src/test/resources/components/no-metadata-component.yaml new file mode 100644 index 0000000..b7efb93 --- /dev/null +++ b/src/test/resources/components/no-metadata-component.yaml @@ -0,0 +1,7 @@ +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: statestore +spec: + type: state.redis + version: v1 \ No newline at end of file From dd72e01dfdb6a0bb077a8f6d889cd9f8cec7715e Mon Sep 17 00:00:00 2001 From: enderdincer Date: Thu, 22 Aug 2024 09:13:05 +0100 Subject: [PATCH 2/2] refactor: remove redundant empty map --- src/main/java/io/diagrid/dapr/DaprContainer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/diagrid/dapr/DaprContainer.java b/src/main/java/io/diagrid/dapr/DaprContainer.java index 6d4362d..17f4898 100644 --- a/src/main/java/io/diagrid/dapr/DaprContainer.java +++ b/src/main/java/io/diagrid/dapr/DaprContainer.java @@ -251,7 +251,7 @@ public DaprContainer withComponent(Path path) { Map spec = (Map) component.get("spec"); String version = (String) spec.get("version"); List> specMetadata = - (List>) spec.getOrDefault("metadata", Arrays.asList(Collections.emptyMap())); + (List>) spec.getOrDefault("metadata", Collections.emptyList()); ArrayList metadataEntries = new ArrayList<>();