package com.braintree.email.listeners;

import com.braintree.email.emailEvents.BraintreeLocalPaymentReversedEmailEvent;
import com.braintree.model.BraintreeLocalPaymentReversedEmailProcessModel;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.basecommerce.model.site.BaseSiteModel;
import de.hybris.platform.commerceservices.enums.SiteChannel;
import de.hybris.platform.core.model.c2l.CurrencyModel;
import de.hybris.platform.core.model.c2l.LanguageModel;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.processengine.BusinessProcessService;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.store.BaseStoreModel;
import org.junit.Before;
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.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    private static final String CONTENT = "Content";
    private static final String CODE = "Code";
    private static final String ORDER_TOTAL_PRICE = "2.0";
    private static final String REVERSED_EMAIL_PROCESS = "braintreeLocalPaymentReversedEmailProcess";

    @Mock
    private ModelService modelService;
    @Mock
    private BusinessProcessService businessProcessService;

    @InjectMocks
    private BraintreeLocalPaymentReversedEmailEventListener unit;

    @Mock
    private BraintreeLocalPaymentReversedEmailEvent event;
    @Mock
    private BaseSiteModel baseSite;
    @Mock
    private BaseStoreModel baseStoreModel;
    @Mock
    private BraintreeLocalPaymentReversedEmailProcessModel braintreeEmailProcess;
    @Mock
    private CustomerModel customerModel;
    @Mock
    private LanguageModel languageModel;
    @Mock
    private CurrencyModel currencyModel;
    @Mock
    private OrderModel orderModel;

    @Before
    public void setUp() {
        when(event.getSite()).thenReturn(baseSite);
    }

    @Test
    public void shouldGetSiteChannelForEvent() {
        //given
        when(baseSite.getChannel()).thenReturn(SiteChannel.B2C);

        //when
        SiteChannel result = unit.getSiteChannelForEvent(event);

        //then
        assertEquals(SiteChannel.B2C, result);
    }

    @Test
    public void shouldExecuteOnSiteEvent() {
        //given
        when(businessProcessService.createProcess(anyString(), eq(REVERSED_EMAIL_PROCESS)))
                .thenReturn(braintreeEmailProcess);
        when(event.getCustomer()).thenReturn(customerModel);
        when(event.getLanguage()).thenReturn(languageModel);
        when(event.getCurrency()).thenReturn(currencyModel);
        when(event.getBaseStore()).thenReturn(baseStoreModel);
        when(event.getContent()).thenReturn(CONTENT);
        when(event.getOrderModel()).thenReturn(orderModel);
        when(orderModel.getCode()).thenReturn(CODE);
        when(orderModel.getTotalPrice()).thenReturn(2d);

        //when
        unit.onSiteEvent(event);

        //then
        verify(braintreeEmailProcess).setCustomer(customerModel);
        verify(braintreeEmailProcess).setSite(baseSite);
        verify(braintreeEmailProcess).setLanguage(languageModel);
        verify(braintreeEmailProcess).setCurrency(currencyModel);
        verify(braintreeEmailProcess).setStore(baseStoreModel);
        verify(braintreeEmailProcess).setContent(CONTENT);
        verify(braintreeEmailProcess).setOrderId(CODE);
        verify(braintreeEmailProcess).setOrderTotalPrice(ORDER_TOTAL_PRICE);
        verify(modelService).save(braintreeEmailProcess);
        verify(businessProcessService).startProcess(braintreeEmailProcess);
    }

}
