DATAES-194 - Tests should clean up "data" directory.

This commit is contained in:
Mohsin Husen 2015-09-17 12:02:51 +01:00
parent 49708d38c0
commit c2f13c3820
11 changed files with 76 additions and 50 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -40,6 +40,7 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
private boolean enableHttp;
private String clusterName;
private NodeClient nodeClient;
private String pathData;
NodeClientFactoryBean() {
}
@ -65,8 +66,10 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
@Override
public void afterPropertiesSet() throws Exception {
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put("http.enabled",
String.valueOf(this.enableHttp));
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
.put("http.enabled", String.valueOf(this.enableHttp))
.put("path.data", this.pathData);
nodeClient = (NodeClient) nodeBuilder().settings(settings).clusterName(this.clusterName).local(this.local).node()
.client();
@ -84,6 +87,10 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
this.clusterName = clusterName;
}
public void setPathData(String pathData) {
this.pathData = pathData;
}
@Override
public void destroy() throws Exception {
try {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -43,6 +43,7 @@ public class NodeClientBeanDefinitionParser extends AbstractBeanDefinitionParser
builder.addPropertyValue("local", Boolean.valueOf(element.getAttribute("local")));
builder.addPropertyValue("clusterName", element.getAttribute("cluster-name"));
builder.addPropertyValue("enableHttp", Boolean.valueOf(element.getAttribute("http-enabled")));
builder.addPropertyValue("pathData", element.getAttribute("path-data"));
}
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,

View File

@ -67,6 +67,13 @@
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="path-data" type="xsd:string" default="">
<xsd:annotation>
<xsd:documentation>
<![CDATA[ path to the data folder for node client ]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

View File

@ -0,0 +1,40 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import java.util.UUID;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.ImmutableSettings;
/**
* @author Mohsin Husen
*/
public class Utils {
public static NodeClient getNodeClient() {
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
.put("http.enabled", "false")
.put("path.data", "target/elasticsearchTestData");
return (NodeClient) nodeBuilder().settings(settings).clusterName(UUID.randomUUID().toString()).local(true).node()
.client();
}
}

View File

@ -15,7 +15,6 @@
*/
package org.springframework.data.elasticsearch.config;
import static org.elasticsearch.node.NodeBuilder.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
@ -27,6 +26,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.Utils;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.entities.SampleEntity;
@ -60,7 +60,7 @@ public class EnableElasticsearchRepositoriesTests implements ApplicationContextA
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(nodeBuilder().local(true).clusterName("testCluster2").node().client());
return new ElasticsearchTemplate(Utils.getNodeClient());
}
}

View File

@ -20,6 +20,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.Utils;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.entities.SampleEntity;
@ -28,7 +29,6 @@ import org.springframework.data.repository.Repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import static org.junit.Assert.assertNotNull;
/**
@ -45,7 +45,7 @@ public class EnableNestedElasticsearchRepositoriesTests {
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(nodeBuilder().local(true).clusterName("testCluster2").node().client());
return new ElasticsearchTemplate(Utils.getNodeClient());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -179,7 +179,6 @@ public class AliasTests {
.withObject(sampleEntity).build();
elasticsearchTemplate.index(indexQuery);
elasticsearchTemplate.refresh(SampleEntity.class, true);
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
.withIndices(alias2).withTypes(TYPE_NAME).build();
@ -194,9 +193,5 @@ public class AliasTests {
//cleanup
elasticsearchTemplate.removeAlias(aliasQuery1);
elasticsearchTemplate.removeAlias(aliasQuery2);
elasticsearchTemplate.deleteIndex(SampleEntity.class);
elasticsearchTemplate.createIndex(SampleEntity.class);
elasticsearchTemplate.putMapping(SampleEntity.class);
elasticsearchTemplate.refresh(SampleEntity.class, true);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -770,14 +770,15 @@ public class ElasticsearchTemplateTests {
.withIndices(INDEX_NAME)
.withTypes(TYPE_NAME)
.withFields("message")
.withQuery(matchAllQuery())
.withPageable(new PageRequest(0, 10))
.build();
String scrollId = elasticsearchTemplate.scan(searchQuery, 5000, false);
String scrollId = elasticsearchTemplate.scan(searchQuery, 10000, false);
List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
boolean hasRecords = true;
while (hasRecords) {
Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 5000L, new SearchResultMapper() {
Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 10000L, new SearchResultMapper() {
@Override
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
List<SampleEntity> result = new ArrayList<SampleEntity>();
@ -821,7 +822,7 @@ public class ElasticsearchTemplateTests {
criteriaQuery.addTypes(TYPE_NAME);
criteriaQuery.setPageable(new PageRequest(0, 10));
String scrollId = elasticsearchTemplate.scan(criteriaQuery, 1000, false);
String scrollId = elasticsearchTemplate.scan(criteriaQuery, 5000, false);
List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
boolean hasRecords = true;
while (hasRecords) {
@ -925,7 +926,7 @@ public class ElasticsearchTemplateTests {
private static List<IndexQuery> createSampleEntitiesWithMessage(String message, int numberOfEntities) {
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
for (int i = 0; i < numberOfEntities; i++) {
String documentId = randomNumeric(5);
String documentId = UUID.randomUUID().toString();
SampleEntity sampleEntity = new SampleEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage(message);
@ -1152,6 +1153,7 @@ public class ElasticsearchTemplateTests {
elasticsearchTemplate.refresh(SampleEntity.class, true);
// when
elasticsearchTemplate.deleteType(INDEX_NAME, TYPE_NAME);
elasticsearchTemplate.refresh(SampleEntity.class, true);
//then
boolean typeExists = elasticsearchTemplate.typeExists(INDEX_NAME, TYPE_NAME);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,20 +15,15 @@
*/
package org.springframework.data.elasticsearch.repositories.cdi;
import static org.elasticsearch.node.NodeBuilder.*;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.elasticsearch.Utils;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
import org.xml.sax.SAXException;
/**
@ -39,33 +34,12 @@ class ElasticsearchTemplateProducer {
@Produces
public ElasticsearchOperations createElasticsearchTemplate() throws IOException, ParserConfigurationException, SAXException {
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put("http.enabled", "false");
NodeClient client = (NodeClient) nodeBuilder().settings(settings).clusterName("testClusterForCDI").local(true).node()
.client();
return new ElasticsearchTemplate(client);
return new ElasticsearchTemplate(Utils.getNodeClient());
}
@PreDestroy
public void shutdown() {
// remove everything to avoid conflicts with other tests in case server not shut down properly
deleteAll();
}
private void deleteAll() {
ElasticsearchOperations template;
try {
template = createElasticsearchTemplate();
DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setQuery(QueryBuilders.matchAllQuery());
deleteQuery.setIndex("test-product-index");
deleteQuery.setType("test-product-type");
template.delete(deleteQuery);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -5,7 +5,7 @@
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" cluster-name="testCluster" http-enabled="false"/>
<elasticsearch:node-client id="client" local="true" cluster-name="#{T(java.util.UUID).randomUUID().toString()}" http-enabled="false" path-data="target/elasticsearchTestData"/>
<!--<elasticsearch:transport-client id="client" cluster-name="elasticsearch" cluster-nodes="127.0.0.1:9300" />-->

View File

@ -6,7 +6,7 @@
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<elasticsearch:node-client id="client" local="true"
cluster-name="testCluster" http-enabled="false"/>
cluster-name="#{T(java.util.UUID).randomUUID().toString()}" http-enabled="false" path-data="target/elasticsearchTestData"/>
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">