removed duplicate test cases for DoubleIDRepository and IntegerIDRepository Tests

This commit is contained in:
Mohsin Husen 2013-05-12 10:39:11 +01:00
parent 8c31761308
commit b943dc2515
6 changed files with 30 additions and 705 deletions

View File

@ -19,11 +19,14 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@Document(indexName = "double-keyed-entity", type = "double-keyed-entity", indexStoreType = "memory", shards = 1 , replicas = 0, refreshInterval = "-1")
public class DoubleIDEntity {
@Id
private Double id;
private String type;

View File

@ -19,6 +19,10 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@Document(indexName = "integer-keyed-entity", type = "integer-keyed-entity", indexStoreType = "memory", shards = 1 , replicas = 0, refreshInterval = "-1")
public class IntegerIDEntity {

View File

@ -29,7 +29,7 @@ public class SampleMappingEntity {
@Id
private String id;
@Field(type = "string",index = "not_analyzed", store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
@Field(type = "string", index = "not_analyzed", store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
private String message;
private NestedEntity nested;

View File

@ -47,7 +47,7 @@ public class EnableElasticsearchRepositoriesTests {
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
return new ElasticsearchTemplate(nodeBuilder().local(true).clusterName("testCluster").node().client());
}
}

View File

@ -15,48 +15,32 @@
*/
package org.springframework.data.elasticsearch.repository.support;
import static org.elasticsearch.index.query.QueryBuilders.fieldQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.math.RandomUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.DoubleIDEntity;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.repositories.DoubleIDRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.Arrays;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/simple-repository-test.xml")
public class DoubleIDRepositoryTests {
@Resource
private DoubleIDRepository repository;
@Before
public void before(){
repository.deleteAll();
@ -101,329 +85,4 @@ public class DoubleIDRepositoryTests {
DoubleIDEntity entityFromElasticSearch = repository.findOne(documentId);
assertThat(entityFromElasticSearch, is(notNullValue()));
}
@Test
public void shouldFindDocumentById(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
DoubleIDEntity entityFromElasticSearch = repository.findOne(documentId);
//then
assertThat(entityFromElasticSearch, is(notNullValue()));
assertThat(sampleEntity, is((equalTo(sampleEntity))));
}
@Test
public void shouldReturnCountOfDocuments(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
Long count = repository.count();
//then
assertThat(count, is(greaterThanOrEqualTo(1L)));
}
@Test
public void shouldFindAllDocuments(){
//when
Iterable<DoubleIDEntity> results = repository.findAll();
//then
assertThat(results, is(notNullValue()));
}
@Test
public void shouldDeleteDocument(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
repository.delete(documentId);
//then
DoubleIDEntity entityFromElasticSearch = repository.findOne(documentId);
assertThat(entityFromElasticSearch, is(nullValue()));
}
@Test
public void shouldSearchDocumentsGivenSearchQuery(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some test message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
SearchQuery query = new NativeSearchQueryBuilder()
.withQuery(termQuery("message", "test"))
.build();
//when
Page<DoubleIDEntity> page = repository.search(query);
//then
assertThat(page, is(notNullValue()));
assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
}
@Test
public void shouldSearchDocumentsGivenElasticsearchQuery(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
Page<DoubleIDEntity> page = repository.search(termQuery("message", "world"), new PageRequest(0,50));
//then
assertThat(page, is(notNullValue()));
assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
}
@Test
@Ignore
public void shouldFindAllByIdQuery(){
//todo : find solution for findAll(Iterable<Ids> ids)
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
//when
Iterable<DoubleIDEntity> sampleEntities=repository.findAll(Arrays.asList(documentId,documentId2));
//then
assertNotNull("sample entities cant be null..", sampleEntities);
}
@Test
public void shouldSaveIterableEntities(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity1 = new DoubleIDEntity();
sampleEntity1.setId(documentId);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
Iterable<DoubleIDEntity> sampleEntities = Arrays.asList(sampleEntity1,sampleEntity2);
//when
repository.save(sampleEntities);
//then
Page<DoubleIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0, 50));
assertNotNull(entities);
}
@Test
public void shouldReturnTrueGivenDocumentWithIdExists(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
boolean exist = repository.exists(documentId);
//then
assertEquals(exist, true);
}
@Test
public void shouldReturnResultsForGivenSearchQuery(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(fieldQuery("id",documentId))
.build();
Page<DoubleIDEntity> sampleEntities= repository.search(searchQuery);
//then
assertThat(sampleEntities.getTotalElements(), equalTo(1L));
}
@Test
public void shouldDeleteAll(){
//when
repository.deleteAll();
//then
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.build();
Page<DoubleIDEntity> sampleEntities= repository.search(searchQuery);
assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}
@Test
public void shouldDeleteEntity(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
repository.delete(sampleEntity);
//then
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(fieldQuery("id", documentId))
.build();
Page<DoubleIDEntity> sampleEntities= repository.search(searchQuery);
assertThat(sampleEntities.getTotalElements(),equalTo(0L));
}
@Test
public void shouldReturnIterableEntities(){
//given
Double documentId1 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity1 = new DoubleIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
repository.save(sampleEntity1);
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
//when
Iterable<DoubleIDEntity> sampleEntities = repository.search(fieldQuery("id",documentId1));
//then
assertNotNull("sample entities cant be null..", sampleEntities);
}
@Test
public void shouldDeleteIterableEntities(){
//given
Double documentId1 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity1 = new DoubleIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
Iterable<DoubleIDEntity> sampleEntities = Arrays.asList(sampleEntity2,sampleEntity2);
//when
repository.delete(sampleEntities);
//then
assertThat(repository.findOne(documentId1),is(nullValue()));
assertThat(repository.findOne(documentId2),is(nullValue()));
}
@Test
public void shouldIndexEntity(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntity.setMessage("some message");
//when
repository.index(sampleEntity);
//then
Page<DoubleIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0,50));
assertThat(entities.getTotalElements(),equalTo(1L));
}
@Test
public void shouldSortByGivenField(){
//todo
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("abc");
repository.save(sampleEntity);
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("xyz");
repository.save(sampleEntity2);
//when
Iterable<DoubleIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
//then
assertThat(sampleEntities,is(notNullValue()));
}
@Test
public void shouldReturnSimilarEntities(){
//given
String sampleMessage = "So we build a web site or an application and want to add search to it, " +
"and then it hits us: getting search working is hard. We want our search solution to be fast," +
" we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, " +
"we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, " +
"we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
List<DoubleIDEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
repository.save(sampleEntities);
//when
Page<DoubleIDEntity> results = repository.searchSimilar(sampleEntities.get(0));
//then
assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));
}
private static List<DoubleIDEntity> createSampleEntitiesWithMessage(String message, int numberOfEntities){
List<DoubleIDEntity> sampleEntities = new ArrayList<DoubleIDEntity>();
for(int i = 0; i < numberOfEntities; i++){
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage(message);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntities.add(sampleEntity);
}
return sampleEntities;
}
}

