Skip to content
This repository has been archived by the owner on May 18, 2020. It is now read-only.

Adds Junit Rule and method isStarted #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
1 change: 1 addition & 0 deletions test-base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}