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 66af0e2939f63c19c4beb858d18cf8e37e851c8e Author: Tran Tien Duc <dt...@linagora.com> AuthorDate: Tue Sep 10 14:47:29 2019 +0700 JAMES-2879 Move fluent builder to ConfigurationClient It's the real place needing this fluent --- .../james/mailets/RemoteDeliveryErrorTest.java | 86 +++++++-------- .../mock/smtp/server/ConfigurationClient.java | 120 +++++++++++++++++++++ .../mock/smtp/server/model/MockSmtpBehaviors.java | 110 ------------------- 3 files changed, 157 insertions(+), 159 deletions(-) diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RemoteDeliveryErrorTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RemoteDeliveryErrorTest.java index 5a1ce2a..493e976 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RemoteDeliveryErrorTest.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RemoteDeliveryErrorTest.java @@ -24,10 +24,10 @@ import static org.apache.james.mailets.configuration.Constants.DEFAULT_DOMAIN; import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP; import static org.apache.james.mailets.configuration.Constants.PASSWORD; import static org.apache.james.mailets.configuration.Constants.awaitAtMostOneMinute; -import static org.apache.james.mock.smtp.server.model.MockSmtpBehaviors.Builder.ConditionStep.anyInput; -import static org.apache.james.mock.smtp.server.model.MockSmtpBehaviors.Builder.ConditionStep.inputContaining; -import static org.apache.james.mock.smtp.server.model.MockSmtpBehaviors.Builder.ResponseStep.doesNotAcceptAnyMail; -import static org.apache.james.mock.smtp.server.model.MockSmtpBehaviors.Builder.ResponseStep.serviceNotAvailable; +import static org.apache.james.mock.smtp.server.ConfigurationClient.BehaviorsParamsBuilder.ConditionStep.anyInput; +import static org.apache.james.mock.smtp.server.ConfigurationClient.BehaviorsParamsBuilder.ConditionStep.inputContaining; +import static org.apache.james.mock.smtp.server.ConfigurationClient.BehaviorsParamsBuilder.ResponseStep.doesNotAcceptAnyMail; +import static org.apache.james.mock.smtp.server.ConfigurationClient.BehaviorsParamsBuilder.ResponseStep.serviceNotAvailable; import static org.assertj.core.api.Assertions.assertThat; import java.net.InetAddress; @@ -43,7 +43,6 @@ import org.apache.james.mailets.configuration.MailetContainer; import org.apache.james.mailets.configuration.ProcessorConfiguration; import org.apache.james.mock.smtp.server.ConfigurationClient; import org.apache.james.mock.smtp.server.model.Mail; -import org.apache.james.mock.smtp.server.model.MockSmtpBehaviors; import org.apache.james.mock.smtp.server.model.SMTPCommand; import org.apache.james.modules.protocols.ImapGuiceProbe; import org.apache.james.modules.protocols.SmtpGuiceProbe; @@ -164,14 +163,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldBounceWhenAlwaysRCPT421() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.RCPT_TO) .matching(anyInput()) .thenRespond(serviceNotAvailable("mock response")) .anyTimes() - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(FROM, RECIPIENT); @@ -186,14 +184,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldBounceWhenAlwaysFROM421() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.MAIL_FROM) .matching(anyInput()) .thenRespond(serviceNotAvailable("mock response")) .anyTimes() - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(FROM, RECIPIENT); @@ -208,14 +205,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldBounceWhenAlwaysDATA421() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.DATA) .matching(anyInput()) .thenRespond(serviceNotAvailable("mock response")) .anyTimes() - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(FROM, RECIPIENT); @@ -230,14 +226,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldNotRetryWhenRCPT500() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.RCPT_TO) .matching(anyInput()) .thenRespond(doesNotAcceptAnyMail("mock message")) .onlySomeTimes(1) - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(FROM, RECIPIENT); @@ -252,14 +247,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldNotRetryWhenFROM500() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.MAIL_FROM) .matching(anyInput()) .thenRespond(doesNotAcceptAnyMail("mock message")) .onlySomeTimes(1) - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(FROM, RECIPIENT); @@ -274,14 +268,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldNotRetryWhenDATA500() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.DATA) .matching(anyInput()) .thenRespond(doesNotAcceptAnyMail("mock message")) .onlySomeTimes(1) - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(FROM, RECIPIENT); @@ -296,14 +289,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldRetryWhenRCPT421() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.RCPT_TO) .matching(anyInput()) .thenRespond(serviceNotAvailable("mock response")) .onlySomeTimes(2) - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(FROM, RECIPIENT); @@ -319,14 +311,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldRetryWhenFROM421() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.MAIL_FROM) .matching(anyInput()) .thenRespond(serviceNotAvailable("mock response")) .onlySomeTimes(2) - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(FROM, RECIPIENT); @@ -342,14 +333,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldRetryWhenDATA421() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.DATA) .matching(anyInput()) .thenRespond(serviceNotAvailable("mock response")) .onlySomeTimes(2) - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(FROM, RECIPIENT); @@ -365,14 +355,13 @@ public class RemoteDeliveryErrorTest { @Test public void remoteDeliveryShouldNotDuplicateContentWhenSendPartial() throws Exception { - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.RCPT_TO) .matching(inputContaining(RECIPIENT1)) .thenRespond(serviceNotAvailable("mock response")) .onlySomeTimes(1) - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(MailImpl.builder() @@ -400,14 +389,13 @@ public class RemoteDeliveryErrorTest { .registerMxRecord(mockSmtp.getContainerIp(), mockSmtp.getContainerIp()) .registerMxRecord(mockSmtp2.getContainerIp(), mockSmtp2.getContainerIp()); - mockSMTP1Configuration.setBehaviors( - MockSmtpBehaviors.builder() - .addNewBehavior() + mockSMTP1Configuration + .addNewBehavior() .expect(SMTPCommand.RCPT_TO) .matching(inputContaining(RECIPIENT2)) .thenRespond(serviceNotAvailable("mock response")) .anyTimes() - .build()); + .post(); messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessage(MailImpl.builder() 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 index ddc4723..0bc85d9 100644 --- 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 @@ -22,15 +22,21 @@ 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.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; +import org.apache.james.mock.smtp.server.model.Response; +import org.apache.james.mock.smtp.server.model.SMTPCommand; 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 com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; import feign.Feign; import feign.Logger; @@ -41,6 +47,115 @@ import feign.slf4j.Slf4jLogger; public interface ConfigurationClient { + class BehaviorsParamsBuilder { + + public static class CommandStep { + private final BehaviorsParamsBuilder backReference; + + CommandStep(BehaviorsParamsBuilder backReference) { + this.backReference = backReference; + } + + public ConditionStep expect(SMTPCommand command) { + Preconditions.checkNotNull(command, "'command' should not be null"); + return new ConditionStep(backReference, command); + } + } + + public static class ConditionStep { + public static Condition anyInput() { + return Condition.MATCH_ALL; + } + + public static Condition inputContaining(String value) { + return new Condition.OperatorCondition(Operator.CONTAINS, value); + } + + private final BehaviorsParamsBuilder backReference; + private final SMTPCommand command; + + ConditionStep(BehaviorsParamsBuilder backReference, SMTPCommand command) { + this.backReference = backReference; + this.command = command; + } + + public BehaviorsParamsBuilder.ResponseStep matching(Condition condition) { + return new BehaviorsParamsBuilder.ResponseStep(backReference, command, condition); + } + } + + public static class ResponseStep { + public static Response serviceNotAvailable(String message) { + return new Response(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, message); + } + + public static Response doesNotAcceptAnyMail(String message) { + return new Response(Response.SMTPStatusCode.DOES_NOT_ACCEPT_MAIL_521, message); + } + + private final BehaviorsParamsBuilder backReference; + private final SMTPCommand command; + private final Condition condition; + + ResponseStep(BehaviorsParamsBuilder backReference, SMTPCommand command, Condition condition) { + this.backReference = backReference; + this.command = command; + this.condition = condition; + } + + public BehaviorsParamsBuilder.NumberOfAnswerStep thenRespond(Response response) { + return new BehaviorsParamsBuilder.NumberOfAnswerStep(backReference, command, response, condition); + } + } + + public static class NumberOfAnswerStep { + private final BehaviorsParamsBuilder backReference; + private final SMTPCommand command; + private final Response response; + private final Condition condition; + + NumberOfAnswerStep(BehaviorsParamsBuilder backReference, SMTPCommand command, Response response, Condition condition) { + this.backReference = backReference; + this.command = command; + this.response = response; + this.condition = condition; + } + + public BehaviorsParamsBuilder anyTimes() { + return backReference.add(toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy.anytime())); + } + + public BehaviorsParamsBuilder onlySomeTimes(int count) { + return backReference.add(toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy.times(count))); + } + + MockSMTPBehavior toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy numberOfAnswersPolicy) { + return new MockSMTPBehavior(command, condition, response, numberOfAnswersPolicy); + } + } + + private final ImmutableList.Builder<MockSMTPBehavior> behaviors; + private final ConfigurationClient client; + + public BehaviorsParamsBuilder(ConfigurationClient client) { + this.behaviors = ImmutableList.builder(); + this.client = client; + } + + public BehaviorsParamsBuilder.CommandStep addNewBehavior() { + return new ConfigurationClient.BehaviorsParamsBuilder.CommandStep(this); + } + + BehaviorsParamsBuilder add(MockSMTPBehavior behavior) { + this.behaviors.add(behavior); + return this; + } + + public void post() { + client.setBehaviors(behaviors.build()); + } + } + @VisibleForTesting static ConfigurationClient fromServer(HTTPConfigurationServer server) { return from(Host.from("localhost", server.getPort().getValue())); @@ -83,4 +198,9 @@ public interface ConfigurationClient { clearBehaviors(); clearMails(); } + + default BehaviorsParamsBuilder.CommandStep addNewBehavior() { + return new BehaviorsParamsBuilder(this) + .addNewBehavior(); + } } diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/MockSmtpBehaviors.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/MockSmtpBehaviors.java index d37a13f..27a1633 100644 --- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/MockSmtpBehaviors.java +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/MockSmtpBehaviors.java @@ -25,119 +25,9 @@ import java.util.Objects; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; public class MockSmtpBehaviors { - public static class Builder { - public static class CommandStep { - private final Builder backReference; - - CommandStep(Builder backReference) { - this.backReference = backReference; - } - - public ConditionStep expect(SMTPCommand command) { - Preconditions.checkNotNull(command, "'command' should not be null"); - return new ConditionStep(backReference, command); - } - } - - public static class ConditionStep { - public static Condition anyInput() { - return Condition.MATCH_ALL; - } - - public static Condition inputContaining(String value) { - return new Condition.OperatorCondition(Operator.CONTAINS, value); - } - - private final Builder backReference; - private final SMTPCommand command; - - ConditionStep(Builder backReference, SMTPCommand command) { - this.backReference = backReference; - this.command = command; - } - - public ResponseStep matching(Condition condition) { - return new ResponseStep(backReference, command, condition); - } - } - - public static class ResponseStep { - public static Response serviceNotAvailable(String message) { - return new Response(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, message); - } - - public static Response doesNotAcceptAnyMail(String message) { - return new Response(Response.SMTPStatusCode.DOES_NOT_ACCEPT_MAIL_521, message); - } - - private final Builder backReference; - private final SMTPCommand command; - private final Condition condition; - - ResponseStep(Builder backReference, SMTPCommand command, Condition condition) { - this.backReference = backReference; - this.command = command; - this.condition = condition; - } - - public NumberOfAnswerStep thenRespond(Response response) { - return new NumberOfAnswerStep(backReference, command, response, condition); - } - } - - public static class NumberOfAnswerStep { - private final Builder backReference; - private final SMTPCommand command; - private final Response response; - private final Condition condition; - - NumberOfAnswerStep(Builder backReference, SMTPCommand command, Response response, Condition condition) { - this.backReference = backReference; - this.command = command; - this.response = response; - this.condition = condition; - } - - public Builder anyTimes() { - return backReference.add(toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy.anytime())); - } - - public Builder onlySomeTimes(int count) { - return backReference.add(toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy.times(count))); - } - - MockSMTPBehavior toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy numberOfAnswersPolicy) { - return new MockSMTPBehavior(command, condition, response, numberOfAnswersPolicy); - } - } - - private final ImmutableList.Builder<MockSMTPBehavior> behaviors; - - public Builder() { - this.behaviors = ImmutableList.builder(); - } - - public CommandStep addNewBehavior() { - return new CommandStep(this); - } - - Builder add(MockSMTPBehavior behavior) { - this.behaviors.add(behavior); - return this; - } - - public MockSmtpBehaviors build() { - return new MockSmtpBehaviors(behaviors.build()); - } - } - - public static Builder builder() { - return new Builder(); - } private final List<MockSMTPBehavior> behaviorList; --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org