diff --git a/CHANGELOG b/CHANGELOG index 065207b..480e18e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,10 +4,8 @@ CHANGELOG 0.5.1 ----- -* Bump mockito-core from 2.21.0 to 2.22.0 (#36) -* Bump mon from 0.12.0 to 1.1.0 (#38) -* Bump mockito-core from 2.22.0 to 2.23.0 (#40) -* Bump mon from 1.1.0 to 1.2.0 (#41) +* Bump mon from 0.12.0 to 2.0.0 (#38)(#41)(#43) +* Bump mockito-core from 2.21.0 to 2.23.0 (#36)(#40) * Bump kemitix-parent from 5.1.1 to 5.2.0 (#39) 0.5.0 diff --git a/pom.xml b/pom.xml index e063470..f9dd896 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ 0 0 1 - 1.2.0 + 2.0.0 diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index 294b436..58060b4 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -22,6 +22,7 @@ package net.kemitix.wiser.assertions; import net.kemitix.mon.maybe.Maybe; +import net.kemitix.mon.result.Result; import org.subethamail.wiser.Wiser; import org.subethamail.wiser.WiserMessage; @@ -31,7 +32,10 @@ import java.util.List; import java.util.Optional; import java.util.function.Predicate; import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import javax.mail.BodyPart; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; @@ -234,11 +238,7 @@ public final class WiserAssertions { private Maybe contentAsMultiPartMime(final Object content) { if (content instanceof MimeMultipart) { - try { - return Maybe.just(getMimeMultipartAsString((MimeMultipart) content)); - } catch (MessagingException | IOException e) { - throw new RuntimeException(e); - } + return mimeMultipartAsString((MimeMultipart) content); } return Maybe.nothing(); } @@ -264,22 +264,39 @@ public final class WiserAssertions { * @param mimeMultipart the message part to convert * * @return the message part as a string - * - * @throws MessagingException if the part is empty - * @throws IOException if there is another error */ - private String getMimeMultipartAsString(final MimeMultipart mimeMultipart) - throws MessagingException, IOException { - final StringBuilder sb = new StringBuilder(); - for (int i = 0; i < mimeMultipart.getCount(); i++) { - Object content = mimeMultipart.getBodyPart(i).getContent(); - if (content instanceof MimeMultipart) { - sb.append(getMimeMultipartAsString((MimeMultipart) content)); - } else { - sb.append(content); - } + private Maybe mimeMultipartAsString(final MimeMultipart mimeMultipart) { + return Result.toMaybe(Result.of(mimeMultipart::getCount) + .map(count -> bodyPartsAsString(mimeMultipart, count))); + } + + private String bodyPartsAsString(final MimeMultipart mimeMultipart, final int count) { + return IntStream.range(0, count) + .mapToObj(i -> bodyPart(mimeMultipart, i)) + .map(this::bodyPartAsString) + .map(Result::orElseThrowUnchecked) + .collect(Collectors.joining()); + } + + private Result bodyPartAsString(final Result bodyPart) { + return bodyPartContent(bodyPart) + .flatMap(this::contentObjectAsString); + } + + private Result bodyPartContent(final Result bodyPart) { + return bodyPart.flatMap(part -> Result.of(part::getContent)); + } + + private Result bodyPart(final MimeMultipart mimeMultipart, final int i) { + return Result.of(() -> mimeMultipart.getBodyPart(i)); + } + + private Result contentObjectAsString(final Object content) { + if (content instanceof MimeMultipart) { + return Result.of(() -> mimeMultipartAsString((MimeMultipart) content).orElse("")); + } else { + return Result.ok((String) content); } - return sb.toString(); } }