DATAES-34 #17 - added option to inject custom implementation of ResultMapper

This commit is contained in:
Artur Konczak
2013-11-17 20:25:50 +00:00
parent 47c82a8f96
commit 56d4079d81
15 changed files with 1001 additions and 706 deletions

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2013 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.data.elasticsearch;
/**
* @author Artur Konczak
*/
public class CarBuilder {
private Car car = new Car();
public CarBuilder name(String name) {
car.setName(name);
return this;
}
public CarBuilder model(String model) {
car.setModel(model);
return this;
}
public Car build(){
return car;
}
}

View File

@@ -0,0 +1,35 @@
package org.springframework.data.elasticsearch.core;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.springframework.data.domain.Pageable;
/**
* User: dead
* Date: 11/11/13
* Time: 17:37
*/
public class CustomResultMapper implements ResultsMapper{
private EntityMapper entityMapper;
public CustomResultMapper(EntityMapper entityMapper) {
this.entityMapper = entityMapper;
}
@Override
public EntityMapper getEntityMapper() {
return entityMapper;
}
@Override
public <T> T mapResult(GetResponse response, Class<T> clazz) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}

View File

@@ -0,0 +1,51 @@
package org.springframework.data.elasticsearch.core;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.elasticsearch.Car;
import org.springframework.data.elasticsearch.CarBuilder;
import java.io.IOException;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* @author Artur Konczak
*/
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();
}
@Test
public void shouldMapObjectToJsonString() throws IOException {
//Given
//When
String jsonResult = entityMapper.mapToString(new CarBuilder().model(CAR_MODEL).name(CAR_NAME).build());
//Then
assertThat(jsonResult, is(JSON_STRING));
}
@Test
public void shouldMapJsonStringToObject() throws IOException {
//Given
//When
Car result = entityMapper.mapToObject(JSON_STRING,Car.class);
//Then
assertThat(result.getName(),is(CAR_NAME));
assertThat(result.getModel(),is(CAR_MODEL));
}
}

View File

@@ -0,0 +1,83 @@
package org.springframework.data.elasticsearch.core;
import org.apache.commons.collections.iterators.ArrayIterator;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.data.elasticsearch.Car;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Artur Konczak
*/
public class DefaultResultMapperTests {
private DefaultResultMapper resultMapper;
@Mock
private SearchResponse response;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
resultMapper = new DefaultResultMapper();
}
@Test
public void shouldMapSearchRequestToPage() {
//Given
SearchHit[] hits = {createCarHit("Ford", "Grat"), createCarHit("BMW", "Arrow")};
SearchHits searchHits = mock(SearchHits.class);
when(searchHits.totalHits()).thenReturn(2L);
when(searchHits.iterator()).thenReturn(new ArrayIterator(hits));
when(response.getHits()).thenReturn(searchHits);
//When
FacetedPage<Car> page = resultMapper.mapResults(response, Car.class, null);
//Then
assertThat(page.hasContent(), is(true));
assertThat(page.getTotalElements(), is(2L));
assertThat(page.getContent().get(0).getName(), is("Ford"));
}
@Test
public void shouldMapGetRequestToObject() {
//Given
GetResponse response = mock(GetResponse.class);
when(response.getSourceAsString()).thenReturn(createJsonCar("Ford", "Grat"));
//When
Car result = resultMapper.mapResult(response, Car.class);
//Then
assertThat(result, notNullValue());
assertThat(result.getModel(), is("Grat"));
assertThat(result.getName(), is("Ford"));
}
private SearchHit createCarHit(String name, String model) {
SearchHit hit = mock(SearchHit.class);
when(hit.sourceAsString()).thenReturn(createJsonCar(name, model));
return hit;
}
private String createJsonCar(String name, String model) {
final String q = "\"";
StringBuffer sb = new StringBuffer();
sb.append("{").append(q).append("name").append(q).append(":").append(q).append(name).append(q).append(",");
sb.append(q).append("model").append(q).append(":").append(q).append(model).append(q).append("}");
return sb.toString();
}
}

View File

@@ -37,12 +37,16 @@ public class ElasticsearchTemplateCustomMapperTests {
@Autowired
private EntityMapper entityMapper;
@Autowired
private ResultsMapper resultsMapper;
@Test
public void shouldUseCustomMapper() {
//given
//when
//them
assertThat(elasticsearchTemplate.getEntityMapper(), is(entityMapper));
assertThat(elasticsearchTemplate.getResultsMapper(), is(resultsMapper));
assertThat(elasticsearchTemplate.getResultsMapper().getEntityMapper(), is(entityMapper));
}
}