com.formkiq.core.service.notification.MailSenderServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for com.formkiq.core.service.notification.MailSenderServiceTest.java

Source

/*
 * Copyright (C) 2017 FormKiQ Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.formkiq.core.service.notification;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import javax.mail.Provider;
import javax.mail.Session;

import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import com.formkiq.core.config.LoggerMailSender;

/**
 * MailSenderService Unit Tests.
 *
 */
public class MailSenderServiceTest {

    /**
     * testAfterPropertiesSet01().
     * expect JavaMailSenderImpl
     * @throws Exception Exception
     */
    @Test
    public void testAfterPropertiesSet01() throws Exception {
        // given
        final int port = 587;
        String data = "mail.host=smtp.gmail.com\n" + "mail.port=" + port + "\n" + "mail.username=test\n"
                + "mail.password=pass\n" + "mail.smtp.auth=true\n" + "mail.smtp.auth=true\n"
                + "mail.smtp.starttls.enable=true\n" + "mail.smtp.quitwait=false";

        InputStream is = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));

        MailSenderService ms = new MailSenderService();

        // when
        ms.afterPropertiesSet(is);

        // then
        assertTrue(ms.getMailSender() instanceof JavaMailSenderImpl);
        JavaMailSenderImpl js = (JavaMailSenderImpl) ms.getMailSender();
        assertEquals("smtp.gmail.com", js.getHost());
        assertEquals(port, js.getPort());
        assertEquals("test", js.getUsername());
        assertEquals("true", js.getJavaMailProperties().getProperty("mail.smtp.starttls.enable"));
        assertEquals("false", js.getJavaMailProperties().getProperty("mail.smtp.quitwait"));
        assertEquals("true", js.getJavaMailProperties().getProperty("mail.smtp.auth"));
    }

    /**
     * testAfterPropertiesSet01().
     * expect LoggerMailSender
     * @throws Exception Exception
     */
    @Test
    public void testAfterPropertiesSet02() throws Exception {
        // given
        MailSenderService ms = new MailSenderService();

        // when
        ms.afterPropertiesSet();

        // then
        assertTrue(ms.getMailSender() instanceof LoggerMailSender);
    }

    /**
     * testLoadMailProperties01().
     * @throws Exception Exception
     */
    @Test
    public void testLoadMailProperties01() throws Exception {
        // given
        String ls = System.getProperty("line.separator");

        String source = "mail.host=smtp.gmail.com" + ls + "mail.port=587" + ls + "mail.username=test@formkiq.com"
                + ls + "mail.password=test" + ls + "mail.smtp.auth=true" + ls + "mail.smtp.starttls.enable=true"
                + ls + "mail.smtp.quitwait=false";

        InputStream in = IOUtils.toInputStream(source, "UTF-8");
        Properties prop = new Properties();
        prop.load(in);

        // when
        Session se = Session.getDefaultInstance(prop, null);
        Provider prov = se.getProvider("smtp");

        Class<?> clazz = Class.forName(prov.getClassName());

        // then
        assertNotNull(clazz);
        in.close();
    }
}