package org.wso2.carbon.device.mgt.ios.payload.beans;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.ios.payload.dto.PayloadOperationWrapper;
import org.wso2.carbon.device.mgt.ios.payload.exception.PListException;
import org.wso2.carbon.device.mgt.ios.payload.exception.ProfileConfigurationException;
import org.wso2.carbon.device.mgt.ios.payload.operation.EmailOperations;

public class EmailTestSuite {

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyAccountType() throws ProfileConfigurationException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(null);
        email.validateRequest();

    }

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyEmailAddress() throws ProfileConfigurationException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setEmailAddress(null);
        email.validateRequest();

    }

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyIncomingMailServerAuthentication() throws ProfileConfigurationException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setEmailAddress("test@test.org");
        email.setIncomingMailServerAuthentication(null);
        email.validateRequest();

    }

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyIncomingMailServerHostName() throws ProfileConfigurationException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setEmailAddress("test@test.org");
        email.setIncomingMailServerAuthentication(MailServerAuthentication.EmailAuthPassword);
        email.setIncomingMailServerHostName("");
        email.validateRequest();

    }

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyIncomingMailServerUsername() throws ProfileConfigurationException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setEmailAddress("test@test.org");
        email.setIncomingMailServerAuthentication(MailServerAuthentication.EmailAuthPassword);
        email.setIncomingMailServerHostName("testhost");
        email.setIncomingMailServerPortNumber(111);
        email.setIncomingMailServerUseSSL(true);
        email.setIncomingMailServerUsername("");
        email.validateRequest();

    }

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyOutgoingMailServerAuthentication() throws ProfileConfigurationException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setEmailAddress("test@test.org");
        email.setIncomingMailServerAuthentication(MailServerAuthentication.EmailAuthPassword);
        email.setIncomingMailServerHostName("testhost");
        email.setIncomingMailServerPortNumber(111);
        email.setIncomingMailServerUseSSL(true);
        email.setIncomingMailServerUsername("testuser");
        email.setIncomingPassword("incomingpassword");
        email.setOutgoingPassword("outgoingpassword");
        email.setOutgoingPasswordSameAsIncomingPassword(true);
        email.setOutgoingMailServerAuthentication(null);
        email.validateRequest();

    }

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyOutgoingMailServerHostName() throws ProfileConfigurationException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setEmailAddress("test@test.org");
        email.setIncomingMailServerAuthentication(MailServerAuthentication.EmailAuthPassword);
        email.setIncomingMailServerHostName("testhost");
        email.setIncomingMailServerPortNumber(111);
        email.setIncomingMailServerUseSSL(true);
        email.setIncomingMailServerUsername("testuser");
        email.setIncomingPassword("incomingpassword");
        email.setOutgoingPassword("outgoingpassword");
        email.setOutgoingPasswordSameAsIncomingPassword(true);
        email.setOutgoingMailServerAuthentication(MailServerAuthentication.EmailAuthNone);
        email.setOutgoingMailServerHostName("");
        email.validateRequest();

    }

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyOutgoingMailServerUsername() throws ProfileConfigurationException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setEmailAddress("test@test.org");
        email.setIncomingMailServerAuthentication(MailServerAuthentication.EmailAuthPassword);
        email.setIncomingMailServerHostName("testhost");
        email.setIncomingMailServerPortNumber(111);
        email.setIncomingMailServerUseSSL(true);
        email.setIncomingMailServerUsername("testuser");
        email.setIncomingPassword("incomingpassword");
        email.setOutgoingPassword("outgoingpassword");
        email.setOutgoingPasswordSameAsIncomingPassword(true);
        email.setOutgoingMailServerAuthentication(MailServerAuthentication.EmailAuthNone);
        email.setOutgoingMailServerHostName("outgoinghost");
        email.setOutgoingMailServerPortNumber(222);
        email.setOutgoingMailServerUseSSL(true);
        email.setOutgoingMailServerUsername("");
        email.validateRequest();

    }

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyMailRecentSyncing() throws ProfileConfigurationException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setEmailAddress("test@test.org");
        email.setIncomingMailServerAuthentication(MailServerAuthentication.EmailAuthPassword);
        email.setIncomingMailServerHostName("testhost");
        email.setIncomingMailServerPortNumber(111);
        email.setIncomingMailServerUseSSL(true);
        email.setIncomingMailServerUsername("testuser");
        email.setIncomingPassword("incomingpassword");
        email.setOutgoingPassword("outgoingpassword");
        email.setOutgoingPasswordSameAsIncomingPassword(true);
        email.setOutgoingMailServerAuthentication(MailServerAuthentication.EmailAuthNone);
        email.setOutgoingMailServerHostName("outgoinghost");
        email.setOutgoingMailServerPortNumber(222);
        email.setOutgoingMailServerUseSSL(true);
        email.setOutgoingMailServerUsername("testuser");
        email.setPreventMove(true);
        email.setPreventAppSheet(true);
        email.setSMIMEEnabled(true);
        email.setSMIMESigningCertificateUUID("testuuid");
        email.setSMIMEEncryptionCertificateUUID("encuuid");
        email.setSMIMEEnablePerMessageSwitch(true);
        email.setDisableMailRecentsSyncing(null);
        email.validateRequest();

    }

    @Test
    public void testEMailPayload() throws ProfileConfigurationException, PListException {

        Email email = new Email();
        email.setEmailAccountDescription("Account description");
        email.setEmailAccountName("Email account name");
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setEmailAddress("test@test.org");
        email.setIncomingMailServerAuthentication(MailServerAuthentication.EmailAuthPassword);
        email.setIncomingMailServerHostName("incominghost");
        email.setIncomingMailServerPortNumber(111);
        email.setIncomingMailServerUseSSL(true);
        email.setIncomingMailServerUsername("incominguser");
        email.setIncomingPassword("incomingpassword");
        email.setOutgoingPassword("outgoingpassword");
        email.setOutgoingPasswordSameAsIncomingPassword(true);
        email.setOutgoingMailServerAuthentication(MailServerAuthentication.EmailAuthNone);
        email.setOutgoingMailServerHostName("outgoinghost");
        email.setOutgoingMailServerPortNumber(222);
        email.setOutgoingMailServerUseSSL(true);
        email.setOutgoingMailServerUsername("outgoinguser");
        email.setPreventMove(true);
        email.setPreventAppSheet(true);
        email.setSMIMEEnabled(true);
        email.setSMIMESigningCertificateUUID("testuuid");
        email.setSMIMEEncryptionCertificateUUID("encuuid");
        email.setSMIMEEnablePerMessageSwitch(true);
        email.setDisableMailRecentsSyncing(true);
        email.setIncomingMailServerIMAPPathPrefix("pathprefix");
        email.validateRequest();

        EmailOperations emailOperations = new EmailOperations();
        PayloadOperationWrapper payload = emailOperations.createEmailPayload(email);

        Assert.assertNotNull(payload);
    }

    @Test
    public void testEmptyEmailAddressPayload() throws ProfileConfigurationException, PListException {
        Email email = new Email();
        email.setEmailAccountType(EmailAccountType.EmailTypeIMAP);
        email.setIncomingMailServerAuthentication(MailServerAuthentication.EmailAuthPassword);
        email.setIncomingMailServerHostName("incominghost");
        email.setIncomingMailServerPortNumber(111);
        email.setIncomingMailServerUseSSL(true);
        email.setIncomingMailServerUsername("incominguser");
        email.setIncomingPassword("incomingpassword");
        email.setOutgoingPassword("outgoingpassword");
        email.setOutgoingPasswordSameAsIncomingPassword(true);
        email.setOutgoingMailServerAuthentication(MailServerAuthentication.EmailAuthNone);
        email.setOutgoingMailServerHostName("outgoinghost");
        email.setOutgoingMailServerPortNumber(222);
        email.setOutgoingMailServerUseSSL(true);
        email.setOutgoingMailServerUsername("outgoinguser");
        email.setPreventMove(true);
        email.setPreventAppSheet(true);
        email.setSMIMEEnabled(true);
        email.setSMIMESigningCertificateUUID("testuuid");
        email.setSMIMEEncryptionCertificateUUID("encuuid");
        email.setSMIMEEnablePerMessageSwitch(true);
        email.setDisableMailRecentsSyncing(true);
        email.setIncomingMailServerIMAPPathPrefix("pathprefix");
        email.validateRequest();

        EmailOperations emailOperations = new EmailOperations();
        PayloadOperationWrapper payload = emailOperations.createEmailPayload(email);

        Assert.assertNotNull(payload);

    }

}
