diff --git a/README.md b/README.md index e0ce191..2605970 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,34 @@ embeddedElastic.start() And that's all, you can connect to your embedded-elastic instance on specified port and use it in your tests. +## Junit Rule + +You can also use `@Rule` or `@ClassRule` to start embedded elastic. +Using this feature you do not have to worry about starting and stopping elastic in your tests. +simple setup: + +```java +@ClassRule +public static EmbeddedElasticRule embeddedElasticRule = new EmbeddedElasticRule("index_name", 9200); +``` + +custom Setup: + +```java +@ClassRule +public static EmbeddedElasticRule embeddedElasticRule = new EmbeddedElasticRule(EmbeddedElastic.builder() + .withElasticVersion("6.8.0") + .withSetting(PopularProperties.HTTP_PORT, 9200) + .withSetting(PopularProperties.CLUSTER_NAME, "cluster_test") + .withSetting("cluster.routing.allocation.disk.threshold_enabled", false) + .withStartTimeout(2, TimeUnit.MINUTES) + .withIndex("test_builder") + .withDownloadDirectory(new File("./")) + .withInstallationDirectory(new File("./")) + .build()); +``` + + ## Available builder options | Method | Description | @@ -82,6 +110,7 @@ Availabe `JavaHomeOption` options | Method | Description | | ------------- | ------------- | | `start()` | downloads Elasticsearch and specified plugins, setups everything and finally starts your Elasticsearch instance | +| `isStarted()` | Checks if you have an instance of Elasticsearch available | | `stop()` | stops your Elasticsearch instance and removes all data | | `index` | index your document, comes with variants that take only document, or document and it's id | | `deleteIndex(String indexName)`, `deleteIndices()` | deletes index with name specified during EmbeddedElastic creation | diff --git a/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/EmbeddedElastic.java b/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/EmbeddedElastic.java index 73bc3bc..8fed3f7 100644 --- a/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/EmbeddedElastic.java +++ b/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/EmbeddedElastic.java @@ -9,15 +9,8 @@ import java.net.Proxy; import java.net.URL; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.toList; @@ -281,6 +274,10 @@ public int getHttpPort() { return elasticServer.getHttpPort(); } + public boolean isStarted() { + return elasticServer.isStarted(); + } + public static final class Builder { private InstallationSource installationSource = null; diff --git a/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/EmbeddedElasticRule.java b/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/EmbeddedElasticRule.java new file mode 100644 index 0000000..006dcbe --- /dev/null +++ b/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/EmbeddedElasticRule.java @@ -0,0 +1,71 @@ +package pl.allegro.tech.embeddedelasticsearch; + + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +public class EmbeddedElasticRule implements TestRule { + + private static final Integer DEFAULT_PORT = 9200; + + public EmbeddedElastic embeddedElastic; + private Integer port = 0; + private String index; + + public EmbeddedElasticRule(String index, Integer port) { + this.port = port; + this.index = index; + } + + public EmbeddedElasticRule(EmbeddedElastic builder) { + this.embeddedElastic = builder; + } + + @Override + public Statement apply(final Statement base, + final Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + validate(); + startElastic(); + + try { + base.evaluate(); + } finally { + embeddedElastic.stop(); + } + } + }; + } + + private void startElastic() throws IOException, InterruptedException { + if (embeddedElastic != null) { + embeddedElastic.start(); + } else { + embeddedElastic = EmbeddedElastic.builder() + .withElasticVersion("6.8.0") + .withSetting(PopularProperties.HTTP_PORT, port != 0 ? port : DEFAULT_PORT) + .withSetting(PopularProperties.CLUSTER_NAME, "test_cluster") + .withSetting("cluster.routing.allocation.disk.threshold_enabled", false) + .withStartTimeout(2, TimeUnit.MINUTES) + .withIndex(index) + .withDownloadDirectory(new File("./")) + .withInstallationDirectory(new File("./")) + .build(); + embeddedElastic.start(); + } + } + + private void validate() { + if (embeddedElastic != null) return; + if (index == null || index.replace(" ", "").isEmpty()) { + throw new IllegalArgumentException("Index can not be null"); + } + } +} diff --git a/test-base/build.gradle b/test-base/build.gradle index f65a9fa..2548cfc 100644 --- a/test-base/build.gradle +++ b/test-base/build.gradle @@ -4,4 +4,5 @@ dependencies { compile group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.4' compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7' compile group: 'org.skyscreamer', name: 'jsonassert', version: '1.3.0' + testCompile group: 'junit', name: 'junit', version: '4.12' } \ No newline at end of file diff --git a/test-base/src/main/groovy/pl/allegro/tech/embeddedelasticsearch/EmbeddedElasticRuleBuilderTest.java b/test-base/src/main/groovy/pl/allegro/tech/embeddedelasticsearch/EmbeddedElasticRuleBuilderTest.java new file mode 100644 index 0000000..67ff71c --- /dev/null +++ b/test-base/src/main/groovy/pl/allegro/tech/embeddedelasticsearch/EmbeddedElasticRuleBuilderTest.java @@ -0,0 +1,35 @@ +package pl.allegro.tech.embeddedelasticsearch; + +import org.junit.ClassRule; +import org.junit.Test; + +import java.io.File; +import java.util.concurrent.TimeUnit; + +import static junit.framework.TestCase.assertTrue; + +public class EmbeddedElasticRuleBuilderTest { + + @ClassRule + public static EmbeddedElasticRule embeddedElasticRule = new EmbeddedElasticRule(EmbeddedElastic.builder() + .withElasticVersion("6.8.0") + .withSetting(PopularProperties.HTTP_PORT, 9200) + .withSetting(PopularProperties.CLUSTER_NAME, "cluster_test") + .withSetting("cluster.routing.allocation.disk.threshold_enabled", false) + .withStartTimeout(2, TimeUnit.MINUTES) + .withIndex("test_builder") + .withDownloadDirectory(new File("./")) + .withInstallationDirectory(new File("./")) + .build()); + + @Test + public void shouldStartElastic() { + assertTrue(embeddedElasticRule.embeddedElastic.isStarted()); + } + + @Test + public void shouldCreateIndex() { + Boolean createdIndex = TestHelper.existsIndex("test_builder", 9200); + assertTrue(createdIndex); + } +} diff --git a/test-base/src/main/groovy/pl/allegro/tech/embeddedelasticsearch/EmbeddedElasticRuleTest.java b/test-base/src/main/groovy/pl/allegro/tech/embeddedelasticsearch/EmbeddedElasticRuleTest.java new file mode 100644 index 0000000..94fc229 --- /dev/null +++ b/test-base/src/main/groovy/pl/allegro/tech/embeddedelasticsearch/EmbeddedElasticRuleTest.java @@ -0,0 +1,24 @@ +package pl.allegro.tech.embeddedelasticsearch; + +import org.junit.ClassRule; +import org.junit.Test; + +import static junit.framework.TestCase.assertTrue; + +public class EmbeddedElasticRuleTest { + + @ClassRule + public static EmbeddedElasticRule embeddedElasticRule = new EmbeddedElasticRule("index_name", 9200); + + @Test + public void shouldStartElastic() { + assertTrue(embeddedElasticRule.embeddedElastic.isStarted()); + } + + @Test + public void shouldCreateIndex() { + Boolean createdIndex = TestHelper.existsIndex("index_name", 9200); + assertTrue(createdIndex); + } + +} diff --git a/test-base/src/main/groovy/pl/allegro/tech/embeddedelasticsearch/TestHelper.java b/test-base/src/main/groovy/pl/allegro/tech/embeddedelasticsearch/TestHelper.java new file mode 100644 index 0000000..c8d9f08 --- /dev/null +++ b/test-base/src/main/groovy/pl/allegro/tech/embeddedelasticsearch/TestHelper.java @@ -0,0 +1,14 @@ +package pl.allegro.tech.embeddedelasticsearch; + +import org.apache.http.client.methods.HttpHead; + +import static pl.allegro.tech.embeddedelasticsearch.HttpStatusCodes.OK; + +public class TestHelper { + + public static boolean existsIndex(String index, Integer port) { + final HttpClient httpClient = new HttpClient(); + HttpHead request = new HttpHead("http://localhost:" + port + "/" + index); + return httpClient.execute(request, response -> response.getStatusLine().getStatusCode() == OK); + } +}