Skip to content

Commit 7113c7a

Browse files
committed
DATAES-281 - Polishing.
Added unit test for change in DefaultResultMapperTests. Tweaked and simplified integration tests. Make use of IdentifierAccessor in ElasticsearcTemplate.getPersistentEntityId(…). Added missing generics in DefaultResultMapper. Removed unused imports, fixed copyright ranges, authors. Original pull request: #156.
1 parent 7e736a6 commit 7113c7a

File tree

6 files changed

+96
-104
lines changed

6 files changed

+96
-104
lines changed

src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
21-
import java.lang.reflect.Method;
2221
import java.nio.charset.Charset;
2322
import java.util.ArrayList;
2423
import java.util.Collection;
@@ -35,9 +34,6 @@
3534
import org.elasticsearch.action.search.SearchResponse;
3635
import org.elasticsearch.search.SearchHit;
3736
import org.elasticsearch.search.SearchHitField;
38-
import org.slf4j.Logger;
39-
import org.slf4j.LoggerFactory;
40-
import org.springframework.data.domain.Page;
4137
import org.springframework.data.domain.Pageable;
4238
import org.springframework.data.elasticsearch.ElasticsearchException;
4339
import org.springframework.data.elasticsearch.annotations.Document;
@@ -52,11 +48,11 @@
5248
/**
5349
* @author Artur Konczak
5450
* @author Petar Tahchiev
51+
* @author Young Gu
52+
* @author Oliver Gierke
5553
*/
5654
public class DefaultResultMapper extends AbstractResultMapper {
5755

58-
private static final Logger LOG = LoggerFactory.getLogger(DefaultResultMapper.class);
59-
6056
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
6157

6258
public DefaultResultMapper() {
@@ -176,12 +172,15 @@ public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz)
176172
}
177173

178174
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
175+
179176
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
180-
ElasticsearchPersistentEntity persistentEntity = mappingContext.getPersistentEntity(clazz);
181-
PersistentProperty idProperty = persistentEntity.getIdProperty();
177+
178+
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
179+
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
180+
182181
// Only deal with String because ES generated Ids are strings !
183182
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
184-
persistentEntity.getPropertyAccessor(result).setProperty(idProperty,id);
183+
persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
185184
}
186185
}
187186
}

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.io.BufferedReader;
2929
import java.io.IOException;
3030
import java.io.InputStreamReader;
31-
import java.lang.reflect.Method;
3231
import java.util.*;
3332

3433
import org.elasticsearch.action.ListenableActionFuture;
@@ -101,6 +100,8 @@
101100
* @author Artur Konczak
102101
* @author Kevin Leturc
103102
* @author Mason Chan
103+
* @author Young Gu
104+
* @author Oliver Gierke
104105
*/
105106

106107
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
@@ -1095,26 +1096,18 @@ public ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz) {
10951096
}
10961097

10971098
private String getPersistentEntityId(Object entity) {
1098-
PersistentProperty idProperty = getPersistentEntityFor(entity.getClass()).getIdProperty();
1099-
if (idProperty != null) {
1100-
Method getter = idProperty.getGetter();
1101-
if (getter != null) {
1102-
try {
1103-
Object id = getter.invoke(entity);
1104-
if (id != null) {
1105-
return String.valueOf(id);
1106-
}
1107-
} catch (Throwable t) {
1108-
t.printStackTrace();
1109-
}
1110-
}
1111-
}
1112-
return null;
1099+
1100+
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
1101+
Object identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier();
1102+
1103+
return identifier == null ? null : String.valueOf(identifier);
11131104
}
11141105

