package com.braintree.customersupportbackoffice.editor;

import com.braintree.enums.BraintreeThreeDSecureFlow;
import com.hybris.cockpitng.components.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.BraintreeGeneralConfigurationModel;
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.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 BrainTreeGeneralConfigurationEnumEditorTest {

    private static final String PARENT_OBJECT = "parentObject";
    private static final String BRAINTREE_THREEDSECURE_SCA_EXEMPTION = "braintreeThreeDSecureSCAExemption";

    @Mock
    private Editor parent;
    @Mock
    private EditorContext<Object> context;
    @Mock
    private EditorListener<Object> listener;
    @Mock
    private BraintreeGeneralConfigurationModel generalConfigurationModel;
    @Mock
    private Button disabled;
    @Spy
    @InjectMocks
    private BrainTreeGeneralConfigurationEnumEditor editor;

    @Test
    public void shouldSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(BraintreeThreeDSecureFlow.DISABLED);
        when(parent.getProperty()).thenReturn(BRAINTREE_THREEDSECURE_SCA_EXEMPTION);
        when(parent.getFirstChild()).thenReturn(disabled);
        doNothing().when(editor).callParentRender(parent, context, listener);

        editor.render(parent, context, listener);

        verify(parent).getFirstChild();
        verify(context).getParameter(PARENT_OBJECT);
        verify(disabled).setDisabled(true);
    }

    @Test
    public void shouldNotSetConfigToDisabled() {
        setUpCommonBraintreeConfiguration(BraintreeThreeDSecureFlow.ALWAYS);
        when(parent.getProperty()).thenReturn(BRAINTREE_THREEDSECURE_SCA_EXEMPTION);

        editor.disableConfig(parent, context);

        verify(parent, never()).getFirstChild();
        verify(context).getParameter(PARENT_OBJECT);
        verify(disabled, never()).setDisabled(true);
    }

    private void setUpCommonBraintreeConfiguration(BraintreeThreeDSecureFlow threeDSecureFlow) {
        when(context.getParameter(PARENT_OBJECT)).thenReturn(generalConfigurationModel);
        when(generalConfigurationModel.getBraintreeThreeDSecureFlow()).thenReturn(threeDSecureFlow);
    }
}
