From c2f13c38208dce18614d00b9e0ed66fc1d61165b Mon Sep 17 00:00:00 2001 From: Mohsin Husen Date: Thu, 17 Sep 2015 12:02:51 +0100 Subject: [PATCH] DATAES-194 - Tests should clean up "data" directory. --- .../client/NodeClientFactoryBean.java | 13 ++++-- .../NodeClientBeanDefinitionParser.java | 3 +- .../config/spring-elasticsearch-1.0.xsd | 7 ++++ .../data/elasticsearch/Utils.java | 40 +++++++++++++++++++ .../EnableElasticsearchRepositoriesTests.java | 4 +- ...eNestedElasticsearchRepositoriesTests.java | 4 +- .../data/elasticsearch/core/AliasTests.java | 7 +--- .../core/ElasticsearchTemplateTests.java | 12 +++--- .../cdi/ElasticsearchTemplateProducer.java | 32 ++------------- src/test/resources/infrastructure.xml | 2 +- .../data/elasticsearch/config/namespace.xml | 2 +- 11 files changed, 76 insertions(+), 50 deletions(-) create mode 100644 src/test/java/org/springframework/data/elasticsearch/Utils.java diff --git a/src/main/java/org/springframework/data/elasticsearch/client/NodeClientFactoryBean.java b/src/main/java/org/springframework/data/elasticsearch/client/NodeClientFactoryBean.java index 8caa15841..6b6bd00fd 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/NodeClientFactoryBean.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/NodeClientFactoryBean.java @@ -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, Initializ private boolean enableHttp; private String clusterName; private NodeClient nodeClient; + private String pathData; NodeClientFactoryBean() { } @@ -65,8 +66,10 @@ public class NodeClientFactoryBean implements FactoryBean, 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, Initializ this.clusterName = clusterName; } + public void setPathData(String pathData) { + this.pathData = pathData; + } + @Override public void destroy() throws Exception { try { diff --git a/src/main/java/org/springframework/data/elasticsearch/config/NodeClientBeanDefinitionParser.java b/src/main/java/org/springframework/data/elasticsearch/config/NodeClientBeanDefinitionParser.java index 909ccfbbc..4d49e2fc5 100644 --- a/src/main/java/org/springframework/data/elasticsearch/config/NodeClientBeanDefinitionParser.java +++ b/src/main/java/org/springframework/data/elasticsearch/config/NodeClientBeanDefinitionParser.java @@ -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, diff --git a/src/main/resources/org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd b/src/main/resources/org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd index 7274ad0bd..74ee065d8 100644 --- a/src/main/resources/org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd +++ b/src/main/resources/org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd @@ -67,6 +67,13 @@ + + + + + + + diff --git a/src/test/java/org/springframework/data/elasticsearch/Utils.java b/src/test/java/org/springframework/data/elasticsearch/Utils.java new file mode 100644 index 000000000..31da1bd7d --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/Utils.java @@ -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(); + } + + + +} diff --git a/src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTests.java b/src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTests.java index a5ac78808..7ba14c0de 100644 --- a/src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTests.java @@ -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()); } } diff --git a/src/test/java/org/springframework/data/elasticsearch/config/EnableNestedElasticsearchRepositoriesTests.java b/src/test/java/org/springframework/data/elasticsearch/config/EnableNestedElasticsearchRepositoriesTests.java index f1c89c3cb..fa483557c 100644 --- a/src/test/java/org/springframework/data/elasticsearch/config/EnableNestedElasticsearchRepositoriesTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/config/EnableNestedElasticsearchRepositoriesTests.java @@ -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()); } } diff --git a/src/test/java/org/springframework/data/elasticsearch/core/AliasTests.java b/src/test/java/org/springframework/data/elasticsearch/core/AliasTests.java index c175a257d..5b0d1d5cc 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/AliasTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/AliasTests.java @@ -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); } } diff --git a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java index 3c5f7439c..16ea4b0ff 100755 --- a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java @@ -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 sampleEntities = new ArrayList(); boolean hasRecords = true; while (hasRecords) { - Page page = elasticsearchTemplate.scroll(scrollId, 5000L, new SearchResultMapper() { + Page page = elasticsearchTemplate.scroll(scrollId, 10000L, new SearchResultMapper() { @Override public FacetedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List result = new ArrayList(); @@ -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 sampleEntities = new ArrayList(); boolean hasRecords = true; while (hasRecords) { @@ -925,7 +926,7 @@ public class ElasticsearchTemplateTests { private static List createSampleEntitiesWithMessage(String message, int numberOfEntities) { List indexQueries = new ArrayList(); 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); diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/ElasticsearchTemplateProducer.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/ElasticsearchTemplateProducer.java index 97e5e9181..d5d8a254b 100644 --- a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/ElasticsearchTemplateProducer.java +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/ElasticsearchTemplateProducer.java @@ -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); - } - } } diff --git a/src/test/resources/infrastructure.xml b/src/test/resources/infrastructure.xml index 3285a3dc1..223cbbaff 100644 --- a/src/test/resources/infrastructure.xml +++ b/src/test/resources/infrastructure.xml @@ -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"> - + diff --git a/src/test/resources/org/springframework/data/elasticsearch/config/namespace.xml b/src/test/resources/org/springframework/data/elasticsearch/config/namespace.xml index 8a8b7726a..efe1ef1f1 100644 --- a/src/test/resources/org/springframework/data/elasticsearch/config/namespace.xml +++ b/src/test/resources/org/springframework/data/elasticsearch/config/namespace.xml @@ -6,7 +6,7 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> + cluster-name="#{T(java.util.UUID).randomUUID().toString()}" http-enabled="false" path-data="target/elasticsearchTestData"/>