package com.braintree.customersupportbackoffice.handler;

import com.hybris.cockpitng.dataaccess.facades.object.ObjectFacade;
import com.hybris.cockpitng.engine.WidgetInstanceManager;
import com.hybris.cockpitng.util.notifications.NotificationService;
import com.hybris.cockpitng.util.notifications.event.NotificationEvent;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
import de.hybris.platform.core.model.BraintreeHostedFieldsConfigurationModel;
import de.hybris.platform.core.model.BraintreePluginConfigurationModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class BraintreeHostedFieldsConfigurationLogicHandlerTest {
    private static final String HOSTED_FIELDS_CONFIGURATION_DISABLED = "hostedFieldsConfigurationDisabled";
    private static final String HOSTED_FIELDS_ACCOUNT_LIMITATION_DISABLED = "hostedFieldsAccountLimitationDisabled";
    @Mock
    private NotificationService notificationService;
    @Mock
    private ObjectFacade objectFacade;
    @Mock
    private WidgetInstanceManager widgetInstanceManager;
    @Mock
    private BraintreeHostedFieldsConfigurationModel braintreeHostedFieldsConfigurationModel;
    @Mock
    private BraintreeGeneralConfigurationModel generalConfigurationModel;
    @Mock
    private BraintreePluginConfigurationModel pluginConfigurationModel;
    @InjectMocks
    private BraintreeHostedFieldsConfigurationLogicHandler unit;
    @Test
    public void shouldPerformSaveWhenHostedFieldsIsDisabled() throws Exception{
        when(braintreeHostedFieldsConfigurationModel.isEnabled()).thenReturn(false);
        unit.performSave(widgetInstanceManager,braintreeHostedFieldsConfigurationModel);
        verify(notificationService).notifyUser(widgetInstanceManager, HOSTED_FIELDS_CONFIGURATION_DISABLED,
                NotificationEvent.Level.INFO);
    }

    @Test
    public void shouldBeforeEditorAreaRenderWhenHostedFieldsIsDisabled() {
        when(braintreeHostedFieldsConfigurationModel.isEnabled()).thenReturn(false);
        when(braintreeHostedFieldsConfigurationModel.getPluginConfiguration()).thenReturn(pluginConfigurationModel);
        when(pluginConfigurationModel.getBraintreeGeneralConfiguration()).thenReturn(generalConfigurationModel);
        unit.beforeEditorAreaRender(widgetInstanceManager, braintreeHostedFieldsConfigurationModel);
        verify(notificationService).notifyUser(widgetInstanceManager, HOSTED_FIELDS_CONFIGURATION_DISABLED,
                NotificationEvent.Level.WARNING);
    }

    @Test
    public void shouldBeforeEditorAreaRenderWhenDropInIsEnabled() {
        when(braintreeHostedFieldsConfigurationModel.getPluginConfiguration()).thenReturn(pluginConfigurationModel);
        when(pluginConfigurationModel.getBraintreeGeneralConfiguration()).thenReturn(generalConfigurationModel);
        when(generalConfigurationModel.isDropIn()).thenReturn(true);
        unit.beforeEditorAreaRender(widgetInstanceManager, braintreeHostedFieldsConfigurationModel);
        verify(notificationService).notifyUser(widgetInstanceManager, HOSTED_FIELDS_ACCOUNT_LIMITATION_DISABLED,
                NotificationEvent.Level.WARNING);
    }

}
