DATAES-352 - Adapt to API changes in repository interfaces.

We now follow a more consistent naming scheme for the methods in repository that are driven by the following guidelines:

* Methods referring to an identifier now all end on …ById(…).
* Methods taking or returning a collection are named …All(…)

Please see DATACMNS-944 for details.
This commit is contained in:
Christoph Strobl 2017-05-03 15:11:34 +02:00 committed by Oliver Gierke
parent 939c0f96f4
commit 416a3f2baf
19 changed files with 158 additions and 141 deletions

View File

@ -30,9 +30,18 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.*; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.query.*; import org.springframework.data.elasticsearch.core.query.DeleteQuery;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -45,6 +54,7 @@ import org.springframework.util.Assert;
* @author Ryan Henszey * @author Ryan Henszey
* @author Kevin Leturc * @author Kevin Leturc
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
public abstract class AbstractElasticsearchRepository<T, ID extends Serializable> public abstract class AbstractElasticsearchRepository<T, ID extends Serializable>
implements ElasticsearchRepository<T, ID> { implements ElasticsearchRepository<T, ID> {
@ -57,18 +67,18 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
public AbstractElasticsearchRepository() {} public AbstractElasticsearchRepository() {}
public AbstractElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) { public AbstractElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) {
Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null!"); Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null!");
this.setElasticsearchOperations(elasticsearchOperations); this.setElasticsearchOperations(elasticsearchOperations);
} }
public AbstractElasticsearchRepository(ElasticsearchEntityInformation<T, ID> metadata, public AbstractElasticsearchRepository(ElasticsearchEntityInformation<T, ID> metadata,
ElasticsearchOperations elasticsearchOperations) { ElasticsearchOperations elasticsearchOperations) {
this(elasticsearchOperations); this(elasticsearchOperations);
Assert.notNull(metadata, "ElasticsearchEntityInformation must not be null!"); Assert.notNull(metadata, "ElasticsearchEntityInformation must not be null!");
this.entityInformation = metadata; this.entityInformation = metadata;
setEntityClass(this.entityInformation.getJavaType()); setEntityClass(this.entityInformation.getJavaType());
try { try {
@ -94,7 +104,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
} }
@Override @Override
public Optional<T> findOne(ID id) { public Optional<T> findById(ID id) {
GetQuery query = new GetQuery(); GetQuery query = new GetQuery();
query.setId(stringIdRepresentation(id)); query.setId(stringIdRepresentation(id));
return Optional.ofNullable(elasticsearchOperations.queryForObject(query, getEntityClass())); return Optional.ofNullable(elasticsearchOperations.queryForObject(query, getEntityClass()));
@ -104,7 +114,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
public Iterable<T> findAll() { public Iterable<T> findAll() {
int itemCount = (int) this.count(); int itemCount = (int) this.count();
if (itemCount == 0) { if (itemCount == 0) {
return new PageImpl<>(Collections.<T>emptyList()); return new PageImpl<>(Collections.<T> emptyList());
} }
return this.findAll(PageRequest.of(0, Math.max(1, itemCount))); return this.findAll(PageRequest.of(0, Math.max(1, itemCount)));
} }
@ -119,7 +129,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
public Iterable<T> findAll(Sort sort) { public Iterable<T> findAll(Sort sort) {
int itemCount = (int) this.count(); int itemCount = (int) this.count();
if (itemCount == 0) { if (itemCount == 0) {
return new PageImpl<>(Collections.<T>emptyList()); return new PageImpl<>(Collections.<T> emptyList());
} }
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
.withPageable(PageRequest.of(0, itemCount, sort)).build(); .withPageable(PageRequest.of(0, itemCount, sort)).build();
@ -127,7 +137,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
} }
@Override @Override
public Iterable<T> findAll(Iterable<ID> ids) { public Iterable<T> findAllById(Iterable<ID> ids) {
Assert.notNull(ids, "ids can't be null."); Assert.notNull(ids, "ids can't be null.");
SearchQuery query = new NativeSearchQueryBuilder().withIds(stringIdsRepresentation(ids)).build(); SearchQuery query = new NativeSearchQueryBuilder().withIds(stringIdsRepresentation(ids)).build();
return elasticsearchOperations.multiGet(query, getEntityClass()); return elasticsearchOperations.multiGet(query, getEntityClass());
@ -165,7 +175,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
} }
@Override @Override
public <S extends T> Iterable<S> save(Iterable<S> entities) { public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "Cannot insert 'null' as a List."); Assert.notNull(entities, "Cannot insert 'null' as a List.");
List<IndexQuery> queries = new ArrayList<>(); List<IndexQuery> queries = new ArrayList<>();
for (S s : entities) { for (S s : entities) {
@ -177,8 +187,8 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
} }
@Override @Override
public boolean exists(ID id) { public boolean existsById(ID id) {
return findOne(id) != null; return findById(id) != null;
} }
@Override @Override
@ -186,7 +196,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(query).build(); SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(query).build();
int count = (int) elasticsearchOperations.count(searchQuery, getEntityClass()); int count = (int) elasticsearchOperations.count(searchQuery, getEntityClass());
if (count == 0) { if (count == 0) {
return new PageImpl<>(Collections.<T>emptyList()); return new PageImpl<>(Collections.<T> emptyList());
} }
searchQuery.setPageable(PageRequest.of(0, count)); searchQuery.setPageable(PageRequest.of(0, count));
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass()); return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
@ -217,7 +227,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
} }
@Override @Override
public void delete(ID id) { public void deleteById(ID id) {
Assert.notNull(id, "Cannot delete entity with id 'null'."); Assert.notNull(id, "Cannot delete entity with id 'null'.");
elasticsearchOperations.delete(entityInformation.getIndexName(), entityInformation.getType(), elasticsearchOperations.delete(entityInformation.getIndexName(), entityInformation.getType(),
stringIdRepresentation(id)); stringIdRepresentation(id));
@ -227,12 +237,12 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
@Override @Override
public void delete(T entity) { public void delete(T entity) {
Assert.notNull(entity, "Cannot delete 'null' entity."); Assert.notNull(entity, "Cannot delete 'null' entity.");
delete(extractIdFromBean(entity)); deleteById(extractIdFromBean(entity));
elasticsearchOperations.refresh(entityInformation.getIndexName()); elasticsearchOperations.refresh(entityInformation.getIndexName());
} }
@Override @Override
public void delete(Iterable<? extends T> entities) { public void deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "Cannot delete 'null' list."); Assert.notNull(entities, "Cannot delete 'null' list.");
for (T entity : entities) { for (T entity : entities) {
delete(entity); delete(entity);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,8 +15,6 @@
*/ */
package org.springframework.data.elasticsearch.repository.support; package org.springframework.data.elasticsearch.repository.support;
import java.io.Serializable;
import org.springframework.data.repository.core.EntityInformation; import org.springframework.data.repository.core.EntityInformation;
/** /**
@ -24,8 +22,9 @@ import org.springframework.data.repository.core.EntityInformation;
* @param <ID> * @param <ID>
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Christoph Strobl
*/ */
public interface ElasticsearchEntityInformation<T, ID extends Serializable> extends EntityInformation<T, ID> { public interface ElasticsearchEntityInformation<T, ID> extends EntityInformation<T, ID> {
String getIdAttribute(); String getIdAttribute();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,15 +15,14 @@
*/ */
package org.springframework.data.elasticsearch.repository.support; package org.springframework.data.elasticsearch.repository.support;
import java.io.Serializable;
/** /**
* ElasticsearchEntityInformationCreator * ElasticsearchEntityInformationCreator
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Christoph Strobl
*/ */
public interface ElasticsearchEntityInformationCreator { public interface ElasticsearchEntityInformationCreator {
<T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass); <T, ID> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass);
} }

View File

@ -15,8 +15,6 @@
*/ */
package org.springframework.data.elasticsearch.repository.support; package org.springframework.data.elasticsearch.repository.support;
import java.io.Serializable;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
@ -29,6 +27,7 @@ import org.springframework.util.Assert;
* @author Mohsin Husen * @author Mohsin Husen
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
public class ElasticsearchEntityInformationCreatorImpl implements ElasticsearchEntityInformationCreator { public class ElasticsearchEntityInformationCreatorImpl implements ElasticsearchEntityInformationCreator {
@ -36,15 +35,15 @@ public class ElasticsearchEntityInformationCreatorImpl implements ElasticsearchE
public ElasticsearchEntityInformationCreatorImpl( public ElasticsearchEntityInformationCreatorImpl(
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) { MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
Assert.notNull(mappingContext, "MappingContext must not be null!"); Assert.notNull(mappingContext, "MappingContext must not be null!");
this.mappingContext = mappingContext; this.mappingContext = mappingContext;
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) { public <T, ID> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
ElasticsearchPersistentEntity<T> persistentEntity = (ElasticsearchPersistentEntity<T>) mappingContext ElasticsearchPersistentEntity<T> persistentEntity = (ElasticsearchPersistentEntity<T>) mappingContext
.getRequiredPersistentEntity(domainClass); .getRequiredPersistentEntity(domainClass);

View File

@ -17,7 +17,6 @@ package org.springframework.data.elasticsearch.repository.support;
import static org.springframework.data.querydsl.QuerydslUtils.*; import static org.springframework.data.querydsl.QuerydslUtils.*;
import java.io.Serializable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@ -47,6 +46,7 @@ import org.springframework.util.Assert;
* @author Ryan Henszey * @author Ryan Henszey
* @author Gad Akuka * @author Gad Akuka
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport { public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
@ -54,16 +54,16 @@ public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
private final ElasticsearchEntityInformationCreator entityInformationCreator; private final ElasticsearchEntityInformationCreator entityInformationCreator;
public ElasticsearchRepositoryFactory(ElasticsearchOperations elasticsearchOperations) { public ElasticsearchRepositoryFactory(ElasticsearchOperations elasticsearchOperations) {
Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null!"); Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null!");
this.elasticsearchOperations = elasticsearchOperations; this.elasticsearchOperations = elasticsearchOperations;
this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl( this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(
elasticsearchOperations.getElasticsearchConverter().getMappingContext()); elasticsearchOperations.getElasticsearchConverter().getMappingContext());
} }
@Override @Override
public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) { public <T, ID> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
return entityInformationCreator.getEntityInformation(domainClass); return entityInformationCreator.getEntityInformation(domainClass);
} }

View File

@ -15,7 +15,6 @@
*/ */
package org.springframework.data.elasticsearch.repository.support; package org.springframework.data.elasticsearch.repository.support;
import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
@ -34,9 +33,10 @@ import org.springframework.util.Assert;
* @author Ryan Henszey * @author Ryan Henszey
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
public class MappingElasticsearchEntityInformation<T, ID extends Serializable> public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEntityInformation<T, ID>
extends PersistentEntityInformation<T, ID> implements ElasticsearchEntityInformation<T, ID> { implements ElasticsearchEntityInformation<T, ID> {
private final ElasticsearchPersistentEntity<T> entityMetadata; private final ElasticsearchPersistentEntity<T> entityMetadata;
private final String indexName; private final String indexName;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2016 the original author or authors. * Copyright 2014-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,17 +33,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** /**
* @author Mohsin Husen * @author Mohsin Husen
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/repository-test-nested-object.xml") @ContextConfiguration("classpath:/repository-test-nested-object.xml")
public class InnerObjectTests { public class InnerObjectTests {
@Autowired @Autowired private SampleElasticSearchBookRepository bookRepository;
private SampleElasticSearchBookRepository bookRepository;
@Autowired @Autowired private ElasticsearchTemplate elasticsearchTemplate;
private ElasticsearchTemplate elasticsearchTemplate;
@Before @Before
public void before() { public void before() {
@ -67,6 +66,6 @@ public class InnerObjectTests {
// when // when
bookRepository.save(book); bookRepository.save(book);
// then // then
assertThat(bookRepository.findOne(id), is(notNullValue())); assertThat(bookRepository.findById(id), is(notNullValue()));
} }
} }

View File

@ -32,6 +32,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Young Gu * @author Young Gu
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:immutable-repository-test.xml") @ContextConfiguration("classpath:immutable-repository-test.xml")
@ -59,7 +60,7 @@ public class ImmutableElasticsearchRepositoryTests {
assertThat(entity.getId(), is(notNullValue())); assertThat(entity.getId(), is(notNullValue()));
// then // then
Optional<ImmutableEntity> entityFromElasticSearch = repository.findOne(entity.getId()); Optional<ImmutableEntity> entityFromElasticSearch = repository.findById(entity.getId());
assertThat(entityFromElasticSearch.isPresent(), is(true)); assertThat(entityFromElasticSearch.isPresent(), is(true));

View File

@ -47,16 +47,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Mohsin Husen * @author Mohsin Husen
* @author Franck Marchand * @author Franck Marchand
* @author Kevin Leturc * @author Kevin Leturc
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:custom-method-repository-test.xml") @ContextConfiguration("classpath:custom-method-repository-test.xml")
public class CustomMethodRepositoryTests { public class CustomMethodRepositoryTests {
@Autowired @Autowired private SampleCustomMethodRepository repository;
private SampleCustomMethodRepository repository;
@Autowired @Autowired private ElasticsearchTemplate elasticsearchTemplate;
private ElasticsearchTemplate elasticsearchTemplate;
@Before @Before
public void before() { public void before() {
@ -460,8 +459,8 @@ public class CustomMethodRepositoryTests {
sampleEntity.setVersion(System.currentTimeMillis()); sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity3); repository.save(sampleEntity3);
// when // when
Page<SampleEntity> pageResult = repository.findByMessageContaining("a", new PageRequest(0, 23, new Sort( Page<SampleEntity> pageResult = repository.findByMessageContaining("a",
new Sort.Order(Sort.Direction.DESC, "message")))); new PageRequest(0, 23, new Sort(new Sort.Order(Sort.Direction.DESC, "message"))));
// then // then
assertThat(pageResult.getContent().isEmpty(), is(false)); assertThat(pageResult.getContent().isEmpty(), is(false));
assertThat(pageResult.getContent().get(0).getMessage(), is(sampleEntity3.getMessage())); assertThat(pageResult.getContent().get(0).getMessage(), is(sampleEntity3.getMessage()));
@ -541,7 +540,8 @@ public class CustomMethodRepositoryTests {
repository.save(sampleEntity); repository.save(sampleEntity);
// when // when
Page<SampleEntity> page = repository.findByLocationAndMessage(new GeoPoint(45.7806d, 3.0875d), "foo", new PageRequest(0, 10)); Page<SampleEntity> page = repository.findByLocationAndMessage(new GeoPoint(45.7806d, 3.0875d), "foo",
new PageRequest(0, 10));
// then // then
assertThat(page, is(notNullValue())); assertThat(page, is(notNullValue()));
assertThat(page.getTotalElements(), is(equalTo(1L))); assertThat(page.getTotalElements(), is(equalTo(1L)));
@ -561,7 +561,8 @@ public class CustomMethodRepositoryTests {
repository.save(sampleEntity); repository.save(sampleEntity);
// when // when
Page<SampleEntity> page = repository.findByLocationWithin(new GeoPoint(45.7806d, 3.0875d), "2km", new PageRequest(0, 10)); Page<SampleEntity> page = repository.findByLocationWithin(new GeoPoint(45.7806d, 3.0875d), "2km",
new PageRequest(0, 10));
// then // then
assertThat(page, is(notNullValue())); assertThat(page, is(notNullValue()));
assertThat(page.getTotalElements(), is(equalTo(1L))); assertThat(page.getTotalElements(), is(equalTo(1L)));
@ -581,7 +582,8 @@ public class CustomMethodRepositoryTests {
repository.save(sampleEntity); repository.save(sampleEntity);
// when // when
Page<SampleEntity> page = repository.findByLocationWithin(new Point(45.7806d, 3.0875d), new Distance(2, Metrics.KILOMETERS), new PageRequest(0, 10)); Page<SampleEntity> page = repository.findByLocationWithin(new Point(45.7806d, 3.0875d),
new Distance(2, Metrics.KILOMETERS), new PageRequest(0, 10));
// then // then
assertThat(page, is(notNullValue())); assertThat(page, is(notNullValue()));
assertThat(page.getTotalElements(), is(equalTo(1L))); assertThat(page.getTotalElements(), is(equalTo(1L)));
@ -617,7 +619,8 @@ public class CustomMethodRepositoryTests {
assertThat(pageAll.getTotalElements(), is(equalTo(2L))); assertThat(pageAll.getTotalElements(), is(equalTo(2L)));
// when // when
Page<SampleEntity> page = repository.findByLocationNear(new Box(new Point(46d, 3d), new Point(45d, 4d)), new PageRequest(0, 10)); Page<SampleEntity> page = repository.findByLocationNear(new Box(new Point(46d, 3d), new Point(45d, 4d)),
new PageRequest(0, 10));
// then // then
assertThat(page, is(notNullValue())); assertThat(page, is(notNullValue()));
assertThat(page.getTotalElements(), is(equalTo(1L))); assertThat(page.getTotalElements(), is(equalTo(1L)));
@ -637,7 +640,8 @@ public class CustomMethodRepositoryTests {
repository.save(sampleEntity); repository.save(sampleEntity);
// when // when
Page<SampleEntity> page = repository.findByLocationNear(new Point(45.7806d, 3.0875d), new Distance(2, Metrics.KILOMETERS), new PageRequest(0, 10)); Page<SampleEntity> page = repository.findByLocationNear(new Point(45.7806d, 3.0875d),
new Distance(2, Metrics.KILOMETERS), new PageRequest(0, 10));
// then // then
assertThat(page, is(notNullValue())); assertThat(page, is(notNullValue()));
assertThat(page.getTotalElements(), is(equalTo(1L))); assertThat(page.getTotalElements(), is(equalTo(1L)));
@ -650,7 +654,7 @@ public class CustomMethodRepositoryTests {
public void shouldAllowReturningJava8StreamInCustomQuery() { public void shouldAllowReturningJava8StreamInCustomQuery() {
// given // given
List<SampleEntity> entities = createSampleEntities("abc", 30); List<SampleEntity> entities = createSampleEntities("abc", 30);
repository.save(entities); repository.saveAll(entities);
// when // when
Stream<SampleEntity> stream = repository.findByType("abc"); Stream<SampleEntity> stream = repository.findByType("abc");
@ -1206,4 +1210,3 @@ public class CustomMethodRepositoryTests {
return entities; return entities;
} }
} }

View File

@ -24,8 +24,9 @@ import org.springframework.data.repository.CrudRepository;
* @author Mohsin Husen * @author Mohsin Husen
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
public interface CdiProductRepository extends CrudRepository<Product, String> { public interface CdiProductRepository extends CrudRepository<Product, String> {
Optional<Product> findOne(String id); Optional<Product> findById(String id);
} }

View File

@ -31,6 +31,7 @@ import org.springframework.data.elasticsearch.entities.Product;
/** /**
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
public class CdiRepositoryTests { public class CdiRepositoryTests {
@ -70,9 +71,9 @@ public class CdiRepositoryTests {
repository.save(bean); repository.save(bean);
assertTrue(repository.exists(bean.getId())); assertTrue(repository.existsById(bean.getId()));
Optional<Product> retrieved = repository.findOne(bean.getId()); Optional<Product> retrieved = repository.findById(bean.getId());
assertTrue(retrieved.isPresent()); assertTrue(retrieved.isPresent());
retrieved.ifPresent(product -> { retrieved.ifPresent(product -> {
@ -82,12 +83,12 @@ public class CdiRepositoryTests {
assertEquals(1, repository.count()); assertEquals(1, repository.count());
assertTrue(repository.exists(bean.getId())); assertTrue(repository.existsById(bean.getId()));
repository.delete(bean); repository.delete(bean);
assertEquals(0, repository.count()); assertEquals(0, repository.count());
retrieved = repository.findOne(bean.getId()); retrieved = repository.findById(bean.getId());
assertFalse(retrieved.isPresent()); assertFalse(retrieved.isPresent());
} }
@ -104,9 +105,9 @@ public class CdiRepositoryTests {
qualifiedProductRepository.save(bean); qualifiedProductRepository.save(bean);
assertTrue(qualifiedProductRepository.exists(bean.getId())); assertTrue(qualifiedProductRepository.existsById(bean.getId()));
Optional<Product> retrieved = qualifiedProductRepository.findOne(bean.getId()); Optional<Product> retrieved = qualifiedProductRepository.findById(bean.getId());
assertTrue(retrieved.isPresent()); assertTrue(retrieved.isPresent());
retrieved.ifPresent(product -> { retrieved.ifPresent(product -> {
@ -116,12 +117,12 @@ public class CdiRepositoryTests {
assertEquals(1, qualifiedProductRepository.count()); assertEquals(1, qualifiedProductRepository.count());
assertTrue(qualifiedProductRepository.exists(bean.getId())); assertTrue(qualifiedProductRepository.existsById(bean.getId()));
qualifiedProductRepository.delete(bean); qualifiedProductRepository.delete(bean);
assertEquals(0, qualifiedProductRepository.count()); assertEquals(0, qualifiedProductRepository.count());
retrieved = qualifiedProductRepository.findOne(bean.getId()); retrieved = qualifiedProductRepository.findById(bean.getId());
assertFalse(retrieved.isPresent()); assertFalse(retrieved.isPresent());
} }

View File

@ -34,6 +34,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** /**
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/repository-spring-data-geo-support.xml") @ContextConfiguration("classpath:/repository-spring-data-geo-support.xml")
@ -59,7 +60,7 @@ public class SpringDataGeoRepositoryTests {
.pointC(toGeoString(point)).pointD(toGeoArray(point)).build(); .pointC(toGeoString(point)).pointD(toGeoArray(point)).build();
// when // when
GeoEntity saved = repository.save(entity); GeoEntity saved = repository.save(entity);
Optional<GeoEntity> result = repository.findOne(entity.getId()); Optional<GeoEntity> result = repository.findById(entity.getId());
// then // then
assertThat(result.isPresent(), is(true)); assertThat(result.isPresent(), is(true));

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,12 +23,16 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
/** /**
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Christoph Strobl
*/ */
public interface SampleElasticsearchRepository extends ElasticsearchRepository<SampleEntity, String> { public interface SampleElasticsearchRepository extends ElasticsearchRepository<SampleEntity, String> {
long deleteById(String id); long deleteSampleEntityById(String id);
List<SampleEntity> deleteByAvailable(boolean available); List<SampleEntity> deleteByAvailable(boolean available);
List<SampleEntity> deleteByMessage(String message); List<SampleEntity> deleteByMessage(String message);
void deleteByType(String type); void deleteByType(String type);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,12 +23,16 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
/** /**
* @author Gad Akuka * @author Gad Akuka
* @author Christoph Strobl
*/ */
public interface SampleUUIDKeyedElasticsearchRepository extends ElasticsearchRepository<SampleEntityUUIDKeyed, UUID> { public interface SampleUUIDKeyedElasticsearchRepository extends ElasticsearchRepository<SampleEntityUUIDKeyed, UUID> {
long deleteById(UUID id); long deleteSampleEntityUUIDKeyedById(UUID id);
List<SampleEntityUUIDKeyed> deleteByAvailable(boolean available); List<SampleEntityUUIDKeyed> deleteByAvailable(boolean available);
List<SampleEntityUUIDKeyed> deleteByMessage(String message); List<SampleEntityUUIDKeyed> deleteByMessage(String message);
void deleteByType(String type); void deleteByType(String type);
} }

View File

@ -36,17 +36,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/double-id-repository-test.xml") @ContextConfiguration("classpath:/double-id-repository-test.xml")
public class DoubleIDRepositoryTests { public class DoubleIDRepositoryTests {
@Autowired @Autowired private DoubleIDRepository repository;
private DoubleIDRepository repository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
@Before @Before
public void before() { public void before() {
@ -71,12 +69,12 @@ public class DoubleIDRepositoryTests {
sampleEntity2.setVersion(System.currentTimeMillis()); sampleEntity2.setVersion(System.currentTimeMillis());
// when // when
repository.save(Arrays.asList(sampleEntity1, sampleEntity2)); repository.saveAll(Arrays.asList(sampleEntity1, sampleEntity2));
// then // then
Optional<DoubleIDEntity> entity1FromElasticSearch = repository.findOne(documentId1); Optional<DoubleIDEntity> entity1FromElasticSearch = repository.findById(documentId1);
assertThat(entity1FromElasticSearch.isPresent(), is(true)); assertThat(entity1FromElasticSearch.isPresent(), is(true));
Optional<DoubleIDEntity> entity2FromElasticSearch = repository.findOne(documentId2); Optional<DoubleIDEntity> entity2FromElasticSearch = repository.findById(documentId2);
assertThat(entity2FromElasticSearch.isPresent(), is(true)); assertThat(entity2FromElasticSearch.isPresent(), is(true));
} }
@ -91,7 +89,7 @@ public class DoubleIDRepositoryTests {
// when // when
repository.save(sampleEntity); repository.save(sampleEntity);
// then // then
Optional<DoubleIDEntity> entityFromElasticSearch = repository.findOne(documentId); Optional<DoubleIDEntity> entityFromElasticSearch = repository.findById(documentId);
assertThat(entityFromElasticSearch.isPresent(), is(true)); assertThat(entityFromElasticSearch.isPresent(), is(true));
} }
} }

View File

@ -36,17 +36,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/integer-id-repository-test.xml") @ContextConfiguration("classpath:/integer-id-repository-test.xml")
public class IntegerIDRepositoryTests { public class IntegerIDRepositoryTests {
@Autowired @Autowired private IntegerIDRepository repository;
private IntegerIDRepository repository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
@Before @Before
public void before() { public void before() {
@ -71,12 +69,12 @@ public class IntegerIDRepositoryTests {
sampleEntity2.setVersion(System.currentTimeMillis()); sampleEntity2.setVersion(System.currentTimeMillis());
// when // when
repository.save(Arrays.asList(sampleEntity1, sampleEntity2)); repository.saveAll(Arrays.asList(sampleEntity1, sampleEntity2));
// then // then
Optional<IntegerIDEntity> entity1FromElasticSearch = repository.findOne(documentId1); Optional<IntegerIDEntity> entity1FromElasticSearch = repository.findById(documentId1);
assertThat(entity1FromElasticSearch.isPresent(), is(true)); assertThat(entity1FromElasticSearch.isPresent(), is(true));
Optional<IntegerIDEntity> entity2FromElasticSearch = repository.findOne(documentId2); Optional<IntegerIDEntity> entity2FromElasticSearch = repository.findById(documentId2);
assertThat(entity2FromElasticSearch.isPresent(), is(true)); assertThat(entity2FromElasticSearch.isPresent(), is(true));
} }
@ -91,7 +89,7 @@ public class IntegerIDRepositoryTests {
// when // when
repository.save(sampleEntity); repository.save(sampleEntity);
// then // then
Optional<IntegerIDEntity> entityFromElasticSearch = repository.findOne(documentId); Optional<IntegerIDEntity> entityFromElasticSearch = repository.findById(documentId);
assertThat(entityFromElasticSearch.isPresent(), is(true)); assertThat(entityFromElasticSearch.isPresent(), is(true));
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** /**
* @author Artur Konczak * @author Artur Konczak
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/repository-query-support.xml") @ContextConfiguration("classpath:/repository-query-support.xml")
@ -50,7 +51,7 @@ public class QueryKeywordsTests {
elasticsearchTemplate.putMapping(Product.class); elasticsearchTemplate.putMapping(Product.class);
elasticsearchTemplate.refresh(Product.class); elasticsearchTemplate.refresh(Product.class);
repository.save(Arrays.asList( repository.saveAll(Arrays.asList(
Product.builder().id("1").name("Sugar").text("Cane sugar").price(1.0f).available(false).build() Product.builder().id("1").name("Sugar").text("Cane sugar").price(1.0f).available(false).build()
, Product.builder().id("2").name("Sugar").text("Cane sugar").price(1.2f).available(true).build() , Product.builder().id("2").name("Sugar").text("Cane sugar").price(1.2f).available(true).build()
, Product.builder().id("3").name("Sugar").text("Beet sugar").price(1.1f).available(true).build() , Product.builder().id("3").name("Sugar").text("Beet sugar").price(1.1f).available(true).build()

View File

@ -46,17 +46,15 @@ import com.google.common.collect.Lists;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/simple-repository-test.xml") @ContextConfiguration("classpath:/simple-repository-test.xml")
public class SimpleElasticsearchRepositoryTests { public class SimpleElasticsearchRepositoryTests {
@Autowired @Autowired private SampleElasticsearchRepository repository;
private SampleElasticsearchRepository repository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
@Before @Before
public void before() { public void before() {
@ -81,12 +79,12 @@ public class SimpleElasticsearchRepositoryTests {
sampleEntity2.setVersion(System.currentTimeMillis()); sampleEntity2.setVersion(System.currentTimeMillis());
// when // when
repository.save(Arrays.asList(sampleEntity1, sampleEntity2)); repository.saveAll(Arrays.asList(sampleEntity1, sampleEntity2));
// then // then
Optional<SampleEntity> entity1FromElasticSearch = repository.findOne(documentId1); Optional<SampleEntity> entity1FromElasticSearch = repository.findById(documentId1);
assertThat(entity1FromElasticSearch.isPresent(), is(true)); assertThat(entity1FromElasticSearch.isPresent(), is(true));
Optional<SampleEntity> entity2FromElasticSearch = repository.findOne(documentId2); Optional<SampleEntity> entity2FromElasticSearch = repository.findById(documentId2);
assertThat(entity2FromElasticSearch.isPresent(), is(true)); assertThat(entity2FromElasticSearch.isPresent(), is(true));
} }
@ -101,7 +99,7 @@ public class SimpleElasticsearchRepositoryTests {
// when // when
repository.save(sampleEntity); repository.save(sampleEntity);
// then // then
Optional<SampleEntity> entityFromElasticSearch = repository.findOne(documentId); Optional<SampleEntity> entityFromElasticSearch = repository.findById(documentId);
assertThat(entityFromElasticSearch.isPresent(), is(true)); assertThat(entityFromElasticSearch.isPresent(), is(true));
} }
@ -127,7 +125,7 @@ public class SimpleElasticsearchRepositoryTests {
sampleEntity.setVersion(System.currentTimeMillis()); sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity); repository.save(sampleEntity);
// when // when
Optional<SampleEntity> entityFromElasticSearch = repository.findOne(documentId); Optional<SampleEntity> entityFromElasticSearch = repository.findById(documentId);
// then // then
assertThat(entityFromElasticSearch.isPresent(), is(true)); assertThat(entityFromElasticSearch.isPresent(), is(true));
assertThat(sampleEntity, is((equalTo(sampleEntity)))); assertThat(sampleEntity, is((equalTo(sampleEntity))));
@ -166,9 +164,9 @@ public class SimpleElasticsearchRepositoryTests {
sampleEntity.setVersion(System.currentTimeMillis()); sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity); repository.save(sampleEntity);
// when // when
repository.delete(documentId); repository.deleteById(documentId);
// then // then
Optional<SampleEntity> entityFromElasticSearch = repository.findOne(documentId); Optional<SampleEntity> entityFromElasticSearch = repository.findById(documentId);
assertThat(entityFromElasticSearch.isPresent(), is(false)); assertThat(entityFromElasticSearch.isPresent(), is(false));
} }
@ -227,7 +225,7 @@ public class SimpleElasticsearchRepositoryTests {
repository.save(sampleEntity2); repository.save(sampleEntity2);
// when // when
Iterable<SampleEntity> sampleEntities = repository.findAll(Arrays.asList(documentId, documentId2)); Iterable<SampleEntity> sampleEntities = repository.findAllById(Arrays.asList(documentId, documentId2));
// then // then
assertNotNull("sample entities cant be null..", sampleEntities); assertNotNull("sample entities cant be null..", sampleEntities);
@ -252,7 +250,7 @@ public class SimpleElasticsearchRepositoryTests {
Iterable<SampleEntity> sampleEntities = Arrays.asList(sampleEntity1, sampleEntity2); Iterable<SampleEntity> sampleEntities = Arrays.asList(sampleEntity1, sampleEntity2);
// when // when
repository.save(sampleEntities); repository.saveAll(sampleEntities);
// then // then
Page<SampleEntity> entities = repository.search(termQuery("id", documentId), new PageRequest(0, 50)); Page<SampleEntity> entities = repository.search(termQuery("id", documentId), new PageRequest(0, 50));
assertNotNull(entities); assertNotNull(entities);
@ -269,7 +267,7 @@ public class SimpleElasticsearchRepositoryTests {
repository.save(sampleEntity); repository.save(sampleEntity);
// when // when
boolean exist = repository.exists(documentId); boolean exist = repository.existsById(documentId);
// then // then
assertEquals(exist, true); assertEquals(exist, true);
@ -311,7 +309,7 @@ public class SimpleElasticsearchRepositoryTests {
sampleEntity.setVersion(System.currentTimeMillis()); sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity); repository.save(sampleEntity);
// when // when
long result = repository.deleteById(documentId); long result = repository.deleteSampleEntityById(documentId);
repository.refresh(); repository.refresh();
// then // then
@ -344,7 +342,7 @@ public class SimpleElasticsearchRepositoryTests {
sampleEntity3.setMessage("hello world 3"); sampleEntity3.setMessage("hello world 3");
sampleEntity3.setAvailable(false); sampleEntity3.setAvailable(false);
sampleEntity3.setVersion(System.currentTimeMillis()); sampleEntity3.setVersion(System.currentTimeMillis());
repository.save(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3)); repository.saveAll(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3));
// when // when
List<SampleEntity> result = repository.deleteByAvailable(true); List<SampleEntity> result = repository.deleteByAvailable(true);
repository.refresh(); repository.refresh();
@ -375,7 +373,7 @@ public class SimpleElasticsearchRepositoryTests {
sampleEntity3.setId(documentId); sampleEntity3.setId(documentId);
sampleEntity3.setMessage("hello world 3"); sampleEntity3.setMessage("hello world 3");
sampleEntity3.setVersion(System.currentTimeMillis()); sampleEntity3.setVersion(System.currentTimeMillis());
repository.save(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3)); repository.saveAll(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3));
// when // when
List<SampleEntity> result = repository.deleteByMessage("hello world 3"); List<SampleEntity> result = repository.deleteByMessage("hello world 3");
repository.refresh(); repository.refresh();
@ -406,7 +404,7 @@ public class SimpleElasticsearchRepositoryTests {
sampleEntity3.setId(documentId); sampleEntity3.setId(documentId);
sampleEntity3.setType("image"); sampleEntity3.setType("image");
sampleEntity3.setVersion(System.currentTimeMillis()); sampleEntity3.setVersion(System.currentTimeMillis());
repository.save(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3)); repository.saveAll(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3));
// when // when
repository.deleteByType("article"); repository.deleteByType("article");
repository.refresh(); repository.refresh();
@ -474,10 +472,10 @@ public class SimpleElasticsearchRepositoryTests {
Iterable<SampleEntity> sampleEntities = Arrays.asList(sampleEntity2, sampleEntity2); Iterable<SampleEntity> sampleEntities = Arrays.asList(sampleEntity2, sampleEntity2);
// when // when
repository.delete(sampleEntities); repository.deleteAll(sampleEntities);
// then // then
assertThat(repository.findOne(documentId1).isPresent(), is(false)); assertThat(repository.findById(documentId1).isPresent(), is(false));
assertThat(repository.findOne(documentId2).isPresent(), is(false)); assertThat(repository.findById(documentId2).isPresent(), is(false));
} }
@Test @Test
@ -525,10 +523,11 @@ public class SimpleElasticsearchRepositoryTests {
+ "we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud."; + "we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
List<SampleEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30); List<SampleEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
repository.save(sampleEntities); repository.saveAll(sampleEntities);
// when // when
Page<SampleEntity> results = repository.searchSimilar(sampleEntities.get(0), new String[]{"message"}, new PageRequest(0, 5)); Page<SampleEntity> results = repository.searchSimilar(sampleEntities.get(0), new String[] { "message" },
new PageRequest(0, 5));
// then // then
assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L))); assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));

View File

@ -46,17 +46,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/simple-repository-test.xml") @ContextConfiguration("classpath:/simple-repository-test.xml")
public class UUIDElasticsearchRepositoryTests { public class UUIDElasticsearchRepositoryTests {
@Autowired @Autowired private SampleUUIDKeyedElasticsearchRepository repository;
private SampleUUIDKeyedElasticsearchRepository repository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
@Before @Before
public void before() { public void before() {
@ -82,12 +80,12 @@ public class UUIDElasticsearchRepositoryTests {
sampleEntityUUIDKeyed2.setVersion(System.currentTimeMillis()); sampleEntityUUIDKeyed2.setVersion(System.currentTimeMillis());
// when // when
repository.save(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2)); repository.saveAll(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2));
// then // then
Optional<SampleEntityUUIDKeyed> entity1FromElasticSearch = repository.findOne(documentId1); Optional<SampleEntityUUIDKeyed> entity1FromElasticSearch = repository.findById(documentId1);
assertThat(entity1FromElasticSearch.isPresent(), is(true)); assertThat(entity1FromElasticSearch.isPresent(), is(true));
Optional<SampleEntityUUIDKeyed> entity2FromElasticSearch = repository.findOne(documentId2); Optional<SampleEntityUUIDKeyed> entity2FromElasticSearch = repository.findById(documentId2);
assertThat(entity2FromElasticSearch.isPresent(), is(true)); assertThat(entity2FromElasticSearch.isPresent(), is(true));
} }
@ -102,7 +100,7 @@ public class UUIDElasticsearchRepositoryTests {
// when // when
repository.save(sampleEntityUUIDKeyed); repository.save(sampleEntityUUIDKeyed);
// then // then
Optional<SampleEntityUUIDKeyed> entityFromElasticSearch = repository.findOne(documentId); Optional<SampleEntityUUIDKeyed> entityFromElasticSearch = repository.findById(documentId);
assertThat(entityFromElasticSearch.isPresent(), is(true)); assertThat(entityFromElasticSearch.isPresent(), is(true));
} }
@ -116,7 +114,7 @@ public class UUIDElasticsearchRepositoryTests {
sampleEntityUUIDKeyed.setVersion(System.currentTimeMillis()); sampleEntityUUIDKeyed.setVersion(System.currentTimeMillis());
repository.save(sampleEntityUUIDKeyed); repository.save(sampleEntityUUIDKeyed);
// when // when
Optional<SampleEntityUUIDKeyed> entityFromElasticSearch = repository.findOne(documentId); Optional<SampleEntityUUIDKeyed> entityFromElasticSearch = repository.findById(documentId);
// then // then
assertThat(entityFromElasticSearch.isPresent(), is(true)); assertThat(entityFromElasticSearch.isPresent(), is(true));
assertThat(sampleEntityUUIDKeyed, is((equalTo(sampleEntityUUIDKeyed)))); assertThat(sampleEntityUUIDKeyed, is((equalTo(sampleEntityUUIDKeyed))));
@ -155,9 +153,9 @@ public class UUIDElasticsearchRepositoryTests {
sampleEntityUUIDKeyed.setVersion(System.currentTimeMillis()); sampleEntityUUIDKeyed.setVersion(System.currentTimeMillis());
repository.save(sampleEntityUUIDKeyed); repository.save(sampleEntityUUIDKeyed);
// when // when
repository.delete(documentId); repository.deleteById(documentId);
// then // then
Optional<SampleEntityUUIDKeyed> entityFromElasticSearch = repository.findOne(documentId); Optional<SampleEntityUUIDKeyed> entityFromElasticSearch = repository.findById(documentId);
assertThat(entityFromElasticSearch.isPresent(), is(false)); assertThat(entityFromElasticSearch.isPresent(), is(false));
} }
@ -216,7 +214,8 @@ public class UUIDElasticsearchRepositoryTests {
repository.save(sampleEntityUUIDKeyed2); repository.save(sampleEntityUUIDKeyed2);
// when // when
LinkedList<SampleEntityUUIDKeyed> sampleEntities = (LinkedList<SampleEntityUUIDKeyed>) repository.findAll(Arrays.asList(documentId, documentId2)); LinkedList<SampleEntityUUIDKeyed> sampleEntities = (LinkedList<SampleEntityUUIDKeyed>) repository
.findAllById(Arrays.asList(documentId, documentId2));
// then // then
assertNotNull("sample entities cant be null..", sampleEntities); assertNotNull("sample entities cant be null..", sampleEntities);
@ -242,7 +241,7 @@ public class UUIDElasticsearchRepositoryTests {
Iterable<SampleEntityUUIDKeyed> sampleEntities = Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2); Iterable<SampleEntityUUIDKeyed> sampleEntities = Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2);
// when // when
repository.save(sampleEntities); repository.saveAll(sampleEntities);
// then // then
Page<SampleEntityUUIDKeyed> entities = repository.search(termQuery("id", documentId), new PageRequest(0, 50)); Page<SampleEntityUUIDKeyed> entities = repository.search(termQuery("id", documentId), new PageRequest(0, 50));
assertNotNull(entities); assertNotNull(entities);
@ -259,7 +258,7 @@ public class UUIDElasticsearchRepositoryTests {
repository.save(sampleEntityUUIDKeyed); repository.save(sampleEntityUUIDKeyed);
// when // when
boolean exist = repository.exists(documentId); boolean exist = repository.existsById(documentId);
// then // then
assertEquals(exist, true); assertEquals(exist, true);
@ -285,7 +284,7 @@ public class UUIDElasticsearchRepositoryTests {
sampleEntityUUIDKeyed.setVersion(System.currentTimeMillis()); sampleEntityUUIDKeyed.setVersion(System.currentTimeMillis());
repository.save(sampleEntityUUIDKeyed); repository.save(sampleEntityUUIDKeyed);
// when // when
long result = repository.deleteById(documentId); long result = repository.deleteSampleEntityUUIDKeyedById(documentId);
// then // then
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("id", documentId)).build(); SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("id", documentId)).build();
Page<SampleEntityUUIDKeyed> sampleEntities = repository.search(searchQuery); Page<SampleEntityUUIDKeyed> sampleEntities = repository.search(searchQuery);
@ -316,7 +315,7 @@ public class UUIDElasticsearchRepositoryTests {
sampleEntityUUIDKeyed3.setMessage("hello world 3"); sampleEntityUUIDKeyed3.setMessage("hello world 3");
sampleEntityUUIDKeyed3.setAvailable(false); sampleEntityUUIDKeyed3.setAvailable(false);
sampleEntityUUIDKeyed3.setVersion(System.currentTimeMillis()); sampleEntityUUIDKeyed3.setVersion(System.currentTimeMillis());
repository.save(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2, sampleEntityUUIDKeyed3)); repository.saveAll(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2, sampleEntityUUIDKeyed3));
// when // when
List<SampleEntityUUIDKeyed> result = repository.deleteByAvailable(true); List<SampleEntityUUIDKeyed> result = repository.deleteByAvailable(true);
repository.refresh(); repository.refresh();
@ -347,7 +346,7 @@ public class UUIDElasticsearchRepositoryTests {
sampleEntityUUIDKeyed3.setId(documentId); sampleEntityUUIDKeyed3.setId(documentId);
sampleEntityUUIDKeyed3.setMessage("hello world 3"); sampleEntityUUIDKeyed3.setMessage("hello world 3");
sampleEntityUUIDKeyed3.setVersion(System.currentTimeMillis()); sampleEntityUUIDKeyed3.setVersion(System.currentTimeMillis());
repository.save(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2, sampleEntityUUIDKeyed3)); repository.saveAll(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2, sampleEntityUUIDKeyed3));
// when // when
List<SampleEntityUUIDKeyed> result = repository.deleteByMessage("hello world 3"); List<SampleEntityUUIDKeyed> result = repository.deleteByMessage("hello world 3");
repository.refresh(); repository.refresh();
@ -378,7 +377,7 @@ public class UUIDElasticsearchRepositoryTests {
sampleEntityUUIDKeyed3.setId(documentId); sampleEntityUUIDKeyed3.setId(documentId);
sampleEntityUUIDKeyed3.setType("image"); sampleEntityUUIDKeyed3.setType("image");
sampleEntityUUIDKeyed3.setVersion(System.currentTimeMillis()); sampleEntityUUIDKeyed3.setVersion(System.currentTimeMillis());
repository.save(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2, sampleEntityUUIDKeyed3)); repository.saveAll(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2, sampleEntityUUIDKeyed3));
// when // when
repository.deleteByType("article"); repository.deleteByType("article");
repository.refresh(); repository.refresh();
@ -388,7 +387,6 @@ public class UUIDElasticsearchRepositoryTests {
assertThat(sampleEntities.getTotalElements(), equalTo(2L)); assertThat(sampleEntities.getTotalElements(), equalTo(2L));
} }
@Test @Test
public void shouldDeleteEntity() { public void shouldDeleteEntity() {
// given // given
@ -448,10 +446,10 @@ public class UUIDElasticsearchRepositoryTests {
Iterable<SampleEntityUUIDKeyed> sampleEntities = Arrays.asList(sampleEntityUUIDKeyed2, sampleEntityUUIDKeyed2); Iterable<SampleEntityUUIDKeyed> sampleEntities = Arrays.asList(sampleEntityUUIDKeyed2, sampleEntityUUIDKeyed2);
// when // when
repository.delete(sampleEntities); repository.deleteAll(sampleEntities);
// then // then
assertThat(repository.findOne(documentId1).isPresent(), is(false)); assertThat(repository.findById(documentId1).isPresent(), is(false));
assertThat(repository.findOne(documentId2).isPresent(), is(false)); assertThat(repository.findById(documentId2).isPresent(), is(false));
} }
@Test @Test
@ -470,7 +468,8 @@ public class UUIDElasticsearchRepositoryTests {
sampleEntityUUIDKeyed2.setMessage("hello"); sampleEntityUUIDKeyed2.setMessage("hello");
repository.save(sampleEntityUUIDKeyed2); repository.save(sampleEntityUUIDKeyed2);
// when // when
Iterable<SampleEntityUUIDKeyed> sampleEntities = repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC, "message"))); Iterable<SampleEntityUUIDKeyed> sampleEntities = repository
.findAll(new Sort(new Sort.Order(Sort.Direction.ASC, "message")));
// then // then
assertThat(sampleEntities, is(notNullValue())); assertThat(sampleEntities, is(notNullValue()));
} }
@ -485,10 +484,11 @@ public class UUIDElasticsearchRepositoryTests {
+ "we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud."; + "we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
List<SampleEntityUUIDKeyed> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30); List<SampleEntityUUIDKeyed> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
repository.save(sampleEntities); repository.saveAll(sampleEntities);
// when // when
Page<SampleEntityUUIDKeyed> results = repository.searchSimilar(sampleEntities.get(0), new String[]{"message"}, new PageRequest(0, 5)); Page<SampleEntityUUIDKeyed> results = repository.searchSimilar(sampleEntities.get(0), new String[] { "message" },
new PageRequest(0, 5));
// then // then
assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L))); assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));