MongoDbBindingsPropertiesProcessor

Signed-off-by: Ben Hale <bhale@vmware.com>
This commit is contained in:
Ben Hale
2020-05-09 07:59:09 -07:00
parent 970620621c
commit ee20ec4e28
6 changed files with 94 additions and 108 deletions

View File

@@ -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<String, Object> 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();
}
}

View File

@@ -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<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "mongodb");
Binding binding = new Binding(bindingMetadata, new HashMap<String, String>());
assertThat(bindingProcessor.accept(binding)).isTrue();
}
@Test
public void rejectIfNotMongoKind() {
MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor();
Map<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "mysql");
Binding binding = new Binding(bindingMetadata, new HashMap<String, String>());
assertThat(bindingProcessor.accept(binding)).isFalse();
}
@Test
public void processDataSourcePropertiesTest() {
MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor();
Map<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "mongodb");
Map<String,String> bindingSecret = new HashMap<String,String>();
bindingSecret.put("uri", "some-uri");
Binding binding = new Binding(bindingMetadata, bindingSecret);
Map<String,Object> properties = new HashMap<String,Object>();
bindingProcessor.process(binding, properties);
assertThat(properties.get("spring.redis.mongodb.uri")).isEqualTo("some-uri");
}
}

View File

@@ -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<String, Object> properties) {
bindings.filterBindings(KIND).forEach(binding -> {
Map<String, String> secret = binding.getSecret();
properties.put("spring.mongodb.uri", secret.get("uri"));
});
}
}

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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<String, Object> 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");
}
}