JAMES-1804 Introduce type for Set<EMailer>
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4295a7f6 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4295a7f6 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4295a7f6 Branch: refs/heads/master Commit: 4295a7f69b799473a182de081a076a105c794a4a Parents: 1616042 Author: Antoine Duprat <[email protected]> Authored: Tue Jul 19 16:44:11 2016 +0200 Committer: Antoine Duprat <[email protected]> Committed: Tue Jul 26 08:57:04 2016 +0200 ---------------------------------------------------------------------- .../mailbox/elasticsearch/json/EMailer.java | 8 ++- .../mailbox/elasticsearch/json/EMailers.java | 52 +++++++++++++++ .../elasticsearch/json/IndexableMessage.java | 30 ++++----- .../elasticsearch/json/Serializable.java | 25 ++++++++ .../elasticsearch/json/EMailersTest.java | 66 ++++++++++++++++++++ 5 files changed, 165 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/4295a7f6/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailer.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailer.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailer.java index 0ed87ac..6fff269 100644 --- a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailer.java +++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailer.java @@ -22,9 +22,10 @@ package org.apache.james.mailbox.elasticsearch.json; import java.util.Objects; import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; -public class EMailer { +public class EMailer implements Serializable { private final String name; private final String address; @@ -45,6 +46,11 @@ public class EMailer { } @Override + public String serialize() { + return Joiner.on(" ").join(name, address); + } + + @Override public boolean equals(Object o) { if (o instanceof EMailer) { EMailer otherEMailer = (EMailer) o; http://git-wip-us.apache.org/repos/asf/james-project/blob/4295a7f6/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailers.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailers.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailers.java new file mode 100644 index 0000000..7679310 --- /dev/null +++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailers.java @@ -0,0 +1,52 @@ +/**************************************************************** + * 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.mailbox.elasticsearch.json; + +import java.util.Set; +import java.util.stream.Collectors; + +import com.fasterxml.jackson.annotation.JsonValue; +import com.google.common.base.Preconditions; + +public class EMailers implements Serializable { + + public static EMailers from(Set<EMailer> emailers) { + Preconditions.checkNotNull(emailers, "'emailers' is mandatory"); + return new EMailers(emailers); + } + + private final Set<EMailer> emailers; + + private EMailers(Set<EMailer> emailers) { + this.emailers = emailers; + } + + @JsonValue + public Set<EMailer> getEmailers() { + return emailers; + } + + @Override + public String serialize() { + return emailers.stream() + .map(EMailer::serialize) + .collect(Collectors.joining(" ")); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/4295a7f6/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessage.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessage.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessage.java index 5b1cec1..d36b1ec 100644 --- a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessage.java +++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessage.java @@ -68,11 +68,11 @@ public class IndexableMessage { private void copyHeaderFields(HeaderCollection headerCollection, ZonedDateTime internalDate) { this.headers = headerCollection.getHeaders(); this.subjects = headerCollection.getSubjectSet(); - this.from = headerCollection.getFromAddressSet(); - this.to = headerCollection.getToAddressSet(); - this.replyTo = headerCollection.getReplyToAddressSet(); - this.cc = headerCollection.getCcAddressSet(); - this.bcc = headerCollection.getBccAddressSet(); + this.from = EMailers.from(headerCollection.getFromAddressSet()); + this.to = EMailers.from(headerCollection.getToAddressSet()); + this.replyTo = EMailers.from(headerCollection.getReplyToAddressSet()); + this.cc = EMailers.from(headerCollection.getCcAddressSet()); + this.bcc = EMailers.from(headerCollection.getBccAddressSet()); this.sentDate = DateResolutionFormater.DATE_TIME_FOMATTER.format(headerCollection.getSentDate().orElse(internalDate)); } @@ -119,11 +119,11 @@ public class IndexableMessage { private boolean isAnswered; private String[] userFlags; private Multimap<String, String> headers; - private Set<EMailer> from; - private Set<EMailer> to; - private Set<EMailer> cc; - private Set<EMailer> bcc; - private Set<EMailer> replyTo; + private EMailers from; + private EMailers to; + private EMailers cc; + private EMailers bcc; + private EMailers replyTo; private Set<String> subjects; private String sentDate; private List<Property> properties; @@ -216,27 +216,27 @@ public class IndexableMessage { } @JsonProperty(JsonMessageConstants.FROM) - public Set<EMailer> getFrom() { + public EMailers getFrom() { return from; } @JsonProperty(JsonMessageConstants.TO) - public Set<EMailer> getTo() { + public EMailers getTo() { return to; } @JsonProperty(JsonMessageConstants.CC) - public Set<EMailer> getCc() { + public EMailers getCc() { return cc; } @JsonProperty(JsonMessageConstants.BCC) - public Set<EMailer> getBcc() { + public EMailers getBcc() { return bcc; } @JsonProperty(JsonMessageConstants.REPLY_TO) - public Set<EMailer> getReplyTo() { + public EMailers getReplyTo() { return replyTo; } http://git-wip-us.apache.org/repos/asf/james-project/blob/4295a7f6/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/Serializable.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/Serializable.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/Serializable.java new file mode 100644 index 0000000..92915df --- /dev/null +++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/Serializable.java @@ -0,0 +1,25 @@ +/**************************************************************** + * 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.mailbox.elasticsearch.json; + +public interface Serializable { + + String serialize(); +} http://git-wip-us.apache.org/repos/asf/james-project/blob/4295a7f6/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/EMailersTest.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/EMailersTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/EMailersTest.java new file mode 100644 index 0000000..2ff133b --- /dev/null +++ b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/EMailersTest.java @@ -0,0 +1,66 @@ +/**************************************************************** + * 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.mailbox.elasticsearch.json; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Test; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableSet; + +public class EMailersTest { + + @Test + public void fromShouldThrowWhenSetIsNull() { + assertThatThrownBy(() -> EMailers.from(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("'emailers' is mandatory"); + } + + @Test + public void serializeShouldReturnEmptyWhenEmptySet() { + EMailers eMailers = EMailers.from(ImmutableSet.of()); + + assertThat(eMailers.serialize()).isEmpty(); + } + + @Test + public void serializeShouldNotJoinWhenOneElement() { + EMailer emailer = new EMailer("name", "address"); + EMailers eMailers = EMailers.from(ImmutableSet.of(emailer)); + + assertThat(eMailers.serialize()).isEqualTo(emailer.serialize()); + } + + @Test + public void serializeShouldJoinWhenMultipleElements() { + EMailer emailer = new EMailer("name", "address"); + EMailer emailer2 = new EMailer("name2", "address2"); + EMailer emailer3 = new EMailer("name3", "address3"); + + String expected = Joiner.on(" ").join(emailer.serialize(), emailer2.serialize(), emailer3.serialize()); + + EMailers eMailers = EMailers.from(ImmutableSet.of(emailer, emailer2, emailer3)); + + assertThat(eMailers.serialize()).isEqualTo(expected); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
