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.
This commit is contained in:
Oliver Gierke
2016-08-20 09:38:34 +02:00
parent 7e736a6271
commit 7113c7ac66
6 changed files with 96 additions and 104 deletions

View File

@@ -19,8 +19,14 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.util.ArrayIterator;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
@@ -35,8 +41,13 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.DefaultResultMapperTests.ImmutableEntity;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.entities.Car;
/**
@@ -53,7 +64,7 @@ public class DefaultResultMapperTests {
@Before
public void init() {
MockitoAnnotations.initMocks(this);
resultMapper = new DefaultResultMapper();
resultMapper = new DefaultResultMapper(new SimpleElasticsearchMappingContext());
}
@Test
@@ -132,6 +143,22 @@ public class DefaultResultMapperTests {
assertThat(result.getModel(), is("Grat"));
assertThat(result.getName(), is("Ford"));
}
/**
* @see DATAES-281.
*/
@Test
public void setsIdentifierOnImmutableType() {
GetResponse response = mock(GetResponse.class);
when(response.getSourceAsString()).thenReturn("{}");
when(response.getId()).thenReturn("identifier");
ImmutableEntity result = resultMapper.mapResult(response, ImmutableEntity.class);
assertThat(result, is(notNullValue()));
assertThat(result.getId(), is("identifier"));
}
private Aggregation createCarAggregation() {
Aggregation aggregation = mock(Terms.class);
@@ -166,4 +193,12 @@ public class DefaultResultMapperTests {
result.put("model", new InternalSearchHitField("model", Arrays.<Object>asList(model)));
return result;
}
@Document(indexName = "someIndex")
@NoArgsConstructor(force = true)
@Getter
static class ImmutableEntity {
private final String id, name;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2016 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.
@@ -15,14 +15,10 @@
*/
package org.springframework.data.elasticsearch.immutable;
import org.springframework.data.elasticsearch.entities.SampleEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Young Gu
* @author Oliver Gierke
*/
public interface ImmutableElasticsearchRepository extends ElasticsearchRepository<ImmutableEntity, String> {
}
public interface ImmutableElasticsearchRepository extends CrudRepository<ImmutableEntity, String> {}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2016 the original author or authors.
* Copyright 2016 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.
@@ -15,65 +15,51 @@
*/
package org.springframework.data.elasticsearch.immutable;
import com.google.common.collect.Lists;
import org.hamcrest.core.IsEqual;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.entities.SampleEntity;
import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Young Gu
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/immutable-repository-test.xml")
@ContextConfiguration("classpath:immutable-repository-test.xml")
public class ImmutableElasticsearchRepositoryTests {
@Autowired
private ImmutableElasticsearchRepository repository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired ImmutableElasticsearchRepository repository;
@Autowired ElasticsearchOperations operations;
@Before
public void before() {
elasticsearchTemplate.deleteIndex(ImmutableEntity.class);
elasticsearchTemplate.createIndex(ImmutableEntity.class);
elasticsearchTemplate.refresh(ImmutableEntity.class);
operations.deleteIndex(ImmutableEntity.class);
operations.createIndex(ImmutableEntity.class);
operations.refresh(ImmutableEntity.class);
}
/**
* @see DATAES-281
*/
@Test
public void shouldSaveAndFindImmutableDocument() {
// when
ImmutableEntity entity = repository.save(new ImmutableEntity("test name"));
assertThat(entity.getId(), is(notNullValue()));
// then
ImmutableEntity entityFromElasticSearch = repository.findOne(entity.getId());
assertThat(entityFromElasticSearch.getName(), new IsEqual("test name"));
assertThat(entityFromElasticSearch.getId(), new IsEqual(entity.getId()));
assertThat(entityFromElasticSearch.getName(), is("test name"));
assertThat(entityFromElasticSearch.getId(), is(entity.getId()));
}
}

View File

@@ -15,41 +15,24 @@
*/
package org.springframework.data.elasticsearch.immutable;
import lombok.*;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.ScriptedField;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Young Gu
* @author Oliver Gierke
*/
@AccessType(AccessType.Type.FIELD)
@Document(indexName = "test-index", type = "immutable-entity", shards = 1, replicas = 0, refreshInterval = "-1")
@Document(indexName = "test-index")
@NoArgsConstructor(force = true)
@Getter
public class ImmutableEntity {
@Id
private String id;
private String name;
private ImmutableEntity() {
}
private final String id, name;
public ImmutableEntity(String name) {
this.id = null;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}