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 7a3d0d550aa8cf86830e6ce8d321ab99d6d82727 Author: Rene Cordier <[email protected]> AuthorDate: Fri Aug 30 09:50:13 2019 +0700 JAMES-2868 Add Mails serialization --- .../apache/james/mock/smtp/server/model/Mail.java | 17 ++++++- .../apache/james/mock/smtp/server/model/Mails.java | 55 ++++++++++++++++++++++ .../org/apache/james/mock/smtp/server/Fixture.java | 9 +++- .../MailsTest.java} | 54 +++++++++++++++------ .../server/{ => model}/MockSmtpBehaviorsTest.java | 3 +- 5 files changed, 119 insertions(+), 19 deletions(-) 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 a212386..12fdd5c 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 @@ -24,13 +24,20 @@ import java.util.Objects; import org.apache.james.core.MailAddress; +import com.fasterxml.jackson.annotation.JsonUnwrapped; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +@JsonDeserialize(builder = Mail.Builder.class) public class Mail { + + @JsonDeserialize(builder = Mail.Envelope.Builder.class) public static class Envelope { + @JsonPOJOBuilder(withPrefix = "") public static class Builder { private MailAddress from; private ImmutableList.Builder<MailAddress> recipients; @@ -45,7 +52,12 @@ public class Mail { } public Builder addRecipient(MailAddress recipient) { - recipients.add(recipient); + this.recipients.add(recipient); + return this; + } + + public Builder recipients(List<MailAddress> recipients) { + this.recipients.addAll(recipients); return this; } @@ -99,7 +111,9 @@ public class Mail { } } + @JsonPOJOBuilder(withPrefix = "") public static class Builder { + @JsonUnwrapped private Envelope envelope; private String message; @@ -118,6 +132,7 @@ public class Mail { } } + @JsonUnwrapped private final Envelope envelope; private final String message; diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mails.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mails.java new file mode 100644 index 0000000..cc29c12 --- /dev/null +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/Mails.java @@ -0,0 +1,55 @@ +/**************************************************************** + * 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.model; + +import java.util.List; +import java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +public class Mails { + private final List<Mail> mailList; + + @JsonCreator + public Mails(List<Mail> mailList) { + this.mailList = mailList; + } + + @JsonValue + public List<Mail> getMailList() { + return mailList; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof Mails) { + Mails that = (Mails) o; + + return Objects.equals(this.mailList, that.mailList); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(mailList); + } +} 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 81e4d15..6f3cc4e 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,6 +19,7 @@ package org.apache.james.mock.smtp.server; +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.MockSMTPBehavior; import org.apache.james.mock.smtp.server.model.MockSmtpBehaviors; @@ -39,7 +40,8 @@ public interface Fixture { ObjectMapper OBJECT_MAPPER = new ObjectMapper() .registerModule(new Jdk8Module()) - .registerModule(new GuavaModule()); + .registerModule(new GuavaModule()) + .registerModule(new MailAddressModule().asJacksonModule()); Response RESPONSE = Response.serverAccept(Response.SMTPStatusCode.of(250), "message"); @@ -90,4 +92,9 @@ public interface Fixture { MockSmtpBehaviors BEHAVIORS = new MockSmtpBehaviors(ImmutableList.of( BEHAVIOR_ALL_FIELDS, BEHAVIOR_COMPULSORY_FIELDS)); + + String JSON_MAILS_LIST = "[" + + " {\"from\":\"[email protected]\",\"recipients\":[\"[email protected]\", \"[email protected]\"],\"message\":\"bob to alice and jack\"}," + + " {\"from\":\"[email protected]\",\"recipients\":[\"[email protected]\"],\"message\":\"alice to bob\"}" + + "]"; } 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/model/MailsTest.java similarity index 54% copy from server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSmtpBehaviorsTest.java copy to server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/model/MailsTest.java index 166c2e8..bb1e9be 100644 --- 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/model/MailsTest.java @@ -17,42 +17,66 @@ * under the License. * ****************************************************************/ -package org.apache.james.mock.smtp.server; +package org.apache.james.mock.smtp.server.model; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; -import static org.apache.james.mock.smtp.server.Fixture.BEHAVIORS; -import static org.apache.james.mock.smtp.server.Fixture.JSON_BEHAVIORS; +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_MAILS_LIST; import static org.apache.james.mock.smtp.server.Fixture.OBJECT_MAPPER; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Java6Assertions.assertThat; -import org.apache.james.mock.smtp.server.model.MockSmtpBehaviors; +import org.apache.james.core.MailAddress; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +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 { +class MailsTest { + private Mails mails; + + @BeforeEach + void setup() throws Exception { + Mail mail1 = new Mail( + new Mail.Envelope( + new MailAddress(BOB), + ImmutableList.of(new MailAddress(ALICE), new MailAddress(JACK))), + "bob to alice and jack"); + + Mail mail2 = new Mail( + new Mail.Envelope( + new MailAddress(ALICE), + ImmutableList.of(new MailAddress(BOB))), + "alice to bob"); + + mails = new Mails(ImmutableList.of(mail1, mail2)); + } + @Test void shouldMatchBeanContract() { - EqualsVerifier.forClass(MockSmtpBehaviors.class) + EqualsVerifier.forClass(Mails.class) .verify(); } @Test - void jacksonShouldDeserializeBehaviors() throws Exception { - MockSmtpBehaviors behaviors = OBJECT_MAPPER.readValue(JSON_BEHAVIORS, MockSmtpBehaviors.class); + void jacksonShouldDeserializeMails() throws Exception { + Mails actualMails = OBJECT_MAPPER.readValue(JSON_MAILS_LIST, Mails.class); - assertThat(behaviors) - .isEqualTo(BEHAVIORS); + assertThat(actualMails) + .isEqualTo(mails); } @Test - void jacksonShouldSerializeBehaviors() throws Exception { - String json = OBJECT_MAPPER.writeValueAsString(BEHAVIORS); + void jacksonShouldSerializeMails() throws Exception { + String json = OBJECT_MAPPER.writeValueAsString(mails); assertThatJson(json) .withOptions(new Options(Option.TREATING_NULL_AS_ABSENT, Option.IGNORING_ARRAY_ORDER)) - .isEqualTo(JSON_BEHAVIORS); + .isEqualTo(JSON_MAILS_LIST); } -} \ No newline at end of file +} 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/model/MockSmtpBehaviorsTest.java similarity index 95% rename from server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSmtpBehaviorsTest.java rename to server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/model/MockSmtpBehaviorsTest.java index 166c2e8..f09e03c 100644 --- 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/model/MockSmtpBehaviorsTest.java @@ -17,7 +17,7 @@ * under the License. * ****************************************************************/ -package org.apache.james.mock.smtp.server; +package org.apache.james.mock.smtp.server.model; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; import static org.apache.james.mock.smtp.server.Fixture.BEHAVIORS; @@ -25,7 +25,6 @@ import static org.apache.james.mock.smtp.server.Fixture.JSON_BEHAVIORS; import static org.apache.james.mock.smtp.server.Fixture.OBJECT_MAPPER; import static org.assertj.core.api.Assertions.assertThat; -import org.apache.james.mock.smtp.server.model.MockSmtpBehaviors; import org.junit.jupiter.api.Test; import net.javacrumbs.jsonunit.core.Option; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
