Skip to content

Commit 358f9a2

Browse files
committed
Add tests for some classes
Signed-off-by: Matheus Cruz <[email protected]>
1 parent 8cb8099 commit 358f9a2

File tree

12 files changed

+430
-3
lines changed

12 files changed

+430
-3
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515

1616
import io.dapr.client.DaprClient;
1717
import io.dapr.client.DaprClientBuilder;
18+
import org.assertj.core.api.SoftAssertions;
1819
import org.junit.jupiter.api.Test;
1920
import org.springframework.boot.autoconfigure.AutoConfigurations;
2021
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
22+
import org.springframework.test.util.ReflectionTestUtils;
23+
24+
import java.util.Map;
2125

2226
import static org.assertj.core.api.Assertions.assertThat;
2327

@@ -39,4 +43,25 @@ void daprClient() {
3943
contextRunner.run(context -> assertThat(context).hasSingleBean(DaprClient.class));
4044
}
4145

46+
@Test
47+
void shouldOverridePropertiesWhenGeneratingDaprClientBuilder() {
48+
PropertiesDaprConnectionDetails details = new PropertiesDaprConnectionDetails(
49+
new DaprClientProperties(
50+
"http://localhost", "localhost", 3500, 50001
51+
)
52+
);
53+
contextRunner.withBean(DaprConnectionDetails.class, () -> details).run(context -> {
54+
55+
DaprClientBuilder builder = context.getBean(DaprClientBuilder.class);
56+
Map<String, String> propertyOverrides =
57+
(Map<String, String>) ReflectionTestUtils.getField(builder, "propertyOverrides");
58+
59+
SoftAssertions.assertSoftly(softAssertions -> {
60+
softAssertions.assertThat(propertyOverrides.get("dapr.grpc.endpoint")).isEqualTo("localhost");
61+
softAssertions.assertThat(propertyOverrides.get("dapr.http.endpoint")).isEqualTo("http://localhost");
62+
softAssertions.assertThat(propertyOverrides.get("dapr.grpc.port")).isEqualTo("50001");
63+
softAssertions.assertThat(propertyOverrides.get("dapr.http.port")).isEqualTo("3500");
64+
});
65+
});
66+
}
4267
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2024 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.Test;
18+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
19+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
20+
21+
public class DaprPubSubPropertiesTest {
22+
23+
final ApplicationContextRunner runner = new ApplicationContextRunner()
24+
.withUserConfiguration(EnableDaprPubSubProperties.class);
25+
26+
27+
@Test
28+
void shouldSetProperties() {
29+
DaprPubSubProperties properties = new DaprPubSubProperties();
30+
properties.setName("pubsub");
31+
properties.setObservationEnabled(false);
32+
33+
SoftAssertions.assertSoftly(softAssertions -> {
34+
softAssertions.assertThat(properties.getName()).isEqualTo("pubsub");
35+
softAssertions.assertThat(properties.isObservationEnabled()).isEqualTo(false);
36+
});
37+
}
38+
39+
@Test
40+
void shouldMapDaprPubSubPropertiesCorrectly() {
41+
runner.withPropertyValues(
42+
"dapr.pubsub.name=pubsub",
43+
"dapr.pubsub.observation-enabled=true"
44+
).run(context -> {
45+
DaprPubSubProperties properties = context.getBean(DaprPubSubProperties.class);
46+
47+
SoftAssertions.assertSoftly(softAssertions -> {
48+
softAssertions.assertThat(properties.getName()).isEqualTo("pubsub");
49+
softAssertions.assertThat(properties.isObservationEnabled()).isEqualTo(true);
50+
});
51+
});
52+
}
53+
54+
@EnableConfigurationProperties(DaprPubSubProperties.class)
55+
static class EnableDaprPubSubProperties {
56+
}
57+
}
Lines changed: 63 additions & 0 deletions
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(softAssertions -> {
51+
softAssertions.assertThat(properties.getBinding()).isEqualTo("binding");
52+
softAssertions.assertThat(properties.getName()).isEqualTo("name");
53+
});
54+
});
55+
56+
}
57+
58+
@EnableConfigurationProperties(DaprStateStoreProperties.class)
59+
static class EnableDaprStateStoreProperties {
60+
61+
}
62+
63+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.Test;
17+
18+
import java.util.Map;
19+
20+
class BulkPublishEntryTest {
21+
22+
@Test
23+
public void shouldCreateWithEmptyMetadataWhenMetadataIsNull() {
24+
BulkPublishEntry<String> entry = new BulkPublishEntry<>(
25+
"entryId", "event", "contentType", null
26+
);
27+
Assertions.assertEquals(0, entry.getMetadata().size());
28+
}
29+
30+
@Test
31+
public void shouldCreateWithMetadata() {
32+
BulkPublishEntry<String> entry = new BulkPublishEntry<>(
33+
"entryId", "event", "application/json", Map.of(
34+
"repo", "dapr/java-sdk"
35+
));
36+
37+
Assertions.assertEquals(1, entry.getMetadata().size());
38+
}
39+
40+
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
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

1615
import org.junit.jupiter.api.Assertions;
16+
import org.junit.jupiter.api.DisplayName;
1717
import org.junit.jupiter.api.Test;
1818

1919
import java.util.Collections;
2020
import java.util.HashMap;
21+
import java.util.List;
2122
import java.util.Map;
2223

2324
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -38,4 +39,12 @@ public void testSetMetadata() {
3839
request.setMetadata(metadata);
3940
Assertions.assertNotSame( request.getMetadata(), initial, "Should not be same map");
4041
}
42+
43+
@Test
44+
@DisplayName("Should create with empty list when entries is null")
45+
public void shouldCreateWithEmptyListWhenEntriesIsNull() {
46+
BulkPublishRequest<String> request = new BulkPublishRequest<>("testPubsub", "testTopic", null);
47+
List<BulkPublishEntry<String>> entries = request.getEntries();
48+
Assertions.assertNotNull(entries);
49+
}
4150
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
class ConfigurationItemTest {
22+
23+
@Test
24+
@DisplayName("Should create ConfiguratiotruenItem correctly")
25+
void shouldCreateConfigurationItemCorrectly() {
26+
ConfigurationItem item = new ConfigurationItem("application", "java-sdk", "0.0.1", Map.of(
27+
"creator", "devs"
28+
));
29+
30+
Assertions.assertEquals("application", item.getKey());
31+
Assertions.assertEquals("java-sdk", item.getValue());
32+
Assertions.assertEquals("0.0.1", item.getVersion());
33+
Assertions.assertEquals(1, item.getMetadata().size());
34+
Assertions.assertEquals("devs", item.getMetadata().get("creator"));
35+
}
36+
37+
@Test
38+
@DisplayName("Should create with immutable metadata")
39+
void shouldCreateWithImmutableMetadata() {
40+
ConfigurationItem item = new ConfigurationItem("application", "java-sdk", "0.0.1", Map.of(
41+
"creator", "devs"
42+
));
43+
44+
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
45+
item.getMetadata().put("language", "javascript");
46+
});
47+
}
48+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 com.google.api.Http;
17+
import io.dapr.client.DaprHttp;
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.DisplayName;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.net.URLEncoder;
23+
import java.nio.charset.StandardCharsets;
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
class HttpExtensionTest {
28+
29+
30+
@Test
31+
@DisplayName("Should encode query params correctly")
32+
void shouldEncodeQueryParamsCorrectly() {
33+
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET,
34+
Map.of("traceparent", List.of("00-4bf92f3577b34da6a3ce929d0e0e4733-00f067aa0ba902b7-01")), Map.of());
35+
36+
String encoded = httpExtension.encodeQueryString();
37+
38+
Assertions.assertEquals("traceparent=00-4bf92f3577b34da6a3ce929d0e0e4733-00f067aa0ba902b7-01", encoded);
39+
}
40+
41+
@Test
42+
@DisplayName("Should encode multiple values for same query param key")
43+
void shouldEncodeMultipleValuesForSameQueryParamKey() {
44+
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET,
45+
Map.of("category", List.of("books", "electronics")), Map.of());
46+
47+
String encoded = httpExtension.encodeQueryString();
48+
49+
Assertions.assertEquals("category=books&category=electronics", encoded);
50+
}
51+
52+
@Test
53+
@DisplayName("Should encode query param with spaces, accents, and special characters")
54+
void shouldEncodeQueryParamWithSpacesAndSpecialCharacters() {
55+
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET,
56+
Map.of("user name", List.of("John Doë & Co.")), Map.of());
57+
58+
String encoded = httpExtension.encodeQueryString();
59+
60+
Assertions.assertEquals("user+name=John+Do%C3%AB+%26+Co.", encoded);
61+
}
62+
63+
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
/*
2+
* Copyright 2023 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+
*/
113
package io.dapr.client.domain;
214

15+
import org.junit.jupiter.api.Assertions;
16+
import org.junit.jupiter.api.DisplayName;
317
import org.junit.jupiter.api.Test;
418

519
import java.util.ArrayList;
@@ -31,4 +45,16 @@ public void testSetStates(){
3145
assertEquals("test var args 1", request.getStates().get(0).getKey(), "Value incorrectly set");
3246
assertEquals("test var args 2", request.getStates().get(1).getKey(), "Value incorrectly set");
3347
}
34-
}
48+
49+
@Test
50+
@DisplayName("Should set states as null when the argument is null")
51+
void testSetStateWithNullParameter() {
52+
53+
SaveStateRequest request = new SaveStateRequest(STORE_NAME);
54+
request.setStates((List<State<?>>) null);
55+
List<State<?>> states = request.getStates();
56+
57+
// TODO: should not be null? It must be an empty collection?
58+
Assertions.assertNull(states);
59+
}
60+
}

0 commit comments

Comments
 (0)