11151106
private void setPersistentEntityId(Object entity, String id) {
1116-
ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(entity.getClass());
1117-
PersistentProperty idProperty = persistentEntity.getIdProperty();
1107+
1108+
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
1109+
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
1110+
11181111
// Only deal with String because ES generated Ids are strings !
11191112
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
11201113
persistentEntity.getPropertyAccessor(entity).setProperty(idProperty,id);

src/test/java/org/springframework/data/elasticsearch/core/DefaultResultMapperTests.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919
import static org.junit.Assert.*;
2020
import static org.mockito.Mockito.*;
2121

22+
import lombok.Getter;
23+
import lombok.NoArgsConstructor;
24+
import lombok.RequiredArgsConstructor;
25+
import lombok.Value;
26+
2227
import java.util.*;
2328

29+
import com.fasterxml.jackson.annotation.JsonCreator;
2430
import com.fasterxml.jackson.databind.util.ArrayIterator;
2531
import org.elasticsearch.action.get.GetResponse;
2632
import org.elasticsearch.action.search.SearchResponse;
@@ -35,8 +41,13 @@
3541
import org.junit.Test;
3642
import org.mockito.Mock;
3743
import org.mockito.MockitoAnnotations;
44+
import org.springframework.data.annotation.AccessType;
45+
import org.springframework.data.annotation.Id;
3846
import org.springframework.data.domain.Page;
47+
import org.springframework.data.elasticsearch.annotations.Document;
48+
import org.springframework.data.elasticsearch.core.DefaultResultMapperTests.ImmutableEntity;
3949
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
50+
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
4051
import org.springframework.data.elasticsearch.entities.Car;
4152

4253
/**
@@ -53,7 +64,7 @@ public class DefaultResultMapperTests {
5364
@Before
5465
public void init() {
5566
MockitoAnnotations.initMocks(this);
56-
resultMapper = new DefaultResultMapper();
67+
resultMapper = new DefaultResultMapper(new SimpleElasticsearchMappingContext());
5768
}
5869

5970
@Test
@@ -132,6 +143,22 @@ public void shouldMapGetRequestToObject() {
132143
assertThat(result.getModel(), is("Grat"));
133144
assertThat(result.getName(), is("Ford"));
134145
}
146+
147+
/**
148+
* @see DATAES-281.
149+
*/
150+
@Test
151+
public void setsIdentifierOnImmutableType() {
152+
153+
GetResponse response = mock(GetResponse.class);
154+
when(response.getSourceAsString()).thenReturn("{}");
155+
when(response.getId()).thenReturn("identifier");
156+
157+
ImmutableEntity result = resultMapper.mapResult(response, ImmutableEntity.class);
158+
159+
assertThat(result, is(notNullValue()));
160+
assertThat(result.getId(), is("identifier"));
161+
}
135162

136163
private Aggregation createCarAggregation() {
137164
Aggregation aggregation = mock(Terms.class);
@@ -166,4 +193,12 @@ private Map<String, SearchHitField> createCarFields(String name, String model) {
166193
result.put("model", new InternalSearchHitField("model", Arrays.<Object>asList(model)));
167194
return result;
168195
}
196+
197+
@Document(indexName = "someIndex")
198+
@NoArgsConstructor(force = true)
199+
@Getter
200+
static class ImmutableEntity {
201+
202+
private final String id, name;
203+
}
169204
}
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,14 +15,10 @@
1515
*/
1616
package org.springframework.data.elasticsearch.immutable;
1717

18-
import org.springframework.data.elasticsearch.entities.SampleEntity;
19-
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
20-
21-
import java.util.List;
18+
import org.springframework.data.repository.CrudRepository;
2219

2320
/**
24-
* @author Rizwan Idrees
25-
* @author Mohsin Husen
21+
* @author Young Gu
22+
* @author Oliver Gierke
2623
*/
27-
public interface ImmutableElasticsearchRepository extends ElasticsearchRepository<ImmutableEntity, String> {
28-
}
24+
public interface ImmutableElasticsearchRepository extends CrudRepository<ImmutableEntity, String> {}
Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 the original author or authors.
2+
* Copyright 2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,65 +15,51 @@
1515
*/
1616
package org.springframework.data.elasticsearch.immutable;
1717

18-
import com.google.common.collect.Lists;
19-
import org.hamcrest.core.IsEqual;
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.*;
20+
2021
import org.junit.Before;
2122
import org.junit.Test;
2223
import org.junit.runner.RunWith;
2324
import org.springframework.beans.factory.annotation.Autowired;
24-
import org.springframework.data.domain.Page;
25-
import org.springframework.data.domain.PageRequest;
26-
import org.springframework.data.domain.Sort;
27-
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
28-
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
29-
import org.springframework.data.elasticsearch.core.query.SearchQuery;
30-
import org.springframework.data.elasticsearch.entities.SampleEntity;
31-
import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository;
25+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
3226
import org.springframework.test.context.ContextConfiguration;
3327
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3428

35-
import java.util.ArrayList;
36-
import java.util.Arrays;
37-
import java.util.List;
38-
39-
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
40-
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
41-
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
42-
import static org.hamcrest.Matchers.*;
43-
import static org.junit.Assert.*;
44-
4529
/**
46-
* @author Rizwan Idrees
47-
* @author Mohsin Husen
30+
* @author Young Gu
31+
* @author Oliver Gierke
4832
*/
49-
5033
@RunWith(SpringJUnit4ClassRunner.class)
51-
@ContextConfiguration("classpath:/immutable-repository-test.xml")
34+
@ContextConfiguration("classpath:immutable-repository-test.xml")
5235
public class ImmutableElasticsearchRepositoryTests {
5336

54-
@Autowired
55-
private ImmutableElasticsearchRepository repository;
56-
57-
@Autowired
58-
private ElasticsearchTemplate elasticsearchTemplate;
37+
@Autowired ImmutableElasticsearchRepository repository;
38+
@Autowired ElasticsearchOperations operations;
5939

6040

6141
@Before
6242
public void before() {
63-
elasticsearchTemplate.deleteIndex(ImmutableEntity.class);
64-
elasticsearchTemplate.createIndex(ImmutableEntity.class);
65-
elasticsearchTemplate.refresh(ImmutableEntity.class);
43+
44+
operations.deleteIndex(ImmutableEntity.class);
45+
operations.createIndex(ImmutableEntity.class);
46+
operations.refresh(ImmutableEntity.class);
6647
}
48+
49+
/**
50+
* @see DATAES-281
51+
*/
6752
@Test
6853
public void shouldSaveAndFindImmutableDocument() {
54+
6955
// when
7056
ImmutableEntity entity = repository.save(new ImmutableEntity("test name"));
7157
assertThat(entity.getId(), is(notNullValue()));
58+
7259
// then
7360
ImmutableEntity entityFromElasticSearch = repository.findOne(entity.getId());
74-
assertThat(entityFromElasticSearch.getName(), new IsEqual("test name"));
75-
assertThat(entityFromElasticSearch.getId(), new IsEqual(entity.getId()));
76-
61+
62+
assertThat(entityFromElasticSearch.getName(), is("test name"));
63+
assertThat(entityFromElasticSearch.getId(), is(entity.getId()));
7764
}
78-
7965
}

src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableEntity.java

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,24 @@
1515
*/
1616
package org.springframework.data.elasticsearch.immutable;
1717

18-
import lombok.*;
19-
import org.springframework.data.annotation.AccessType;
20-
import org.springframework.data.annotation.Id;
21-
import org.springframework.data.annotation.Version;
18+
import lombok.Getter;
19+
import lombok.NoArgsConstructor;
20+
2221
import org.springframework.data.elasticsearch.annotations.Document;
23-
import org.springframework.data.elasticsearch.annotations.Field;
24-
import org.springframework.data.elasticsearch.annotations.FieldType;
25-
import org.springframework.data.elasticsearch.annotations.ScriptedField;
26-
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
2722

2823
/**
29-
* @author Rizwan Idrees
30-
* @author Mohsin Husen
24+
* @author Young Gu
25+
* @author Oliver Gierke
3126
*/
32-
@AccessType(AccessType.Type.FIELD)
33-
@Document(indexName = "test-index", type = "immutable-entity", shards = 1, replicas = 0, refreshInterval = "-1")
27+
@Document(indexName = "test-index")
28+
@NoArgsConstructor(force = true)
29+
@Getter
3430
public class ImmutableEntity {
35-
36-
@Id
37-
private String id;
38-
private String name;
39-
40-
private ImmutableEntity() {
41-
}
42-
31+
private final String id, name;
32+
4333
public ImmutableEntity(String name) {
34+
35+
this.id = null;
4436
this.name = name;
4537
}
46-
47-
public String getId() {
48-
return id;
49-
}
50-
51-
public String getName() {
52-
return name;
53-
}
54-
5538
}

0 commit comments

Comments
 (0)