package com.braintree.order.service.impl;

import com.braintree.model.BrainTreePaymentInfoModel;
import com.braintree.order.dao.BrainTreeOrderDao;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.model.order.AbstractOrderModel;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.payment.model.PaymentTransactionModel;
import de.hybris.platform.servicelayer.session.SessionService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    private static final String TRANSACTION_ID = "testId";
    private static final String ORDER_CODE = "orderCode";

    @Mock
    private SessionService sessionService;
    @Mock
    private BrainTreeOrderDao brainTreeOrderDao;
    @InjectMocks
    private DefaultBraintreeOrderRetrievalService unit;

    @Test
    public void getOrderCodeForTransactionShouldReturnExceptedOrderCode() {
        final PaymentTransactionModel transactionModel = Mockito.mock(PaymentTransactionModel.class);
        final AbstractOrderModel orderModel = Mockito.mock(AbstractOrderModel.class);
        when(orderModel.getCode()).thenReturn(ORDER_CODE);
        when(transactionModel.getOrder()).thenReturn(orderModel);
        when(sessionService.executeInLocalView(any())).thenReturn(transactionModel);

        Assert.assertEquals(ORDER_CODE, unit.getOrderCodeForTransaction(TRANSACTION_ID));
    }

    @Test
    public void getOrderForTransactionShouldReturnNullWhenTransactionModelNull() {
        when(sessionService.executeInLocalView(any())).thenReturn(null);
        Assert.assertNull(unit.getOrderCodeForTransaction(TRANSACTION_ID));
    }

    @Test
    public void getOrderCodeForTransactionShouldReturnNullWhenTransactionModelNull() {
        when(sessionService.executeInLocalView(any())).thenReturn(null);
        Assert.assertNull(unit.getOrderForTransaction(TRANSACTION_ID));
    }

    @Test
    public void testGetTransactionForIdShouldReturnTransaction() {
        unit.getTransactionForId(TRANSACTION_ID);
        verify(sessionService).executeInLocalView(any());
    }

    @Test
    public void testСountOrdersWithExpiredTransactions() {
        BrainTreePaymentInfoModel testPaymentInfo = new BrainTreePaymentInfoModel();
        int testExpirationDays = 0;
        unit.countOrdersWithExpiredTransactions(testPaymentInfo, testExpirationDays);
        verify(brainTreeOrderDao).countAllOrdersWithExpiredTransactions(testPaymentInfo, testExpirationDays);
    }

    @Test
    public void testСountOrdersWithExpiredTransactionsByCustomer() {
        CustomerModel customerModel = new CustomerModel();
        int testExpirationDays = 0;
        unit.countOrdersWithExpiredTransactionsByCustomer(customerModel, testExpirationDays);
        verify(brainTreeOrderDao).countOrdersWithExpiredTransactionsByCustomer(customerModel, testExpirationDays);
    }

}
