DATAES-6 : Instead of storing data in "fs" it should store "in memory" for unit tests

This commit is contained in:
Mohsin Husen 2013-05-11 14:23:29 +01:00
parent db5d9b8fc7
commit 86c2b1d1c5
19 changed files with 49 additions and 32 deletions

View File

@ -35,4 +35,5 @@ public @interface Document {
String indexName();
String type() default "";
String indexStoreType() default "fs";
}

View File

@ -17,6 +17,7 @@ package org.springframework.data.elasticsearch.client;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
@ -36,7 +37,8 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
private static final Logger logger = LoggerFactory.getLogger(NodeClientFactoryBean.class);
private boolean local;
private boolean purgeDataOnShutdown;
private boolean enableHttp;
private String clusterName;
private NodeClient nodeClient;
NodeClientFactoryBean() {
@ -63,15 +65,23 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
@Override
public void afterPropertiesSet() throws Exception {
nodeClient = (NodeClient) nodeBuilder().local(this.local).data(!this.purgeDataOnShutdown).node().client();
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
.put("http.enabled", String.valueOf(this.enableHttp));
nodeClient = (NodeClient) nodeBuilder().settings(settings)
.clusterName(this.clusterName).local(this.local).node().client();
}
public void setLocal(boolean local) {
this.local = local;
}
public void setPurgeDataOnShutdown(boolean purgeDataOnShutdown) {
this.purgeDataOnShutdown = purgeDataOnShutdown;
public void setEnableHttp(boolean enableHttp) {
this.enableHttp = enableHttp;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
@Override

View File

@ -41,7 +41,8 @@ public class NodeClientBeanDefinitionParser extends AbstractBeanDefinitionParser
private void setLocalSettings(Element element, BeanDefinitionBuilder builder) {
builder.addPropertyValue("local", Boolean.valueOf(element.getAttribute("local")));
builder.addPropertyValue("purgeDataOnShutdown", Boolean.valueOf(element.getAttribute("purge-data-on-shutdown")));
builder.addPropertyValue("clusterName", element.getAttribute("cluster-name"));
builder.addPropertyValue("enableHttp",Boolean.valueOf(element.getAttribute("http-enabled")));
}

View File

@ -96,7 +96,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
@Override
public <T> boolean createIndex(Class<T> clazz) {
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
return createIndexIfNotCreated(persistentEntity.getIndexName());
return createIndexIfNotCreated(clazz);
}
@Override
@ -369,8 +369,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
return searchRequest.setQuery(query).execute().actionGet();
}
private boolean createIndexIfNotCreated(String indexName) {
return indexExists(indexName) || createIndex(indexName);
private <T> boolean createIndexIfNotCreated(Class<T> clazz) {
return indexExists(getPersistentEntityFor(clazz).getIndexName()) || createIndexWithSettings(clazz);
}
private boolean indexExists(String indexName) {
@ -379,9 +379,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
.exists(indicesExistsRequest(indexName)).actionGet().exists();
}
private boolean createIndex(String indexName) {
return client.admin().indices().create(Requests.createIndexRequest(indexName).
settings(new MapBuilder<String, String>().put("index.refresh_interval", "-1").map())).actionGet().acknowledged();
private <T> boolean createIndexWithSettings(Class<T> clazz) {
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
return client.admin().indices().create(Requests.createIndexRequest(persistentEntity.getIndexName()).
settings(new MapBuilder<String, String>()
.put("index.store.type", persistentEntity.getIndexStoreType())
.map())).actionGet().acknowledged();
}
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz){

View File

@ -27,5 +27,6 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
String getIndexName();
String getIndexType();
String getIndexStoreType();
ElasticsearchPersistentProperty getVersionProperty();
}

View File

@ -45,6 +45,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private final StandardEvaluationContext context;
private String indexName;
private String indexType;
private String indexStoreType;
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
super(typeInformation);
@ -55,6 +56,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
Assert.hasText(document.indexName(), " Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")");
this.indexName = typeInformation.getType().getAnnotation(Document.class).indexName();
this.indexType = hasText(document.type())? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH);
this.indexStoreType = typeInformation.getType().getAnnotation(Document.class).indexStoreType();
}
}
@ -75,6 +77,10 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
return indexType;
}
public String getIndexStoreType() {
return indexStoreType;
}
@Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);

