diff --git a/README.md b/README.md index dfab5ab..ff86c6d 100644 --- a/README.md +++ b/README.md @@ -38,9 +38,13 @@ Disable Property: `org.springframework.cloud.bindings.boot.cassandra.enable` | Property | Value | -------- | ----- -| `spring.data.cassandra.contact-points` | `{secret.node_ips}` +| `spring.data.cassandra.cluster-name` | `{secret.cluster-name}` +| `spring.data.cassandra.compression` | `{secret.compression}` +| `spring.data.cassandra.contact-points` | `{secret.contact-points}` +| `spring.data.cassandra.keyspace-name` | `{secret.keyspace-name}` | `spring.data.cassandra.password` | `{secret.password}` | `spring.data.cassandra.port` | `{secret.port}` +| `spring.data.cassandra.ssl` | `{secret.ssl}` | `spring.data.cassandra.username` | `{secret.username}` ### DB2 RDBMS diff --git a/src/main/java/org/springframework/cloud/bindings/boot/CassandraBindingsPropertiesProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/CassandraBindingsPropertiesProcessor.java index 0c3cf45..bbfe3d5 100644 --- a/src/main/java/org/springframework/cloud/bindings/boot/CassandraBindingsPropertiesProcessor.java +++ b/src/main/java/org/springframework/cloud/bindings/boot/CassandraBindingsPropertiesProcessor.java @@ -22,6 +22,7 @@ import org.springframework.cloud.bindings.Bindings; import java.util.Map; import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled; +import static org.springframework.cloud.bindings.boot.PutIfPresent.put; /** * An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}. @@ -42,10 +43,14 @@ public final class CassandraBindingsPropertiesProcessor implements BindingsPrope bindings.filterBindings(KIND).forEach(binding -> { Map secret = binding.getSecret(); - properties.put("spring.data.cassandra.contact-points", secret.get("node_ips")); - properties.put("spring.data.cassandra.password", secret.get("password")); - properties.put("spring.data.cassandra.port", secret.get("port")); - properties.put("spring.data.cassandra.username", secret.get("username")); + put(properties, "spring.data.cassandra.cluster-name").ifPresent(secret, "cluster-name"); + put(properties, "spring.data.cassandra.compression").ifPresent(secret, "compression"); + put(properties, "spring.data.cassandra.contact-points").ifPresent(secret, "contact-points"); + put(properties, "spring.data.cassandra.keyspace-name").ifPresent(secret, "keyspace-name"); + put(properties, "spring.data.cassandra.password").ifPresent(secret, "password"); + put(properties, "spring.data.cassandra.port").ifPresent(secret, "port"); + put(properties, "spring.data.cassandra.ssl").ifPresent(secret, "ssl"); + put(properties, "spring.data.cassandra.username").ifPresent(secret, "username"); }); } diff --git a/src/main/java/org/springframework/cloud/bindings/boot/PutIfPresent.java b/src/main/java/org/springframework/cloud/bindings/boot/PutIfPresent.java new file mode 100644 index 0000000..df24889 --- /dev/null +++ b/src/main/java/org/springframework/cloud/bindings/boot/PutIfPresent.java @@ -0,0 +1,44 @@ +/* + * Copyright 2020 the original author or 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 org.springframework.cloud.bindings.boot; + +import java.util.Map; + +final class PutIfPresent { + + private final Map destination; + + private final String key; + + PutIfPresent(Map destination, String key) { + this.destination = destination; + this.key = key; + } + + static PutIfPresent put(Map destination, String key) { + return new PutIfPresent(destination, key); + } + + void ifPresent(Map source, String key) { + if (!source.containsKey(key)) { + return; + } + + destination.put(this.key, source.get(key)); + } + +} diff --git a/src/test/java/org/springframework/cloud/bindings/boot/CassandraBindingsPropertiesProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/CassandraBindingsPropertiesProcessorTest.java index 975ee2b..8bd5840 100644 --- a/src/test/java/org/springframework/cloud/bindings/boot/CassandraBindingsPropertiesProcessorTest.java +++ b/src/test/java/org/springframework/cloud/bindings/boot/CassandraBindingsPropertiesProcessorTest.java @@ -37,9 +37,13 @@ final class CassandraBindingsPropertiesProcessorTest { new Binding("test-name", Paths.get("test-path"), Collections.singletonMap("kind", KIND), new FluentMap() - .withEntry("node_ips", "test-node-ips") + .withEntry("cluster-name", "test-cluster-name") + .withEntry("compression", "test-compression") + .withEntry("contact-points", "test-contact-points") + .withEntry("keyspace-name", "test-keyspace-name") .withEntry("password", "test-password") .withEntry("port", "test-port") + .withEntry("ssl", "test-ssl") .withEntry("username", "test-username") ) ); @@ -51,9 +55,13 @@ final class CassandraBindingsPropertiesProcessorTest { void test() { new CassandraBindingsPropertiesProcessor().process(bindings, properties); assertThat(properties) - .containsEntry("spring.data.cassandra.contact-points", "test-node-ips") + .containsEntry("spring.data.cassandra.cluster-name", "test-cluster-name") + .containsEntry("spring.data.cassandra.compression", "test-compression") + .containsEntry("spring.data.cassandra.contact-points", "test-contact-points") + .containsEntry("spring.data.cassandra.keyspace-name", "test-keyspace-name") .containsEntry("spring.data.cassandra.password", "test-password") .containsEntry("spring.data.cassandra.port", "test-port") + .containsEntry("spring.data.cassandra.ssl", "test-ssl") .containsEntry("spring.data.cassandra.username", "test-username"); } diff --git a/src/test/java/org/springframework/cloud/bindings/boot/PutIfPresentTest.java b/src/test/java/org/springframework/cloud/bindings/boot/PutIfPresentTest.java new file mode 100644 index 0000000..88cdecb --- /dev/null +++ b/src/test/java/org/springframework/cloud/bindings/boot/PutIfPresentTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2020 the original author or 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 org.springframework.cloud.bindings.boot; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.bindings.boot.PutIfPresent.put; + +@DisplayName("Put if present") +final class PutIfPresentTest { + + @Test + @DisplayName("puts if present") + void present() { + Map source = Collections.singletonMap("test-source-key", "test-source-value"); + Map destination = new HashMap<>(); + + put(destination, "test-destination-key").ifPresent(source, "test-source-key"); + + assertThat(destination).containsEntry("test-destination-key", "test-source-value"); + } + + @Test + @DisplayName("does not put if not present") + void notPresent() { + Map source = Collections.emptyMap(); + Map destination = new HashMap<>(); + + put(destination, "test-destination-key").ifPresent(source, "test-source-key"); + + assertThat(destination).doesNotContainKey("test-destination-key"); + } + +}