DATAES-629 - ElasticsearchEntityMapper refactoring.

Original PR: #309
This commit is contained in:
Peter-Josef Meisch
2019-09-16 19:37:51 +02:00
parent 7a4aebf9f4
commit b820c9a422
29 changed files with 876 additions and 1510 deletions

View File

@@ -19,11 +19,9 @@ import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.EntityMapper;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
/**
* configuration class for the classic ElasticsearchTemplate. Needs a {@link TestNodeResource} bean that should be set up in
@@ -42,20 +40,8 @@ public class ElasticsearchTestConfiguration extends ElasticsearchConfigurationSu
}
@Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" })
public ElasticsearchTemplate elasticsearchTemplate(Client elasticsearchClient, EntityMapper entityMapper) {
public ElasticsearchTemplate elasticsearchTemplate(Client elasticsearchClient, MappingElasticsearchConverter entityMapper) {
return new ElasticsearchTemplate(elasticsearchClient, entityMapper);
}
/*
* need the ElasticsearchMapper, because some tests rely on @Field(name) being handled correctly
*/
@Bean
@Override
public EntityMapper entityMapper() {
ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(elasticsearchMappingContext(),
new DefaultConversionService());
entityMapper.setConversions(elasticsearchCustomConversions());
return entityMapper;
}
}

View File

@@ -18,10 +18,7 @@ package org.springframework.data.elasticsearch;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper;
import org.springframework.data.elasticsearch.core.EntityMapper;
/**
* @author Peter-Josef Meisch
@@ -29,22 +26,9 @@ import org.springframework.data.elasticsearch.core.EntityMapper;
@Configuration
public class RestElasticsearchTestConfiguration extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
return TestUtils.restHighLevelClient();
}
/*
* need the ElasticsearchMapper, because some tests rely on @Field(name) being handled correctly
*/
@Bean
@Override
public EntityMapper entityMapper() {
ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(elasticsearchMappingContext(),
new DefaultConversionService());
entityMapper.setConversions(elasticsearchCustomConversions());
return entityMapper;
}
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
return TestUtils.restHighLevelClient();
}
}

View File

@@ -25,17 +25,15 @@ import org.apache.commons.lang.ClassUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.EntityMapper;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
/**
@@ -108,7 +106,7 @@ public class ElasticsearchConfigurationSupportUnitTests {
public void usesConfiguredEntityMapper() {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(EntityMapperConfig.class);
assertThat(context.getBean(EntityMapper.class)).isInstanceOf(ElasticsearchEntityMapper.class);
assertThat(context.getBean(EntityMapper.class)).isInstanceOf(MappingElasticsearchConverter.class);
}
@Configuration
@@ -135,19 +133,7 @@ public class ElasticsearchConfigurationSupportUnitTests {
}
@Configuration
static class EntityMapperConfig extends ElasticsearchConfigurationSupport {
@Bean
@Override
public EntityMapper entityMapper() {
ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(elasticsearchMappingContext(),
new DefaultConversionService());
entityMapper.setConversions(elasticsearchCustomConversions());
return entityMapper;
}
}
static class EntityMapperConfig extends ElasticsearchConfigurationSupport {}
@Document(indexName = "config-support-tests")
static class Entity {}

View File

@@ -1,54 +0,0 @@
/*
* Copyright 2013-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
*
* 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.data.elasticsearch.core;
import java.io.IOException;
import org.springframework.data.elasticsearch.Document;
/**
* @author Artur Konczak
* @author Mohsin Husen
* @author Mark Paluch
*/
public class CustomEntityMapper implements EntityMapper {
public CustomEntityMapper() {
// custom configuration/implementation (e.g. FasterXML/jackson)
}
@Override
public String mapToString(Object object) throws IOException {
// mapping Object to text
return null;
}
@Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
// mapping text to Object
return null;
}
@Override
public Document mapObject(Object source) {
return null;
}
@Override
public <T> T readObject(Document source, Class<T> targetType) {
return null;
}
}

