Skip to content

Commit

Permalink
use Map.of
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
  • Loading branch information
artursouza committed Oct 2, 2024
1 parent 9dbe04f commit 20bf045
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 43 deletions.
14 changes: 6 additions & 8 deletions sdk-tests/src/test/java/io/dapr/it/DaprPorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -40,12 +38,12 @@ private DaprPorts(Integer appPort, Integer httpPort, Integer grpcPort) {
this.grpcPort = grpcPort;
this.httpPort = httpPort;
this.appPort = appPort;
this.overrides = Collections.unmodifiableMap(new HashMap<>(){{
put(Properties.GRPC_PORT, grpcPort.toString());
put(Properties.HTTP_PORT, httpPort.toString());
put(Properties.HTTP_ENDPOINT, "http://127.0.0.1:" + httpPort);
put(Properties.GRPC_ENDPOINT, "127.0.0.1:" + grpcPort);
}});
this.overrides = Map.of(
Properties.GRPC_PORT, grpcPort.toString(),
Properties.HTTP_PORT, httpPort.toString(),
Properties.HTTP_ENDPOINT, "http://127.0.0.1:" + httpPort,
Properties.GRPC_ENDPOINT, "127.0.0.1:" + grpcPort
);
}

public static DaprPorts build(boolean appPort, boolean httpPort, boolean grpcPort) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

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

import static io.dapr.it.Retry.callWithRetry;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -115,14 +116,14 @@ public void inputOutputBinding() throws Exception {

System.out.println("sending first message");
client.invokeBinding(
bidingName, "create", myClass, Collections.singletonMap("MyMetadata", "MyValue"), Void.class).block();
bidingName, "create", myClass, Map.of("MyMetadata", "MyValue"), Void.class).block();

// This is an example of sending a plain string. The input binding will receive
// cat
final String m = "cat";
System.out.println("sending " + m);
client.invokeBinding(
bidingName, "create", m, Collections.singletonMap("MyMetadata", "MyValue"), Void.class).block();
bidingName, "create", m, Map.of("MyMetadata", "MyValue"), Void.class).block();

// Metadata is not used by Kafka component, so it is not possible to validate.
callWithRetry(() -> {
Expand Down
5 changes: 3 additions & 2 deletions sdk-tests/src/test/java/io/dapr/it/pubsub/http/PubSubIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
Expand Down Expand Up @@ -534,7 +535,7 @@ public void testPubSubTTLMetadata() throws Exception {
PUBSUB_NAME,
TTL_TOPIC_NAME,
message,
Collections.singletonMap(Metadata.TTL_IN_SECONDS, "1")).block();
Map.of(Metadata.TTL_IN_SECONDS, "1")).block();
System.out.println(String.format("Published message: '%s' to topic '%s' pubsub_name '%s'", message, TOPIC_NAME, PUBSUB_NAME));
}
}
Expand Down Expand Up @@ -652,7 +653,7 @@ public void testLongValues() throws Exception {
PUBSUB_NAME,
LONG_TOPIC_NAME,
value,
Collections.singletonMap(Metadata.TTL_IN_SECONDS, "30")).block();
Map.of(Metadata.TTL_IN_SECONDS, "30")).block();

