Commit af4fdf0d authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Add support for Spring Data Couchbase custom type key"

See gh-19789
parent d1a44dfa
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -41,8 +41,8 @@ public class CouchbaseDataProperties { ...@@ -41,8 +41,8 @@ public class CouchbaseDataProperties {
private Consistency consistency = Consistency.READ_YOUR_OWN_WRITES; private Consistency consistency = Consistency.READ_YOUR_OWN_WRITES;
/** /**
* Name of the field that will store the type information for complex types when using * Name of the field that stores the type information for complex types when using
* MappingCouchbaseConverter. * "MappingCouchbaseConverter".
*/ */
private String typeKey = DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY; private String typeKey = DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY;
......
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -73,6 +73,11 @@ class SpringBootCouchbaseDataConfiguration extends AbstractCouchbaseDataConfigur ...@@ -73,6 +73,11 @@ class SpringBootCouchbaseDataConfiguration extends AbstractCouchbaseDataConfigur
return new EntityScanner(this.applicationContext).scan(Document.class, Persistent.class); return new EntityScanner(this.applicationContext).scan(Document.class, Persistent.class);
} }
@Override
public String typeKey() {
return this.properties.getTypeKey();
}
@Override @Override
@ConditionalOnMissingBean(name = BeanNames.COUCHBASE_TEMPLATE) @ConditionalOnMissingBean(name = BeanNames.COUCHBASE_TEMPLATE)
@Bean(name = BeanNames.COUCHBASE_TEMPLATE) @Bean(name = BeanNames.COUCHBASE_TEMPLATE)
...@@ -97,9 +102,4 @@ class SpringBootCouchbaseDataConfiguration extends AbstractCouchbaseDataConfigur ...@@ -97,9 +102,4 @@ class SpringBootCouchbaseDataConfiguration extends AbstractCouchbaseDataConfigur
return new IndexManager(false, false, false); return new IndexManager(false, false, false);
} }
@Override
public String typeKey() {
return this.properties.getTypeKey();
}
} }
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -121,27 +121,24 @@ class CouchbaseDataAutoConfigurationTests { ...@@ -121,27 +121,24 @@ class CouchbaseDataAutoConfigurationTests {
} }
@Test @Test
void customConversions() { void typeKeyDefault() {
load(CustomConversionsConfig.class); load(CouchbaseTestConfigurer.class);
CouchbaseTemplate template = this.context.getBean(CouchbaseTemplate.class); assertThat(this.context.getBean(AbstractCouchbaseDataConfiguration.class).typeKey())
assertThat(template.getConverter().getConversionService().canConvert(CouchbaseProperties.class, Boolean.class)) .isEqualTo(DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY);
.isTrue();
} }
@Test @Test
void typeKeyIsClassByDefault() { void typeKeyCanBeCustomized() {
load(CouchbaseTestConfigurer.class); load(CouchbaseTestConfigurer.class, "spring.data.couchbase.type-key=_custom");
AbstractCouchbaseDataConfiguration couchbaseDataConfiguration = this.context assertThat(this.context.getBean(AbstractCouchbaseDataConfiguration.class).typeKey()).isEqualTo("_custom");
.getBean(AbstractCouchbaseDataConfiguration.class);
assertThat(couchbaseDataConfiguration.typeKey()).isEqualTo(DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY);
} }
@Test @Test
void customTypeKey() { void customConversions() {
load(CouchbaseTestConfigurer.class, "spring.data.couchbase.type-key=custom"); load(CustomConversionsConfig.class);
AbstractCouchbaseDataConfiguration couchbaseDataConfiguration = this.context CouchbaseTemplate template = this.context.getBean(CouchbaseTemplate.class);
.getBean(AbstractCouchbaseDataConfiguration.class); assertThat(template.getConverter().getConversionService().canConvert(CouchbaseProperties.class, Boolean.class))
assertThat(couchbaseDataConfiguration.typeKey()).isEqualTo("custom"); .isTrue();
} }
private void load(Class<?> config, String... environment) { private void load(Class<?> config, String... environment) {
......
/*
* Copyright 2012-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
*
* https://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.boot.autoconfigure.data.couchbase;
import org.junit.jupiter.api.Test;
import org.springframework.data.couchbase.config.CouchbaseConfigurationSupport;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link CouchbaseDataProperties}.
*
* @author Stephane Nicoll
*/
class CouchbaseDataPropertiesTests {
@Test
void typeKeyHasConsistentDefault() {
assertThat(new CouchbaseDataProperties().getTypeKey()).isEqualTo(new CouchbaseConfigurationSupport().typeKey());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment