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

io.tapack.satisfy.email.props.PropertyAccessor Maven / Gradle / Ivy

/*
 * Copyright © 2015 Tapack, and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * The Research Projects is dual-licensed under the GNU General Public
 * License, version 2.0 (GPLv2) and the Tapack Commercial License.
 *
 * Solely for non-commercial purposes. A purpose is non-commercial only if
 * it is in no manner primarily intended for or directed toward commercial
 * advantage or private monetary compensation.
 *
 * This Tapack Software is supplied to you by Tapack in consideration of your
 * agreement to the following terms, and your use, installation, modification
 * or redistribution of this Tapack Software constitutes acceptance of these
 * terms. If you do not agree with these terms, please do not use, install,
 * modify or redistribute this Tapack Software.
 *
 * Neither the name, trademarks, service marks or logos of Tapack may be used
 * to endorse or promote products derived from the Tapack Software without
 * specific prior written permission from Tapack.
 *
 * The Tapack Software is provided by Tapack on an "AS IS" basis. TAPACK MAKES NO
 * WARRANTIES, EXPRESS  OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE, REGARDING THE TAPACK SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
 * COMBINATION WITH YOUR PRODUCTS.
 *
 * IN NO EVENT SHALL TAPACK BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
 * AND/OR DISTRIBUTION OF THE TAPACK SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER
 * THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
 * OTHERWISE, EVEN IF TAPACK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * A copy of the GNU General Public License is included in the distribution in
 * the file LICENSE and at
 *
 *     http://www.gnu.org/licenses/gpl-2.0.html
 *
 * If you are using the Research Projects for commercial purposes, we
 * encourage you to visit
 *
 *     http://products.tapack.io/license
 *
 * for more details.
 *
 * This software or hardware and documentation may provide access to
 * or information on content, products, and services from third parties.
 * Tapack and its affiliates are not responsible for and expressly disclaim
 * all warranties of any kind with respect to third-party content, products,
 * and services. Tapack and its affiliates will not be responsible for any loss,
 * costs, or damages incurred due to your access to or use of third-party
 * content, products, or services. If a third-party content exists, the
 * additional copyright notices and license terms applicable to portions of the
 * software are set forth in the THIRD_PARTY_LICENSE_README file.
 *
 * Please contact Tapack or visit www.tapack.io if you need additional
 * information or have any questions.
 */

package io.tapack.satisfy.email.props;

import com.icegreen.greenmail.util.ServerSetup;
import net.thucydides.core.Thucydides;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

public class PropertyAccessor {
    private static final EnvironmentProperties ENVIRONMENT_PROPERTIES = new EnvironmentProperties();
    public static final String LOCAL_HOST = "127.0.0.1";
    public static final String DEFAULT_SSL_SOCKET_FACTORY = "com.icegreen.greenmail.util.DummySSLSocketFactory";
    private static final Logger LOG = LoggerFactory.getLogger(PropertyAccessor.class);

    private PropertyAccessor() {
    }

    public static Properties getPop3SslProperties() {
        SimplifiedProperties props = new SimplifiedProperties();
        props.set(EmailProperty.MAIL_STORE_PROTOCOL, ServerSetup.PROTOCOL_POP3S);
        props.set(EmailProperty.MAIL_POP3S_HOST, getTestPop3SslHost());
        props.set(EmailProperty.MAIL_POP3S_PORT, getTestPop3SslPort());
        props.set(EmailProperty.MAIL_POP3S_SOCKETFACTORY_CLASS, getSslSocketFactory());
        props.set(EmailProperty.MAIL_POP3S_SOCKETFACTORY_PORT, getTestPop3SslPort());
        props.set(EmailProperty.MAIL_POP3S_SOCKETFACTORY_FALLBACK, false);
        props.setDefault(EmailProperty.MAIL_POP3S_SSL_TRUST);
        props.set(EmailProperty.MAIL_POP3S_AUTH, true);
        return props;
    }

    public static Properties getPop3Properties() {
        SimplifiedProperties props = new SimplifiedProperties();
        props.set(EmailProperty.MAIL_STORE_PROTOCOL, ServerSetup.PROTOCOL_POP3);
        props.set(EmailProperty.MAIL_POP3_HOST, getTestPop3Host());
        props.set(EmailProperty.MAIL_POP3_PORT, getTestPop3Port());
        props.set(EmailProperty.MAIL_POP3_AUTH, true);
        return props;
    }

    public static Properties getImapProperties() {
        SimplifiedProperties props = new SimplifiedProperties();
        props.set(EmailProperty.MAIL_STORE_PROTOCOL, ServerSetup.PROTOCOL_IMAP);
        props.set(EmailProperty.MAIL_IMAP_HOST, getTestImapHost());
        props.set(EmailProperty.MAIL_IMAP_PORT, getTestImapPort());
        props.set(EmailProperty.MAIL_IMAP_AUTH, true);
        return props;
    }

