From ee20ec4e2872e8b59a906683e6365059fea4b1a3 Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Sat, 9 May 2020 07:59:09 -0700 Subject: [PATCH] MongoDbBindingsPropertiesProcessor Signed-off-by: Ben Hale --- .../cnb/boot/MongoCnbBindingProcessor.java | 44 ------------- .../boot/MongoCnbBindingProcessorTests.java | 62 ------------------- .../MongoDbBindingsPropertiesProcessor.java | 42 +++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- .../BindingsEnvironmentPostProcessorTest.java | 2 +- ...ongoDbBindingsPropertiesProcessorTest.java | 49 +++++++++++++++ 6 files changed, 94 insertions(+), 108 deletions(-) delete mode 100644 cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessor.java delete mode 100644 cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessorTests.java create mode 100644 cnb-bindings/src/main/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessor.java create mode 100644 cnb-bindings/src/test/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessorTest.java diff --git a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessor.java b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessor.java deleted file mode 100644 index 1baae33..0000000 --- a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessor.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2019 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.cnb.boot; - -import java.util.Map; - -import org.springframework.cloud.cnb.Binding; - -public class MongoCnbBindingProcessor implements CnbBindingProcessor { - private static final String MONGO_KIND = "mongodb"; - - @Override - public boolean accept(Binding binding) { - return binding.getKind().equals(MONGO_KIND); - } - - @Override - public void process(Binding binding, Map properties) { - properties.put("spring.redis.mongodb.uri", binding.getSecret().get("uri")); - - // TODO: build uri from discrete fields? - } - - @Override - public CnbBindingProcessorProperties getProperties() { - return CnbBindingProcessorProperties.builder() - .propertyPrefixes("spring.data.mongodb") - .serviceName("MongoDB") - .build(); - } -} diff --git a/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessorTests.java b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessorTests.java deleted file mode 100644 index 9e158ba..0000000 --- a/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessorTests.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2019 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.cnb.boot; - - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; - -import org.springframework.cloud.cnb.Binding; - - -import static org.assertj.core.api.Assertions.assertThat; - -public class MongoCnbBindingProcessorTests { - - @Test - public void acceptIfMongoKind() { - MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor(); - Map bindingMetadata = new HashMap(); - bindingMetadata.put("kind", "mongodb"); - Binding binding = new Binding(bindingMetadata, new HashMap()); - assertThat(bindingProcessor.accept(binding)).isTrue(); - } - - @Test - public void rejectIfNotMongoKind() { - MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor(); - Map bindingMetadata = new HashMap(); - bindingMetadata.put("kind", "mysql"); - Binding binding = new Binding(bindingMetadata, new HashMap()); - assertThat(bindingProcessor.accept(binding)).isFalse(); - } - - @Test - public void processDataSourcePropertiesTest() { - MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor(); - Map bindingMetadata = new HashMap(); - bindingMetadata.put("kind", "mongodb"); - Map bindingSecret = new HashMap(); - bindingSecret.put("uri", "some-uri"); - Binding binding = new Binding(bindingMetadata, bindingSecret); - Map properties = new HashMap(); - bindingProcessor.process(binding, properties); - assertThat(properties.get("spring.redis.mongodb.uri")).isEqualTo("some-uri"); - } - -} diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessor.java new file mode 100644 index 0000000..209dbc1 --- /dev/null +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessor.java @@ -0,0 +1,42 @@ +/* + * 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; + +import org.springframework.lang.NonNull; + +import java.util.Map; + +/** + * An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}. + */ +public final class MongoDbBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "mongodb"; + + @Override + public void process(@NonNull Bindings bindings, @NonNull Map properties) { + bindings.filterBindings(KIND).forEach(binding -> { + Map secret = binding.getSecret(); + + properties.put("spring.mongodb.uri", secret.get("uri")); + }); + } + +} diff --git a/cnb-bindings/src/main/resources/META-INF/spring.factories b/cnb-bindings/src/main/resources/META-INF/spring.factories index d06d7a4..5220940 100644 --- a/cnb-bindings/src/main/resources/META-INF/spring.factories +++ b/cnb-bindings/src/main/resources/META-INF/spring.factories @@ -2,4 +2,5 @@ org.springframework.boot.env.EnvironmentPostProcessor=\ org.springframework.cloud.bindings.BindingsEnvironmentPostProcessor # Included implementations org.springframework.cloud.bindings.BindingsPropertiesProcessor=\ - org.springframework.cloud.bindings.CassandraBindingsPropertiesProcessor + org.springframework.cloud.bindings.CassandraBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.MongoDbBindingsPropertiesProcessor diff --git a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/BindingsEnvironmentPostProcessorTest.java b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/BindingsEnvironmentPostProcessorTest.java index fa84261..1d1701b 100644 --- a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/BindingsEnvironmentPostProcessorTest.java +++ b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/BindingsEnvironmentPostProcessorTest.java @@ -117,7 +117,7 @@ final class BindingsEnvironmentPostProcessorTest { @Test @DisplayName("included implementations are registered") void includedImplementations() { - assertThat(new BindingsEnvironmentPostProcessor().processors).hasSize(1); + assertThat(new BindingsEnvironmentPostProcessor().processors).hasSize(2); } } diff --git a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessorTest.java b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..3bf3c69 --- /dev/null +++ b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessorTest.java @@ -0,0 +1,49 @@ +/* + * 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; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +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.MongoDbBindingsPropertiesProcessor.KIND; + +@DisplayName("MongoDB BindingsPropertiesProcessor") +final class MongoDbBindingsPropertiesProcessorTest { + + @Test + @DisplayName("contributes properties") + void test() { + HashMap properties = new HashMap<>(); + + new MongoDbBindingsPropertiesProcessor().process(new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("uri", "test-uri") + ) + ), properties); + + assertThat(properties) + .containsEntry("spring.mongodb.uri", "test-uri"); + } + +}