package com.braintree.converters.populators.configuration;

import com.braintree.enums.BraintreePaymentMethodLimit;
import com.braintree.enums.BraintreePaymentMethodLimitData;
import com.braintree.hybris.data.BraintreeACHConfigurationData;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreeACHConfigurationModel;
import de.hybris.platform.core.model.c2l.CurrencyModel;
import de.hybris.platform.servicelayer.session.Session;
import de.hybris.platform.servicelayer.session.SessionService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

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

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

    private static final String CURRENCY = "currency";
    private static final String USD = "USD";
    private static final String EUR = "EUR";
    private static final String MERCHANT_NAME = "merchantName";

    @Mock
    private BraintreeACHConfigurationModel model;
    @Mock
    private CurrencyModel currencyModel;
    @Mock
    private Session session;
    @Mock
    private SessionService sessionService;
    @InjectMocks
    private BraintreeACHConfigurationPopulator unit;


    @Test
    public void shouldPopulateACHConfiguration() {
        BraintreeACHConfigurationData data = new BraintreeACHConfigurationData();

        when(sessionService.getCurrentSession()).thenReturn(session);
        when(session.getAttribute(CURRENCY)).thenReturn(currencyModel);
        when(currencyModel.getIsocode()).thenReturn(USD);

        when(model.isEnabled()).thenReturn(true);
        when(model.getMerchantName()).thenReturn(MERCHANT_NAME);
        when(model.getSavedACHAccountLimit()).thenReturn(BraintreePaymentMethodLimit.ONE);

        unit.populate(model, data);

        assertTrue(data.isEnabled());
        assertEquals(MERCHANT_NAME, data.getMerchantName());
        assertEquals(BraintreePaymentMethodLimitData.ONE, data.getSavedACHAccountLimit());
    }

    @Test
    public void shouldPopulateEnableFalseIfSessionCurrencyNotUSD() {
        BraintreeACHConfigurationData data = new BraintreeACHConfigurationData();

        when(sessionService.getCurrentSession()).thenReturn(session);
        when(session.getAttribute(CURRENCY)).thenReturn(currencyModel);
        when(currencyModel.getIsocode()).thenReturn(EUR);

        when(model.isEnabled()).thenReturn(true);
        when(model.getMerchantName()).thenReturn(MERCHANT_NAME);
        when(model.getSavedACHAccountLimit()).thenReturn(BraintreePaymentMethodLimit.ONE);

        unit.populate(model, data);

        assertFalse(data.isEnabled());
        assertEquals(MERCHANT_NAME, data.getMerchantName());
        assertEquals(BraintreePaymentMethodLimitData.ONE, data.getSavedACHAccountLimit());
    }

}