View File

@ -45,7 +45,8 @@
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="local" type="xsd:boolean" default="false"/>
<xsd:attribute name="purge-data-on-shutdown" type="xsd:boolean" default="true"/>
<xsd:attribute name="cluster-name" type="xsd:string" default="elasticsearch"/>
<xsd:attribute name="http-enabled" type="xsd:boolean" default="true"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

View File

@ -21,7 +21,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@Document(indexName = "book",type = "book")
@Document(indexName = "book",type = "book", indexStoreType = "memory")
public class Book {
@Id

View File

@ -20,7 +20,7 @@ import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "double-keyed-entity", type = "double-keyed-entity")
@Document(indexName = "double-keyed-entity", type = "double-keyed-entity", indexStoreType = "memory")
public class DoubleIDEntity {

View File

@ -22,7 +22,7 @@ import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "integer-keyed-entity", type = "integer-keyed-entity")
@Document(indexName = "integer-keyed-entity", type = "integer-keyed-entity", indexStoreType = "memory")
public class IntegerIDEntity {

View File

@ -25,7 +25,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@Document(indexName = "test-index", type = "test-type")
@Document(indexName = "test-index", type = "test-type", indexStoreType = "memory")
public class SampleEntity {
@Id

View File

@ -24,7 +24,7 @@ import org.springframework.data.elasticsearch.annotations.Field;
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@Document(indexName = "test-mapping", type = "mapping")
@Document(indexName = "test-mapping", type = "mapping", indexStoreType = "memory")
public class SampleMappingEntity {
@Id

View File

@ -25,17 +25,13 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
@ -56,7 +52,6 @@ public class CriteriaQueryTests {
elasticsearchTemplate.refresh(SampleEntity.class, true);
}
@Test
public void shouldPerformAndOperation(){
//given

View File

@ -16,7 +16,6 @@
package org.springframework.data.elasticsearch.repositories;
import org.springframework.data.elasticsearch.IntegerIDEntity;
import org.springframework.data.elasticsearch.SampleEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface IntegerIDRepository extends ElasticsearchRepository<IntegerIDEntity,Integer> {

View File

@ -377,13 +377,13 @@ public class DoubleIDRepositoryTests {
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("A. hello world.");
sampleEntity.setMessage("abc");
repository.save(sampleEntity);
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("B.hello world.");
sampleEntity2.setMessage("xyz");
repository.save(sampleEntity2);
//when
Iterable<DoubleIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));

View File

@ -377,13 +377,13 @@ public class IntegerIDRepositoryTests {
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("A. hello world.");
sampleEntity.setMessage("hello");
repository.save(sampleEntity);
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("B.hello world.");
sampleEntity2.setMessage("world");
repository.save(sampleEntity2);
//when
Iterable<IntegerIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));

View File

@ -373,13 +373,13 @@ public class SimpleElasticsearchRepositoryTests {
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("A. hello world.");
sampleEntity.setMessage("world");
repository.save(sampleEntity);
String documentId2 = randomNumeric(5);
SampleEntity sampleEntity2 = new SampleEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("B.hello world.");
sampleEntity2.setMessage("hello");
repository.save(sampleEntity2);
//when
Iterable<SampleEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));

View File

@ -5,6 +5,6 @@
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<elasticsearch:node-client id="client" local="true" purge-data-on-shutdown="true" />
<elasticsearch:node-client id="client" local="true" cluster-name="testCluster" http-enabled="false" />
</beans>

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<elasticsearch:node-client id="client" local="true"/>
<elasticsearch:node-client id="client" local="true" cluster-name="testCluster" http-enabled="false"/>
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>