DATAES-198 - Fixed @Version annotation on fields.
This commit is contained in:
@@ -15,13 +15,17 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.util.ArrayIterator;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.get.MultiGetItemResponse;
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.document.DocumentField;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
@@ -39,6 +43,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
||||
import org.springframework.data.elasticsearch.entities.Car;
|
||||
import org.springframework.data.elasticsearch.entities.SampleEntity;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
@@ -48,6 +53,7 @@ import static org.mockito.Mockito.*;
|
||||
/**
|
||||
* @author Artur Konczak
|
||||
* @author Mohsin Husen
|
||||
* @author Chris White
|
||||
* @author Mark Paluch
|
||||
* @author Ilkang Na
|
||||
*/
|
||||
@@ -152,6 +158,69 @@ public class DefaultResultMapperTests {
|
||||
assertThat(result.getId(), is("identifier"));
|
||||
}
|
||||
|
||||
@Test // DATAES-198
|
||||
public void setsVersionFromGetResponse() {
|
||||
GetResponse response = mock(GetResponse.class);
|
||||
when(response.getSourceAsString()).thenReturn("{}");
|
||||
when(response.getVersion()).thenReturn(1234L);
|
||||
|
||||
SampleEntity result = resultMapper.mapResult(response, SampleEntity.class);
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.getVersion(), is(1234L));
|
||||
}
|
||||
|
||||
@Test // DATAES-198
|
||||
public void setsVersionFromMultiGetResponse() {
|
||||
GetResponse response1 = mock(GetResponse.class);
|
||||
when(response1.getSourceAsString()).thenReturn("{}");
|
||||
when(response1.isExists()).thenReturn(true);
|
||||
when(response1.getVersion()).thenReturn(1234L);
|
||||
|
||||
GetResponse response2 = mock(GetResponse.class);
|
||||
when(response2.getSourceAsString()).thenReturn("{}");
|
||||
when(response2.isExists()).thenReturn(true);
|
||||
when(response2.getVersion()).thenReturn(5678L);
|
||||
|
||||
MultiGetResponse multiResponse = mock(MultiGetResponse.class);
|
||||
when(multiResponse.getResponses()).thenReturn(new MultiGetItemResponse[] {
|
||||
new MultiGetItemResponse(response1, null), new MultiGetItemResponse(response2, null) });
|
||||
|
||||
LinkedList<SampleEntity> results = resultMapper.mapResults(multiResponse, SampleEntity.class);
|
||||
|
||||
assertThat(results, is(notNullValue()));
|
||||
assertThat(results, hasSize(2));
|
||||
|
||||
assertThat(results.get(0).getVersion(), is(1234L));
|
||||
assertThat(results.get(1).getVersion(), is(5678L));
|
||||
}
|
||||
|
||||
@Test // DATAES-198
|
||||
public void setsVersionFromSearchResponse() {
|
||||
SearchHit hit1 = mock(SearchHit.class);
|
||||
when(hit1.getSourceAsString()).thenReturn("{}");
|
||||
when(hit1.getVersion()).thenReturn(1234L);
|
||||
|
||||
SearchHit hit2 = mock(SearchHit.class);
|
||||
when(hit2.getSourceAsString()).thenReturn("{}");
|
||||
when(hit2.getVersion()).thenReturn(5678L);
|
||||
|
||||
SearchHits searchHits = mock(SearchHits.class);
|
||||
when(searchHits.getTotalHits()).thenReturn(2L);
|
||||
when(searchHits.iterator()).thenReturn(Arrays.asList(hit1, hit2).iterator());
|
||||
|
||||
SearchResponse searchResponse = mock(SearchResponse.class);
|
||||
when(searchResponse.getHits()).thenReturn(searchHits);
|
||||
|
||||
AggregatedPage<SampleEntity> results = resultMapper.mapResults(searchResponse, SampleEntity.class,
|
||||
mock(Pageable.class));
|
||||
|
||||
assertThat(results, is(notNullValue()));
|
||||
|
||||
assertThat(results.getContent().get(0).getVersion(), is(1234L));
|
||||
assertThat(results.getContent().get(1).getVersion(), is(5678L));
|
||||
}
|
||||
|
||||
private Aggregation createCarAggregation() {
|
||||
Aggregation aggregation = mock(Terms.class);
|
||||
when(aggregation.getName()).thenReturn("Diesel");
|
||||
|
||||
@@ -71,6 +71,7 @@ import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
|
||||
* @author Abdul Mohammed
|
||||
* @author Kevin Leturc
|
||||
* @author Mason Chan
|
||||
* @author Chris White
|
||||
* @author Ilkang Na
|
||||
* @author Alen Turkovic
|
||||
*/
|
||||
@@ -2017,13 +2018,17 @@ public class ElasticsearchTemplateTests {
|
||||
}
|
||||
|
||||
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
|
||||
return new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity).build();
|
||||
return new IndexQueryBuilder()
|
||||
.withId(sampleEntity.getId())
|
||||
.withObject(sampleEntity)
|
||||
.withVersion(sampleEntity.getVersion())
|
||||
.build();
|
||||
}
|
||||
|
||||
private List<IndexQuery> getIndexQueries(List<SampleEntity> sampleEntities) {
|
||||
List<IndexQuery> indexQueries = new ArrayList<>();
|
||||
for (SampleEntity sampleEntity : sampleEntities) {
|
||||
indexQueries.add(new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity).build());
|
||||
indexQueries.add(getIndexQuery(sampleEntity));
|
||||
}
|
||||
return indexQueries;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Version;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
@@ -34,12 +35,14 @@ import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Chris White
|
||||
*/
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@ToString
|
||||
@Builder
|
||||
@Document(indexName = "test-index-sample", type = "test-type", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class SampleEntity {
|
||||
|
||||
Reference in New Issue
Block a user