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

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

public class VPNTestSuite {


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

        VPN vpn = new VPN();
        vpn.setUserDefinedName("");
        vpn.validateRequest();

    }

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

        VPN vpn = new VPN();
        vpn.setUserDefinedName("Test VPN");
        vpn.setOverridePrimary(null);
        vpn.validateRequest();

    }

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

        VPN vpn = new VPN();
        vpn.setUserDefinedName("Test VPN");
        vpn.setOverridePrimary(true);
        vpn.setVpnType(null);
        vpn.validateRequest();

    }

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

        VPN vpn = new VPN();
        vpn.setUserDefinedName("Test VPN");
        vpn.setOverridePrimary(true);
        vpn.setVpnType(VPNType.PPTP);
        vpn.setPpp(null);
        vpn.validateRequest();

    }

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

        VPN vpn = new VPN();
        vpn.setUserDefinedName("Test VPN");
        vpn.setOverridePrimary(true);
        vpn.setVpnType(VPNType.PPTP);
        vpn.setPpp(new PPP());
        vpn.setOnDemandEnabled(null);
        vpn.validateRequest();

    }

    @Test
    public void testVPNPayload() throws ProfileConfigurationException, PListException {
        VPN vpn = new VPN();
        vpn.setUserDefinedName("Test VPN");
        vpn.setOverridePrimary(true);
        vpn.setVpnType(VPNType.IPSec);
        vpn.setOnDemandEnabled(true);
        vpn.setIpSec(new IPSec());

        ActionParameter actionParameter = new ActionParameter();
        List arrDomains = new ArrayList();
        arrDomains.add("domain1");
        arrDomains.add("domain2");

        List arrDNS = new ArrayList();
        arrDNS.add("dns1");
        arrDNS.add("dns2");
        actionParameter.setDomains(arrDomains);
        actionParameter.setDomainAction(DomainAction.ConnectIfNeeded);
        actionParameter.setRequiredDNSServers(arrDNS);
        actionParameter.setRequiredURLStringProbe("probe");

        OnDemandRule onDemandRule = new OnDemandRule();
        onDemandRule.setOnDemandRuleAction(OnDemandRuleAction.Allow);
        List actionParams = new ArrayList();
        actionParams.add(actionParameter);
        onDemandRule.setActionParameters(actionParams);
        onDemandRule.setDNSDomainMatch(arrDomains);
        onDemandRule.setDNSServerAddressMatch(arrDomains);
        onDemandRule.setInterfaceTypeMatch(InterfaceTypeMatch.Ethernet);
        onDemandRule.setSSIDMatch("ssidmatch");
        onDemandRule.setURLStringProbe("urlprobe");

        List arrOndemand = new ArrayList();
        arrOndemand.add(onDemandRule);
        vpn.setOnDemandRules(arrOndemand);

        VendorConfig vendorConfig = new VendorConfig();
        vendorConfig.setKey("key");
        vendorConfig.setValue("value");
        List arrVenconfig = new ArrayList();
        arrVenconfig.add(vendorConfig);
        vpn.setVendorConfigs(arrVenconfig);

        vpn.validateRequest();

        VPNOperations vpnOperations = new VPNOperations();
        PayloadOperationWrapper payload = vpnOperations.createVPNPayload(vpn);

        Assert.assertNotNull(payload);
    }

    @Test
    public void testIKEV2() throws ProfileConfigurationException, PListException {
        VPN vpn = new VPN();
        vpn.setUserDefinedName("Test VPN");
        vpn.setOverridePrimary(true);
        vpn.setVpnType(VPNType.IKEv2);
        vpn.setOnDemandEnabled(true);

        ActionParameter actionParameter = new ActionParameter();
        List arrDomains = new ArrayList();
        arrDomains.add("domain1");
        arrDomains.add("domain2");

        List arrDNS = new ArrayList();
        arrDNS.add("dns1");
        arrDNS.add("dns2");
        actionParameter.setDomains(arrDomains);
        actionParameter.setDomainAction(DomainAction.ConnectIfNeeded);
        actionParameter.setRequiredDNSServers(arrDNS);
        actionParameter.setRequiredURLStringProbe("probe");

        OnDemandRule onDemandRule = new OnDemandRule();
        onDemandRule.setOnDemandRuleAction(OnDemandRuleAction.Allow);
        List actionParams = new ArrayList();
        actionParams.add(actionParameter);
        onDemandRule.setActionParameters(actionParams);
        onDemandRule.setDNSDomainMatch(arrDomains);
        onDemandRule.setDNSServerAddressMatch(arrDomains);
        onDemandRule.setInterfaceTypeMatch(InterfaceTypeMatch.Ethernet);
        onDemandRule.setSSIDMatch("ssidmatch");
        onDemandRule.setURLStringProbe("urlprobe");

        List arrOndemand = new ArrayList();
        arrOndemand.add(onDemandRule);
        vpn.setOnDemandRules(arrOndemand);

        VendorConfig vendorConfig = new VendorConfig();
        vendorConfig.setKey("key");
        vendorConfig.setValue("value");
        List arrVenconfig = new ArrayList();
        arrVenconfig.add(vendorConfig);
        vpn.setVendorConfigs(arrVenconfig);

        IKEv2 ikEv2 = new IKEv2();
        ikEv2.setRemoteAddress("http://remoteaddress");
        ikEv2.setLocalIdentifier(IdentifierFormats.FQDN);
        ikEv2.setRemoteIdentifier(IdentifierFormats.UserFQDN);
        ikEv2.setAuthenticationMethod(AuthenticationMethod.SharedSecret);
        ikEv2.setPayloadCertificateUUID("uuid");
        ikEv2.setSharedSecret("sharedSecret");
        ikEv2.setExtendedAuthEnabled(1);
        ikEv2.setAuthName("authname");
        ikEv2.setAuthPassword("authpassword");
        ikEv2.setDeadPeerDetectionInterval(DeadPeerDetectionInterval.High);
        ikEv2.setServerCertificateIssuerCommonName("issuercommonname");
        ikEv2.setServerCertificateCommonName("commonname");

        SecurityAssociationParameters securityAssociationParameters = new SecurityAssociationParameters();
        securityAssociationParameters.setEncryptionAlgorithm(EncryptionAlgorithm.DES_3);
        securityAssociationParameters.setIntegrityAlgorithm(IntegrityAlgorithm.SHA2_256);
        securityAssociationParameters.setLifeTimeInMinutes(100);
        securityAssociationParameters.setDiffieHellmanGroup(1);

        ikEv2.setIKESecurityAssociationParameters(securityAssociationParameters);
        ikEv2.setChildSecurityAssociationParameters(securityAssociationParameters);

        vpn.setIkEv2(ikEv2);

        vpn.validateRequest();

        VPNOperations vpnOperations = new VPNOperations();
        PayloadOperationWrapper payload = vpnOperations.createVPNPayload(vpn);

        Assert.assertNotNull(payload);
    }

}