    public static Properties getImapSslProperties() {
        SimplifiedProperties props = new SimplifiedProperties();
        props.set(EmailProperty.MAIL_STORE_PROTOCOL, ServerSetup.PROTOCOL_IMAPS);
        props.set(EmailProperty.MAIL_IMAPS_HOST, getTestImapSslHost());
        props.set(EmailProperty.MAIL_IMAPS_PORT, getTestImapSslPort());
        props.set(EmailProperty.MAIL_IMAPS_AUTH, true);
        props.set(EmailProperty.MAIL_IMAPS_SOCKETFACTORY_CLASS, getSslSocketFactory());
        props.set(EmailProperty.MAIL_IMAPS_SOCKETFACTORY_PORT, getTestImapSslPort());
        return props;
    }

    public static Properties getSmtpSslProperties() {
        SimplifiedProperties props = new SimplifiedProperties();
        props.set(EmailProperty.MAIL_TRANSPORT_PROTOCOL, ServerSetup.PROTOCOL_SMTPS);
        props.set(EmailProperty.MAIL_SMTPS_HOST, getTestSmtpSslHost());
        props.set(EmailProperty.MAIL_SMTPS_PORT, getTestSmtpSslPort());
        props.set(EmailProperty.MAIL_SMTPS_AUTH, true);
        props.set(EmailProperty.MAIL_SMTPS_SSL_SOCKETFACTORY_CLASS, getSslSocketFactory());
        props.set(EmailProperty.MAIL_SMTPS_SOCKETFACTORY_PORT, getTestSmtpSslPort());
        props.set(EmailProperty.MAIL_SMTPS_SSL_TRUST, getTestSmtpSslHost());
        return props;
    }

    public static Properties getSmtpProperties() {
        SimplifiedProperties props = new SimplifiedProperties();
        props.set(EmailProperty.MAIL_TRANSPORT_PROTOCOL, ServerSetup.PROTOCOL_SMTP);
        props.set(EmailProperty.MAIL_SMTP_HOST, getTestSmtpHost());
        props.set(EmailProperty.MAIL_SMTP_PORT, getTestSmtpPort());
        return props;
    }

    public static String getTestSmtpHost() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_SMTP_HOST);
    }

    public static String getTestSmtpPort() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_SMTP_PORT);
    }

    public static String getTestPop3Host() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_POP3_HOST);
    }

    public static String getTestPop3Port() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_POP3_PORT);
    }

    public static String getTestSmtpSslHost() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_SMTPSSL_HOST);
    }

    public static String getTestSmtpSslPort() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_SMTPSSL_PORT);
    }

    public static String getTestPop3SslHost() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_POP3SSL_HOST);
    }

    public static String getTestPop3SslPort() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_POP3SSL_PORT);
    }

    public static String getTestImapHost() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_IMAP_HOST);
    }

    public static String getTestImapPort() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_IMAP_PORT);
    }

    public static String getTestImapSslHost() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_IMAPSSL_HOST);
    }

    public static String getTestImapSslPort() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_IMAPSSL_PORT);
    }

    public static String getTestMailBoxLogin() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_MAILBOX_LOGIN);
    }

    public static String getTestMailBoxPass() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_MAILBOX_PASSWORD);
    }

    public static String getTestSmtpLogin() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_SMTP_SERVER_LOGIN);
    }

    public static String getTestSmtpPass() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_SMTP_SERVER_PASSWORD);
    }

    public static String getSendingProtocol() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_SEND_PROTOCOL);
    }

    public static void setSendingProtocol(String protocol) {
        setProperty(EmailProperty.TEST_SEND_PROTOCOL, protocol);
    }

    public static String getUseFakeProperty() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_USE_FAKE_SERVERS);
    }

    public static String getReceiveProtocol() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_RECEIVE_PROTOCOL);
    }

    public static void setReceiveProtocol(String protocol) {
        setProperty(EmailProperty.TEST_RECEIVE_PROTOCOL, protocol);
    }

    public static String getTestPortPropertyKey(String serverProtocol) {
        for (EmailProperty property : EmailProperty.values()) {
            String propertyKey = property.toString();
            if (propertyKey.contains("test") && propertyKey.contains(serverProtocol.toLowerCase()) && propertyKey.contains("port")) {
                return propertyKey;
            }
        }
        return "";
    }

    private static String getSslSocketFactory() {
        return getPropertyByPriorityFromSessionToDefault(EmailProperty.TEST_SSL_SOCKETFACTORY);
    }

    private static String getPropertyByPriorityFromSessionToDefault(EmailProperty testingEmailProperty) {
        String actualProperty;
        if (currentSessionContainsValueForProperty(testingEmailProperty)) {
            actualProperty = (String) Thucydides.getCurrentSession().get(testingEmailProperty.toString());
        } else {
            actualProperty = loadConfiguredOrDefaultProperty(testingEmailProperty);
        }
        LOG.debug("Property <=" + testingEmailProperty.toString() + "=> was loaded with value <=" + actualProperty + "=>");
        return actualProperty;
    }

    private static String loadConfiguredOrDefaultProperty(EmailProperty testingEmailProperty) {
        String property = ENVIRONMENT_PROPERTIES.getPropertyWithName(testingEmailProperty.toString());
        return property.isEmpty() ? testingEmailProperty.getDefault() : property;
    }

    private static boolean currentSessionContainsValueForProperty(EmailProperty testingEmailProperty) {
        return Thucydides.getCurrentSession().containsKey(testingEmailProperty.toString());
    }

    private static void setProperty(EmailProperty emailProperty, String value) {
        ENVIRONMENT_PROPERTIES.setProperty(emailProperty, value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy