Skip to content

Add coverage for some properties #1297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 16, 2025
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
@@ -1,5 +1,5 @@
/*
* Copyright 2024 The Dapr Authors
* 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
Expand All @@ -14,8 +14,9 @@
package io.dapr.spring.boot.autoconfigure.client;

import org.assertj.core.api.SoftAssertions;
import org.junit.Test;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

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

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
softAssertions.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
softAssertions.assertThat(properties.getHttpPort()).isEqualTo(3500);
softAssertions.assertThat(properties.getGrpcPort()).isEqualTo(50001);
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
softly.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
softly.assertThat(properties.getHttpPort()).isEqualTo(3500);
softly.assertThat(properties.getGrpcPort()).isEqualTo(50001);
});
}

Expand Down Expand Up @@ -71,11 +72,11 @@ public void shouldMapDaprClientProperties() {
"dapr.client.grpc-port=50001"
).run(context -> {
DaprClientProperties properties = context.getBean(DaprClientProperties.class);
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
softAssertions.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
softAssertions.assertThat(properties.getHttpPort()).isEqualTo(3500);
softAssertions.assertThat(properties.getGrpcPort()).isEqualTo(50001);
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
softly.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
softly.assertThat(properties.getHttpPort()).isEqualTo(3500);
softly.assertThat(properties.getGrpcPort()).isEqualTo(50001);
});

});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.spring.boot.autoconfigure.pubsub;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

public class DaprPubSubPropertiesTest {

final ApplicationContextRunner runner = new ApplicationContextRunner()
.withUserConfiguration(EnableDaprPubSubProperties.class);


@Test
@DisplayName("Should configure properties with setters")
void shouldSetProperties() {
DaprPubSubProperties properties = new DaprPubSubProperties();
properties.setName("pubsub");
properties.setObservationEnabled(false);

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(properties.getName()).isEqualTo("pubsub");
softAssertions.assertThat(properties.isObservationEnabled()).isEqualTo(false);
});
}

@Test
@DisplayName("Should map DaprPubSubProperties correctly")
void shouldMapDaprPubSubPropertiesCorrectly() {
runner.withPropertyValues(
"dapr.pubsub.name=pubsub",
"dapr.pubsub.observation-enabled=true"
).run(context -> {
DaprPubSubProperties properties = context.getBean(DaprPubSubProperties.class);

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(properties.getName()).isEqualTo("pubsub");
softAssertions.assertThat(properties.isObservationEnabled()).isEqualTo(true);
});
});
}

@EnableConfigurationProperties(DaprPubSubProperties.class)
static class EnableDaprPubSubProperties {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 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.spring.boot.autoconfigure.statestore;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

public class DaprStateStorePropertiesTest {


final ApplicationContextRunner runner = new ApplicationContextRunner()
.withUserConfiguration(EnableDaprStateStoreProperties.class);

@Test
@DisplayName("Should create DaprStateStoreProperties via constructor")
void shouldSetDaprStateStorePropertiesCorrectly() {
DaprStateStoreProperties properties = new DaprStateStoreProperties();
properties.setBinding("binding");
properties.setName("name");

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(properties.getName()).isEqualTo("name");
softAssertions.assertThat(properties.getBinding()).isEqualTo("binding");
});
}

@Test
@DisplayName("Should map Dapr state store properties correctly")
void shouldMapDaprStateStoreProperties() {
runner.withPropertyValues(
"dapr.statestore.name=name",
"dapr.statestore.binding=binding"
).run(context -> {
DaprStateStoreProperties properties = context.getBean(DaprStateStoreProperties.class);

SoftAssertions.assertSoftly(softly -> {
softly.assertThat(properties.getBinding()).isEqualTo("binding");
softly.assertThat(properties.getName()).isEqualTo("name");
});
});

}

@EnableConfigurationProperties(DaprStateStoreProperties.class)
static class EnableDaprStateStoreProperties {

}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<testcontainers.version>1.20.5</testcontainers.version>
<springboot.version>3.4.3</springboot.version>
<nexus-staging-maven-plugin.version>1.7.0</nexus-staging-maven-plugin.version>
<assertj.version>3.27.3</assertj.version>
</properties>

<distributionManagement>
Expand Down
5 changes: 5 additions & 0 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.client.domain;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Map;

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

class BulkPublishEntryTest {

@Test
@DisplayName("Should create an empty metadata when metadata argument is null")
public void shouldCreateWithEmptyMetadataWhenMetadataIsNull() {
BulkPublishEntry<String> entry = new BulkPublishEntry<>(
"entryId", "event", "contentType", null
);
assertThat(entry.getMetadata()).isEmpty();
}

@Test
@DisplayName("Should create with non null metadata")
public void shouldCreateWithMetadata() {
BulkPublishEntry<String> entry = new BulkPublishEntry<>(
"entryId", "event", "application/json", Map.of(
"repo", "dapr/java-sdk"
));
assertThat(entry.getMetadata()).hasSize(1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
* See the License for the specific language governing permissions and
limitations under the License.
*/

