]> git.basschouten.com Git - openhab-addons.git/blob
03fdf636c06bbae81c2676e276999e61e6034231
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mail.internal;
14
15 import static org.hamcrest.CoreMatchers.instanceOf;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18
19 import java.io.IOException;
20 import java.net.MalformedURLException;
21 import java.nio.file.Path;
22 import java.util.Map;
23
24 import javax.mail.MessagingException;
25 import javax.mail.internet.AddressException;
26
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;
34
35 /**
36  * The {@link MailBuilderTest} class defines tests for the {@link MailBuilder} class
37  *
38  * @author Jan N. Klug - Initial contribution
39  */
40 @NonNullByDefault
41 public class MailBuilderTest {
42
43     private static final String TEST_STRING = "test";
44     private static final String TEST_EMAIL = "foo@bar.zinga";
45
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";
50
51     @Test
52     public void illegalToAddressThrowsException() {
53         assertThrows(AddressException.class, () -> new MailBuilder("foo bar.zinga"));
54     }
55
56     @Test
57     public void illegalFromAddressThrowsException() {
58         assertThrows(EmailException.class, () -> new MailBuilder("TEST_EMAIL").withSender("foo bar.zinga").build());
59     }
60
61     @Test
62     public void illegalURLThrowsException() {
63         assertThrows(MalformedURLException.class,
64                 () -> new MailBuilder("TEST_EMAIL").withURLAttachment("foo bar.zinga"));
65     }
66
67     @Test
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));
72     }
73
74     @Test
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));
81     }
82
83     @Test
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));
88     }
89
90     @Test
91     public void fieldsSetInMail() throws EmailException, MessagingException, IOException {
92         MailBuilder builder = new MailBuilder(TEST_EMAIL);
93
94         assertEquals("(no subject)", builder.build().getSubject());
95         assertEquals(TEST_STRING, builder.withSubject(TEST_STRING).build().getSubject());
96
97         assertEquals(TEST_EMAIL, builder.withSender(TEST_EMAIL).build().getFromAddress().getAddress());
98
99         assertEquals(TEST_EMAIL, builder.build().getToAddresses().get(0).getAddress());
100         assertEquals(2, builder.withRecipients(TEST_EMAIL).build().getToAddresses().size());
101     }
102
103     @Test
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();
107
108         Map<String, String> headers = mail.getHeaders();
109
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));
113     }
114 }