View File

@ -15,48 +15,32 @@
*/
package org.springframework.data.elasticsearch.repository.support;
import static org.elasticsearch.index.query.QueryBuilders.fieldQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.math.RandomUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.IntegerIDEntity;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.repositories.IntegerIDRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.Arrays;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/simple-repository-test.xml")
public class IntegerIDRepositoryTests {
@Resource
private IntegerIDRepository repository;
@Before
public void before(){
repository.deleteAll();
@ -101,329 +85,4 @@ public class IntegerIDRepositoryTests {
IntegerIDEntity entityFromElasticSearch = repository.findOne(documentId);
assertThat(entityFromElasticSearch, is(notNullValue()));
}
@Test
public void shouldFindDocumentById(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
IntegerIDEntity entityFromElasticSearch = repository.findOne(documentId);
//then
assertThat(entityFromElasticSearch, is(notNullValue()));
assertThat(sampleEntity, is((equalTo(sampleEntity))));
}
@Test
public void shouldReturnCountOfDocuments(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
Long count = repository.count();
//then
assertThat(count, is(greaterThanOrEqualTo(1L)));
}
@Test
public void shouldFindAllDocuments(){
//when
Iterable<IntegerIDEntity> results = repository.findAll();
//then
assertThat(results, is(notNullValue()));
}
@Test
public void shouldDeleteDocument(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
repository.delete(documentId);
//then
IntegerIDEntity entityFromElasticSearch = repository.findOne(documentId);
assertThat(entityFromElasticSearch, is(nullValue()));
}
@Test
public void shouldSearchDocumentsGivenSearchQuery(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some test message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
SearchQuery query = new NativeSearchQueryBuilder()
.withQuery(termQuery("message", "test"))
.build();
//when
Page<IntegerIDEntity> page = repository.search(query);
//then
assertThat(page, is(notNullValue()));
assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
}
@Test
public void shouldSearchDocumentsGivenElasticsearchQuery(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
Page<IntegerIDEntity> page = repository.search(termQuery("message", "world"), new PageRequest(0,50));
//then
assertThat(page, is(notNullValue()));
assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
}
@Test
@Ignore
public void shouldFindAllByIdQuery(){
//todo : find solution for findAll(Iterable<Ids> ids)
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
//when
Iterable<IntegerIDEntity> sampleEntities=repository.findAll(Arrays.asList(documentId,documentId2));
//then
assertNotNull("sample entities cant be null..", sampleEntities);
}
@Test
public void shouldSaveIterableEntities(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity1 = new IntegerIDEntity();
sampleEntity1.setId(documentId);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
Iterable<IntegerIDEntity> sampleEntities = Arrays.asList(sampleEntity1,sampleEntity2);
//when
repository.save(sampleEntities);
//then
Page<IntegerIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0, 50));
assertNotNull(entities);
}
@Test
public void shouldReturnTrueGivenDocumentWithIdExists(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
boolean exist = repository.exists(documentId);
//then
assertEquals(exist, true);
}
@Test
public void shouldReturnResultsForGivenSearchQuery(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(fieldQuery("id",documentId))
.build();
Page<IntegerIDEntity> sampleEntities= repository.search(searchQuery);
//then
assertThat(sampleEntities.getTotalElements(), equalTo(1L));
}
@Test
public void shouldDeleteAll(){
//when
repository.deleteAll();
//then
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.build();
Page<IntegerIDEntity> sampleEntities= repository.search(searchQuery);
assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}
@Test
public void shouldDeleteEntity(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
repository.delete(sampleEntity);
//then
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(fieldQuery("id", documentId))
.build();
Page<IntegerIDEntity> sampleEntities= repository.search(searchQuery);
assertThat(sampleEntities.getTotalElements(),equalTo(0L));
}
@Test
public void shouldReturnIterableEntities(){
//given
Integer documentId1 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity1 = new IntegerIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
repository.save(sampleEntity1);
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
//when
Iterable<IntegerIDEntity> sampleEntities = repository.search(fieldQuery("id",documentId1));
//then
assertNotNull("sample entities cant be null..", sampleEntities);
}
@Test
public void shouldDeleteIterableEntities(){
//given
Integer documentId1 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity1 = new IntegerIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
Iterable<IntegerIDEntity> sampleEntities = Arrays.asList(sampleEntity2,sampleEntity2);
//when
repository.delete(sampleEntities);
//then
assertThat(repository.findOne(documentId1),is(nullValue()));
assertThat(repository.findOne(documentId2),is(nullValue()));
}
@Test
public void shouldIndexEntity(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntity.setMessage("some message");
//when
repository.index(sampleEntity);
//then
Page<IntegerIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0,50));
assertThat(entities.getTotalElements(),equalTo(1L));
}
@Test
public void shouldSortByGivenField(){
//todo
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello");
repository.save(sampleEntity);
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("world");
repository.save(sampleEntity2);
//when
Iterable<IntegerIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
//then
assertThat(sampleEntities,is(notNullValue()));
}
@Test
public void shouldReturnSimilarEntities(){
//given
String sampleMessage = "So we build a web site or an application and want to add search to it, " +
"and then it hits us: getting search working is hard. We want our search solution to be fast," +
" we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, " +
"we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, " +
"we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
List<IntegerIDEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
repository.save(sampleEntities);
//when
Page<IntegerIDEntity> results = repository.searchSimilar(sampleEntities.get(0));
//then
assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));
}
private static List<IntegerIDEntity> createSampleEntitiesWithMessage(String message, int numberOfEntities){
List<IntegerIDEntity> sampleEntities = new ArrayList<IntegerIDEntity>();
for(int i = 0; i < numberOfEntities; i++){
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage(message);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntities.add(sampleEntity);
}
return sampleEntities;
}
}