diff --git a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessor.java b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessor.java deleted file mode 100644 index 616a813..0000000 --- a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessor.java +++ /dev/null @@ -1,47 +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 RedisCnbBindingProcessor implements CnbBindingProcessor { - - public static final String REDIS_KIND = "redis"; - - @Override - public boolean accept(Binding binding) { - return binding.getKind().equals(REDIS_KIND); - } - - @Override - public void process(Binding binding, Map properties) { - properties.put("spring.redis.host", binding.getSecret().get("hostname")); //TODO: also support "host" - properties.put("spring.redis.port", binding.getSecret().get("port")); //TODO: handle missing - properties.put("spring.redis.password", binding.getSecret().get("password")); //TODO: handle missing - - // TODO: spring.redis.ssl - } - - @Override - public CnbBindingProcessorProperties getProperties() { - return CnbBindingProcessorProperties.builder() - .propertyPrefixes("spring.redis") - .build(); - } -} diff --git a/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessorTests.java b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessorTests.java deleted file mode 100644 index 9f089ca..0000000 --- a/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessorTests.java +++ /dev/null @@ -1,68 +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 RedisCnbBindingProcessorTests { - - @Test - public void acceptIfRedisKind() { - RedisCnbBindingProcessor bindingProcessor = new RedisCnbBindingProcessor(); - Map bindingMetadata = new HashMap(); - bindingMetadata.put("kind", "redis"); - Binding binding = new Binding(bindingMetadata, new HashMap()); - assertThat(bindingProcessor.accept(binding)).isTrue(); - } - - @Test - public void rejectIfNotRedisKind() { - RedisCnbBindingProcessor bindingProcessor = new RedisCnbBindingProcessor(); - Map bindingMetadata = new HashMap(); - bindingMetadata.put("kind", "mysql"); - Binding binding = new Binding(bindingMetadata, new HashMap()); - assertThat(bindingProcessor.accept(binding)).isFalse(); - } - - @Test - public void processDataSourcePropertiesTest() { - RedisCnbBindingProcessor bindingProcessor = new RedisCnbBindingProcessor(); - Map bindingMetadata = new HashMap(); - bindingMetadata.put("kind", "redis"); - Map bindingSecret = new HashMap(); - bindingSecret.put("hostname", "10.0.4.35"); - bindingSecret.put("port", "6379"); - bindingSecret.put("password", "some-password"); - Binding binding = new Binding(bindingMetadata, bindingSecret); - Map properties = new HashMap(); - bindingProcessor.process(binding, properties); - assertThat(properties.get("spring.redis.host")).isEqualTo("10.0.4.35"); - assertThat(properties.get("spring.redis.port")).isEqualTo("6379"); - assertThat(properties.get("spring.redis.password")).isEqualTo("some-password"); - } - - //TODO: TLS for redis - -} diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessor.java new file mode 100644 index 0000000..b303e8f --- /dev/null +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessor.java @@ -0,0 +1,45 @@ +/* + * 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.jetbrains.annotations.NotNull; +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 RedisBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "redis"; + + @Override + public void process(@NonNull Bindings bindings, @NotNull Map properties) { + bindings.filterBindings(KIND).forEach(binding -> { + Map secret = binding.getSecret(); + + properties.put("spring.redis.host", secret.get("hostname")); + properties.put("spring.redis.password", secret.get("password")); + properties.put("spring.redis.port", secret.get("port")); + }); + } + +} diff --git a/cnb-bindings/src/main/resources/META-INF/spring.factories b/cnb-bindings/src/main/resources/META-INF/spring.factories index 5220940..31b34d1 100644 --- a/cnb-bindings/src/main/resources/META-INF/spring.factories +++ b/cnb-bindings/src/main/resources/META-INF/spring.factories @@ -3,4 +3,5 @@ org.springframework.boot.env.EnvironmentPostProcessor=\ # Included implementations org.springframework.cloud.bindings.BindingsPropertiesProcessor=\ org.springframework.cloud.bindings.CassandraBindingsPropertiesProcessor, \ - org.springframework.cloud.bindings.MongoDbBindingsPropertiesProcessor + org.springframework.cloud.bindings.MongoDbBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.RedisBindingsPropertiesProcessor 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 1d1701b..542bade 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(2); + assertThat(new BindingsEnvironmentPostProcessor().processors).hasSize(3); } } diff --git a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessorTest.java b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..16bc5af --- /dev/null +++ b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessorTest.java @@ -0,0 +1,53 @@ +/* + * 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.RedisBindingsPropertiesProcessor.KIND; + +@DisplayName("Redis BindingsPropertiesProcessor") +final class RedisBindingsPropertiesProcessorTest { + + @Test + @DisplayName("contributes properties") + void test() { + HashMap properties = new HashMap<>(); + + new RedisBindingsPropertiesProcessor().process(new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("hostname", "test-hostname") + .withEntry("password", "test-password") + .withEntry("port", "test-port") + ) + ), properties); + + assertThat(properties) + .containsEntry("spring.redis.host", "test-hostname") + .containsEntry("spring.redis.password", "test-password") + .containsEntry("spring.redis.port", "test-port"); + } + +}