From e15d0a1b61786c789293b5323ce9e94059328959 Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Mon, 11 May 2020 16:57:02 -0700 Subject: [PATCH] Neo4J This change contributes Neo4J support. [resolves #20] Signed-off-by: Ben Hale --- README.md | 10 +++ .../Neo4JBindingsPropertiesProcessor.java | 51 ++++++++++++++ src/main/resources/META-INF/spring.factories | 1 + ...gSpecificEnvironmentPostProcessorTest.java | 2 +- .../Neo4JBindingsPropertiesProcessorTest.java | 70 +++++++++++++++++++ 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/springframework/cloud/bindings/boot/Neo4JBindingsPropertiesProcessor.java create mode 100644 src/test/java/org/springframework/cloud/bindings/boot/Neo4JBindingsPropertiesProcessorTest.java diff --git a/README.md b/README.md index be7de9e..07eb4ae 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,16 @@ Disable Property: `org.springframework.cloud.bindings.boot.mysql.enable` | `spring.datasource.url` | `jdbc:mysql://{host}:{port}/{database}` | `spring.datasource.username` | `{username}` +### Neo4J +Kind: `Neo4J` +Disable Property: `org.springframework.cloud.bindings.boot.neo4j.enable` + +| Property | Value (`{secret}`) +| -------- | ------------------ +| `spring.data.neo4j.password` | `{password}` +| `spring.data.neo4j.uri` | `{uri}` +| `spring.data.neo4j.username` | `{username}` + ### Oracle RDBMS Kind: `Oracle` Disable Property: `org.springframework.cloud.bindings.boot.oracle.enable` diff --git a/src/main/java/org/springframework/cloud/bindings/boot/Neo4JBindingsPropertiesProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/Neo4JBindingsPropertiesProcessor.java new file mode 100644 index 0000000..72b8750 --- /dev/null +++ b/src/main/java/org/springframework/cloud/bindings/boot/Neo4JBindingsPropertiesProcessor.java @@ -0,0 +1,51 @@ +/* + * 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.springframework.cloud.bindings.Binding; +import org.springframework.cloud.bindings.Bindings; +import org.springframework.core.env.Environment; + +import java.util.Map; + +import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled; + +/** + * An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}. + */ +final class Neo4JBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "Neo4J"; + + @Override + public void process(Environment environment, Bindings bindings, Map properties) { + if (!isKindEnabled(environment, KIND)) { + return; + } + + bindings.filterBindings(KIND).forEach(binding -> { + MapMapper map = new MapMapper(binding.getSecret(), properties); + + map.from("password").to("spring.data.neo4j.password"); + map.from("uri").to("spring.data.neo4j.uri"); + map.from("username").to("spring.data.neo4j.username"); + }); + } + +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index 116d0c2..21d688d 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -9,6 +9,7 @@ org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor=\ org.springframework.cloud.bindings.boot.ElasticsearchBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.MongoDbBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.MySqlBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.boot.Neo4JBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.OracleBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.PostgreSqlBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.RedisBindingsPropertiesProcessor, \ diff --git a/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java index d87106b..049aec5 100644 --- a/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java +++ b/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java @@ -98,7 +98,7 @@ final class BindingSpecificEnvironmentPostProcessorTest { @Test @DisplayName("included implementations are registered") void includedImplementations() { - assertThat(new BindingSpecificEnvironmentPostProcessor().processors).hasSize(10); + assertThat(new BindingSpecificEnvironmentPostProcessor().processors).hasSize(11); } } diff --git a/src/test/java/org/springframework/cloud/bindings/boot/Neo4JBindingsPropertiesProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/Neo4JBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..e1cc542 --- /dev/null +++ b/src/test/java/org/springframework/cloud/bindings/boot/Neo4JBindingsPropertiesProcessorTest.java @@ -0,0 +1,70 @@ +/* + * 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 org.springframework.cloud.bindings.Binding; +import org.springframework.cloud.bindings.Bindings; +import org.springframework.cloud.bindings.FluentMap; +import org.springframework.mock.env.MockEnvironment; + +import java.nio.file.Paths; +import java.util.Collections; +import java.util.HashMap; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.bindings.boot.Neo4JBindingsPropertiesProcessor.KIND; + +@DisplayName("Neo4J BindingsPropertiesProcessor") +final class Neo4JBindingsPropertiesProcessorTest { + + private final Bindings bindings = new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("password", "test-password") + .withEntry("uri", "test-uri") + .withEntry("username", "test-username") + ) + ); + + private final MockEnvironment environment = new MockEnvironment(); + + private final HashMap properties = new HashMap<>(); + + @Test + @DisplayName("contributes properties") + void test() { + new Neo4JBindingsPropertiesProcessor().process(environment, bindings, properties); + assertThat(properties) + .containsEntry("spring.data.neo4j.password", "test-password") + .containsEntry("spring.data.neo4j.uri", "test-uri") + .containsEntry("spring.data.neo4j.username", "test-username"); + } + + @Test + @DisplayName("can be disabled") + void disabled() { + environment.setProperty("org.springframework.cloud.bindings.boot.neo4j.enable", "false"); + + new Neo4JBindingsPropertiesProcessor().process(environment, bindings, properties); + + assertThat(properties).isEmpty(); + } + +}