View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.email.functional;
8   
9   import org.mule.tck.functional.CountdownCallback;
10  import org.mule.tck.functional.FunctionalTestComponent;
11  import org.mule.transport.email.GreenMailUtilities;
12  import org.mule.transport.email.ImapConnector;
13  
14  import com.icegreen.greenmail.store.StoredMessage;
15  import com.icegreen.greenmail.user.UserManager;
16  
17  import java.util.List;
18  
19  import javax.mail.Flags;
20  import javax.mail.internet.MimeMessage;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  public class ImapCustomSeenHandlingTestCase extends AbstractEmailFunctionalTestCase
28  {
29      private CountdownCallback messageReceived = new CountdownCallback(1);
30  
31      public ImapCustomSeenHandlingTestCase()
32      {
33          super(false, ImapConnector.IMAP);
34  
35          // do not start Mule the IMAP server must be filled with appropriate test data first
36          setStartContext(false);
37      }
38  
39      @Override
40      protected String getConfigResources()
41      {
42          return "imap-custom-seen-flag.xml";
43      }
44  
45      @Test
46      public void testMessagesMatchingFilterGetCustomFlagSet() throws Exception
47      {
48          putMailMessageMatchingFilterIntoImapServer();
49          setupTestComponentListener();
50  
51          // now that everything is set up, start the context and let the polling begin
52          muleContext.start();
53  
54          // wait for the message to be processed
55          assertTrue(messageReceived.await(RECEIVE_TIMEOUT));
56          assertMessageIsFlagged();
57      }
58  
59      private void putMailMessageMatchingFilterIntoImapServer() throws Exception
60      {
61          String email = "barney@mule.org";
62  
63          UserManager userManager = server.getManagers().getUserManager();
64          MimeMessage message = GreenMailUtilities.toMessage(TEST_MESSAGE, email, null);
65          GreenMailUtilities.storeEmail(userManager, email, DEFAULT_USER, DEFAULT_PASSWORD, message);
66      }
67  
68      private void setupTestComponentListener() throws Exception
69      {
70          FunctionalTestComponent component = getFunctionalTestComponent("custom-flags");
71          assertNotNull(component);
72  
73          component.setEventCallback(messageReceived);
74      }
75  
76      private void assertMessageIsFlagged() throws Exception
77      {
78          // our superclass puts one message per default so we definitely have more than one message
79          // here. Just check if one is there with the required flag
80          boolean flaggedMessageFound = anyMessageIsFlagged();
81          assertTrue("no FLAGGED message found", flaggedMessageFound);
82      }
83  
84      private boolean anyMessageIsFlagged()
85      {
86          for (StoredMessage message : allImapMessages())
87          {
88              if (message.getFlags().contains(Flags.Flag.FLAGGED))
89              {
90                  return true;
91              }
92          }
93          return false;
94      }
95  
96      /**
97       * We cannot use <code>server.getReceivedMessages()</code> to obtain all messages from the
98       * IMAP server, the {@link MimeMessage} instances that are returned by that method do not
99       * have all the flags set.
100      */
101     @SuppressWarnings("unchecked")
102     private List<StoredMessage> allImapMessages()
103     {
104         return server.getManagers().getImapHostManager().getAllMessages();
105     }
106 }