All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.mailer.runtime.MockMailboxImpl Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package io.quarkus.mailer.runtime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.quarkus.mailer.Mail;
import io.quarkus.mailer.MockMailbox;
import io.smallrye.mutiny.Uni;
import io.vertx.ext.mail.MailMessage;

/**
 * Mock mailbox bean, will be populated if mocking emails.
 */
public class MockMailboxImpl implements MockMailbox {

    private Map> sentMails = new HashMap<>();
    private Map> sentMailMessages = new HashMap<>();
    private int sentMessagesCount;

    Uni send(Mail email, MailMessage mailMessage) {
        if (email.getTo() != null) {
            for (String to : email.getTo()) {
                send(email, mailMessage, to);
            }
        }
        if (email.getCc() != null) {
            for (String to : email.getCc()) {
                send(email, mailMessage, to);
            }
        }
        if (email.getBcc() != null) {
            for (String to : email.getBcc()) {
                send(email, mailMessage, to);
            }
        }
        return Uni.createFrom().item(() -> null);
    }

    private void send(Mail sentMail, MailMessage sentMailMessage, String to) {
        sentMails.computeIfAbsent(to, k -> new ArrayList<>()).add(sentMail);
        sentMailMessages.computeIfAbsent(to, k -> new ArrayList<>()).add(sentMailMessage);
        sentMessagesCount++;
    }

    @Override
    public List getMessagesSentTo(String address) {
        return sentMails.getOrDefault(address, List.of());
    }

    @Override
    public List getMailsSentTo(String address) {
        return sentMails.getOrDefault(address, List.of());
    }

    @Override
    public List getMailMessagesSentTo(String address) {
        return sentMailMessages.getOrDefault(address, List.of());
    }

    @Override
    public void clear() {
        sentMessagesCount = 0;
        sentMails.clear();
        sentMailMessages.clear();
    }

    @Override
    public int getTotalMessagesSent() {
        return sentMessagesCount;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy