Skip to content

Commit

Permalink
DONT REVIEW
Browse files Browse the repository at this point in the history
  • Loading branch information
trantienduchn committed May 8, 2019
1 parent 355a7b9 commit d9ede4c
Show file tree
Hide file tree
Showing 20 changed files with 359 additions and 80 deletions.
8 changes: 8 additions & 0 deletions backends-common/elasticsearch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you 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.apache.james.backends.es;

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

import java.util.Optional;

import org.apache.http.HttpStatus;
import org.apache.james.util.Host;
import org.apache.james.util.docker.DockerGenericContainer;
import org.apache.james.util.docker.Images;
import org.apache.james.util.docker.RateLimiters;
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;

import com.google.common.collect.ImmutableList;

import feign.Feign;
import feign.Logger;
import feign.RequestLine;
import feign.Response;
import feign.slf4j.Slf4jLogger;

public class DockerElasticSearch {

interface ElasticSearchAPI {

static ElasticSearchAPI from(Host esHttpHost) {
return Feign.builder()
.logger(new Slf4jLogger(ElasticSearchAPI.class))
.logLevel(Logger.Level.FULL)
.target(ElasticSearchAPI.class, "http://" + esHttpHost.getHostName() + ":" + esHttpHost.getPort());
}

@RequestLine("DELETE /_all")
Response deleteAllIndexes();

@RequestLine("POST /_flush?force&wait_if_ongoing=true")
Response flush();
}

private static final int ES_HTTP_PORT = 9200;
private static final int ES_TCP_PORT = 9300;

private final DockerGenericContainer eSContainer;

public DockerElasticSearch() {
this.eSContainer = new DockerGenericContainer(Images.ELASTICSEARCH_2)
.withExposedPorts(ES_HTTP_PORT, ES_TCP_PORT)
.waitingFor(new HostPortWaitStrategy().withRateLimiter(RateLimiters.TWENTIES_PER_SECOND));
}

public void start() {
if (!eSContainer.isRunning()) {
eSContainer.start();
}
}

public void stop() {
eSContainer.stop();
}

public int getHttpPort() {
return eSContainer.getMappedPort(ES_HTTP_PORT);
}

public int getTcpPort() {
return eSContainer.getMappedPort(ES_TCP_PORT);
}

public String getIp() {
return eSContainer.getHostIp();
}

public Host getTcpHost() {
return Host.from(getIp(), getTcpPort());
}

public Host getHttpHost() {
return Host.from(getIp(), getHttpPort());
}

public void pause() {
eSContainer.pause();
}

public void unpause() {
eSContainer.unpause();
}

public void cleanUpData() {
assertThat(esAPI().deleteAllIndexes().status())
.isEqualTo(HttpStatus.SC_OK);
}

public void awaitForElasticSearch() {
assertThat(esAPI().flush().status())
.isEqualTo(HttpStatus.SC_OK);
}

public ClientProvider clientProvider() {
Optional<String> noClusterName = Optional.empty();
return ClientProviderImpl.fromHosts(ImmutableList.of(getTcpHost()), noClusterName);
}

private ElasticSearchAPI esAPI() {
return ElasticSearchAPI.from(getHttpHost());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you 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.apache.james.backends.es;

public class DockerElasticSearchSingleton {
public static DockerElasticSearch INSTANCE = new DockerElasticSearch();

static {
INSTANCE.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import org.apache.james.mailbox.extractor.TextExtractor;
import org.apache.james.mailbox.store.search.PDFTextExtractor;
import org.apache.james.modules.TestESMetricReporterModule;
import org.apache.james.modules.TestDockerESMetricReporterModule;
import org.apache.james.modules.TestJMAPServerModule;
import org.apache.james.server.core.configuration.Configuration;
import org.apache.james.webadmin.WebAdminConfiguration;
Expand All @@ -42,17 +42,20 @@ public class CassandraJmapTestRule implements TestRule {
private final TemporaryFolder temporaryFolder;

public static CassandraJmapTestRule defaultTestRule() {
return new CassandraJmapTestRule(new EmbeddedElasticSearchRule());
return new CassandraJmapTestRule();
}

private final GuiceModuleTestRule guiceModuleTestRule;
private final DockerElasticSearchRule dockerElasticSearchRule;

public CassandraJmapTestRule(GuiceModuleTestRule... guiceModuleTestRule) {
TempFilesystemTestRule tempFilesystemTestRule = new TempFilesystemTestRule();
this.dockerElasticSearchRule = new DockerElasticSearchRule();
this.temporaryFolder = tempFilesystemTestRule.getTemporaryFolder();
this.guiceModuleTestRule =
AggregateGuiceModuleTestRule
.of(guiceModuleTestRule)
.aggregate(dockerElasticSearchRule)
.aggregate(tempFilesystemTestRule);
}

Expand All @@ -66,7 +69,7 @@ public GuiceJamesServer jmapServer(Module... additionals) throws IOException {
.combineWith(ALL_BUT_JMX_CASSANDRA_MODULE)
.overrideWith(binder -> binder.bind(TextExtractor.class).to(PDFTextExtractor.class))
.overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES))
.overrideWith(new TestESMetricReporterModule())
.overrideWith(new TestDockerESMetricReporterModule(dockerElasticSearchRule.getDockerEs().getHttpHost()))
.overrideWith(guiceModuleTestRule.getModule())
.overrideWith((binder -> binder.bind(CleanupTasksPerformer.class).asEagerSingleton()))
.overrideWith(binder -> binder.bind(WebAdminConfiguration.class).toInstance(WebAdminConfiguration.TEST_CONFIGURATION))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,22 @@

package org.apache.james;

import org.apache.james.backends.es.DockerElasticSearch;
import org.apache.james.backends.es.DockerElasticSearchSingleton;
import org.apache.james.backends.es.ElasticSearchConfiguration;
import org.apache.james.util.Host;
import org.apache.james.util.docker.DockerGenericContainer;
import org.junit.jupiter.api.extension.ExtensionContext;

import com.google.inject.Module;

public class DockerElasticSearchExtension implements GuiceModuleTestExtension {
public static final int ELASTIC_SEARCH_PORT = 9300;
public static final int ELASTIC_SEARCH_HTTP_PORT = 9200;

private final DockerGenericContainer elasticSearchContainer;

public DockerElasticSearchExtension(DockerGenericContainer elasticSearchContainer) {
this.elasticSearchContainer = elasticSearchContainer;
}

@Override
public void beforeEach(ExtensionContext extensionContext) {
elasticSearchContainer.start();
getDockerES().start();
}

@Override
public void afterEach(ExtensionContext extensionContext) {
elasticSearchContainer.stop();
}

@Override
Expand All @@ -54,7 +45,11 @@ public Module getModule() {

private ElasticSearchConfiguration getElasticSearchConfigurationForDocker() {
return ElasticSearchConfiguration.builder()
.addHost(Host.from(elasticSearchContainer.getHostIp(), elasticSearchContainer.getMappedPort(ELASTIC_SEARCH_PORT)))
.addHost(getDockerES().getTcpHost())
.build();
}

public DockerElasticSearch getDockerES() {
return DockerElasticSearchSingleton.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,35 @@

package org.apache.james;

import org.apache.james.backends.es.EmbeddedElasticSearch;
import org.apache.james.modules.TestElasticSearchModule;
import org.elasticsearch.node.Node;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;
import org.apache.james.backends.es.DockerElasticSearch;
import org.apache.james.backends.es.DockerElasticSearchSingleton;
import org.apache.james.modules.TestDockerElasticSearchModule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import com.google.inject.Module;


public class EmbeddedElasticSearchRule implements GuiceModuleTestRule {
public class DockerElasticSearchRule implements GuiceModuleTestRule {

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

private final RuleChain chain = RuleChain
.outerRule(temporaryFolder)
.around(embeddedElasticSearch);
private final DockerElasticSearch elasticSearch = DockerElasticSearchSingleton.INSTANCE;

@Override
public Statement apply(Statement base, Description description) {
return chain.apply(base, description);
return base;
}

@Override
public void await() {
embeddedElasticSearch.awaitForElasticSearch();
elasticSearch.awaitForElasticSearch();
}


@Override
public Module getModule() {
return new TestElasticSearchModule(embeddedElasticSearch);
return new TestDockerElasticSearchModule(elasticSearch);
}

public Node getNode() {
return embeddedElasticSearch.getNode();
public DockerElasticSearch getDockerEs() {
return elasticSearch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.apache.james.jmap.api.access.AccessToken;
import org.apache.james.mailbox.extractor.TextExtractor;
import org.apache.james.mailbox.store.search.PDFTextExtractor;
import org.apache.james.modules.TestESMetricReporterModule;
import org.apache.james.modules.TestEmbeddedESMetricReporterModule;
import org.apache.james.modules.TestJMAPServerModule;
import org.apache.james.modules.protocols.ImapGuiceProbe;
import org.apache.james.utils.DataProbeImpl;
Expand Down Expand Up @@ -71,7 +71,7 @@ class ESReporterTest {
.combineWith(ALL_BUT_JMX_CASSANDRA_MODULE)
.overrideWith(binder -> binder.bind(TextExtractor.class).to(PDFTextExtractor.class))
.overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES))
.overrideWith(new TestESMetricReporterModule()))
.overrideWith(new TestEmbeddedESMetricReporterModule()))
.build();

private static final int DELAY_IN_MS = 100;
Expand Down Expand Up @@ -156,7 +156,7 @@ private boolean checkMetricRecordedInElasticSearch() {
return !Arrays.stream(client.prepareSearch()
.setQuery(QueryBuilders.matchAllQuery())
.get().getHits().getHits())
.filter(searchHit -> searchHit.getIndex().startsWith(TestESMetricReporterModule.METRICS_INDEX))
.filter(searchHit -> searchHit.getIndex().startsWith(TestEmbeddedESMetricReporterModule.METRICS_INDEX))
.collect(Collectors.toList())
.isEmpty();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import org.apache.james.backends.es.EmbeddedElasticSearch;
import org.apache.james.junit.TemporaryFolderExtension;
import org.apache.james.modules.TestElasticSearchModule;
import org.apache.james.modules.TestEmbeddedElasticSearchModule;
import org.junit.jupiter.api.extension.ExtensionContext;

import com.google.inject.Module;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void afterEach(ExtensionContext extensionContext) throws Exception {

@Override
public Module getModule() {
return new TestElasticSearchModule(embeddedElasticSearch);
return new TestEmbeddedElasticSearchModule(embeddedElasticSearch);
}

@Override
Expand Down
Loading

0 comments on commit d9ede4c

Please sign in to comment.