package com.braintreeocc.helper;

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commercefacades.order.OrderFacade;
import de.hybris.platform.commercefacades.order.data.OrderHistoryData;
import de.hybris.platform.commerceservices.search.pagedata.PageableData;
import de.hybris.platform.commerceservices.search.pagedata.SearchPageData;
import de.hybris.platform.webservicescommons.mapping.DataMapper;
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 static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;

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

    private static final String STATUSES = "statuses";
    private static final int CURRENT_PAGE = 1;
    private static final int PAGE_SIZE = 1;
    private static final String SORT = "sort";
    private static final String FIELDS = "fields";

    @Mock
    private OrderFacade orderFacade;
    @Mock
    private DataMapper dataMapper;
    @Mock
    private SearchPageData<OrderHistoryData> result;

    @Spy
    @InjectMocks
    private BraintreeOrdersHelper unit;

    @Test
    public void searchOrderHistoryWhenStatusesAreNotNull() {
        when(orderFacade.getPagedOrderHistoryForStatuses(any(PageableData.class), any())).thenReturn(result);
        doReturn(dataMapper).when(unit).getDataMapper();

        unit.searchOrderHistory(STATUSES, CURRENT_PAGE, PAGE_SIZE, SORT, FIELDS);

        verify(orderFacade).getPagedOrderHistoryForStatuses(any(PageableData.class),  any());
        verify(unit).getDataMapper();
    }

    @Test
    public void searchOrderHistoryWhenStatusesAreNull() {
        when(orderFacade.getPagedOrderHistoryForStatuses(any(PageableData.class))).thenReturn(result);
        doReturn(dataMapper).when(unit).getDataMapper();

        unit.searchOrderHistory(null, CURRENT_PAGE, PAGE_SIZE, SORT, FIELDS);

        verify(orderFacade).getPagedOrderHistoryForStatuses(any(PageableData.class));
        verify(unit).getDataMapper();
    }
}
