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 62c296365795c36a1799fd1cd493226a4280877d Author: Tran Tien Duc <[email protected]> AuthorDate: Wed Aug 21 11:03:43 2019 +0700 JAMES-2864 Response POJO for SMTP server --- .../apache/james/mock/smtp/server/Response.java | 71 +++++++++++++++ .../james/mock/smtp/server/ResponseTest.java | 100 +++++++++++++++++++++ 2 files changed, 171 insertions(+) diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/Response.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/Response.java new file mode 100644 index 0000000..c82e699 --- /dev/null +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/Response.java @@ -0,0 +1,71 @@ +/**************************************************************** + * 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 com.google.common.base.Preconditions; + +class Response { + static class SMTPStatusCode { + public static SMTPStatusCode of(int code) { + return new SMTPStatusCode(code); + } + + private final int code; + + private SMTPStatusCode(int code) { + Preconditions.checkArgument(code >= 100 && code < 600, "statusCode needs to be within the 1xx - 5xx range"); + + this.code = code; + } + + public int getCode() { + return code; + } + } + + public static Response serverReject(SMTPStatusCode code, String message) { + return new Response(code, message, true); + } + + public static Response serverAccept(SMTPStatusCode code, String message) { + return new Response(code, message, false); + } + + private final SMTPStatusCode code; + private final String message; + private final boolean serverRejected; + + private Response(SMTPStatusCode code, String message, boolean serverRejected) { + Preconditions.checkNotNull(message); + Preconditions.checkNotNull(code); + + this.code = code; + this.message = message; + this.serverRejected = serverRejected; + } + + String asReplyString() { + return code.getCode() + " " + message; + } + + boolean isServerRejected() { + return serverRejected; + } +} diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ResponseTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ResponseTest.java new file mode 100644 index 0000000..21d07d7 --- /dev/null +++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ResponseTest.java @@ -0,0 +1,100 @@ +/**************************************************************** + * 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 static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +class ResponseTest { + static final int OK_250_CODE = 250; + static final Response.SMTPStatusCode OK_250 = Response.SMTPStatusCode.of(OK_250_CODE); + + @Nested + class SMTPStatusCodeTest { + @Test + void constructorShouldThrowWhenStatusCodeIsNegative() { + assertThatThrownBy(() -> Response.SMTPStatusCode.of(-1)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void constructorShouldThrowWhenStatusCodeIsZero() { + assertThatThrownBy(() -> Response.SMTPStatusCode.of(0)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void constructorShouldThrowWhenStatusCodeIsTooBig() { + assertThatThrownBy(() -> Response.SMTPStatusCode.of(600)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void constructorShouldThrowWhenStatusCodeIsTooLittle() { + assertThatThrownBy(() -> Response.SMTPStatusCode.of(99)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void getCodeShouldReturnInternalValue() { + assertThat(OK_250.getCode()) + .isEqualTo(OK_250_CODE); + } + } + + @Test + void constructorShouldThrowWhenMessageIsNull() { + assertThatThrownBy(() -> Response.serverReject(OK_250, null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + void constructorShouldThrowWhenCodeIsNull() { + assertThatThrownBy(() -> Response.serverReject(null, "message")) + .isInstanceOf(NullPointerException.class); + } + + @Test + void asReplyStringShouldReturnASMTPResponseLine() { + assertThat(Response.serverReject(OK_250, "message").asReplyString()) + .isEqualTo("250 message"); + } + + @Test + void asReplyStringShouldReturnASMTPResponseLineWhenEmptyMessage() { + assertThat(Response.serverReject(OK_250, "").asReplyString()) + .isEqualTo("250 "); + } + + @Test + void isServerRejectedShouldReturnTrueWhenServerReject() { + assertThat(Response.serverReject(OK_250, "message").isServerRejected()) + .isTrue(); + } + + @Test + void isServerRejectedShouldReturnFalseWhenServerAccept() { + assertThat(Response.serverAccept(OK_250, "message").isServerRejected()) + .isFalse(); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
