package com.braintree.converters.populators.configuration;

import com.braintree.enums.BraintreeThreeDSecureFlow;
import com.braintree.enums.BraintreeThreeDSecureSCAExemptionType;
import com.braintree.hybris.data.BraintreeGeneralConfigurationData;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
import de.hybris.platform.core.model.c2l.CurrencyModel;
import de.hybris.platform.servicelayer.i18n.CommonI18NService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class BraintreeGeneralConfigurationPopulatorTest {

    private static final String CURRENCY = "currency";
    private static final String ACCOUNT = "account";
    private static final Map<String, String> MERCHANT_ACCOUNT_ID_MAP = Map.of(CURRENCY, ACCOUNT);
    private static final Integer REMOVAL_EXPIRATION = 1;
    private static final String THREE_D_SECURE_FLOW = "AUTO";
    private static final String SCA_EXEMPTION_TYPE = "DISABLED";
    public static final String BUSINESS_NAME = "businessName";
    public static final String PRODUCT_NAME = "productName";
    public static final String PHONE_NUMBER = "phoneNumber";
    public static final String URL = "url";

    @Mock
    private CurrencyModel currencyModel;
    @Mock
    private BraintreeGeneralConfigurationModel model;
    @Mock
    private CommonI18NService commonI18NService;

    @InjectMocks
    private BraintreeGeneralConfigurationPopulator unit;

    @Test
    public void shouldPopulateGeneralConfiguration() {
        BraintreeGeneralConfigurationData data = new BraintreeGeneralConfigurationData();

        when(commonI18NService.getCurrentCurrency()).thenReturn(currencyModel);
        when(currencyModel.getIsocode()).thenReturn(CURRENCY);

        when(model.isAdvancedFraudTools()).thenReturn(true);
        when(model.isDropIn()).thenReturn(true);
        when(model.isLevel2Level3()).thenReturn(true);
        when(model.getMerchantAccountId()).thenReturn(MERCHANT_ACCOUNT_ID_MAP);
        when(model.isSubmitForSettlement()).thenReturn(true);
        when(model.isStoreInVault()).thenReturn(true);
        when(model.isRestrictionOfPaymentMethodRemoval()).thenReturn(true);
        when(model.getRestrictionOfPaymentMethodRemovalExpiration()).thenReturn(REMOVAL_EXPIRATION);
        when(model.getBraintreeThreeDSecureFlow()).thenReturn(BraintreeThreeDSecureFlow.AUTO);
        when(model.getBraintreeThreeDSecureSCAExemption()).thenReturn(BraintreeThreeDSecureSCAExemptionType.DISABLED);
        when(model.getDescriptorBusinessName()).thenReturn(BUSINESS_NAME);
        when(model.getDescriptorProductName()).thenReturn(PRODUCT_NAME);
        when(model.getDescriptorPhoneNumber()).thenReturn(PHONE_NUMBER);
        when(model.getDescriptorUrl()).thenReturn(URL);

        unit.populate(model, data);

        assertTrue(data.isAdvancedFraudTools());
        assertTrue(data.isDropIn());
        assertTrue(data.isLevel2Level3());
        assertEquals(ACCOUNT, data.getMerchantAccountId());
        assertTrue(data.isSubmitForSettlement());
        assertTrue(data.isStoreInVault());
        assertTrue(data.isRestrictionOfPaymentMethodRemoval());
        assertEquals(REMOVAL_EXPIRATION, data.getRestrictionOfPaymentMethodRemovalExpiration());
        assertEquals(THREE_D_SECURE_FLOW, data.getThreeDSecureFlow());
        assertEquals(SCA_EXEMPTION_TYPE, data.getThreeDSecureSCAExemption());
        assertEquals(BUSINESS_NAME, data.getDescriptorBusinessName());
        assertEquals(PRODUCT_NAME, data.getDescriptorProductName());
        assertEquals(PHONE_NUMBER, data.getDescriptorPhoneNumber());
        assertEquals(URL, data.getDescriptorUrl());
    }

    @Test
    public void shouldPopulateGeneralConfigurationWithSomeNullValues() {
        BraintreeGeneralConfigurationData data = new BraintreeGeneralConfigurationData();

        when(commonI18NService.getCurrentCurrency()).thenReturn(currencyModel);
        when(currencyModel.getIsocode()).thenReturn(CURRENCY);

        when(model.isAdvancedFraudTools()).thenReturn(true);
        when(model.isDropIn()).thenReturn(true);
        when(model.isLevel2Level3()).thenReturn(true);
        when(model.getMerchantAccountId()).thenReturn(MERCHANT_ACCOUNT_ID_MAP);
        when(model.isSubmitForSettlement()).thenReturn(true);
        when(model.isStoreInVault()).thenReturn(true);
        when(model.isRestrictionOfPaymentMethodRemoval()).thenReturn(true);
        when(model.getRestrictionOfPaymentMethodRemovalExpiration()).thenReturn(REMOVAL_EXPIRATION);
        when(model.getBraintreeThreeDSecureFlow()).thenReturn(BraintreeThreeDSecureFlow.AUTO);
        when(model.getBraintreeThreeDSecureSCAExemption()).thenReturn(BraintreeThreeDSecureSCAExemptionType.DISABLED);

        unit.populate(model, data);

        assertTrue(data.isAdvancedFraudTools());
        assertTrue(data.isDropIn());
        assertTrue(data.isLevel2Level3());
        assertEquals(ACCOUNT, data.getMerchantAccountId());
        assertTrue(data.isSubmitForSettlement());
        assertTrue(data.isStoreInVault());
        assertTrue(data.isRestrictionOfPaymentMethodRemoval());
        assertEquals(REMOVAL_EXPIRATION, data.getRestrictionOfPaymentMethodRemovalExpiration());
        assertEquals(THREE_D_SECURE_FLOW, data.getThreeDSecureFlow());
        assertEquals(SCA_EXEMPTION_TYPE, data.getThreeDSecureSCAExemption());
        assertNull(data.getDescriptorBusinessName());
        assertNull(data.getDescriptorProductName());
        assertNull(data.getDescriptorPhoneNumber());
        assertNull(data.getDescriptorUrl());
    }

}