package com.braintree.customersupportbackoffice.actions.transaction.refresh;

import com.braintree.customersupportbackoffice.facade.BrainTreeCustomerSupportFacade;
import com.braintree.hybris.data.BraintreeTransactionEntryData;
import com.braintree.model.BrainTreeTransactionDetailModel;
import com.hybris.cockpitng.actions.ActionContext;
import com.hybris.cockpitng.actions.ActionResult;
import com.hybris.cockpitng.core.impl.DefaultWidgetModel;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.servicelayer.dto.converter.Converter;
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.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class BrainTreeRefreshTransactionActionTest {
    private static final String ID = "id";
    private static final String PARENT_WIDGET_MODEL = "parentWidgetModel";
    private static final String CURRENT_OBJECT = "currentObject";

    @Mock
    private BrainTreeCustomerSupportFacade brainTreeCustomerSupportFacade;
    @Mock
    private Converter<BraintreeTransactionEntryData, BrainTreeTransactionDetailModel> transactionDetailPopulator;


    @InjectMocks
    private BrainTreeRefreshTransactionAction unit;

    @Mock
    private ActionContext<BrainTreeTransactionDetailModel> actionContext;
    @Mock
    private BrainTreeTransactionDetailModel transactionDetail;
    @Mock
    private DefaultWidgetModel defaultWidget;

    @Test
    public void shouldSuccessfullyPerform() {
        BraintreeTransactionEntryData transactionEntryData = new BraintreeTransactionEntryData();

        when(actionContext.getData()).thenReturn(transactionDetail);
        when(actionContext.getParameter(PARENT_WIDGET_MODEL)).thenReturn(defaultWidget);
        when(transactionDetail.getId()).thenReturn(ID);
        when(brainTreeCustomerSupportFacade.findTransaction(ID)).thenReturn(transactionEntryData);
        when(transactionDetailPopulator.convert(transactionEntryData)).thenReturn(transactionDetail);

        ActionResult<BrainTreeTransactionDetailModel> result = unit.perform(actionContext);

        assertEquals(ActionResult.SUCCESS, result.getResultCode());
        verify(defaultWidget).put(CURRENT_OBJECT, transactionDetail);
    }

    @Test
    public void shouldReturnTrueIfRefreshActionPossible() {
        boolean result = unit.canPerform(actionContext);

        assertTrue(result);
    }

}