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

import org.testng.Assert;
import org.testng.annotations.Test;
import org.wso2.carbon.context.PrivilegedCarbonContext;
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.CellularOperations;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class APNConfigTestSuite {

    @Test(expectedExceptions = ProfileConfigurationException.class)
    public void testEmptyAPNConfig() throws Exception {
        initiatePrivilegedCaronContext();
        APNConfig apnConfig = new APNConfig();
        apnConfig.validateRequest();

    }

    public void initiatePrivilegedCaronContext() throws Exception {

        if (System.getProperty("carbon.home") == null) {
            File file = new File("src/test/resources/carbon-home");
            if (file.exists()) {
                System.setProperty("carbon.home", file.getAbsolutePath());
            }
            file = new File("../resources/carbon-home");
            if (file.exists()) {
                System.setProperty("carbon.home", file.getAbsolutePath());
            }
            file = new File("../../resources/carbon-home");
            if (file.exists()) {
                System.setProperty("carbon.home", file.getAbsolutePath());
            }
            file = new File("../../../resources/carbon-home");
            if (file.exists()) {
                System.setProperty("carbon.home", file.getAbsolutePath());
            }
        }

        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);

    }

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

        APNConfig apnConfig = new APNConfig();
        List<LegacyAPNConfiguration> legacyAPNConfigurations = new ArrayList<LegacyAPNConfiguration>();
        apnConfig.setApnConfigurations(legacyAPNConfigurations);

        apnConfig.validateRequest();

    }

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

        APNConfig apnConfig = new APNConfig();
        List<LegacyAPNConfiguration> legacyAPNConfigurations = new ArrayList<LegacyAPNConfiguration>();
        LegacyAPNConfiguration legacyAPNConfiguration = new LegacyAPNConfiguration();
        legacyAPNConfiguration.setApn("");
        legacyAPNConfigurations.add(legacyAPNConfiguration);
        apnConfig.setApnConfigurations(legacyAPNConfigurations);

        apnConfig.validateRequest();

    }

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

        APNConfig apnConfig = new APNConfig();
        List<LegacyAPNConfiguration> legacyAPNConfigurations = new ArrayList<LegacyAPNConfiguration>();
        LegacyAPNConfiguration legacyAPNConfiguration = new LegacyAPNConfiguration();
        legacyAPNConfiguration.setApn("apn");
        legacyAPNConfiguration.setUsername("");
        legacyAPNConfigurations.add(legacyAPNConfiguration);
        apnConfig.setApnConfigurations(legacyAPNConfigurations);

        apnConfig.validateRequest();

    }


    @Test
    public void testCorrectAPNConfig() throws ProfileConfigurationException {

        APNConfig apnConfig = new APNConfig();
        List<LegacyAPNConfiguration> legacyAPNConfigurations = new ArrayList<LegacyAPNConfiguration>();
        LegacyAPNConfiguration legacyAPNConfiguration = new LegacyAPNConfiguration();
        legacyAPNConfiguration.setApn("apn");
        legacyAPNConfiguration.setUsername("username");
        legacyAPNConfigurations.add(legacyAPNConfiguration);
        apnConfig.setApnConfigurations(legacyAPNConfigurations);

        apnConfig.validateRequest();

    }

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

        APNConfig apnConfig = new APNConfig();
        List<LegacyAPNConfiguration> legacyAPNConfigurations = new ArrayList<LegacyAPNConfiguration>();
        LegacyAPNConfiguration legacyAPNConfiguration = new LegacyAPNConfiguration();
        legacyAPNConfiguration.setApn("apn");
        legacyAPNConfiguration.setUsername("username");
        legacyAPNConfigurations.add(legacyAPNConfiguration);
        apnConfig.setApnConfigurations(legacyAPNConfigurations);

        CellularOperations cellularOperations = new CellularOperations();
        PayloadOperationWrapper payload = cellularOperations.createAPNPayload(apnConfig);

        Assert.assertNotNull(payload);

    }
}


