package com.braintree.populators;

import com.braintree.command.result.BrainTreeRefundTransactionResult;
import com.braintree.hybris.data.graphql.commands.request.BrainTreeExchangeLegacyIdRequest;
import com.braintree.hybris.data.graphql.commands.response.BrainTreeMonetaryAmount;
import com.braintree.hybris.data.graphql.commands.response.BrainTreeRefund;
import com.braintree.method.BrainTreePaymentService;
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.runners.MockitoJUnitRunner;

import java.math.BigDecimal;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

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

    private static final String REFUND_ID = "RefundId";
    private static final String AMOUNT = "100";
    private static final String EXCHANGE_LEGACY_ID_TO_GRAPH_QL_ID = "ExchangeLegacyIdToGraphQLId";

    @InjectMocks
    private BraintreeRefundToBrainTreeRefundTransactionResultPopulator unit;
    @Mock
    private BrainTreePaymentService brainTreePaymentService;
    @Mock
    private BrainTreeRefund brainTreeRefund;
    @Mock
    private BrainTreeMonetaryAmount brainTreeMonetaryAmount;

    @Test
    public void shouldPopulateBraintreeRefundToBrainTreeRefundTransactionResultTest(){
        BrainTreeRefundTransactionResult refundTransactionResult = new BrainTreeRefundTransactionResult();

        when(brainTreeRefund.getId()).thenReturn(REFUND_ID);
        when(brainTreePaymentService.exchangeLegacyIdToGraphQLId(any(BrainTreeExchangeLegacyIdRequest.class))).thenReturn(EXCHANGE_LEGACY_ID_TO_GRAPH_QL_ID);
        when(brainTreeRefund.getAmount()).thenReturn(brainTreeMonetaryAmount);
        when(brainTreeMonetaryAmount.getValue()).thenReturn(AMOUNT);

        unit.populate(brainTreeRefund, refundTransactionResult);

        assertEquals(REFUND_ID, refundTransactionResult.getTransactionId());
        assertEquals(EXCHANGE_LEGACY_ID_TO_GRAPH_QL_ID, refundTransactionResult.getTransactionGraphQLId());
        assertEquals(new BigDecimal(AMOUNT), refundTransactionResult.getAmount());
    }
}
