package com.braintree.customersupportbackoffice.widgets.braintreePluginConfiguration.importConfiguration;

import de.hybris.bootstrap.annotations.UnitTest;
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.bind.BindContext;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.UploadEvent;

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

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

    private static final String JAVA = "java";
    private static final String ZIP = "zip";

    @Mock
    BindContext ctx;
    @Mock
    UploadEvent event;
    @Mock
    Event wrongEvent;
    @Spy
    @InjectMocks
    private BraintreeImportConfigurationController configurationController;


    @Test
    public void shouldNotImportConfigurationWhenWrongEventTriggered() {
        when(ctx.getTriggerEvent()).thenReturn(wrongEvent);

        configurationController.onFileUpload(ctx);

        verify(configurationController, never()).createWrongExtensionMessage();
        verify(configurationController, never()).createApplyConfigurationPopup(event);

    }

    @Test
    public void whenFileHasWrongExtension() {
        when(ctx.getTriggerEvent()).thenReturn(event);
        doNothing().when(configurationController).createWrongExtensionMessage();
        doReturn(false).when(configurationController).isMediaInZipExtension(event);

        configurationController.onFileUpload(ctx);

        verify(configurationController).isMediaInZipExtension(event);
        verify(configurationController).createWrongExtensionMessage();
        verify(configurationController, never()).createApplyConfigurationPopup(event);
    }

    @Test
    public void shouldImportConfiguration() {
        when(ctx.getTriggerEvent()).thenReturn(event);
        doReturn(true).when(configurationController).isMediaInZipExtension(event);
        doNothing().when(configurationController).createApplyConfigurationPopup(event);

        configurationController.onFileUpload(ctx);

        verify(configurationController, never()).createWrongExtensionMessage();
        verify(configurationController).createApplyConfigurationPopup(event);
        verify(configurationController).isMediaInZipExtension(event);
    }
}
