1 /*
  2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.skara.email;
 24 
 25 import org.junit.jupiter.api.Test;
 26 
 27 import java.time.*;
 28 import java.util.*;
 29 
 30 import static org.junit.jupiter.api.Assertions.assertEquals;
 31 
 32 class EmailTests {
 33     @Test
 34     void parseSimple() {
 35         var mail = Email.parse("Message-Id: <a@b.c>\n" +
 36                 "Date: Wed, 27 Mar 2019 14:31:00 +0100\n" +
 37                 "Subject: hello\n" +
 38                 "From: B <b@b.c>\n" +
 39                 "To: C <c@c.c>, <d@d.c>\n" +
 40                 "\n" +
 41                 "The body"
 42         );
 43 
 44         assertEquals(EmailAddress.from("a@b.c"), mail.id());
 45         assertEquals("hello", mail.subject());
 46         assertEquals(EmailAddress.from("B", "b@b.c"), mail.author());
 47         assertEquals(EmailAddress.from("B", "b@b.c"), mail.sender());
 48         assertEquals(List.of(EmailAddress.from("C", "c@c.c"),
 49                              EmailAddress.from("d@d.c")),
 50                      mail.recipients());
 51         assertEquals("The body", mail.body());
 52     }
 53 
 54     @Test
 55     void buildFrom() {
 56         var original = Email.create(EmailAddress.from("A", "a@b.c"), "Subject", "body")
 57                             .header("X", "y")
 58                             .header("Z", "a")
 59                             .recipient(EmailAddress.from("B", "b@b.c"))
 60                             .build();
 61         var copy = Email.from(original).build();
 62         assertEquals("Subject", copy.subject());
 63         assertEquals("body", copy.body());
 64         assertEquals(Set.of("X", "Z"), copy.headers());
 65         assertEquals("y", copy.headerValue("X"));
 66         assertEquals("a", copy.headerValue("z"));
 67         assertEquals(original, copy);
 68     }
 69 
 70     @Test
 71     void caseInsensitiveHeaders() {
 72         var mail = Email.parse("Message-ID: <a@b.c>\n" +
 73                                        "date: Wed, 27 Mar 2019 14:31:00 +0100\n" +
 74                                        "SUBJECT: hello\n" +
 75                                        "FRom: B <b@b.c>\n" +
 76                                        "tO: C <c@c.c>, <d@d.c>\n" +
 77                                        "Extra-header: hello\n" +
 78                                        "\n" +
 79                                        "The body"
 80         );
 81 
 82         assertEquals(EmailAddress.from("a@b.c"), mail.id());
 83         assertEquals("hello", mail.subject());
 84         assertEquals(EmailAddress.from("B", "b@b.c"), mail.author());
 85         assertEquals(EmailAddress.from("B", "b@b.c"), mail.sender());
 86         assertEquals(List.of(EmailAddress.from("C", "c@c.c"),
 87                              EmailAddress.from("d@d.c")),
 88                      mail.recipients());
 89         assertEquals("The body", mail.body());
 90         assertEquals(Set.of("Extra-header"), mail.headers());
 91         assertEquals("hello", mail.headerValue("ExTra-HeaDer"));
 92     }
 93 
 94     @Test
 95     void redundantTimeZone() {
 96         var mail = Email.parse("Message-Id: <a@b.c>\n" +
 97                                        "Date: Wed, 27 Mar 2019 14:31:00 +0700 (PDT)\n" +
 98                                        "Subject: hello\n" +
 99                                        "From: B <b@b.c>\n" +
100                                        "To: C <c@c.c>, <d@d.c>\n" +
101                                        "\n" +
102                                        "The body"
103         );
104         assertEquals(ZonedDateTime.of(2019, 3, 27, 14, 31, 0, 0, ZoneOffset.ofHours(7)), mail.date());
105         assertEquals(EmailAddress.from("a@b.c"), mail.id());
106         assertEquals("hello", mail.subject());
107         assertEquals(EmailAddress.from("B", "b@b.c"), mail.author());
108         assertEquals(EmailAddress.from("B", "b@b.c"), mail.sender());
109         assertEquals(List.of(EmailAddress.from("C", "c@c.c"),
110                              EmailAddress.from("d@d.c")),
111                      mail.recipients());
112         assertEquals("The body", mail.body());
113     }
114 
115     @Test
116     void parseEncoded() {
117         var mail = Email.parse("Message-Id: <a@b.c>\n" +
118                                        "Date: Wed, 27 Mar 2019 14:31:00 +0100\n" +
119                                        "Subject: hello\n" +
120                                        "From: r.b at c.d (r =?iso-8859-1?Q?b=E4?=)\n" +
121                                        "To: C <c@c.c>, <d@d.c>\n" +
122                                        "\n" +
123                                        "The body"
124         );
125 
126         assertEquals(EmailAddress.from("a@b.c"), mail.id());
127         assertEquals("hello", mail.subject());
128         assertEquals(EmailAddress.from("r bä", "r.b@c.d"), mail.author());
129         assertEquals(EmailAddress.from("r bä", "r.b@c.d"), mail.sender());
130         assertEquals(List.of(EmailAddress.from("C", "c@c.c"),
131                              EmailAddress.from("d@d.c")),
132                      mail.recipients());
133         assertEquals("The body", mail.body());
134     }
135 }