|
| 1 | +/**************************************************************** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one * |
| 3 | + * or more contributor license agreements. See the NOTICE file * |
| 4 | + * distributed with this work for additional information * |
| 5 | + * regarding copyright ownership. The ASF licenses this file * |
| 6 | + * to you under the Apache License, Version 2.0 (the * |
| 7 | + * "License"); you may not use this file except in compliance * |
| 8 | + * with the License. You may obtain a copy of the License at * |
| 9 | + * * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 11 | + * * |
| 12 | + * Unless required by applicable law or agreed to in writing, * |
| 13 | + * software distributed under the License is distributed on an * |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
| 15 | + * KIND, either express or implied. See the License for the * |
| 16 | + * specific language governing permissions and limitations * |
| 17 | + * under the License. * |
| 18 | + ****************************************************************/ |
| 19 | +package org.apache.james.transport.mailets; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +import java.util.Properties; |
| 24 | + |
| 25 | +import jakarta.mail.Session; |
| 26 | +import jakarta.mail.internet.MimeMessage; |
| 27 | + |
| 28 | +import org.apache.mailet.base.test.FakeMail; |
| 29 | +import org.apache.mailet.base.test.FakeMailetConfig; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +public class SanitizeMimeMessageIdTest { |
| 34 | + |
| 35 | + private SanitizeMimeMessageId testee; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void setUp() throws Exception { |
| 39 | + testee = new SanitizeMimeMessageId(); |
| 40 | + testee.init(FakeMailetConfig.builder() |
| 41 | + .mailetName("SanitizeMimeMessageId") |
| 42 | + .build()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void shouldAddMessageIdWhenMissing() throws Exception { |
| 47 | + MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties())); |
| 48 | + FakeMail mail = FakeMail.builder() |
| 49 | + .name("mail") |
| 50 | + .mimeMessage(mimeMessage) |
| 51 | + .build(); |
| 52 | + |
| 53 | + testee.service(mail); |
| 54 | + |
| 55 | + assertThat(mail.getMessage().getHeader("Message-ID")).isNotNull(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void shouldNotModifyExistingMessageId() throws Exception { |
| 60 | + MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties())); |
| 61 | + mimeMessage.setHeader("Message-ID", "<existing-id@domain.com>"); |
| 62 | + FakeMail mail = FakeMail.builder() |
| 63 | + .name("mail") |
| 64 | + .mimeMessage(mimeMessage) |
| 65 | + .build(); |
| 66 | + |
| 67 | + testee.service(mail); |
| 68 | + |
| 69 | + assertThat(mail.getMessage().getHeader("Message-ID")).containsExactly("<existing-id@domain.com>"); |
| 70 | + } |
| 71 | +} |
| 72 | + |
0 commit comments