package at.procon.dip.ingestion.mail;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import at.procon.dip.ingestion.config.DipIngestionProperties;
import org.junit.jupiter.api.Test;

class CamelMailServerEndpointUriFactoryTest {

    private final CamelMailServerEndpointUriFactory factory = new CamelMailServerEndpointUriFactory();

    @Test
    void shouldBuildImapsConsumerUri() {
        DipIngestionProperties.MailRouteProperties properties = new DipIngestionProperties.MailRouteProperties();
        properties.setProtocol(MailServerProtocol.IMAPS);
        properties.setHost("mail.example.org");
        properties.setPort(993);
        properties.setUsername("user@example.org");
        properties.setPassword("p@ss word");
        properties.setFolderName("INBOX/Orders");
        properties.setDelete(false);
        properties.setPeek(true);
        properties.setUnseen(true);
        properties.setDelay(15000);
        properties.setMaxMessagesPerPoll(25);

        String uri = factory.buildConsumerUri(properties);

        assertThat(uri).startsWith("imaps://mail.example.org:993?");
        assertThat(uri).contains("username=user%40example.org");
        assertThat(uri).contains("password=p%40ss+word");
        assertThat(uri).contains("folderName=INBOX%2FOrders");
        assertThat(uri).contains("peek=true");
        assertThat(uri).contains("unseen=true");
        assertThat(uri).contains("maxMessagesPerPoll=25");
    }

    @Test
    void shouldFailWhenRequiredValuesAreMissing() {
        DipIngestionProperties.MailRouteProperties properties = new DipIngestionProperties.MailRouteProperties();
        properties.setProtocol(MailServerProtocol.IMAPS);

        assertThatThrownBy(() -> factory.buildConsumerUri(properties))
                .isInstanceOf(IllegalArgumentException.class)
                .hasMessageContaining("host");
    }
}
