Skip to content

Commit

Permalink
JAMES-2578 Fix warning using Mailet new API
Browse files Browse the repository at this point in the history
  • Loading branch information
blackheaven authored and Raphael Ouazana committed Feb 15, 2019
1 parent 9c836c5 commit 23e94c4
Show file tree
Hide file tree
Showing 26 changed files with 449 additions and 348 deletions.
Expand Up @@ -152,7 +152,7 @@ void shouldHaveTwoAttributesMap() {

@SuppressWarnings("deprecation")
@Nested
public class DeprecatedtedAttributes {
public class DeprecatedAttributes {
@Test
void setAttributeShouldReturnNullWhenNoPreviousValue() {
Mail mail = newMail();
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.apache.james.transport.mailets.model.ICAL;
import org.apache.james.util.OptionalUtils;
import org.apache.james.util.StreamUtils;
import org.apache.mailet.AttributeName;
import org.apache.mailet.Mail;
import org.apache.mailet.base.GenericMailet;
import org.slf4j.Logger;
Expand Down Expand Up @@ -90,6 +91,12 @@ public class ICALToJsonAttribute extends GenericMailet {
public static final String DEFAULT_SOURCE_ATTRIBUTE_NAME = "icalendar";
public static final String DEFAULT_RAW_SOURCE_ATTRIBUTE_NAME = "attachments";
public static final String DEFAULT_DESTINATION_ATTRIBUTE_NAME = "icalendarJson";
public static final AttributeName SOURCE = AttributeName.of(SOURCE_ATTRIBUTE_NAME);
public static final AttributeName RAW_SOURCE = AttributeName.of(RAW_SOURCE_ATTRIBUTE_NAME);
public static final AttributeName DESTINATION = AttributeName.of(DESTINATION_ATTRIBUTE_NAME);
public static final AttributeName DEFAULT_SOURCE = AttributeName.of(DEFAULT_SOURCE_ATTRIBUTE_NAME);
public static final AttributeName DEFAULT_RAW_SOURCE = AttributeName.of(DEFAULT_RAW_SOURCE_ATTRIBUTE_NAME);
public static final AttributeName DEFAULT_DESTINATION = AttributeName.of(DEFAULT_DESTINATION_ATTRIBUTE_NAME);

static {
ICal4JConfigurator.configure();
Expand Down

Large diffs are not rendered by default.

Expand Up @@ -22,10 +22,15 @@

import java.io.Serializable;
import java.util.Map;
import java.util.Optional;

import javax.mail.MessagingException;

import org.apache.james.util.ClassLoaderUtils;
import org.apache.mailet.Attribute;
import org.apache.mailet.AttributeName;
import org.apache.mailet.AttributeUtils;
import org.apache.mailet.AttributeValue;
import org.apache.mailet.Mail;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailetConfig;
Expand All @@ -43,7 +48,9 @@ public class ICalendarParserTest {
private static final String SOURCE_ATTRIBUTE = "sourceAttribute";

private static final String DESTINATION_CUSTOM_ATTRIBUTE = "ics.dest.attribute";
private static final AttributeName DESTINATION_CUSTOM_ATTRIBUTE_NAME = AttributeName.of(DESTINATION_CUSTOM_ATTRIBUTE);
private static final String SOURCE_CUSTOM_ATTRIBUTE = "ics.source.attribute";
private static final AttributeName SOURCE_CUSTOM_ATTRIBUTE_NAME = AttributeName.of(SOURCE_CUSTOM_ATTRIBUTE);

private static final String RIGHT_ICAL_VALUE = "BEGIN:VCALENDAR\n" +
"END:VCALENDAR";
Expand Down Expand Up @@ -128,7 +135,7 @@ public void serviceShouldNotSetCalendarDataIntoMailAttributeWhenNoSourceAttribut

mailet.service(mail);

assertThat(mail.getAttributeNames()).isEmpty();
assertThat(mail.attributes()).isEmpty();
}

@Test
Expand All @@ -141,13 +148,14 @@ public void serviceShouldSetEmptyCalendarDataIntoMailAttributeWhenEmptyICSAttach
mailet.init(mailetConfiguration);

Mail mail = FakeMail.builder()
.attribute(SOURCE_CUSTOM_ATTRIBUTE, ImmutableMap.of())
.attribute(new Attribute(SOURCE_CUSTOM_ATTRIBUTE_NAME, AttributeValue.of(ImmutableMap.of())))
.build();

mailet.service(mail);

assertThat((Map<?, ?>)mail.getAttribute(DESTINATION_CUSTOM_ATTRIBUTE))
.isEmpty();
assertThat(mail.getAttribute(DESTINATION_CUSTOM_ATTRIBUTE_NAME))
.isPresent()
.hasValueSatisfying(attribute -> assertThat((Map) attribute.getValue().value()).isEmpty());
}

@Test
Expand All @@ -160,12 +168,12 @@ public void serviceShouldNotSetCalendarDataIntoMailAttributeWhenSourceAttributeI
mailet.init(mailetConfiguration);

Mail mail = FakeMail.builder()
.attribute(SOURCE_CUSTOM_ATTRIBUTE, "anyValue")
.attribute(new Attribute(SOURCE_CUSTOM_ATTRIBUTE_NAME, AttributeValue.of("anyValue")))
.build();

mailet.service(mail);

assertThat(mail.getAttribute(DESTINATION_CUSTOM_ATTRIBUTE)).isNull();
assertThat(mail.getAttribute(DESTINATION_CUSTOM_ATTRIBUTE_NAME)).isEmpty();
}

@Test
Expand All @@ -188,8 +196,12 @@ public void serviceShouldReturnRightMapOfCalendarWhenRightAttachments() throws E

mailet.service(mail);

Map<String, Calendar> expectedCalendars = (Map<String, Calendar>)mail.getAttribute(DESTINATION_CUSTOM_ATTRIBUTE);
assertThat(expectedCalendars).hasSize(1);
Optional<Map<String, Calendar>> expectedCalendars = AttributeUtils.getValueAndCastFromMail(mail, DESTINATION_CUSTOM_ATTRIBUTE_NAME, (Class<Map<String, Calendar>>)(Object) Map.class);
assertThat(expectedCalendars)
.isPresent()
.hasValueSatisfying(calendars ->
assertThat(calendars)
.hasSize(1));
}

@Test
Expand Down
Expand Up @@ -23,10 +23,13 @@

import javax.mail.MessagingException;

import org.apache.james.util.streams.Iterators;
import org.apache.mailet.AttributeName;
import org.apache.mailet.Mail;
import org.apache.mailet.MailetException;
import org.apache.mailet.base.GenericMailet;

import com.github.steveash.guavate.Guavate;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
Expand All @@ -51,7 +54,7 @@ public class RemoveMailAttribute extends GenericMailet {
private static final char ATTRIBUTE_SEPARATOR_CHAR = ',';
protected static final String MAILET_NAME_PARAMETER = "name";

private ImmutableList<String> attributesToRemove;
private ImmutableList<AttributeName> attributesToRemove;

@Override
public String getMailetInfo() {
Expand All @@ -69,17 +72,18 @@ public void init() throws MailetException {
attributesToRemove = getAttributes(name);
}

private ImmutableList<String> getAttributes(String name) {
return ImmutableList.copyOf(Splitter.on(ATTRIBUTE_SEPARATOR_CHAR)
private ImmutableList<AttributeName> getAttributes(String name) {
return Iterators.toStream(Splitter.on(ATTRIBUTE_SEPARATOR_CHAR)
.trimResults()
.split(name));
.split(name)
.iterator())
.map(AttributeName::of)
.collect(Guavate.toImmutableList());
}

@Override
public void service(Mail mail) throws MessagingException {
Preconditions.checkNotNull(mail);
for (String attributeToRemove : attributesToRemove) {
mail.removeAttribute(attributeToRemove);
}
attributesToRemove.forEach(mail::removeAttribute);
}
}
Expand Up @@ -24,6 +24,7 @@

import javax.mail.MessagingException;

import org.apache.mailet.Attribute;
import org.apache.mailet.Mail;
import org.apache.mailet.Mailet;
import org.apache.mailet.base.test.FakeMailetConfig;
Expand Down Expand Up @@ -53,11 +54,11 @@ public void getMailetInfoShouldReturnValue() {
@Test
public void serviceShouldRemoveAllMailAttributes() throws MessagingException {
mail = MailUtil.createMockMail2Recipients();
mail.setAttribute("org.apache.james.test.junit", "true");
mail.setAttribute(Attribute.convertToAttribute("org.apache.james.test.junit", "true"));

mailet.service(mail);

assertThat(mail.getAttributeNames()).isEmpty();
assertThat(mail.attributes()).isEmpty();
}

@Test
Expand All @@ -66,6 +67,6 @@ public void serviceShouldRemoveAllMailAttributesWhenNone() throws MessagingExcep

mailet.service(mail);

assertThat(mail.getAttributeNames()).isEmpty();
assertThat(mail.attributes()).isEmpty();
}
}
Expand Up @@ -25,6 +25,9 @@
import javax.mail.MessagingException;

import org.apache.james.util.MimeMessageUtil;
import org.apache.mailet.Attribute;
import org.apache.mailet.AttributeName;
import org.apache.mailet.AttributeValue;
import org.apache.mailet.Mail;
import org.apache.mailet.Mailet;
import org.apache.mailet.base.test.FakeMailetConfig;
Expand All @@ -43,10 +46,16 @@ void setupMailet() {

@Test
void shouldAddConfiguredAttributes() throws MessagingException {
AttributeName name1 = AttributeName.of("org.apache.james.junit1");
AttributeName name2 = AttributeName.of("org.apache.james.junit2");
AttributeValue<String> value1 = AttributeValue.of("true");
AttributeValue<String> value2 = AttributeValue.of("happy");
Attribute attribute1 = new Attribute(name1, value1);
Attribute attribute2 = new Attribute(name2, value2);
FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
.mailetName("Test")
.setProperty("org.apache.james.junit1", "true")
.setProperty("org.apache.james.junit2", "happy")
.setProperty(name1.asString(), value1.value())
.setProperty(name2.asString(), value2.value())
.build();

mailet.init(mailetConfig);
Expand All @@ -55,8 +64,8 @@ void shouldAddConfiguredAttributes() throws MessagingException {

mailet.service(mail);

assertThat(mail.getAttribute("org.apache.james.junit1")).isEqualTo("true");
assertThat(mail.getAttribute("org.apache.james.junit2")).isEqualTo("happy");
assertThat(mail.getAttribute(name1)).contains(attribute1);
assertThat(mail.getAttribute(name2)).contains(attribute2);
}

@Test
Expand All @@ -71,7 +80,7 @@ void shouldAddNothingWhenNoConfiguredAttribute() throws MessagingException {

mailet.service(mail);

assertThat(mail.getAttributeNames()).isEmpty();
assertThat(mail.attributes()).isEmpty();
}

@Test
Expand Down
Expand Up @@ -29,6 +29,7 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
Expand All @@ -37,6 +38,8 @@
import org.apache.james.core.Domain;
import org.apache.james.core.MailAddress;
import org.apache.james.core.builder.MimeMessageWrapper;
import org.apache.mailet.Attribute;
import org.apache.mailet.AttributeName;
import org.apache.mailet.HostAddress;
import org.apache.mailet.LookupException;
import org.apache.mailet.Mail;
Expand All @@ -45,11 +48,11 @@

import com.github.fge.lambdas.Throwing;
import com.github.fge.lambdas.functions.ThrowingFunction;
import com.github.steveash.guavate.Guavate;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

@SuppressWarnings("deprecation")
public class FakeMailContext implements MailetContext {
Expand All @@ -68,19 +71,10 @@ public static SentMail.Builder fromMail(Mail mail) throws MessagingException {
.recipients(mail.getRecipients())
.message(mail.getMessage())
.state(mail.getState())
.attributes(buildAttributesMap(mail))
.attributes(mail.attributes().collect(Guavate.toImmutableList()))
.fromMailet();
}

private static ImmutableMap<String, Serializable> buildAttributesMap(Mail mail) {
Map<String, Serializable> result = new HashMap<>();
List<String> attributesNames = Lists.newArrayList(mail.getAttributeNames());
for (String attributeName: attributesNames) {
result.put(attributeName, mail.getAttribute(attributeName));
}
return ImmutableMap.copyOf(result);
}

public static FakeMailContext defaultContext() {
return builder().build();
}
Expand Down Expand Up @@ -123,7 +117,7 @@ public static class Builder {
private MailAddress sender;
private Optional<Collection<MailAddress>> recipients = Optional.empty();
private MimeMessage msg;
private Map<String, Serializable> attributes = new HashMap<>();
private Map<AttributeName, Attribute> attributes = new HashMap<>();
private Optional<String> state = Optional.empty();
private Optional<Boolean> fromMailet = Optional.empty();
private Optional<Delay> delay = Optional.empty();
Expand Down Expand Up @@ -172,13 +166,23 @@ public Builder message(MimeMessage mimeMessage) {
return this;
}

public Builder attributes(Map<String, Serializable> attributes) {
this.attributes.putAll(attributes);
public Builder attributes(List<Attribute> attributes) {
this.attributes.putAll(attributes
.stream()
.collect(Guavate.toImmutableMap(
attribute -> attribute.getName(),
Function.identity()
)));
return this;
}

@Deprecated
public Builder attribute(String key, Serializable value) {
this.attributes.put(key, value);
return attribute(Attribute.convertToAttribute(key, value));
}

public Builder attribute(Attribute attribute) {
this.attributes.put(attribute.getName(), attribute);
return this;
}

Expand All @@ -205,11 +209,11 @@ public SentMail build() throws MessagingException {
private final Collection<MailAddress> recipients;
private final MimeMessage msg;
private final Optional<String> subject;
private final Map<String, Serializable> attributes;
private final Map<AttributeName, Attribute> attributes;
private final String state;
private final Optional<Delay> delay;

private SentMail(MailAddress sender, Collection<MailAddress> recipients, MimeMessage msg, Map<String, Serializable> attributes, String state, Optional<Delay> delay) throws MessagingException {
private SentMail(MailAddress sender, Collection<MailAddress> recipients, MimeMessage msg, Map<AttributeName, Attribute> attributes, String state, Optional<Delay> delay) throws MessagingException {
this.sender = sender;
this.recipients = ImmutableList.copyOf(recipients);
this.msg = tryCopyMimeMessage(msg);
Expand Down Expand Up @@ -369,7 +373,7 @@ public String toString() {
}
}

private final HashMap<String, Object> attributes;
private final HashMap<AttributeName, Attribute> attributes;
private final Collection<SentMail> sentMails;
private final Collection<BouncedMail> bouncedMails;
private final Optional<Logger> logger;
Expand Down Expand Up @@ -413,7 +417,11 @@ public Object getAttribute(String name) {

@Override
public Iterator<String> getAttributeNames() {
return attributes.keySet().iterator();
return attributes
.keySet()
.stream()
.map(AttributeName::asString)
.iterator();
}

@Override
Expand Down Expand Up @@ -525,8 +533,13 @@ public void sendMail(Mail mail, String state, long delay, TimeUnit unit) throws
.build());
}

@Deprecated
public void setAttribute(String name, Serializable object) {
attributes.put(name,object);
setAttribute(Attribute.convertToAttribute(name, object));
}

public void setAttribute(Attribute attribute) {
attributes.put(attribute.getName(), attribute);
}

public void storeMail(MailAddress sender, MailAddress recipient, MimeMessage msg) throws MessagingException {
Expand Down
Expand Up @@ -222,12 +222,6 @@ public Builder attribute(String name, Serializable object) {
return this;
}

@Deprecated
public Builder attributes(Map<String, Serializable> attributes) {
this.attributes(toAttributeMap(attributes).values());
return this;
}

public Builder attribute(Attribute attribute) {
this.attributes.put(attribute.getName(), attribute);
return this;
Expand Down

0 comments on commit 23e94c4

Please sign in to comment.