You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ad...@apache.org on 2017/03/03 17:26:22 UTC

[14/14] james-project git commit: JAMES-1950 Add integration test on ES/TimeMetric

JAMES-1950 Add integration test on ES/TimeMetric


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/94d2ce3d
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/94d2ce3d
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/94d2ce3d

Branch: refs/heads/master
Commit: 94d2ce3dafdf315ecc24eb391d7341322cf4c51b
Parents: 6c9f703
Author: Antoine Duprat <ad...@linagora.com>
Authored: Wed Mar 1 11:58:12 2017 +0100
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Fri Mar 3 18:17:21 2017 +0100

----------------------------------------------------------------------
 server/container/guice/cassandra-guice/pom.xml  |   6 +
 .../org/apache/james/CassandraJmapTestRule.java |   2 +
 .../java/org/apache/james/ESReporterTest.java   | 163 +++++++++++++++++++
 .../apache/james/EmbeddedElasticSearchRule.java |   5 +
 .../modules/TestESMetricReporterModule.java     |  51 ++++++
 .../apache/james/metric/es/ESReporterTest.java  |  18 ++
 6 files changed, 245 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/94d2ce3d/server/container/guice/cassandra-guice/pom.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/pom.xml b/server/container/guice/cassandra-guice/pom.xml
index e4c3e67..957f51d 100644
--- a/server/container/guice/cassandra-guice/pom.xml
+++ b/server/container/guice/cassandra-guice/pom.xml
@@ -261,6 +261,12 @@
                 </dependency>
                 <dependency>
                     <groupId>${project.groupId}</groupId>
+                    <artifactId>james-server-jmap-integration-testing</artifactId>
+                    <type>test-jar</type>
+                    <scope>test</scope>
+                </dependency>
+                <dependency>
+                    <groupId>${project.groupId}</groupId>
                     <artifactId>james-server-util-java8</artifactId>
                     <scope>test</scope>
                     <type>test-jar</type>

http://git-wip-us.apache.org/repos/asf/james-project/blob/94d2ce3d/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java
index 2ad1b33..68d3371 100644
--- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java
@@ -18,6 +18,7 @@
  ****************************************************************/
 
 package org.apache.james;
+import org.apache.james.modules.TestESMetricReporterModule;
 import org.apache.james.modules.TestJMAPServerModule;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
