Skip to content

Commit 9b635da

Browse files
authored
Add coverage for some properties (#1297)
1 parent ed9a3fb commit 9b635da

File tree

13 files changed

+451
-16
lines changed

13 files changed

+451
-16
lines changed

dapr-spring/dapr-spring-boot-autoconfigure/src/test/java/io/dapr/spring/boot/autoconfigure/client/DaprClientPropertiesTest.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 The Dapr Authors
2+
* Copyright 2025 The Dapr Authors
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -14,8 +14,9 @@
1414
package io.dapr.spring.boot.autoconfigure.client;
1515

1616
import org.assertj.core.api.SoftAssertions;
17-
import org.junit.Test;
17+
1818
import org.junit.jupiter.api.DisplayName;
19+
import org.junit.jupiter.api.Test;
1920
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2021
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2122

@@ -33,11 +34,11 @@ public void shouldCreateDaprClientPropertiesCorrectly() {
3334
"http://localhost", "localhost", 3500, 50001
3435
);
3536

36-
SoftAssertions.assertSoftly(softAssertions -> {
37-
softAssertions.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
38-
softAssertions.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
39-
softAssertions.assertThat(properties.getHttpPort()).isEqualTo(3500);
40-
softAssertions.assertThat(properties.getGrpcPort()).isEqualTo(50001);
37+
SoftAssertions.assertSoftly(softly -> {
38+
softly.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
39+
softly.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
40+
softly.assertThat(properties.getHttpPort()).isEqualTo(3500);
41+
softly.assertThat(properties.getGrpcPort()).isEqualTo(50001);
4142
});
4243
}
4344

@@ -71,11 +72,11 @@ public void shouldMapDaprClientProperties() {
7172
"dapr.client.grpc-port=50001"
7273
).run(context -> {
7374
DaprClientProperties properties = context.getBean(DaprClientProperties.class);
74-
SoftAssertions.assertSoftly(softAssertions -> {
75-
softAssertions.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
76-
softAssertions.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
77-
softAssertions.assertThat(properties.getHttpPort()).isEqualTo(3500);
78-
softAssertions.assertThat(properties.getGrpcPort()).isEqualTo(50001);
75+
SoftAssertions.assertSoftly(softly -> {
76+
softly.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
77+
softly.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
78+
softly.assertThat(properties.getHttpPort()).isEqualTo(3500);
79+
softly.assertThat(properties.getGrpcPort()).isEqualTo(50001);
7980
});
8081

8182
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.spring.boot.autoconfigure.pubsub;
15+
16+
import org.assertj.core.api.SoftAssertions;
17+
import org.junit.jupiter.api.DisplayName;
18+
import org.junit.jupiter.api.Test;
19+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
20+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
21+
22+
public class DaprPubSubPropertiesTest {
23+
24+
final ApplicationContextRunner runner = new ApplicationContextRunner()
25+
.withUserConfiguration(EnableDaprPubSubProperties.class);
26+
27+
28+
@Test
29+
@DisplayName("Should configure properties with setters")
30+
void shouldSetProperties() {
31+
DaprPubSubProperties properties = new DaprPubSubProperties();
32+
properties.setName("pubsub");
33+
properties.setObservationEnabled(false);
34+
35+
SoftAssertions.assertSoftly(softAssertions -> {
36+
softAssertions.assertThat(properties.getName()).isEqualTo("pubsub");
37+
softAssertions.assertThat(properties.isObservationEnabled()).isEqualTo(false);
38+
});
39+
}
40+
41+
@Test
42+
@DisplayName("Should map DaprPubSubProperties correctly")
43+
void shouldMapDaprPubSubPropertiesCorrectly() {
44+
runner.withPropertyValues(
45+
"dapr.pubsub.name=pubsub",
46+
"dapr.pubsub.observation-enabled=true"
47+
).run(context -> {
48+
DaprPubSubProperties properties = context.getBean(DaprPubSubProperties.class);
49+
50+
SoftAssertions.assertSoftly(softAssertions -> {
51+
softAssertions.assertThat(properties.getName()).isEqualTo("pubsub");
52+
softAssertions.assertThat(properties.isObservationEnabled()).isEqualTo(true);
53+
});
54+
});
55+
}
56+
57+
@EnableConfigurationProperties(DaprPubSubProperties.class)
58+
static class EnableDaprPubSubProperties {
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2021 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.spring.boot.autoconfigure.statestore;
15+
16+
import org.assertj.core.api.SoftAssertions;
17+
import org.junit.jupiter.api.DisplayName;
18+
import org.junit.jupiter.api.Test;
19+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
20+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
21+
22+
public class DaprStateStorePropertiesTest {
23+
24+
25+
final ApplicationContextRunner runner = new ApplicationContextRunner()
26+
.withUserConfiguration(EnableDaprStateStoreProperties.class);
27+
28+
@Test
29+
@DisplayName("Should create DaprStateStoreProperties via constructor")
30+
void shouldSetDaprStateStorePropertiesCorrectly() {
31+
DaprStateStoreProperties properties = new DaprStateStoreProperties();
32+
properties.setBinding("binding");
33+
properties.setName("name");
34+
35+
SoftAssertions.assertSoftly(softAssertions -> {
36+
softAssertions.assertThat(properties.getName()).isEqualTo("name");
37+
softAssertions.assertThat(properties.getBinding()).isEqualTo("binding");
38+
});
39+
}
40+
41+
@Test
42+
@DisplayName("Should map Dapr state store properties correctly")
43+
void shouldMapDaprStateStoreProperties() {
44+
runner.withPropertyValues(
45+
"dapr.statestore.name=name",
46+
"dapr.statestore.binding=binding"
47+
).run(context -> {
48+
DaprStateStoreProperties properties = context.getBean(DaprStateStoreProperties.class);
49+
50+
SoftAssertions.assertSoftly(softly -> {
51+
softly.assertThat(properties.getBinding()).isEqualTo("binding");
52+
softly.assertThat(properties.getName()).isEqualTo("name");
53+
});
54+
});
55+
56+
}
57+
58+
@EnableConfigurationProperties(DaprStateStoreProperties.class)
59+
static class EnableDaprStateStoreProperties {
60+
61+
}
62+
63+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<testcontainers.version>1.20.5</testcontainers.version>
5151
<springboot.version>3.4.3</springboot.version>
5252
<nexus-staging-maven-plugin.version>1.7.0</nexus-staging-maven-plugin.version>
53+
<assertj.version>3.27.3</assertj.version>
5354
</properties>
5455

5556
<distributionManagement>

sdk/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@
127127
<version>${jackson.version}</version>
128128
<scope>test</scope>
129129
</dependency>
130+
<dependency>
131+
<groupId>org.assertj</groupId>
132+
<artifactId>assertj-core</artifactId>
133+
<version>${assertj.version}</version>
134+
</dependency>
130135
</dependencies>
131136

132137
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.dapr.client.domain;
14+
15+
import org.junit.jupiter.api.Assertions;
16+
import org.junit.jupiter.api.DisplayName;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.Map;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
class BulkPublishEntryTest {
24+
25+
@Test
26+
@DisplayName("Should create an empty metadata when metadata argument is null")
27+
public void shouldCreateWithEmptyMetadataWhenMetadataIsNull() {
28+
BulkPublishEntry<String> entry = new BulkPublishEntry<>(
29+
"entryId", "event", "contentType", null
30+
);
31+
assertThat(entry.getMetadata()).isEmpty();
32+
}
33+
34+
@Test
35+
@DisplayName("Should create with non null metadata")
36+
public void shouldCreateWithMetadata() {
37+
BulkPublishEntry<String> entry = new BulkPublishEntry<>(
38+
"entryId", "event", "application/json", Map.of(
39+
"repo", "dapr/java-sdk"
40+
));
41+
assertThat(entry.getMetadata()).hasSize(1);
42+
}
43+
44+
}

sdk/src/test/java/io/dapr/client/domain/BulkPublishRequestTest.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
* See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
13-
1413
package io.dapr.client.domain;
1514

16-
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.DisplayName;
1716
import org.junit.jupiter.api.Test;
1817

1918
import java.util.Collections;
2019
import java.util.HashMap;
20+
import java.util.List;
2121
import java.util.Map;
2222

23+
import static org.assertj.core.api.Assertions.assertThat;
2324
import static org.junit.jupiter.api.Assertions.assertNull;
2425

2526
public class BulkPublishRequestTest {
@@ -36,6 +37,15 @@ public void testSetMetadata() {
3637
request.setMetadata(metadata);
3738
Map<String, String> initial = request.getMetadata();
3839
request.setMetadata(metadata);
39-
Assertions.assertNotSame( request.getMetadata(), initial, "Should not be same map");
40+
41+
assertThat(request.getMetadata()).isNotSameAs(initial);
42+
}
43+
44+
@Test
45+
@DisplayName("Should create a BulkPublishRequest with empty list when entries is null")
46+
public void shouldCreateWithEmptyListWhenEntriesIsNull() {
47+
BulkPublishRequest<String> request = new BulkPublishRequest<>("testPubsub", "testTopic", null);
48+
List<BulkPublishEntry<String>> entries = request.getEntries();
49+
assertThat(entries).isNotNull();
4050
}
4151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.dapr.client.domain;
14+
15+
import org.junit.jupiter.api.Assertions;
16+
import org.junit.jupiter.api.DisplayName;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.Map;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
23+
24+
class ConfigurationItemTest {
25+
26+
@Test
27+
@DisplayName("Should create ConfigurationItem correctly")
28+
void shouldCreateConfigurationItemCorrectly() {
29+
ConfigurationItem item = new ConfigurationItem("application", "java-sdk", "0.0.1", Map.of(
30+
"creator", "devs"
31+
));
32+
33+
assertThat(item.getKey()).isEqualTo("application");
34+
assertThat(item.getValue()).isEqualTo("java-sdk");
35+
assertThat(item.getVersion()).isEqualTo("0.0.1");
36+
assertThat(item.getMetadata()).hasSize(1);
37+
assertThat(item.getMetadata()).hasEntrySatisfying("creator", value -> {
38+
assertThat(value).isEqualTo("devs");
39+
});
40+
}
41+
42+
@Test
43+
@DisplayName("Should create with immutable metadata")
44+
void shouldCreateWithImmutableMetadata() {
45+
ConfigurationItem item = new ConfigurationItem("application", "java-sdk", "0.0.1", Map.of(
46+
"creator", "devs"
47+
));
48+
assertThatThrownBy(() -> item.getMetadata().put("language", "javascript"));
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.client.domain;
15+
16+
import io.dapr.client.DaprHttp;
17+
import org.junit.jupiter.api.Assertions;
18+
import org.junit.jupiter.api.DisplayName;
19+
import org.junit.jupiter.api.Test;
20+
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
class HttpExtensionTest {
25+
26+
27+
@Test
28+
@DisplayName("Should encode query params correctly")
29+
void shouldEncodeQueryParamsCorrectly() {
30+
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET,
31+
Map.of("traceparent", List.of("00-4bf92f3577b34da6a3ce929d0e0e4733-00f067aa0ba902b7-01")), Map.of());
32+
33+
String encoded = httpExtension.encodeQueryString();
34+
35+
Assertions.assertEquals("traceparent=00-4bf92f3577b34da6a3ce929d0e0e4733-00f067aa0ba902b7-01", encoded);
36+
}
37+
38+
@Test
39+
@DisplayName("Should encode multiple values for same query param key")
40+
void shouldEncodeMultipleValuesForSameQueryParamKey() {
41+
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET,
42+
Map.of("category", List.of("books", "electronics")), Map.of());
43+
44+
String encoded = httpExtension.encodeQueryString();
45+
46+
Assertions.assertEquals("category=books&category=electronics", encoded);
47+
}
48+
49+
@Test
50+
@DisplayName("Should encode query param with spaces, accents, and special characters")
51+
void shouldEncodeQueryParamWithSpacesAndSpecialCharacters() {
52+
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET,
53+
Map.of("user name", List.of("John Doë & Co.")), Map.of());
54+
55+
String encoded = httpExtension.encodeQueryString();
56+
57+
Assertions.assertEquals("user+name=John+Do%C3%AB+%26+Co.", encoded);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)