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 bt...@apache.org on 2019/09/17 02:27:44 UTC
[james-project] 02/05: JAMES-2879 MockSMTPServer
ConfigurationClient with feign
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 8fb81ef175d3069e0278941cdc85d96f996a8e5c
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Mon Sep 9 18:31:32 2019 +0700
JAMES-2879 MockSMTPServer ConfigurationClient with feign
---
server/mailet/mock-smtp-server/pom.xml | 12 +++
.../mock/smtp/server/ConfigurationClient.java | 86 ++++++++++++++++
.../mock/smtp/server/HTTPConfigurationServer.java | 4 +-
.../apache/james/mock/smtp/server/model/Mail.java | 5 +
.../mock/smtp/server/ConfigurationClientTest.java | 109 +++++++++++++++++++++
.../org/apache/james/mock/smtp/server/Fixture.java | 29 +++++-
.../smtp/server/HTTPConfigurationServerTest.java | 34 ++-----
.../james/mock/smtp/server/model/MailTest.java | 5 +-
8 files changed, 252 insertions(+), 32 deletions(-)
diff --git a/server/mailet/mock-smtp-server/pom.xml b/server/mailet/mock-smtp-server/pom.xml
index d371982..d29267d 100644
--- a/server/mailet/mock-smtp-server/pom.xml
+++ b/server/mailet/mock-smtp-server/pom.xml
@@ -76,6 +76,18 @@
<artifactId>guava</artifactId>
</dependency>
<dependency>
+ <groupId>io.github.openfeign</groupId>
+ <artifactId>feign-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.github.openfeign</groupId>
+ <artifactId>feign-jackson</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.github.openfeign</groupId>
+ <artifactId>feign-slf4j</artifactId>
+ </dependency>
+ <dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ConfigurationClient.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ConfigurationClient.java
new file mode 100644
index 0000000..ddc4723
--- /dev/null
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ConfigurationClient.java
@@ -0,0 +1,86 @@
+/****************************************************************
+ * 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.mock.smtp.server;
+
+import java.util.List;
+
+import org.apache.james.mock.smtp.server.jackson.MailAddressModule;
+import org.apache.james.mock.smtp.server.model.Mail;
+import org.apache.james.mock.smtp.server.model.MockSMTPBehavior;
+import org.apache.james.mock.smtp.server.model.MockSmtpBehaviors;
+import org.apache.james.util.Host;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.guava.GuavaModule;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+import com.google.common.annotations.VisibleForTesting;
+
+import feign.Feign;
+import feign.Logger;
+import feign.RequestLine;
+import feign.jackson.JacksonDecoder;
+import feign.jackson.JacksonEncoder;
+import feign.slf4j.Slf4jLogger;
+
+public interface ConfigurationClient {
+
+ @VisibleForTesting
+ static ConfigurationClient fromServer(HTTPConfigurationServer server) {
+ return from(Host.from("localhost", server.getPort().getValue()));
+ }
+
+ static ConfigurationClient from(Host mockServerHttpHost) {
+ return Feign.builder()
+ .logger(new Slf4jLogger(ConfigurationClient.class))
+ .logLevel(Logger.Level.FULL)
+ .encoder(new JacksonEncoder(OBJECT_MAPPER))
+ .decoder(new JacksonDecoder(OBJECT_MAPPER))
+ .target(ConfigurationClient.class, "http://" + mockServerHttpHost.asString());
+ }
+
+ ObjectMapper OBJECT_MAPPER = new ObjectMapper()
+ .registerModule(new Jdk8Module())
+ .registerModule(new GuavaModule())
+ .registerModule(MailAddressModule.MODULE);
+
+ @RequestLine("PUT " + HTTPConfigurationServer.SMTP_BEHAVIORS)
+ void setBehaviors(MockSmtpBehaviors behaviors);
+
+ @RequestLine("DELETE " + HTTPConfigurationServer.SMTP_BEHAVIORS)
+ void clearBehaviors();
+
+ @RequestLine("GET " + HTTPConfigurationServer.SMTP_BEHAVIORS)
+ List<MockSMTPBehavior> listBehaviors();
+
+ @RequestLine("GET " + HTTPConfigurationServer.SMTP_MAILS)
+ List<Mail> listMails();
+
+ @RequestLine("DELETE " + HTTPConfigurationServer.SMTP_MAILS)
+ void clearMails();
+
+ default void setBehaviors(List<MockSMTPBehavior> behaviors) {
+ setBehaviors(new MockSmtpBehaviors(behaviors));
+ }
+
+ default void cleanServer() {
+ clearBehaviors();
+ clearMails();
+ }
+}
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java
index cfc6e2f..b3963ed 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java
@@ -101,8 +101,8 @@ public class HTTPConfigurationServer {
}
}
- private static final String SMTP_BEHAVIORS = "/smtpBehaviors";
- private static final String SMTP_MAILS = "/smtpMails";
+ static final String SMTP_BEHAVIORS = "/smtpBehaviors";
+ static final String SMTP_MAILS = "/smtpMails";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.registerModule(new Jdk8Module())
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mail.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mail.java
index 12fdd5c..2c1cb6a 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mail.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mail.java
@@ -19,6 +19,7 @@
package org.apache.james.mock.smtp.server.model;
+import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@@ -69,6 +70,10 @@ public class Mail {
private final MailAddress from;
private final List<MailAddress> recipients;
+ public Envelope(MailAddress from, MailAddress... recipients) {
+ this(from, ImmutableList.copyOf(Arrays.asList(recipients)));
+ }
+
public Envelope(MailAddress from, List<MailAddress> recipients) {
Preconditions.checkNotNull(from);
Preconditions.checkNotNull(recipients);
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ConfigurationClientTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ConfigurationClientTest.java
new file mode 100644
index 0000000..21128ec
--- /dev/null
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ConfigurationClientTest.java
@@ -0,0 +1,109 @@
+/****************************************************************
+ * 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/LICENSE2.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.mock.smtp.server;
+
+import static org.apache.james.mock.smtp.server.Fixture.BEHAVIOR_LIST;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.mock.smtp.server.Fixture.MailsFixutre;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class ConfigurationClientTest {
+ private ConfigurationClient testee;
+ private HTTPConfigurationServer server;
+ private SMTPBehaviorRepository behaviorRepository;
+ private ReceivedMailRepository mailRepository;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ behaviorRepository = new SMTPBehaviorRepository();
+ mailRepository = new ReceivedMailRepository();
+ server = HTTPConfigurationServer.onRandomPort(behaviorRepository, mailRepository);
+ server.start();
+
+ testee = ConfigurationClient.fromServer(server);
+ }
+
+ @AfterEach
+ void tearDown() throws Exception {
+ server.stop();
+ }
+
+ @Test
+ void listBehaviorsShouldReturnEmptyWhenNoSet() {
+ assertThat(testee.listBehaviors())
+ .isEmpty();
+ }
+
+ @Test
+ void listBehaviorsShouldReturnDefinedBehaviors() {
+ behaviorRepository.setBehaviors(Fixture.BEHAVIORS);
+
+ assertThat(testee.listBehaviors())
+ .isEqualTo(Fixture.BEHAVIORS.getBehaviorList());
+ }
+
+ @Test
+ void setBehaviorsShouldStoreBehaviors() {
+ testee.setBehaviors(BEHAVIOR_LIST);
+
+ assertThat(testee.listBehaviors())
+ .isEqualTo(BEHAVIOR_LIST);
+ }
+
+ @Test
+ void clearBehaviorsShouldRemoveAllBehaviors() {
+ testee.setBehaviors(BEHAVIOR_LIST);
+
+ testee.clearBehaviors();
+
+ assertThat(testee.listBehaviors())
+ .isEmpty();
+ }
+
+ @Test
+ void listMailsShouldReturnEmptyWhenNoStore() {
+ assertThat(testee.listMails())
+ .isEmpty();
+ }
+
+ @Test
+ void listMailsShouldReturnStoredMails() {
+ mailRepository.store(MailsFixutre.MAIL_1);
+ mailRepository.store(MailsFixutre.MAIL_2);
+
+ assertThat(testee.listMails())
+ .containsExactly(MailsFixutre.MAIL_1, MailsFixutre.MAIL_2);
+ }
+
+ @Test
+ void clearMailsRemoveAllStoredMails() {
+ mailRepository.store(MailsFixutre.MAIL_1);
+ mailRepository.store(MailsFixutre.MAIL_2);
+
+ testee.clearMails();
+
+ assertThat(testee.listMails())
+ .isEmpty();
+ }
+
+}
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java
index 2961401..c984794 100644
--- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java
@@ -19,8 +19,10 @@
package org.apache.james.mock.smtp.server;
+import org.apache.james.core.MailAddress;
import org.apache.james.mock.smtp.server.jackson.MailAddressModule;
import org.apache.james.mock.smtp.server.model.Condition;
+import org.apache.james.mock.smtp.server.model.Mail;
import org.apache.james.mock.smtp.server.model.MockSMTPBehavior;
import org.apache.james.mock.smtp.server.model.MockSmtpBehaviors;
import org.apache.james.mock.smtp.server.model.Operator;
@@ -33,6 +35,28 @@ import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.google.common.collect.ImmutableList;
public interface Fixture {
+
+ class MailsFixutre {
+ static Mail MAIL_1;
+ static Mail MAIL_2;
+ static {
+ try {
+ MAIL_1 = new Mail(
+ new Mail.Envelope(
+ new MailAddress(BOB), new MailAddress(ALICE), new MailAddress(JACK)),
+ "bob to alice and jack");
+
+ MAIL_2 = new Mail(
+ new Mail.Envelope(
+ new MailAddress(ALICE), new MailAddress(BOB)),
+ "alice to bob");
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ }
+
String DOMAIN = "james.org";
String BOB = "bob@" + DOMAIN;
String ALICE = "alice@" + DOMAIN;
@@ -89,9 +113,10 @@ public interface Fixture {
String JSON_BEHAVIORS = "[" + JSON_BEHAVIOR_ALL_FIELDS + ", "
+ JSON_BEHAVIOR_COMPULSORY_FIELDS + "]";
- MockSmtpBehaviors BEHAVIORS = new MockSmtpBehaviors(ImmutableList.of(
+ ImmutableList<MockSMTPBehavior> BEHAVIOR_LIST = ImmutableList.of(
BEHAVIOR_ALL_FIELDS,
- BEHAVIOR_COMPULSORY_FIELDS));
+ BEHAVIOR_COMPULSORY_FIELDS);
+ MockSmtpBehaviors BEHAVIORS = new MockSmtpBehaviors(BEHAVIOR_LIST);
String JSON_MAILS_LIST = "[" +
" {\"from\":\"bob@james.org\",\"recipients\":[\"alice@james.org\", \"jack@james.org\"],\"message\":\"bob to alice and jack\"}," +
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java
index a65d261..d15f607 100644
--- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java
@@ -26,9 +26,6 @@ import static io.restassured.config.EncoderConfig.encoderConfig;
import static io.restassured.config.RestAssuredConfig.newConfig;
import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
-import static org.apache.james.mock.smtp.server.Fixture.ALICE;
-import static org.apache.james.mock.smtp.server.Fixture.BOB;
-import static org.apache.james.mock.smtp.server.Fixture.JACK;
import static org.apache.james.mock.smtp.server.Fixture.JSON_BEHAVIORS;
import static org.apache.james.mock.smtp.server.Fixture.JSON_MAIL;
import static org.apache.james.mock.smtp.server.Fixture.JSON_MAILS_LIST;
@@ -36,16 +33,13 @@ import static org.hamcrest.Matchers.hasSize;
import java.nio.charset.StandardCharsets;
-import org.apache.james.core.MailAddress;
-import org.apache.james.mock.smtp.server.model.Mail;
+import org.apache.james.mock.smtp.server.Fixture.MailsFixutre;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
-import com.google.common.collect.ImmutableList;
-
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
@@ -148,8 +142,6 @@ class HTTPConfigurationServerTest {
@Nested
class SMTPMailsTest {
private ReceivedMailRepository mailRepository;
- private Mail mail1;
- private Mail mail2;
@BeforeEach
void setUp() throws Exception {
@@ -165,18 +157,6 @@ class HTTPConfigurationServerTest {
.setPort(server.getPort().getValue())
.setBasePath("/smtpMails")
.build();
-
- mail1 = new Mail(
- new Mail.Envelope(
- new MailAddress(BOB),
- ImmutableList.of(new MailAddress(ALICE), new MailAddress(JACK))),
- "bob to alice and jack");
-
- mail2 = new Mail(
- new Mail.Envelope(
- new MailAddress(ALICE),
- ImmutableList.of(new MailAddress(BOB))),
- "alice to bob");
}
@AfterEach
@@ -195,7 +175,7 @@ class HTTPConfigurationServerTest {
@Test
void getShouldReturnPreviouslyStoredData() {
- mailRepository.store(mail1);
+ mailRepository.store(MailsFixutre.MAIL_1);
String response = when()
.get()
@@ -211,8 +191,8 @@ class HTTPConfigurationServerTest {
@Test
void getShouldReturnMultipleEmails() {
- mailRepository.store(mail1);
- mailRepository.store(mail2);
+ mailRepository.store(MailsFixutre.MAIL_1);
+ mailRepository.store(MailsFixutre.MAIL_2);
String response = when()
.get()
@@ -228,8 +208,8 @@ class HTTPConfigurationServerTest {
@Test
void getShouldNotReturnClearedEmails() {
- mailRepository.store(mail1);
- mailRepository.store(mail2);
+ mailRepository.store(MailsFixutre.MAIL_1);
+ mailRepository.store(MailsFixutre.MAIL_2);
with()
.delete();
@@ -250,7 +230,7 @@ class HTTPConfigurationServerTest {
@Test
void getShouldReturnEmptyAfterClear() {
- mailRepository.store(mail1);
+ mailRepository.store(MailsFixutre.MAIL_1);
mailRepository.clear();
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/model/MailTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/model/MailTest.java
index b9d695a..e65e4d0 100644
--- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/model/MailTest.java
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/model/MailTest.java
@@ -21,6 +21,8 @@ package org.apache.james.mock.smtp.server.model;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import java.util.List;
+
import org.apache.james.core.MailAddress;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
@@ -58,7 +60,8 @@ class MailTest {
@Test
void constructorShouldThrowWhenNullRecipients() {
- assertThatThrownBy(() -> new Mail.Envelope(bob, null))
+ List<MailAddress> nullList = null;
+ assertThatThrownBy(() -> new Mail.Envelope(bob, nullList))
.isInstanceOf(NullPointerException.class);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org