@@ -48,6 +49,7 @@ public class CassandraJmapTestRule implements TestRule {
         return new JmapJamesServer()
             .combineWith(CassandraJamesServerMain.cassandraServerModule)
             .overrideWith(new TestJMAPServerModule(LIMIT_TO_3_MESSAGES))
+            .overrideWith(new TestESMetricReporterModule())
             .overrideWith(guiceModuleTestRule.getModule())
             .overrideWith(additionals);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/94d2ce3d/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java
new file mode 100644
index 0000000..ef33b42
--- /dev/null
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java
@@ -0,0 +1,163 @@
+/****************************************************************
+ * 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;
+
+import static com.jayway.awaitility.Awaitility.await;
+import static com.jayway.restassured.RestAssured.given;
+import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
+import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.util.Arrays;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.stream.Collectors;
+
+import org.apache.commons.net.imap.IMAPClient;
+import org.apache.http.client.utils.URIBuilder;
+import org.apache.james.jmap.HttpJmapAuthentication;
+import org.apache.james.jmap.api.access.AccessToken;
+import org.apache.james.modules.TestESMetricReporterModule;
+import org.apache.james.utils.GuiceServerProbe;
+import org.elasticsearch.client.Client;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.google.common.base.Charsets;
+import com.google.common.base.Throwables;
+import com.jayway.awaitility.Duration;
+import com.jayway.restassured.RestAssured;
+import com.jayway.restassured.builder.RequestSpecBuilder;
+import com.jayway.restassured.http.ContentType;
+
+public class ESReporterTest {
+
+    private static final int IMAP_PORT = 1143;
+    private static final int DELAY_IN_MS = 100;
+    private static final int PERIOD_IN_MS = 100;
+
+    private static final String DOMAIN = "james.org";
+    private static final String USERNAME = "user1@" + DOMAIN;
+    private static final String PASSWORD = "secret";
+
+    private EmbeddedElasticSearchRule embeddedElasticSearchRule = new EmbeddedElasticSearchRule();
+
+    private Timer timer;
+
+    @Rule
+    public CassandraJmapTestRule cassandraJmap = new CassandraJmapTestRule(embeddedElasticSearchRule, new EmbeddedCassandraRule());
+
+    private JmapJamesServer server;
+    private AccessToken accessToken;
+
+    @Before
+    public void setup() throws Exception {
+        server = cassandraJmap.jmapServer();
+        server.start();
+        GuiceServerProbe serverProbe = server.getGuiceProbeProvider().getProbe(GuiceServerProbe.class);
+        serverProbe.addDomain(DOMAIN);
+        serverProbe.addUser(USERNAME, PASSWORD);
+
+        RestAssured.requestSpecification = new RequestSpecBuilder()
+                .setContentType(ContentType.JSON)
+                .setAccept(ContentType.JSON)
+                .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
+                .setPort(server.getJmapProbe().getJmapPort())
+                .build();
+        accessToken = HttpJmapAuthentication.authenticateJamesUser(baseUri(), USERNAME, PASSWORD);
+
+        timer = new Timer();
+    }
+
+    private URIBuilder baseUri() {
+        return new URIBuilder()
+            .setScheme("http")
+            .setHost("localhost")
+            .setPort(server.getJmapProbe().getJmapPort())
+            .setCharset(Charsets.UTF_8);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (server != null) {
+            server.stop();
+        }
+
+        timer.cancel();
+    }
+
+    @Test
+    public void timeMetricsShouldBeReportedWhenImapCommandsReceived() throws Exception {
+        IMAPClient client = new IMAPClient();
+        client.connect(InetAddress.getLocalHost(), IMAP_PORT);
+        client.login(USERNAME, PASSWORD);
+        
+        TimerTask timerTask = new TimerTask() {
+            @Override
+            public void run() {
+                try {
+                    client.list("", "*");
+                } catch (IOException e) {
+                    throw Throwables.propagate(e);
+                }
+            }
+        };
+        timer.schedule(timerTask, DELAY_IN_MS, PERIOD_IN_MS);
+
+        await().atMost(Duration.TEN_MINUTES)
+            .until(() -> checkMetricRecordedInElasticSearch());
+    }
+
+    @Test
+    public void timeMetricsShouldBeReportedWhenJmapRequestsReceived() throws Exception {
+        TimerTask timerTask = new TimerTask() {
+            @Override
+            public void run() {
+                    given()
+                        .header("Authorization", accessToken.serialize())
+                        .body("[[\"getMailboxes\", {}, \"#0\"]]")
+                    .when()
+                        .post("/jmap")
+                    .then()
+                        .statusCode(200);
+            }
+        };
+        timer.schedule(timerTask, DELAY_IN_MS, PERIOD_IN_MS);
+
+        await().atMost(Duration.TEN_MINUTES)
+            .until(() -> checkMetricRecordedInElasticSearch());
+    }
+
+    private boolean checkMetricRecordedInElasticSearch() {
+        try (Client client = embeddedElasticSearchRule.getNode().client()) {
+            return !Arrays.stream(client.prepareSearch()
+                    .setQuery(QueryBuilders.matchAllQuery())
+                    .get().getHits().getHits())
+                .filter(searchHit -> searchHit.getIndex().startsWith(TestESMetricReporterModule.METRICS_INDEX))
+                .collect(Collectors.toList())
+                .isEmpty();
+        } catch (Exception e) {
+            return false;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/94d2ce3d/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchRule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchRule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchRule.java
index 3568efb..8c300da 100644
--- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchRule.java
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchRule.java
@@ -22,6 +22,7 @@ package org.apache.james;
 import org.apache.james.backends.es.EmbeddedElasticSearch;
 import org.apache.james.mailbox.elasticsearch.MailboxElasticsearchConstants;
 import org.apache.james.modules.TestElasticSearchModule;
+import org.elasticsearch.node.Node;
 import org.junit.rules.RuleChain;
 import org.junit.rules.TemporaryFolder;
 import org.junit.runner.Description;
@@ -54,4 +55,8 @@ public class EmbeddedElasticSearchRule implements GuiceModuleTestRule {
     public Module getModule() {
         return new TestElasticSearchModule(embeddedElasticSearch);
     }
+
+    public Node getNode() {
+        return embeddedElasticSearch.getNode();
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/94d2ce3d/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestESMetricReporterModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestESMetricReporterModule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestESMetricReporterModule.java
new file mode 100644
index 0000000..6f2bd04
--- /dev/null
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestESMetricReporterModule.java
@@ -0,0 +1,51 @@
+/****************************************************************
+ * 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.modules;
+
+import javax.inject.Singleton;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.metrics.es.ESReporterConfiguration;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Provides;
+
+public class TestESMetricReporterModule 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 configure() {
+    }
+
+    @Provides
+    @Singleton
+    public ESReporterConfiguration provideConfiguration(FileSystem fileSystem) throws ConfigurationException {
+        return ESReporterConfiguration.builder()
+            .enabled()
+            .onHost(LOCALHOST, DEFAULT_ES_HTTP_PORT)
+            .onIndex(METRICS_INDEX)
+            .periodInSecond(1l)
+            .build();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/94d2ce3d/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java
----------------------------------------------------------------------
diff --git a/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java
index f592ddc..fa5aa43 100644
--- a/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java
+++ b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java
@@ -27,6 +27,7 @@ import java.util.TimerTask;
 import org.apache.james.backends.es.ClientProvider;
 import org.apache.james.backends.es.ClientProviderImpl;
 import org.apache.james.metrics.api.Metric;
+import org.apache.james.metrics.api.TimeMetric;
 import org.apache.james.metrics.dropwizard.DropWizardMetricFactory;
 import org.apache.james.metrics.es.ESMetricReporter;
 import org.apache.james.metrics.es.ESReporterConfiguration;
@@ -100,6 +101,23 @@ public class ESReporterTest {
             .until(() -> done(clientProvider));
     }
 
+    @Test
+    public void esMetricReporterShouldProduceDocumentsOnAnElasticsearchContainerWhenRecordingTimeMetric() {
+        esMetricReporter.start();
+
+        TimeMetric metric = new DropWizardMetricFactory(registry).timer("itstime");
+        TimerTask timerTask = new TimerTask() {
+            @Override
+            public void run() {
+                metric.stopAndPublish();
+            }
+        };
+        timer.schedule(timerTask, DELAY_IN_MS, PERIOD_IN_MS);
+
+        await().atMost(Duration.TEN_MINUTES)
+            .until(() -> done(clientProvider));
+    }
+
     private boolean elasticSearchStarted(ClientProvider clientProvider) {
         try (Client client = clientProvider.get()) {
             return true;


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org