package com.braintree.customersupportbackoffice.editor;

import com.hybris.cockpitng.editors.EditorContext;
import com.hybris.cockpitng.editors.EditorListener;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.BraintreePayPalConfigurationModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Button;

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


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

    private static final String PARENT_OBJECT = "parentObject";

    @Mock
    private Component parent;
    @Mock
    private EditorContext<String> context;
    @Mock
    private EditorListener<String> listener;
    @Mock
    private BraintreePayPalConfigurationModel payPalConfigurationModel;
    @Mock
    private Button disabled;
    @Spy
    @InjectMocks
    private BrainTreePayPalConfigurationStringEditor editor;

    @Test
    public void shouldSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(false);
        when(parent.getFirstChild()).thenReturn(disabled);
        doNothing().when(disabled).setDisabled(true);
        doNothing().when(editor).callParentRender(parent, context, listener);

        editor.render(parent, context, listener);

        verify(parent).getFirstChild();
        verify(payPalConfigurationModel).isPayPalEnabled();
        verify(disabled).setDisabled(true);
    }

    @Test
    public void shouldNotSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(true);

        editor.disableConfig(parent, context);

        verify(parent, never()).getFirstChild();
        verify(payPalConfigurationModel).isPayPalEnabled();
        verify(disabled, never()).setDisabled(true);
    }

    private void setUpCommonBraintreeConfiguration(boolean isPayPalEnabled) {
        when(context.getParameter(PARENT_OBJECT)).thenReturn(payPalConfigurationModel);
        when(payPalConfigurationModel.isPayPalEnabled()).thenReturn(isPayPalEnabled);
    }
}