package io.dapr.client.domain;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;

public class BulkPublishRequestTest {
Expand All @@ -36,6 +37,15 @@ public void testSetMetadata() {
request.setMetadata(metadata);
Map<String, String> initial = request.getMetadata();
request.setMetadata(metadata);
Assertions.assertNotSame( request.getMetadata(), initial, "Should not be same map");

assertThat(request.getMetadata()).isNotSameAs(initial);
}

@Test
@DisplayName("Should create a BulkPublishRequest with empty list when entries is null")
public void shouldCreateWithEmptyListWhenEntriesIsNull() {
BulkPublishRequest<String> request = new BulkPublishRequest<>("testPubsub", "testTopic", null);
List<BulkPublishEntry<String>> entries = request.getEntries();
assertThat(entries).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.client.domain;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class ConfigurationItemTest {

@Test
@DisplayName("Should create ConfigurationItem correctly")
void shouldCreateConfigurationItemCorrectly() {
ConfigurationItem item = new ConfigurationItem("application", "java-sdk", "0.0.1", Map.of(
"creator", "devs"
));

assertThat(item.getKey()).isEqualTo("application");
assertThat(item.getValue()).isEqualTo("java-sdk");
assertThat(item.getVersion()).isEqualTo("0.0.1");
assertThat(item.getMetadata()).hasSize(1);
assertThat(item.getMetadata()).hasEntrySatisfying("creator", value -> {
assertThat(value).isEqualTo("devs");
});
}

@Test
@DisplayName("Should create with immutable metadata")
void shouldCreateWithImmutableMetadata() {
ConfigurationItem item = new ConfigurationItem("application", "java-sdk", "0.0.1", Map.of(
"creator", "devs"
));
assertThatThrownBy(() -> item.getMetadata().put("language", "javascript"));
}
}
60 changes: 60 additions & 0 deletions sdk/src/test/java/io/dapr/client/domain/HttpExtensionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.client.domain;

import io.dapr.client.DaprHttp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

class HttpExtensionTest {


@Test
@DisplayName("Should encode query params correctly")
void shouldEncodeQueryParamsCorrectly() {
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET,
Map.of("traceparent", List.of("00-4bf92f3577b34da6a3ce929d0e0e4733-00f067aa0ba902b7-01")), Map.of());

String encoded = httpExtension.encodeQueryString();

Assertions.assertEquals("traceparent=00-4bf92f3577b34da6a3ce929d0e0e4733-00f067aa0ba902b7-01", encoded);
}

@Test
@DisplayName("Should encode multiple values for same query param key")
void shouldEncodeMultipleValuesForSameQueryParamKey() {
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET,
Map.of("category", List.of("books", "electronics")), Map.of());

String encoded = httpExtension.encodeQueryString();

Assertions.assertEquals("category=books&category=electronics", encoded);
}

@Test
@DisplayName("Should encode query param with spaces, accents, and special characters")
void shouldEncodeQueryParamWithSpacesAndSpecialCharacters() {
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET,
Map.of("user name", List.of("John Doë & Co.")), Map.of());

String encoded = httpExtension.encodeQueryString();

Assertions.assertEquals("user+name=John+Do%C3%AB+%26+Co.", encoded);
}

}
Loading
Loading