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 0f02246908797e6fa70563fdbca361f163196c1a Author: Benoit Tellier <[email protected]> AuthorDate: Wed Aug 21 15:55:04 2019 +0700 JAMES-2866 MockSMTPBehaviors POJO Directly correnspond to HTTP configuration output --- server/mailet/mock-smtp-server/pom.xml | 4 ++ .../james/mock/smtp/server/MockSmtpBehaviors.java | 56 +++++++++++++++++ .../mock/smtp/server/MockSMTPBehaviorTest.java | 8 +-- .../mock/smtp/server/MockSmtpBehaviorsTest.java | 73 ++++++++++++++++++++++ 4 files changed, 137 insertions(+), 4 deletions(-) diff --git a/server/mailet/mock-smtp-server/pom.xml b/server/mailet/mock-smtp-server/pom.xml index ce806b9..f7ea8c8 100644 --- a/server/mailet/mock-smtp-server/pom.xml +++ b/server/mailet/mock-smtp-server/pom.xml @@ -47,6 +47,10 @@ </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> + <artifactId>jackson-datatype-guava</artifactId> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jdk8</artifactId> </dependency> <dependency> diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSmtpBehaviors.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSmtpBehaviors.java new file mode 100644 index 0000000..7ebe28a --- /dev/null +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSmtpBehaviors.java @@ -0,0 +1,56 @@ +/**************************************************************** + * 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 java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; +import com.google.common.collect.ImmutableList; + +public class MockSmtpBehaviors { + private final List<MockSMTPBehavior> behaviorList; + + @JsonCreator + public MockSmtpBehaviors(List<MockSMTPBehavior> behaviorList) { + this.behaviorList = ImmutableList.copyOf(behaviorList); + } + + @JsonValue + public List<MockSMTPBehavior> getBehaviorList() { + return behaviorList; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof MockSmtpBehaviors) { + MockSmtpBehaviors that = (MockSmtpBehaviors) o; + + return Objects.equals(this.behaviorList, that.behaviorList); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(behaviorList); + } +} diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPBehaviorTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPBehaviorTest.java index 606c4bc..91f760f 100644 --- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPBehaviorTest.java +++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPBehaviorTest.java @@ -40,25 +40,25 @@ class MockSMTPBehaviorTest { private static final Response RESPONSE = Response.serverAccept(SMTPStatusCode.ACTION_COMPLETE_250, "message"); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().registerModule(new Jdk8Module()); - private static final String JSON_COMPULSORY_FIELDS = "{" + + static final String JSON_COMPULSORY_FIELDS = "{" + " \"response\": {\"code\":250, \"message\":\"OK\", \"rejected\":false}," + " \"command\": \"EHLO\"" + "}"; - private static final MockSMTPBehavior POJO_COMPULSORY_FIELDS = new MockSMTPBehavior( + static final MockSMTPBehavior POJO_COMPULSORY_FIELDS = new MockSMTPBehavior( SMTPCommand.EHLO, Optional.empty(), Response.serverAccept(Response.SMTPStatusCode.of(250), "OK"), MockSMTPBehavior.NumberOfAnswersPolicy.anytime()); - private static final String JSON_ALL_FIELDS = "{" + + static final String JSON_ALL_FIELDS = "{" + " \"response\": {\"code\":250, \"message\":\"OK\", \"rejected\":false}," + " \"condition\": {\"operator\":\"contains\", \"matchingValue\":\"matchme\"}," + " \"command\": \"EHLO\"," + " \"numberOfAnswer\": 7" + "}"; - private static final MockSMTPBehavior POJO_ALL_FIELDS = new MockSMTPBehavior( + static final MockSMTPBehavior POJO_ALL_FIELDS = new MockSMTPBehavior( SMTPCommand.EHLO, Optional.of(new Condition.OperatorCondition(Operator.CONTAINS, "matchme")), Response.serverAccept(Response.SMTPStatusCode.of(250), "OK"), diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSmtpBehaviorsTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSmtpBehaviorsTest.java new file mode 100644 index 0000000..83ffaaf --- /dev/null +++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSmtpBehaviorsTest.java @@ -0,0 +1,73 @@ +/**************************************************************** + * 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 net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.guava.GuavaModule; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.google.common.collect.ImmutableList; + +import net.javacrumbs.jsonunit.core.Option; +import net.javacrumbs.jsonunit.core.internal.Options; +import nl.jqno.equalsverifier.EqualsVerifier; + +class MockSmtpBehaviorsTest { + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper() + .registerModule(new Jdk8Module()) + .registerModule(new GuavaModule()); + + private static final String JSON = "[" + MockSMTPBehaviorTest.JSON_ALL_FIELDS + ", " + + MockSMTPBehaviorTest.JSON_COMPULSORY_FIELDS + "]"; + + private static final MockSmtpBehaviors POJO = new MockSmtpBehaviors(ImmutableList.of( + MockSMTPBehaviorTest.POJO_ALL_FIELDS, + MockSMTPBehaviorTest.POJO_COMPULSORY_FIELDS)); + + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(MockSmtpBehaviors.class) + .verify(); + } + + @Test + void jacksonShouldDeserializeBehaviors() throws Exception { + MockSmtpBehaviors behaviors = OBJECT_MAPPER.readValue("[" + MockSMTPBehaviorTest.JSON_ALL_FIELDS + ", " + + MockSMTPBehaviorTest.JSON_COMPULSORY_FIELDS + "]", MockSmtpBehaviors.class); + + assertThat(behaviors) + .isEqualTo(new MockSmtpBehaviors(ImmutableList.of( + MockSMTPBehaviorTest.POJO_ALL_FIELDS, + MockSMTPBehaviorTest.POJO_COMPULSORY_FIELDS))); + } + + @Test + void jacksonShouldSerializeBehaviors() throws Exception { + String json = OBJECT_MAPPER.writeValueAsString(POJO); + + assertThatJson(json) + .withOptions(new Options(Option.TREATING_NULL_AS_ABSENT, Option.IGNORING_ARRAY_ORDER)) + .isEqualTo(JSON); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
