2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mail.internal;
15 import static org.hamcrest.CoreMatchers.instanceOf;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
19 import java.io.IOException;
20 import java.net.MalformedURLException;
21 import java.nio.file.Path;
24 import javax.mail.MessagingException;
25 import javax.mail.internet.AddressException;
27 import org.apache.commons.mail.Email;
28 import org.apache.commons.mail.EmailException;
29 import org.apache.commons.mail.HtmlEmail;
30 import org.apache.commons.mail.MultiPartEmail;
31 import org.apache.commons.mail.SimpleEmail;
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.junit.jupiter.api.Test;
36 * The {@link MailBuilderTest} class defines tests for the {@link MailBuilder} class
38 * @author Jan N. Klug - Initial contribution
41 public class MailBuilderTest {
43 private static final String TEST_STRING = "test";
44 private static final String TEST_EMAIL = "foo@bar.zinga";
46 private static final String HEADER_1_KEY = "key_one";
47 private static final String HEADER_1_VAL = "value_one";
48 private static final String HEADER_2_KEY = "key_two";
49 private static final String HEADER_2_VAL = "value_two";
52 public void illegalToAddressThrowsException() {
53 assertThrows(AddressException.class, () -> new MailBuilder("foo bar.zinga"));
57 public void illegalFromAddressThrowsException() {
58 assertThrows(EmailException.class, () -> new MailBuilder("TEST_EMAIL").withSender("foo bar.zinga").build());
62 public void illegalURLThrowsException() {
63 assertThrows(MalformedURLException.class,
64 () -> new MailBuilder("TEST_EMAIL").withURLAttachment("foo bar.zinga"));
68 public void withTextOnlyReturnsSimpleEmail() throws AddressException, EmailException {
69 MailBuilder builder = new MailBuilder(TEST_EMAIL);
70 Email mail = builder.withText("boo").build();
71 assertThat(mail, instanceOf(SimpleEmail.class));
75 public void withURLAttachmentReturnsMultiPartEmail()
76 throws AddressException, EmailException, MalformedURLException {
77 MailBuilder builder = new MailBuilder(TEST_EMAIL);
78 String url = Path.of("src/test/resources/attachment.txt").toUri().toURL().toString();
79 Email mail = builder.withText("boo").withURLAttachment(url).build();
80 assertThat(mail, instanceOf(MultiPartEmail.class));
84 public void withHtmlReturnsHtmlEmail() throws AddressException, EmailException {
85 MailBuilder builder = new MailBuilder(TEST_EMAIL);
86 Email mail = builder.withHtml("<html>test</html>").build();
87 assertThat(mail, instanceOf(HtmlEmail.class));
91 public void fieldsSetInMail() throws EmailException, MessagingException, IOException {
92 MailBuilder builder = new MailBuilder(TEST_EMAIL);
94 assertEquals("(no subject)", builder.build().getSubject());
95 assertEquals(TEST_STRING, builder.withSubject(TEST_STRING).build().getSubject());
97 assertEquals(TEST_EMAIL, builder.withSender(TEST_EMAIL).build().getFromAddress().getAddress());
99 assertEquals(TEST_EMAIL, builder.build().getToAddresses().get(0).getAddress());
100 assertEquals(2, builder.withRecipients(TEST_EMAIL).build().getToAddresses().size());
104 public void withHeaders() throws EmailException, MessagingException, IOException {
105 MailBuilder builder = new MailBuilder(TEST_EMAIL);
106 Email mail = builder.withHeader(HEADER_1_KEY, HEADER_1_VAL).withHeader(HEADER_2_KEY, HEADER_2_VAL).build();
108 Map<String, String> headers = mail.getHeaders();
110 assertEquals(2, headers.size());
111 assertEquals(HEADER_2_VAL, headers.get(HEADER_2_KEY));
112 assertEquals(HEADER_1_VAL, headers.get(HEADER_1_KEY));