Search in sources :

Example 1 with MessageBuilder

use of com.zimbra.qa.unittest.MessageBuilder in project zm-mailbox by Zimbra.

the class MailSenderTest method updateReferenceHeaders.

@Test
public void updateReferenceHeaders() throws Exception {
    MailSender sender = new MailSender();
    Provisioning prov = Provisioning.getInstance();
    Account account = prov.getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    String from = "sender@example.com";
    String to = "recipient@example.com";
    String subject = "test subject";
    MessageBuilder builder = new MessageBuilder().withFrom(from).withToRecipient(to).withSubject(subject).withBody("test body");
    String content = builder.create();
    MimeMessage msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    //default
    Assert.assertNull(msg.getHeader("Thread-Index"));
    Assert.assertNull(msg.getHeader("Thread-Topic"));
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    //technically the normalized subject, but our test message already had normalized subject
    Assert.assertEquals(subject, msg.getHeader("Thread-Topic")[0]);
    //index set by sending agent (bg 96107)
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    String presetIndex = "1234";
    msg.setHeader("Thread-Index", presetIndex);
    Assert.assertNull(msg.getHeader("Thread-Topic"));
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(subject, msg.getHeader("Thread-Topic")[0]);
    Assert.assertEquals(presetIndex, msg.getHeader("Thread-Index")[0]);
    //topic set by sending agent (bug 96107)
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    Assert.assertNull(msg.getHeader("Thread-Index"));
    String presetTopic = "mytopic";
    msg.setHeader("Thread-Topic", presetTopic);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(presetTopic, msg.getHeader("Thread-Topic")[0]);
    //both
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    msg.setHeader("Thread-Topic", presetTopic);
    msg.setHeader("Thread-Index", presetIndex);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(presetTopic, msg.getHeader("Thread-Topic")[0]);
    Assert.assertEquals(presetIndex, msg.getHeader("Thread-Index")[0]);
    //subject changed from orig parent subject (bug 96954)
    String parentSubject = "parent subject";
    builder = new MessageBuilder().withFrom(from).withToRecipient(to).withSubject(parentSubject).withBody("parent body");
    String parentContent = builder.create();
    MimeMessage parentMsg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(parentContent.getBytes()));
    sender.updateReferenceHeaders(parentMsg, null, account);
    ParsedMessage pm = new ParsedMessage(parentMsg, false);
    Message mboxMessage = mbox.addMessage(null, pm, MailboxTest.STANDARD_DELIVERY_OPTIONS, null);
    sender = new MailSender();
    sender.setOriginalMessageId(new ItemId(mboxMessage));
    sender.setReplyType(MailSender.MSGTYPE_REPLY);
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(subject, msg.getHeader("Thread-Topic")[0]);
    //keep agent specified topic and/or index even when parent specified
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    msg.setHeader("Thread-Topic", presetTopic);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(presetTopic, msg.getHeader("Thread-Topic")[0]);
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    msg.setHeader("Thread-Index", presetIndex);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(subject, msg.getHeader("Thread-Topic")[0]);
    Assert.assertEquals(presetIndex, msg.getHeader("Thread-Index")[0]);
    msg = new FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    msg.setHeader("Thread-Topic", presetTopic);
    msg.setHeader("Thread-Index", presetIndex);
    sender.updateReferenceHeaders(msg, null, account);
    Assert.assertEquals(1, msg.getHeader("Thread-Index").length);
    Assert.assertEquals(1, msg.getHeader("Thread-Topic").length);
    Assert.assertEquals(presetTopic, msg.getHeader("Thread-Topic")[0]);
    Assert.assertEquals(presetIndex, msg.getHeader("Thread-Index")[0]);
}
Also used : Account(com.zimbra.cs.account.Account) MessageBuilder(com.zimbra.qa.unittest.MessageBuilder) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ItemId(com.zimbra.cs.service.util.ItemId) MockProvisioning(com.zimbra.cs.account.MockProvisioning) Provisioning(com.zimbra.cs.account.Provisioning) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Aggregations

Account (com.zimbra.cs.account.Account)1 MockProvisioning (com.zimbra.cs.account.MockProvisioning)1 Provisioning (com.zimbra.cs.account.Provisioning)1 FixedMimeMessage (com.zimbra.cs.mime.Mime.FixedMimeMessage)1 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)1 ItemId (com.zimbra.cs.service.util.ItemId)1 MessageBuilder (com.zimbra.qa.unittest.MessageBuilder)1 MimeMessage (javax.mail.internet.MimeMessage)1 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)1 Test (org.junit.Test)1