View File

@@ -1,164 +0,0 @@
/*
* Copyright 2013-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
*
* 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.data.elasticsearch.core;
import static org.assertj.core.api.Assertions.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.IOException;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Transient;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.GeoPointField;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.geo.Point;
/**
* @author Artur Konczak
* @author Mohsin Husen
* @author Oliver Gierke
* @author Peter-Josef Meisch
*/
public class DefaultEntityMapperTests {
public static final String JSON_STRING = "{\"name\":\"Grat\",\"model\":\"Ford\"}";
public static final String CAR_MODEL = "Ford";
public static final String CAR_NAME = "Grat";
DefaultEntityMapper entityMapper;
@Before
public void init() {
entityMapper = new DefaultEntityMapper(new SimpleElasticsearchMappingContext());
}
@Test
public void shouldMapObjectToJsonString() throws IOException {
// given
// when
String jsonResult = entityMapper.mapToString(new Car(CAR_NAME, CAR_MODEL));
// then
assertThat(jsonResult).isEqualTo(JSON_STRING);
}
@Test
public void shouldMapJsonStringToObject() throws IOException {
// given
// when
Car result = entityMapper.mapToObject(JSON_STRING, Car.class);
// then
assertThat(result.getName()).isEqualTo(CAR_NAME);
assertThat(result.getModel()).isEqualTo(CAR_MODEL);
}
@Test
public void shouldMapGeoPointElasticsearchNames() throws IOException {
// given
Point point = new Point(10, 20);
String pointAsString = point.getX() + "," + point.getY();
double[] pointAsArray = { point.getX(), point.getY() };
GeoEntity geoEntity = GeoEntity.builder().pointA(point).pointB(GeoPoint.fromPoint(point)).pointC(pointAsString)
.pointD(pointAsArray).build();
// when
String jsonResult = entityMapper.mapToString(geoEntity);
// then
assertThat(jsonResult).contains(pointTemplate("pointA", point));
assertThat(jsonResult).contains(pointTemplate("pointB", point));
assertThat(jsonResult).contains(String.format(Locale.ENGLISH, "\"%s\":\"%s\"", "pointC", pointAsString));
assertThat(jsonResult)
.contains(String.format(Locale.ENGLISH, "\"%s\":[%.1f,%.1f]", "pointD", pointAsArray[0], pointAsArray[1]));
}
@Test // DATAES-464
public void ignoresReadOnlyProperties() throws IOException {
// given
Sample sample = new Sample();
sample.readOnly = "readOnly";
sample.property = "property";
sample.transientProperty = "transient";
sample.annotatedTransientProperty = "transient";
// when
String result = entityMapper.mapToString(sample);
// then
assertThat(result).contains("\"property\"");
assertThat(result).doesNotContain("readOnly");
assertThat(result).doesNotContain("transientProperty");
assertThat(result).doesNotContain("annotatedTransientProperty");
}
private String pointTemplate(String name, Point point) {
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
}
public static class Sample {
public @ReadOnlyProperty String readOnly;
public @Transient String annotatedTransientProperty;
public transient String transientProperty;
public String property;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
static class Car {
private String name;
private String model;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(indexName = "test-index-geo-core-default-entity-mapper", type = "geo-test-index", shards = 1, replicas = 0,
refreshInterval = "-1")
static class GeoEntity {
@Id private String id;
// geo point - Custom implementation + Spring Data
@GeoPointField private Point pointA;
private GeoPoint pointB;
@GeoPointField private String pointC;
@GeoPointField private double[] pointD;
}
}

View File

@@ -64,6 +64,7 @@ import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.Score;
import org.springframework.data.elasticsearch.annotations.ScriptedField;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
@@ -78,36 +79,13 @@ import com.fasterxml.jackson.databind.util.ArrayIterator;
* @author Christoph Strobl
* @author Peter-Josef Meisch
*/
@RunWith(Parameterized.class)
public class DefaultResultMapperTests {
private DefaultResultMapper resultMapper;
private SimpleElasticsearchMappingContext context;
private EntityMapper entityMapper;
private SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
private EntityMapper entityMapper = new MappingElasticsearchConverter(context);
private DefaultResultMapper resultMapper = new DefaultResultMapper(context, entityMapper);
@Mock private SearchResponse response;
public DefaultResultMapperTests(SimpleElasticsearchMappingContext context, EntityMapper entityMapper) {
this.context = context;
this.entityMapper = entityMapper;
}
@Parameters
public static Collection<Object[]> data() {
SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
return Arrays.asList(new Object[] { context, new DefaultEntityMapper(context) },
new Object[] { context, new ElasticsearchEntityMapper(context, new DefaultConversionService()) });
}
@Before
public void init() {
MockitoAnnotations.initMocks(this);
resultMapper = new DefaultResultMapper(context, entityMapper);
}
private SearchResponse response = mock(SearchResponse.class);
@Test
public void shouldMapAggregationsToPage() {

View File

@@ -1,51 +0,0 @@
/*
* Copyright 2013-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
*
* 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.data.elasticsearch.core;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Artur Konczak
* @author Peter-Josef Meisch
*/
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:elasticsearch-template-custom-mapper.xml")
public class ElasticsearchTemplateCustomMapperTests {
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
@Autowired private EntityMapper entityMapper;
@Autowired private ResultsMapper resultsMapper;
@Test
public void shouldUseCustomMapper() {
// given
// when
// then
assertThat(elasticsearchTemplate.getResultsMapper()).isSameAs(resultsMapper);
assertThat(elasticsearchTemplate.getResultsMapper().getEntityMapper()).isSameAs(entityMapper);
}
}

View File

@@ -3164,8 +3164,7 @@ public class ElasticsearchTemplateTests {
static class SampleEntity {
@Id private String id;
@org.springframework.data.elasticsearch.annotations.Field(type = Text, store = true,
fielddata = true) private String type;
@Field(type = Text, store = true, fielddata = true) private String type;
@Field(type = Text, store = true, fielddata = true) private String message;
private int rate;
@ScriptedField private Double scriptedRate;

View File

@@ -24,6 +24,7 @@ import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -102,7 +103,7 @@ public class ReactiveElasticsearchTemplateTests {
restTemplate.refresh(SampleEntity.class);
template = new ReactiveElasticsearchTemplate(TestUtils.reactiveClient(), restTemplate.getElasticsearchConverter(),
new DefaultResultMapper(new ElasticsearchEntityMapper(
new DefaultResultMapper(new MappingElasticsearchConverter(
restTemplate.getElasticsearchConverter().getMappingContext(), new DefaultConversionService())));
}

View File

@@ -1,64 +0,0 @@
/*
* Copyright 2013-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
*
* 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.data.elasticsearch.core.convert;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.mapping.context.MappingContext;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Peter-Josef Meisch
*/
public class MappingElasticsearchConverterTests {
@Test(expected = IllegalArgumentException.class)
public void shouldFailToInitializeGivenMappingContextIsNull() {
// given
new MappingElasticsearchConverter(null);
}
@Test
public void shouldReturnMappingContextWithWhichItWasInitialized() {
// given
MappingContext mappingContext = new SimpleElasticsearchMappingContext();
MappingElasticsearchConverter converter = new MappingElasticsearchConverter(mappingContext);
// then
assertThat(converter.getMappingContext()).isNotNull();
assertThat(converter.getMappingContext()).isSameAs(mappingContext);
}
@Test
public void shouldReturnDefaultConversionService() {
// given
MappingElasticsearchConverter converter = new MappingElasticsearchConverter(
new SimpleElasticsearchMappingContext());
// when
ConversionService conversionService = converter.getConversionService();
// then
assertThat(conversionService).isNotNull();
}
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.core;
package org.springframework.data.elasticsearch.core.convert;
import static org.assertj.core.api.Assertions.*;
@@ -39,6 +39,7 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.annotation.Id;
@@ -49,26 +50,27 @@ import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.elasticsearch.Document;
import org.springframework.data.elasticsearch.annotations.GeoPointField;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.geo.Box;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Point;
import org.springframework.data.geo.Polygon;
import org.springframework.data.mapping.context.MappingContext;
/**
* Unit tests for {@link ElasticsearchEntityMapper}.
* Unit tests for {@link MappingElasticsearchConverter}.
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Peter-Josef Meisch
*/
public class ElasticsearchEntityMapperUnitTests {
public class MappingElasticsearchConverterUnitTests {
static final String JSON_STRING = "{\"_class\":\"org.springframework.data.elasticsearch.core.ElasticsearchEntityMapperUnitTests$Car\",\"name\":\"Grat\",\"model\":\"Ford\"}";
static final String JSON_STRING = "{\"_class\":\"org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$Car\",\"name\":\"Grat\",\"model\":\"Ford\"}";
static final String CAR_MODEL = "Ford";
static final String CAR_NAME = "Grat";
ElasticsearchEntityMapper entityMapper;
MappingElasticsearchConverter mappingElasticsearchConverter;
Person sarahConnor;
Person kyleReese;
@@ -100,10 +102,10 @@ public class ElasticsearchEntityMapperUnitTests {
mappingContext.setInitialEntitySet(Collections.singleton(Rifle.class));
mappingContext.afterPropertiesSet();
entityMapper = new ElasticsearchEntityMapper(mappingContext, new GenericConversionService());
entityMapper.setConversions(
mappingElasticsearchConverter = new MappingElasticsearchConverter(mappingContext, new GenericConversionService());
mappingElasticsearchConverter.setConversions(
new ElasticsearchCustomConversions(Arrays.asList(new ShotGunToMapConverter(), new MapToShotGunConverter())));
entityMapper.afterPropertiesSet();
mappingElasticsearchConverter.afterPropertiesSet();
sarahConnor = new Person();
sarahConnor.id = "sarah";
@@ -124,7 +126,7 @@ public class ElasticsearchEntityMapperUnitTests {
t800AsMap.put("id", "t800");
t800AsMap.put("name", "T-800");
t800AsMap.put("gender", "MACHINE");
t800AsMap.put("_class", "org.springframework.data.elasticsearch.core.ElasticsearchEntityMapperUnitTests$Person");
t800AsMap.put("_class", "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$Person");
observatoryRoad = new Address();
observatoryRoad.city = "Los Angeles";
@@ -141,7 +143,7 @@ public class ElasticsearchEntityMapperUnitTests {
sarahAsMap.put("id", "sarah");
sarahAsMap.put("name", "Sarah Connor");
sarahAsMap.put("gender", "MAN");
sarahAsMap.put("_class", "org.springframework.data.elasticsearch.core.ElasticsearchEntityMapperUnitTests$Person");
sarahAsMap.put("_class", "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$Person");
kyleAsMap = Document.create();
kyleAsMap.put("id", "kyle");
@@ -165,7 +167,7 @@ public class ElasticsearchEntityMapperUnitTests {
((HashMap<String, Object>) bigBunsCafeAsMap.get("location")).put("lat", 34.0945637D);
((HashMap<String, Object>) bigBunsCafeAsMap.get("location")).put("lon", -118.1545845D);
bigBunsCafeAsMap.put("_class",
"org.springframework.data.elasticsearch.core.ElasticsearchEntityMapperUnitTests$Place");
"org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$Place");
gunAsMap = Document.create();
gunAsMap.put("label", "Glock 19");
@@ -187,12 +189,46 @@ public class ElasticsearchEntityMapperUnitTests {
shotGunAsMap.put("_class", ShotGun.class.getName());
}
@Test // DATAES-530
@Test(expected = IllegalArgumentException.class)
public void shouldFailToInitializeGivenMappingContextIsNull() {
// given
new MappingElasticsearchConverter(null);
}
@Test
public void shouldReturnMappingContextWithWhichItWasInitialized() {
// given
MappingContext mappingContext = new SimpleElasticsearchMappingContext();
MappingElasticsearchConverter converter = new MappingElasticsearchConverter(mappingContext);
// then
assertThat(converter.getMappingContext()).isNotNull();
assertThat(converter.getMappingContext()).isSameAs(mappingContext);
}
@Test
public void shouldReturnDefaultConversionService() {
// given
MappingElasticsearchConverter converter = new MappingElasticsearchConverter(
new SimpleElasticsearchMappingContext());
// when
ConversionService conversionService = converter.getConversionService();
// then
assertThat(conversionService).isNotNull();
}
@Test // DATAES-530
public void shouldMapObjectToJsonString() throws IOException {
// Given
// When
String jsonResult = entityMapper.mapToString(Car.builder().model(CAR_MODEL).name(CAR_NAME).build());
String jsonResult = mappingElasticsearchConverter.mapToString(Car.builder().model(CAR_MODEL).name(CAR_NAME).build());
// Then
assertThat(jsonResult).isEqualTo(JSON_STRING);
@@ -203,7 +239,7 @@ public class ElasticsearchEntityMapperUnitTests {
// Given
// When
Car result = entityMapper.mapToObject(JSON_STRING, Car.class);
Car result = mappingElasticsearchConverter.mapToObject(JSON_STRING, Car.class);
// Then
assertThat(result.getName()).isEqualTo(CAR_NAME);
@@ -219,7 +255,7 @@ public class ElasticsearchEntityMapperUnitTests {
GeoEntity geoEntity = GeoEntity.builder().pointA(point).pointB(GeoPoint.fromPoint(point)).pointC(pointAsString)
.pointD(pointAsArray).build();
// when
String jsonResult = entityMapper.mapToString(geoEntity);
String jsonResult = mappingElasticsearchConverter.mapToString(geoEntity);
// then
assertThat(jsonResult).contains(pointTemplate("pointA", point));
@@ -240,7 +276,7 @@ public class ElasticsearchEntityMapperUnitTests {
sample.annotatedTransientProperty = "transient";
// when
String result = entityMapper.mapToString(sample);
String result = mappingElasticsearchConverter.mapToString(sample);
// then
assertThat(result).contains("\"property\"");
@@ -291,7 +327,7 @@ public class ElasticsearchEntityMapperUnitTests {
@Test // DATAES-530
public void readTypeCorrectly() {
Person target = entityMapper.read(Person.class, sarahAsMap);
Person target = mappingElasticsearchConverter.read(Person.class, sarahAsMap);
assertThat(target).isEqualTo(sarahConnor);
}
@@ -301,7 +337,7 @@ public class ElasticsearchEntityMapperUnitTests {
sarahAsMap.put("coWorkers", Arrays.asList(kyleAsMap));
Person target = entityMapper.read(Person.class, sarahAsMap);
Person target = mappingElasticsearchConverter.read(Person.class, sarahAsMap);
assertThat(target.getCoWorkers()).contains(kyleReese);
}
@@ -311,7 +347,7 @@ public class ElasticsearchEntityMapperUnitTests {
sarahAsMap.put("inventoryList", Arrays.asList(gunAsMap, grenadeAsMap));
Person target = entityMapper.read(Person.class, sarahAsMap);
Person target = mappingElasticsearchConverter.read(Person.class, sarahAsMap);
assertThat(target.getInventoryList()).containsExactly(gun, grenade);
}
@@ -345,7 +381,7 @@ public class ElasticsearchEntityMapperUnitTests {
sarahAsMap.put("shippingAddresses", Collections.singletonMap("home", gratiotAveAsMap));
Person target = entityMapper.read(Person.class, sarahAsMap);
Person target = mappingElasticsearchConverter.read(Person.class, sarahAsMap);
assertThat(target.getShippingAddresses()).hasSize(1).containsEntry("home", observatoryRoad);
}
@@ -355,7 +391,7 @@ public class ElasticsearchEntityMapperUnitTests {
sarahAsMap.put("inventoryMap", Collections.singletonMap("glock19", gunAsMap));
Person target = entityMapper.read(Person.class, sarahAsMap);
Person target = mappingElasticsearchConverter.read(Person.class, sarahAsMap);
assertThat(target.getInventoryMap()).hasSize(1).containsEntry("glock19", gun);
}
@@ -379,7 +415,7 @@ public class ElasticsearchEntityMapperUnitTests {
Document source = Document.create();
source.put("objectList", Arrays.asList(t800AsMap, gunAsMap));
Skynet target = entityMapper.read(Skynet.class, source);
Skynet target = mappingElasticsearchConverter.read(Skynet.class, source);
assertThat(target.getObjectList()).containsExactly(t800, gun);
}
@@ -402,7 +438,7 @@ public class ElasticsearchEntityMapperUnitTests {
Document source = Document.create();
source.put("objectList", Arrays.asList(Arrays.asList(t800AsMap, gunAsMap)));
Skynet target = entityMapper.read(Skynet.class, source);
Skynet target = mappingElasticsearchConverter.read(Skynet.class, source);
assertThat(target.getObjectList()).containsExactly(Arrays.asList(t800, gun));
}
@@ -427,7 +463,7 @@ public class ElasticsearchEntityMapperUnitTests {
Document source = Document.create();
source.put("objectMap", Collections.singletonMap("glock19", gunAsMap));
Skynet target = entityMapper.read(Skynet.class, source);
Skynet target = mappingElasticsearchConverter.read(Skynet.class, source);
assertThat(target.getObjectMap()).containsEntry("glock19", gun);
}
@@ -451,7 +487,7 @@ public class ElasticsearchEntityMapperUnitTests {
Document source = Document.create();
source.put("objectMap", Collections.singletonMap("inventory", Collections.singletonMap("glock19", gunAsMap)));
Skynet target = entityMapper.read(Skynet.class, source);
Skynet target = mappingElasticsearchConverter.read(Skynet.class, source);
assertThat(target.getObjectMap()).containsEntry("inventory", Collections.singletonMap("glock19", gun));
}
@@ -461,7 +497,7 @@ public class ElasticsearchEntityMapperUnitTests {
sarahAsMap.put("address", gratiotAveAsMap);
Person target = entityMapper.read(Person.class, sarahAsMap);
Person target = mappingElasticsearchConverter.read(Person.class, sarahAsMap);
assertThat(target.getAddress()).isEqualTo(observatoryRoad);
}
@@ -472,7 +508,7 @@ public class ElasticsearchEntityMapperUnitTests {
Document source = Document.create();
source.put("object", t800AsMap);
Skynet target = entityMapper.read(Skynet.class, source);
Skynet target = mappingElasticsearchConverter.read(Skynet.class, source);
assertThat(target.getObject()).isEqualTo(t800);
}
@@ -493,7 +529,7 @@ public class ElasticsearchEntityMapperUnitTests {
@Test // DATAES-530
public void readsAliased() {
assertThat(entityMapper.read(Inventory.class, rifleAsMap)).isEqualTo(rifle);
assertThat(mappingElasticsearchConverter.read(Inventory.class, rifleAsMap)).isEqualTo(rifle);
}
@Test // DATAES-530
@@ -501,7 +537,7 @@ public class ElasticsearchEntityMapperUnitTests {
t800AsMap.put("inventoryList", Collections.singletonList(rifleAsMap));
assertThat(entityMapper.read(Person.class, t800AsMap).getInventoryList()).containsExactly(rifle);
assertThat(mappingElasticsearchConverter.read(Person.class, t800AsMap).getInventoryList()).containsExactly(rifle);
}
@Test // DATAES-530
@@ -511,7 +547,7 @@ public class ElasticsearchEntityMapperUnitTests {
@Test // DATAES-530
public void appliesCustomConverterForRead() {
assertThat(entityMapper.read(Inventory.class, shotGunAsMap)).isEqualTo(shotGun);
assertThat(mappingElasticsearchConverter.read(Inventory.class, shotGunAsMap)).isEqualTo(shotGun);
}
@Test // DATAES-530
@@ -529,7 +565,7 @@ public class ElasticsearchEntityMapperUnitTests {
sarahAsMap.put("address", bigBunsCafeAsMap);
Person target = entityMapper.read(Person.class, sarahAsMap);
Person target = mappingElasticsearchConverter.read(Person.class, sarahAsMap);
assertThat(target.address).isEqualTo(bigBunsCafe);
}
@@ -541,7 +577,7 @@ public class ElasticsearchEntityMapperUnitTests {
private Map<String, Object> writeToMap(Object source) {
Document sink = Document.create();
entityMapper.write(source, sink);
mappingElasticsearchConverter.write(source, sink);
return sink;
}

View File

@@ -59,7 +59,7 @@ import org.springframework.data.repository.core.support.DefaultRepositoryMetadat
public class ElasticsearchStringQueryUnitTests {
@Mock ElasticsearchOperations operations;
ElasticsearchConverter converter;
ElasticsearchConverter converter;
@Before
public void setUp() {
@@ -107,6 +107,16 @@ public class ElasticsearchStringQueryUnitTests {
new SpelAwareProxyProjectionFactory(), converter.getMappingContext());
}
private interface SampleRepository extends Repository<Person, String> {
@Query("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }")
Person findByName(String name);
@Query(value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)")
Person findWithRepeatedPlaceholder(String arg0, String arg1, String arg2, String arg3, String arg4, String arg5,
String arg6, String arg7, String arg8, String arg9, String arg10, String arg11);
}
/**
* @author Rizwan Idrees
* @author Mohsin Husen
@@ -158,16 +168,6 @@ public class ElasticsearchStringQueryUnitTests {
}
}
private interface SampleRepository extends Repository<Person, String> {
@Query("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }")
Person findByName(String name);
@Query(value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)")
Person findWithRepeatedPlaceholder(String arg0, String arg1, String arg2, String arg3, String arg4, String arg5,
String arg6, String arg7, String arg8, String arg9, String arg10, String arg11);
}
/**
* @author Rizwan Idrees
* @author Mohsin Husen

View File

@@ -22,6 +22,7 @@ import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -46,7 +47,6 @@ import org.springframework.data.elasticsearch.annotations.InnerField;
import org.springframework.data.elasticsearch.annotations.MultiField;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.core.query.StringQuery;
@@ -58,7 +58,6 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
/**
* @author Christoph Strobl
* @currentRead Fool's Fate - Robin Hobb
* @author Peter-Josef Meisch
*/
@RunWith(MockitoJUnitRunner.class)

View File

@@ -25,10 +25,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
@@ -37,19 +34,17 @@ import org.springframework.data.repository.core.support.DefaultRepositoryMetadat
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Mark Paluch
* @author Peter-Josef Meisch
*/
@RunWith(MockitoJUnitRunner.class)
public class ElasticsearchRepositoryFactoryTests {
@Mock private ElasticsearchOperations operations;
private ElasticsearchConverter converter;
@Mock private ElasticsearchOperations operations;
private ElasticsearchRepositoryFactory factory;
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext = new SimpleElasticsearchMappingContext();
@Before
public void before() {
converter = new MappingElasticsearchConverter(mappingContext);
ElasticsearchConverter converter = new MappingElasticsearchConverter(new SimpleElasticsearchMappingContext());
when(operations.getElasticsearchConverter()).thenReturn(converter);
factory = new ElasticsearchRepositoryFactory(operations);
}