Search in sources :

Example 1 with FromTerm

use of javax.mail.search.FromTerm in project NoraUi by NoraUi.

the class MailSteps method validActivationEmail.

/**
 * Valid activation email.
 *
 * @param mailHost
 *            example: imap.gmail.com
 * @param mailUser
 *            login of mail box
 * @param mailPassword
 *            password of mail box
 * @param firstCssQuery
 *            the first matching element
 * @param conditions
 *            list of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
 * @throws TechnicalException
 *             is throws if you have a technical error (format, configuration, datas, ...) in NoraUi.
 *             Exception with message and with screenshot and with exception if functional error but no screenshot and no exception if technical error.
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
@RetryOnFailure(attempts = 3, delay = 60)
@Conditioned
@Et("Je valide le mail d'activation '(.*)'[\\.|\\?]")
@And("I valid activation email '(.*)'[\\.|\\?]")
public void validActivationEmail(String mailHost, String mailUser, String mailPassword, String senderMail, String subjectMail, String firstCssQuery, List<GherkinStepCondition> conditions) throws FailureException, TechnicalException {
    RestTemplate restTemplate = createRestTemplate();
    try {
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imap");
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imaps");
        store.connect(mailHost, mailUser, mailPassword);
        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        SearchTerm filterA = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        SearchTerm filterB = new FromTerm(new InternetAddress(senderMail));
        SearchTerm filterC = new SubjectTerm(subjectMail);
        SearchTerm[] filters = { filterA, filterB, filterC };
        SearchTerm searchTerm = new AndTerm(filters);
        Message[] messages = inbox.search(searchTerm);
        for (Message message : messages) {
            Document doc = Jsoup.parse(getTextFromMessage(message));
            Element link = doc.selectFirst(firstCssQuery);
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<String> entity = new HttpEntity<>(headers);
            ResponseEntity<String> response = restTemplate.exchange(link.attr("href"), HttpMethod.GET, entity, String.class);
            if (!response.getStatusCode().equals(HttpStatus.OK)) {
                new Result.Failure<>("", Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_MAIL_ACTIVATION), subjectMail), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
            }
        }
    } catch (Exception e) {
        new Result.Failure<>("", Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_MAIL_ACTIVATION), subjectMail), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) HttpEntity(org.springframework.http.HttpEntity) Element(org.jsoup.nodes.Element) Store(javax.mail.Store) Properties(java.util.Properties) Folder(javax.mail.Folder) Document(org.jsoup.nodes.Document) Result(com.github.noraui.exception.Result) AndTerm(javax.mail.search.AndTerm) FlagTerm(javax.mail.search.FlagTerm) RetryOnFailure(com.github.noraui.cucumber.annotation.RetryOnFailure) Flags(javax.mail.Flags) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) MessagingException(javax.mail.MessagingException) FailureException(com.github.noraui.exception.FailureException) IOException(java.io.IOException) TechnicalException(com.github.noraui.exception.TechnicalException) RestTemplate(org.springframework.web.client.RestTemplate) FromTerm(javax.mail.search.FromTerm) Session(javax.mail.Session) Conditioned(com.github.noraui.cucumber.annotation.Conditioned) And(cucumber.api.java.en.And) RetryOnFailure(com.github.noraui.cucumber.annotation.RetryOnFailure) Et(cucumber.api.java.fr.Et)

Example 2 with FromTerm

use of javax.mail.search.FromTerm in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testIdleWithServerCustomSearch.

@Test
public void testIdleWithServerCustomSearch() throws Exception {
    ImapMailReceiver receiver = new ImapMailReceiver("imap://user:pw@localhost:" + imapIdleServer.getPort() + "/INBOX");
    receiver.setSearchTermStrategy((supportedFlags, folder) -> {
        try {
            FromTerm fromTerm = new FromTerm(new InternetAddress("bar@baz"));
            return new AndTerm(fromTerm, new FlagTerm(new Flags(Flag.SEEN), false));
        } catch (AddressException e) {
            throw new RuntimeException(e);
        }
    });
    testIdleWithServerGuts(receiver, false);
}
Also used : AndTerm(javax.mail.search.AndTerm) InternetAddress(javax.mail.internet.InternetAddress) FlagTerm(javax.mail.search.FlagTerm) AddressException(javax.mail.internet.AddressException) Flags(javax.mail.Flags) FromTerm(javax.mail.search.FromTerm) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 3 with FromTerm

use of javax.mail.search.FromTerm in project alfresco-repository by Alfresco.

the class ImapServiceImplTest method testSearchTerms.

/**
 * MNT-12773
 * @throws AddressException
 */
public void testSearchTerms() throws AddressException {
    List<AlfrescoImapFolder> mf = imapService.listMailboxes(user, IMAP_ROOT + "/" + TEST_IMAP_FOLDER_NAME + "/_*", false);
    ArrayList<Long> res = new ArrayList<Long>();
    SearchTerm st = null;
    st = new SentDateTerm(ComparisonTerm.LT, new Date());
    extractSearchTermResultsToList(mf, st, res);
    assertEquals("Size of mails isn't correct. Search by sent date", 3, res.size());
    res.clear();
    st = new SubjectTerm("For Test");
    extractSearchTermResultsToList(mf, st, res);
    assertEquals("Size of mails isn't correct. Search by subject", 1, res.size());
    res.clear();
    st = new FromTerm(new InternetAddress("admin@alfresco.com"));
    extractSearchTermResultsToList(mf, st, res);
    assertEquals("Size of mails isn't correct. Search by \"From\" term", 1, res.size());
    res.clear();
    st = new AndTerm(st, new SubjectTerm("For Test"));
    extractSearchTermResultsToList(mf, st, res);
    assertEquals("Size of mails isn't correct. Search by \"From\" and \"Subject\" terms", 1, res.size());
}
Also used : AndTerm(javax.mail.search.AndTerm) InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) SentDateTerm(javax.mail.search.SentDateTerm) SearchTerm(javax.mail.search.SearchTerm) FromTerm(javax.mail.search.FromTerm) SubjectTerm(javax.mail.search.SubjectTerm) Date(java.util.Date)

Aggregations

InternetAddress (javax.mail.internet.InternetAddress)3 AndTerm (javax.mail.search.AndTerm)3 FromTerm (javax.mail.search.FromTerm)3 Flags (javax.mail.Flags)2 FlagTerm (javax.mail.search.FlagTerm)2 SearchTerm (javax.mail.search.SearchTerm)2 SubjectTerm (javax.mail.search.SubjectTerm)2 Conditioned (com.github.noraui.cucumber.annotation.Conditioned)1 RetryOnFailure (com.github.noraui.cucumber.annotation.RetryOnFailure)1 FailureException (com.github.noraui.exception.FailureException)1 Result (com.github.noraui.exception.Result)1 TechnicalException (com.github.noraui.exception.TechnicalException)1 And (cucumber.api.java.en.And)1 Et (cucumber.api.java.fr.Et)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Properties (java.util.Properties)1 Folder (javax.mail.Folder)1 Message (javax.mail.Message)1