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.LDAPOperations;

import java.util.ArrayList;
import java.util.List;

public class LDAPTestSuite {

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

        LDAP ldap = new LDAP();
        ldap.setAccountHostName("");
        ldap.validateRequest();

    }

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

        LDAP ldap = new LDAP();
        ldap.setAccountHostName("hostname");
        ldap.setAccountUseSSL(null);
        ldap.validateRequest();

    }

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

        LDAP ldap = new LDAP();
        ldap.setAccountHostName("hostname");
        ldap.setAccountUseSSL(true);
        ldap.setLdapSearchSettings(null);
        ldap.validateRequest();

    }

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

        LDAP ldap = new LDAP();
        ldap.setAccountHostName("hostname");
        ldap.setAccountUseSSL(true);

        List<LDAPSearchSetting> searchSettings = new ArrayList<LDAPSearchSetting>();
        LDAPSearchSetting ldapSearchSetting = new LDAPSearchSetting();
        ldapSearchSetting.setSearchSettingScope(null);
        ldapSearchSetting.setSearchSettingSearchBase("ou=people,o=example");
        searchSettings.add(ldapSearchSetting);

        ldap.setLdapSearchSettings(searchSettings);
        ldap.validateRequest();

    }

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

        LDAP ldap = new LDAP();
        ldap.setAccountHostName("hostname");
        ldap.setAccountUseSSL(true);

        List<LDAPSearchSetting> searchSettings = new ArrayList<LDAPSearchSetting>();
        LDAPSearchSetting ldapSearchSetting = new LDAPSearchSetting();
        ldapSearchSetting.setSearchSettingScope(LDAPSearchSettingScope.LDAPSearchSettingScopeBase);
        ldapSearchSetting.setSearchSettingSearchBase("");
        searchSettings.add(ldapSearchSetting);

        ldap.setLdapSearchSettings(searchSettings);
        ldap.validateRequest();

    }

    @Test
    public void testCorrectLDAP() throws ProfileConfigurationException {

        LDAP ldap = new LDAP();
        ldap.setAccountHostName("hostname");
        ldap.setAccountUseSSL(true);

        List<LDAPSearchSetting> searchSettings = new ArrayList<LDAPSearchSetting>();
        LDAPSearchSetting ldapSearchSetting = new LDAPSearchSetting();
        ldapSearchSetting.setSearchSettingScope(LDAPSearchSettingScope.LDAPSearchSettingScopeBase);
        ldapSearchSetting.setSearchSettingSearchBase("ou=people,o=example");
        searchSettings.add(ldapSearchSetting);

        ldap.setLdapSearchSettings(searchSettings);
        ldap.validateRequest();

    }

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

        LDAP ldap = new LDAP();
        ldap.setAccountHostName("hostname");
        ldap.setAccountUseSSL(true);

        List<LDAPSearchSetting> searchSettings = new ArrayList<LDAPSearchSetting>();

        LDAPSearchSetting ldapSearchSettingBase = new LDAPSearchSetting();
        ldapSearchSettingBase.setSearchSettingScope(LDAPSearchSettingScope.LDAPSearchSettingScopeBase);
        ldapSearchSettingBase.setSearchSettingSearchBase("ou=people,o=example");
        searchSettings.add(ldapSearchSettingBase);

        LDAPSearchSetting ldapSearchSettingOneLevel = new LDAPSearchSetting();
        ldapSearchSettingOneLevel.setSearchSettingScope(LDAPSearchSettingScope.LDAPSearchSettingScopeOneLevel);
        ldapSearchSettingOneLevel.setSearchSettingSearchBase("ou=people,o=example");
        searchSettings.add(ldapSearchSettingOneLevel);

        LDAPSearchSetting ldapSearchSettingScopeSubtree = new LDAPSearchSetting();
        ldapSearchSettingScopeSubtree.setSearchSettingScope(LDAPSearchSettingScope.LDAPSearchSettingScopeSubtree);
        ldapSearchSettingScopeSubtree.setSearchSettingSearchBase("ou=people,o=example");
        searchSettings.add(ldapSearchSettingScopeSubtree);

        ldap.setLdapSearchSettings(searchSettings);
        ldap.validateRequest();

        LDAPOperations ldapOperations = new LDAPOperations();
        PayloadOperationWrapper payload = ldapOperations.createLDAPPayload(ldap);

        Assert.assertNotNull(payload);

    }
}
