Skip to content

Commit

Permalink
DONT REVIEW
Browse files Browse the repository at this point in the history
  • Loading branch information
trantienduchn committed May 9, 2019
1 parent c61f770 commit 2503937
Show file tree
Hide file tree
Showing 23 changed files with 258 additions and 325 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,34 @@
* under the License. *
****************************************************************/

package org.apache.james.modules;
package org.apache.james.backends.es;

import javax.inject.Singleton;
import org.apache.james.util.Host;
import org.junit.rules.ExternalResource;

import org.apache.james.metrics.es.ESReporterConfiguration;
public class DockerElasticSearchRule extends ExternalResource {

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
private final DockerElasticSearch dockerElasticSearch = DockerElasticSearchSingleton.INSTANCE;

public class TestEmbeddedESMetricReporterModule extends AbstractModule {

private static final String LOCALHOST = "localhost";
private static final int DEFAULT_ES_HTTP_PORT = 9200;
public static final String METRICS_INDEX = "metrics";
@Override
protected void before() throws Throwable {
dockerElasticSearch.start();
}

@Override
protected void configure() {
protected void after() {
dockerElasticSearch.cleanUpData();
}

public ClientProvider clientProvider() {
return dockerElasticSearch.clientProvider();
}

public void awaitForElasticSearch() {
dockerElasticSearch.awaitForElasticSearch();
}

@Provides
@Singleton
public ESReporterConfiguration provideConfiguration() {
return ESReporterConfiguration.builder()
.enabled()
.onHost(LOCALHOST, DEFAULT_ES_HTTP_PORT)
.onIndex(METRICS_INDEX)
.periodInSecond(1L)
.build();
public Host getTcpHost() {
return dockerElasticSearch.getTcpHost();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@

import java.util.concurrent.Executors;

import org.apache.james.backends.es.utils.TestingClientProvider;
import org.apache.james.util.concurrent.NamedThreadFactory;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;

import com.google.common.collect.ImmutableList;

Expand All @@ -45,37 +41,35 @@ public class ElasticSearchIndexerTest {
private static final IndexName INDEX_NAME = new IndexName("index_name");
private static final WriteAliasName ALIAS_NAME = new WriteAliasName("alias_name");
private static final TypeName TYPE_NAME = new TypeName("type_name");
private TemporaryFolder temporaryFolder = new TemporaryFolder();
private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder);

@Rule
public RuleChain ruleChain = RuleChain.outerRule(temporaryFolder).around(embeddedElasticSearch);

private Node node;
public DockerElasticSearchRule elasticSearch = new DockerElasticSearchRule();
private ElasticSearchIndexer testee;

@Before
public void setup() {
node = embeddedElasticSearch.getNode();
TestingClientProvider clientProvider = new TestingClientProvider(node);
new IndexCreationFactory(ElasticSearchConfiguration.DEFAULT_CONFIGURATION)
.useIndex(INDEX_NAME)
.addAlias(ALIAS_NAME)
.createIndexAndAliases(clientProvider.get());
testee = new ElasticSearchIndexer(clientProvider.get(),
.createIndexAndAliases(getESClient());
testee = new ElasticSearchIndexer(getESClient(),
Executors.newSingleThreadExecutor(NamedThreadFactory.withClassName(getClass())),
ALIAS_NAME, TYPE_NAME, MINIMUM_BATCH_SIZE);
}


private Client getESClient() {
return elasticSearch.clientProvider().get();
}

@Test
public void indexMessageShouldWork() {
String messageId = "1";
String content = "{\"message\": \"trying out Elasticsearch\"}";

testee.index(messageId, content);
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

try (Client client = node.client()) {
try (Client client = getESClient()) {
SearchResponse searchResponse = client.prepareSearch(INDEX_NAME.getValue())
.setTypes(TYPE_NAME.getValue())
.setQuery(QueryBuilders.matchQuery("message", "trying"))
Expand All @@ -96,20 +90,20 @@ public void updateMessages() {
String content = "{\"message\": \"trying out Elasticsearch\",\"field\":\"Should be unchanged\"}";

testee.index(messageId, content);
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

testee.update(ImmutableList.of(new UpdatedRepresentation(messageId, "{\"message\": \"mastering out Elasticsearch\"}")));
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

try (Client client = node.client()) {
try (Client client = getESClient()) {
SearchResponse searchResponse = client.prepareSearch(INDEX_NAME.getValue())
.setTypes(TYPE_NAME.getValue())
.setQuery(QueryBuilders.matchQuery("message", "mastering"))
.get();
assertThat(searchResponse.getHits().getTotalHits()).isEqualTo(1);
}

try (Client client = node.client()) {
try (Client client = getESClient()) {
SearchResponse searchResponse = client.prepareSearch(INDEX_NAME.getValue())
.setTypes(TYPE_NAME.getValue())
.setQuery(QueryBuilders.matchQuery("field", "unchanged"))
Expand Down Expand Up @@ -148,12 +142,12 @@ public void deleteByQueryShouldWorkOnSingleMessage() throws Exception {
String content = "{\"message\": \"trying out Elasticsearch\", \"property\":\"1\"}";

testee.index(messageId, content);
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

testee.deleteAllMatchingQuery(termQuery("property", "1")).get();
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

try (Client client = node.client()) {
try (Client client = getESClient()) {
SearchResponse searchResponse = client.prepareSearch(INDEX_NAME.getValue())
.setTypes(TYPE_NAME.getValue())
.setQuery(QueryBuilders.matchAllQuery())
Expand All @@ -178,12 +172,12 @@ public void deleteByQueryShouldWorkWhenMultipleMessages() throws Exception {
String content3 = "{\"message\": \"trying out Elasticsearch 3\", \"property\":\"2\"}";

testee.index(messageId3, content3);
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

testee.deleteAllMatchingQuery(termQuery("property", "1")).get();
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

try (Client client = node.client()) {
try (Client client = getESClient()) {
SearchResponse searchResponse = client.prepareSearch(INDEX_NAME.getValue())
.setTypes(TYPE_NAME.getValue())
.setQuery(QueryBuilders.matchAllQuery())
Expand All @@ -198,12 +192,12 @@ public void deleteMessage() {
String content = "{\"message\": \"trying out Elasticsearch\"}";

testee.index(messageId, content);
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

testee.delete(ImmutableList.of(messageId));
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

try (Client client = node.client()) {
try (Client client = getESClient()) {
SearchResponse searchResponse = client.prepareSearch(INDEX_NAME.getValue())
.setTypes(TYPE_NAME.getValue())
.setQuery(QueryBuilders.matchAllQuery())
Expand All @@ -228,12 +222,12 @@ public void deleteShouldWorkWhenMultipleMessages() {
String content3 = "{\"message\": \"trying out Elasticsearch 3\", \"mailboxId\":\"2\"}";

testee.index(messageId3, content3);
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

testee.delete(ImmutableList.of(messageId, messageId3));
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

try (Client client = node.client()) {
try (Client client = getESClient()) {
SearchResponse searchResponse = client.prepareSearch(INDEX_NAME.getValue())
.setTypes(TYPE_NAME.getValue())
.setQuery(QueryBuilders.matchAllQuery())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,21 @@

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.apache.james.backends.es.utils.TestingClientProvider;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;

public class IndexCreationFactoryTest {
public static final IndexName INDEX_NAME = new IndexName("index");
public static final ReadAliasName ALIAS_NAME = new ReadAliasName("alias");

private TemporaryFolder temporaryFolder = new TemporaryFolder();
private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder);

@Rule
public RuleChain ruleChain = RuleChain.outerRule(temporaryFolder).around(embeddedElasticSearch);

public DockerElasticSearchRule elasticSearch = new DockerElasticSearchRule();
private ClientProvider clientProvider;

@Before
public void setUp() {
clientProvider = new TestingClientProvider(embeddedElasticSearch.getNode());
clientProvider = elasticSearch.clientProvider();
new IndexCreationFactory(ElasticSearchConfiguration.DEFAULT_CONFIGURATION)
.useIndex(INDEX_NAME)
.addAlias(ALIAS_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,24 @@

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import org.apache.james.backends.es.utils.TestingClientProvider;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;

public class NodeMappingFactoryTest {
public static final String MESSAGE = "message";
public static final IndexName INDEX_NAME = new IndexName("index");
public static final ReadAliasName ALIAS_NAME = new ReadAliasName("alias");
public static final TypeName TYPE_NAME = new TypeName("type");

private TemporaryFolder temporaryFolder = new TemporaryFolder();
private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder);

@Rule
public RuleChain ruleChain = RuleChain.outerRule(temporaryFolder).around(embeddedElasticSearch);

public DockerElasticSearchRule elasticSearch = new DockerElasticSearchRule();
private ClientProvider clientProvider;

@Before
public void setUp() throws Exception {
clientProvider = new TestingClientProvider(embeddedElasticSearch.getNode());
clientProvider = elasticSearch.clientProvider();
new IndexCreationFactory(ElasticSearchConfiguration.DEFAULT_CONFIGURATION)
.useIndex(INDEX_NAME)
.addAlias(ALIAS_NAME)
Expand All @@ -71,7 +64,7 @@ public void applyMappingShouldNotThrowWhenIndexerChanges() throws Exception {
TYPE_NAME,
getMappingsSources());

embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();

NodeMappingFactory.applyMapping(clientProvider.get(),
INDEX_NAME,
Expand Down
Loading

0 comments on commit 2503937

Please sign in to comment.