try {
Thread.sleep((long) (1000 * Math.random()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class DaprKeyValueRepositoryIT {
"host=postgres-repository user=postgres password=password port=5432 connect_timeout=10 database=dapr_db_repository";
private static final Map<String, String> STATE_STORE_PROPERTIES = createStateStoreProperties();

private static final Map<String, String> BINDING_PROPERTIES = Collections.singletonMap("connectionString", CONNECTION_STRING);
private static final Map<String, String> BINDING_PROPERTIES = Map.of("connectionString", CONNECTION_STRING);

private static final Network DAPR_NETWORK = Network.newNetwork();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class MySQLDaprKeyValueTemplateIT {
private static final String BINDING_DSN = "mysql:password@tcp(mysql:3306)/dapr_db";
private static final Map<String, String> STATE_STORE_PROPERTIES = createStateStoreProperties();

private static final Map<String, String> BINDING_PROPERTIES = Collections.singletonMap("url", BINDING_DSN);
private static final Map<String, String> BINDING_PROPERTIES = Map.of("url", BINDING_DSN);

private static final Network DAPR_NETWORK = Network.newNetwork();

Expand Down Expand Up @@ -114,7 +114,7 @@ private static Map<String, String> createStateStoreProperties() {
*/
@AfterEach
public void tearDown() {
var meta = Collections.singletonMap("sql", "delete from state");
var meta = Map.of("sql", "delete from state");

daprClient.invokeBinding(BINDING_NAME, "exec", null, meta).block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class PostgreSQLDaprKeyValueTemplateIT {
"host=postgres user=postgres password=password port=5432 connect_timeout=10 database=dapr_db";
private static final Map<String, String> STATE_STORE_PROPERTIES = createStateStoreProperties();

private static final Map<String, String> BINDING_PROPERTIES = Collections.singletonMap("connectionString", CONNECTION_STRING);
private static final Map<String, String> BINDING_PROPERTIES = Map.of("connectionString", CONNECTION_STRING);

private static final Network DAPR_NETWORK = Network.newNetwork();

Expand Down Expand Up @@ -96,7 +96,7 @@ private static Map<String, String> createStateStoreProperties() {

@BeforeEach
public void setUp() {
var meta = Collections.singletonMap("sql", "delete from state");
var meta = Map.of("sql", "delete from state");

daprClient.invokeBinding(BINDING_NAME, "exec", null, meta).block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static java.util.Collections.singletonMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -149,7 +148,7 @@ public void testPubSub() throws Exception {

try (DaprClient client = (builder).build()) {
String message = "message content";
Map<String, String> metadata = singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS);
Map<String, String> metadata = Map.of(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS);
client.publishEvent(PUBSUB_NAME, PUBSUB_TOPIC_NAME, message, metadata).block();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -59,7 +60,7 @@ public class DaprWorkflowsIT {
.withAppName("workflow-dapr-app")
.withNetwork(DAPR_NETWORK)
.withComponent(new Component("kvstore", "state.in-memory", "v1",
Collections.singletonMap("actorStateStore", "true")))
Map.of("actorStateStore", "true")))
.withComponent(new Component("pubsub", "pubsub.in-memory", "v1", Collections.emptyMap()))
.withDaprLogLevel(DaprLogLevel.DEBUG)
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/main/java/io/dapr/client/DaprHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public class DaprHttp implements AutoCloseable {
/**
* Context entries allowed to be in HTTP Headers.
*/
private static final Set<String> ALLOWED_CONTEXT_IN_HEADERS =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("grpc-trace-bin", "traceparent", "tracestate")));
private static final Set<String> ALLOWED_CONTEXT_IN_HEADERS = Set.of("grpc-trace-bin", "traceparent", "tracestate");

/**
* Object mapper to parse DaprError with or without details.
Expand Down
5 changes: 2 additions & 3 deletions sdk/src/main/java/io/dapr/client/domain/BulkPublishEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public BulkPublishEntry(String entryId, T event, String contentType) {
this.entryId = entryId;
this.event = event;
this.contentType = contentType;
this.metadata = Collections.unmodifiableMap(new HashMap<>());
this.metadata = Map.of();
}

/**
Expand All @@ -70,8 +70,7 @@ public BulkPublishEntry(String entryId, T event, String contentType, Map<String,
this.entryId = entryId;
this.event = event;
this.contentType = contentType;
this.metadata = metadata == null ? Collections.unmodifiableMap(new HashMap<>()) :
Collections.unmodifiableMap(metadata);
this.metadata = metadata == null ? Map.of() : Collections.unmodifiableMap(metadata);
}

public String getEntryId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ public BulkPublishRequest(String pubsubName, String topic, List<BulkPublishEntry
Map<String, String> metadata) {
this.pubsubName = pubsubName;
this.topic = topic;
this.entries = entries == null ? Collections.unmodifiableList(new ArrayList<>()) :
Collections.unmodifiableList(entries);
this.metadata = metadata == null ? Collections.unmodifiableMap(new HashMap<>()) :
Collections.unmodifiableMap(metadata);
this.entries = entries == null ? List.of() : Collections.unmodifiableList(entries);
this.metadata = metadata == null ? Map.of() : Collections.unmodifiableMap(metadata);
}

public String getPubsubName() {
Expand Down
26 changes: 12 additions & 14 deletions sdk/src/main/java/io/dapr/exceptions/DaprErrorDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ public class DaprErrorDetails {
static final DaprErrorDetails EMPTY_INSTANCE = new DaprErrorDetails((Status) null);

private static final Map<Class<? extends Message>, ErrorDetailType> SUPPORTED_ERROR_TYPES =
Collections.unmodifiableMap(new HashMap<>() {
{
put(com.google.rpc.ErrorInfo.class, ErrorDetailType.ERROR_INFO);
put(com.google.rpc.RetryInfo.class, ErrorDetailType.RETRY_INFO);
put(com.google.rpc.DebugInfo.class, ErrorDetailType.DEBUG_INFO);
put(com.google.rpc.QuotaFailure.class, ErrorDetailType.QUOTA_FAILURE);
put(com.google.rpc.PreconditionFailure.class, ErrorDetailType.PRECONDITION_FAILURE);
put(com.google.rpc.BadRequest.class, ErrorDetailType.BAD_REQUEST);
put(com.google.rpc.RequestInfo.class, ErrorDetailType.REQUEST_INFO);
put(com.google.rpc.ResourceInfo.class, ErrorDetailType.RESOURCE_INFO);
put(com.google.rpc.Help.class, ErrorDetailType.HELP);
put(com.google.rpc.LocalizedMessage.class, ErrorDetailType.LOCALIZED_MESSAGE);
}
});
Map.of(
com.google.rpc.ErrorInfo.class, ErrorDetailType.ERROR_INFO,
com.google.rpc.RetryInfo.class, ErrorDetailType.RETRY_INFO,
com.google.rpc.DebugInfo.class, ErrorDetailType.DEBUG_INFO,
com.google.rpc.QuotaFailure.class, ErrorDetailType.QUOTA_FAILURE,
com.google.rpc.PreconditionFailure.class, ErrorDetailType.PRECONDITION_FAILURE,
com.google.rpc.BadRequest.class, ErrorDetailType.BAD_REQUEST,
com.google.rpc.RequestInfo.class, ErrorDetailType.REQUEST_INFO,
com.google.rpc.ResourceInfo.class, ErrorDetailType.RESOURCE_INFO,
com.google.rpc.Help.class, ErrorDetailType.HELP,
com.google.rpc.LocalizedMessage.class, ErrorDetailType.LOCALIZED_MESSAGE
);

private static final Map<String, Class<? extends Message>> ERROR_TYPES_FQN_REVERSE_LOOKUP =
SUPPORTED_ERROR_TYPES.keySet().stream().collect(Collectors.toMap(
Expand Down

0 comments on commit 20bf045

Please sign in to comment.