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 1769a9aac47108cd95a8b998112dbf9643cdb6b2 Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Fri Sep 6 09:44:17 2019 +0700 JAMES-2865 Enhance behaviors builder --- .../james/mailets/RemoteDeliveryErrorTest.java | 93 +++++++++++----------- .../mock/smtp/server/model/MockSmtpBehaviors.java | 48 ++++++----- 2 files changed, 78 insertions(+), 63 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 5b29992..720c335 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 @@ -29,6 +29,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.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -46,7 +50,6 @@ import org.apache.james.mailets.configuration.MailetConfiguration; import org.apache.james.mailets.configuration.MailetContainer; import org.apache.james.mailets.configuration.ProcessorConfiguration; import org.apache.james.mock.smtp.server.model.MockSmtpBehaviors; -import org.apache.james.mock.smtp.server.model.Response; import org.apache.james.mock.smtp.server.model.SMTPCommand; import org.apache.james.modules.protocols.ImapGuiceProbe; import org.apache.james.modules.protocols.SmtpGuiceProbe; @@ -93,80 +96,80 @@ public class RemoteDeliveryErrorTest { private static final MockSmtpBehaviors ALWAYS_421_RCPT_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.RCPT_TO) - .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response") - .forAnyInput() - .unlimitedNumberOfAnswer() + .expect(SMTPCommand.RCPT_TO) + .matching(anyInput()) + .thenRespond(serviceNotAvailable("mock response")) + .anyTimes() .build(); private static final MockSmtpBehaviors ALWAYS_421_FROM_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.MAIL_FROM) - .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response") - .forAnyInput() - .unlimitedNumberOfAnswer() + .expect(SMTPCommand.MAIL_FROM) + .matching(anyInput()) + .thenRespond(serviceNotAvailable("mock response")) + .anyTimes() .build(); private static final MockSmtpBehaviors ALWAYS_421_DATA_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.DATA) - .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response") - .forAnyInput() - .unlimitedNumberOfAnswer() + .expect(SMTPCommand.DATA) + .matching(anyInput()) + .thenRespond(serviceNotAvailable("mock response")) + .anyTimes() .build(); private static final MockSmtpBehaviors TWICE_421_RCPT_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.RCPT_TO) - .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response") - .forAnyInput() - .onlySomeAnswers(2) + .expect(SMTPCommand.RCPT_TO) + .matching(anyInput()) + .thenRespond(serviceNotAvailable("mock response")) + .onlySomeTimes(2) .build(); private static final MockSmtpBehaviors TWICE_421_FROM_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.MAIL_FROM) - .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response") - .forAnyInput() - .onlySomeAnswers(2) + .expect(SMTPCommand.MAIL_FROM) + .matching(anyInput()) + .thenRespond(serviceNotAvailable("mock response")) + .onlySomeTimes(2) .build(); private static final MockSmtpBehaviors TWICE_421_DATA_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.DATA) - .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response") - .forAnyInput() - .onlySomeAnswers(2) + .expect(SMTPCommand.DATA) + .matching(anyInput()) + .thenRespond(serviceNotAvailable("mock response")) + .onlySomeTimes(2) .build(); private static final MockSmtpBehaviors SINGLE_500_RCPT_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.RCPT_TO) - .respond(Response.SMTPStatusCode.DOES_NOT_ACCEPT_MAIL_521, "mock response") - .forAnyInput() - .onlySomeAnswers(1) + .expect(SMTPCommand.RCPT_TO) + .matching(anyInput()) + .thenRespond(doesNotAcceptAnyMail("mock message")) + .onlySomeTimes(1) .build(); private static final MockSmtpBehaviors SINGLE_500_FROM_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.MAIL_FROM) - .respond(Response.SMTPStatusCode.DOES_NOT_ACCEPT_MAIL_521, "mock response") - .forAnyInput() - .onlySomeAnswers(1) + .expect(SMTPCommand.MAIL_FROM) + .matching(anyInput()) + .thenRespond(doesNotAcceptAnyMail("mock message")) + .onlySomeTimes(1) .build(); private static final MockSmtpBehaviors SINGLE_500_DATA_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.DATA) - .respond(Response.SMTPStatusCode.DOES_NOT_ACCEPT_MAIL_521, "mock response") - .forAnyInput() - .onlySomeAnswers(1) + .expect(SMTPCommand.DATA) + .matching(anyInput()) + .thenRespond(doesNotAcceptAnyMail("mock message")) + .onlySomeTimes(1) .build(); private static final MockSmtpBehaviors SINGLE_PARTIAL_RCPT_421_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.RCPT_TO) - .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response") - .forInputContaining(RECIPIENT1) - .onlySomeAnswers(1) + .expect(SMTPCommand.RCPT_TO) + .matching(inputContaining(RECIPIENT1)) + .thenRespond(serviceNotAvailable("mock response")) + .onlySomeTimes(1) .build(); private static final MockSmtpBehaviors ALWAYS_PARTIAL_RCPT_421_BEHAVIOR = MockSmtpBehaviors.builder() .addNewBehavior() - .onCommand(SMTPCommand.RCPT_TO) - .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response") - .forInputContaining(RECIPIENT2) - .unlimitedNumberOfAnswer() + .expect(SMTPCommand.RCPT_TO) + .matching(inputContaining(RECIPIENT2)) + .thenRespond(serviceNotAvailable("mock response")) + .anyTimes() .build(); private static final String BOUNCE_MESSAGE = "Hi. This is the James mail server at localhost.\n" + "I'm afraid I wasn't able to deliver your message to the following addresses.\n" + 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 16a7bc7..d37a13f 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 @@ -37,43 +37,55 @@ public class MockSmtpBehaviors { this.backReference = backReference; } - public ResponseStep onCommand(SMTPCommand command) { + public ConditionStep expect(SMTPCommand command) { Preconditions.checkNotNull(command, "'command' should not be null"); - return new ResponseStep(backReference, command); + return new ConditionStep(backReference, command); } } - public static class ResponseStep { + 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; - ResponseStep(Builder backReference, SMTPCommand command) { + ConditionStep(Builder backReference, SMTPCommand command) { this.backReference = backReference; this.command = command; } - public ConditionStep respond(Response.SMTPStatusCode statusCode, String message) { - return new ConditionStep(backReference, command, new Response(statusCode, message)); + public ResponseStep matching(Condition condition) { + return new ResponseStep(backReference, command, condition); } } - public static class ConditionStep { + 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 Response response; + private final Condition condition; - ConditionStep(Builder backReference, SMTPCommand command, Response response) { + ResponseStep(Builder backReference, SMTPCommand command, Condition condition) { this.backReference = backReference; this.command = command; - this.response = response; - } - - public NumberOfAnswerStep forAnyInput() { - return new NumberOfAnswerStep(backReference, command, response, Condition.MATCH_ALL); + this.condition = condition; } - public NumberOfAnswerStep forInputContaining(String value) { - return new NumberOfAnswerStep(backReference, command, response, new Condition.OperatorCondition(Operator.CONTAINS, value)); + public NumberOfAnswerStep thenRespond(Response response) { + return new NumberOfAnswerStep(backReference, command, response, condition); } } @@ -90,11 +102,11 @@ public class MockSmtpBehaviors { this.condition = condition; } - public Builder unlimitedNumberOfAnswer() { + public Builder anyTimes() { return backReference.add(toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy.anytime())); } - public Builder onlySomeAnswers(int count) { + public Builder onlySomeTimes(int count) { return backReference.add(toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy.times(count))); } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org