package com.braintree.listener;

import com.braintree.event.BraintreeLinkCustomerNotificationEvent;
import com.braintree.model.BraintreeLinkCustomerNotificationProcessModel;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.basecommerce.model.site.BaseSiteModel;
import de.hybris.platform.core.model.c2l.CurrencyModel;
import de.hybris.platform.core.model.c2l.LanguageModel;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.processengine.BusinessProcessService;
import de.hybris.platform.servicelayer.i18n.CommonI18NService;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.user.UserService;
import de.hybris.platform.site.BaseSiteService;
import de.hybris.platform.store.BaseStoreModel;
import de.hybris.platform.store.services.BaseStoreService;
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.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.doNothing;

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

    private static final String ID = "ID";

    private static final String NOTIFICATION_PROCESS = "braintreeLinkCustomerNotificationProcess";

    @Mock
    private BusinessProcessService businessProcessService;
    @Mock
    private ModelService modelService;
    @Mock
    private BaseStoreService baseStoreService;
    @Mock
    private BaseSiteService baseSiteService;
    @Mock
    private CommonI18NService commonI18NService;
    @Mock
    private UserService userService;

    @InjectMocks
    private BraintreeLinkCustomerNotificationListener unit;

    @Test
    public void shouldStartProcess() {
        final BraintreeLinkCustomerNotificationEvent event = mock(BraintreeLinkCustomerNotificationEvent.class);
        final BraintreeLinkCustomerNotificationProcessModel process = mock(BraintreeLinkCustomerNotificationProcessModel.class);
        final BaseStoreModel baseStoreModel = mock(BaseStoreModel.class);
        final LanguageModel languageModel = mock(LanguageModel.class);
        final CurrencyModel currencyModel = mock(CurrencyModel.class);
        final BaseSiteModel baseSiteModel = mock(BaseSiteModel.class);
        final CustomerModel customerModel = mock(CustomerModel.class);

        when(businessProcessService.createProcess(anyString(), eq(NOTIFICATION_PROCESS))).thenReturn(process);
        when(baseStoreService.getCurrentBaseStore()).thenReturn(baseStoreModel);
        when(commonI18NService.getCurrentLanguage()).thenReturn(languageModel);
        when(commonI18NService.getCurrentCurrency()).thenReturn(currencyModel);
        when(baseSiteService.getCurrentBaseSite()).thenReturn(baseSiteModel);
        when(event.getUid()).thenReturn(ID);
        when(userService.getUserForUID(ID)).thenReturn(customerModel);
        doNothing().when(modelService).save(process);
        doNothing().when(businessProcessService).startProcess(process);

        unit.onEvent(event);

        verify(businessProcessService).createProcess(anyString(), eq(NOTIFICATION_PROCESS));
        verify(process).setStore(baseStoreModel);
        verify(process).setLanguage(languageModel);
        verify(process).setCurrency(currencyModel);
        verify(process).setSite(baseSiteModel);
        verify(event).getUid();
        verify(process).setCustomer(customerModel);
        verify(modelService).save(process);
        verify(businessProcessService).startProcess(process);
    }

}
