package com.braintree.graphql.commands.impl;

import com.braintree.command.result.BrainTreePaymentDetailsResult;
import com.braintree.hybris.data.graphql.commands.response.BraintreePaymentMethodData;
import com.braintree.payment.info.service.BraintreePaymentInfoService;
import com.braintreegateway.ValidationError;
import com.braintreegateway.ValidationErrorCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.payment.AdapterException;
import de.hybris.platform.servicelayer.dto.converter.Converter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    private static final String PAYMENT_METHOD_TOKEN = "paymentMethodToken";
    private static final String DATA_MAP_KEY = "data";
    private static final String DEFINITION_FILE_NAME = "node";
    private static final String ID_PARAMETER = "id";
    private static final String GRAPH_QL_TOKEN_FOR_PAYMENT_METHOD = "graphQLTokenForPaymentMethod";
    private static final String RESULT_ERRORS = "errors";
    private static final String ERROR_MESSAGE = "errorMessage";
    private static final String ERROR_ATTRIBUTE = "errorAttribute";
    private static final String ERROR_KEY_MAP = "error";

    @Mock
    private BraintreePaymentInfoService paymentInfoService;
    @Mock
    private Converter<BraintreePaymentMethodData, BrainTreePaymentDetailsResult> graphQLPaymentMethodToPaymentDetailsConverter;
    @Mock
    private ObjectMapper objectMapper;

    @Spy
    @InjectMocks
    private DefaultBrainTreeGraphQLGetPaymentMethodByTokenCommand command;

    @Captor
    private ArgumentCaptor<Map<String, Object>> variables;

    private Map<String, Object> mapResult;
    private Map<String, Object> nodeMap;

    @Before
    public void setUp() throws IOException {
        nodeMap = new HashMap<>();
        mapResult = new HashMap<>();
        mapResult.put(DATA_MAP_KEY, Map.of(DEFINITION_FILE_NAME, nodeMap));

        doReturn(mapResult).when(command).makeGraphQlCall(eq(DEFINITION_FILE_NAME), any());
        when(paymentInfoService.getGraphQLTokenForPaymentMethod(PAYMENT_METHOD_TOKEN)).thenReturn(GRAPH_QL_TOKEN_FOR_PAYMENT_METHOD);
    }

    @Test
    public void shouldCreateVariablesMap() throws IOException {
        //when
        command.perform(PAYMENT_METHOD_TOKEN);

        //then
        verify(command).makeGraphQlCall(eq(DEFINITION_FILE_NAME), variables.capture());
        assertEquals(GRAPH_QL_TOKEN_FOR_PAYMENT_METHOD, variables.getValue().get(ID_PARAMETER));

    }

    @Test
    public void shouldTranslateResponse() throws IOException {
        //given
        BrainTreePaymentDetailsResult paymentDetailsResult = new BrainTreePaymentDetailsResult();
        BraintreePaymentMethodData brainTreePaymentMethod = new BraintreePaymentMethodData();

        when(objectMapper.convertValue(nodeMap, BraintreePaymentMethodData.class)).thenReturn(brainTreePaymentMethod);
        when(graphQLPaymentMethodToPaymentDetailsConverter.convert(brainTreePaymentMethod)).thenReturn(paymentDetailsResult);

        //when
        BrainTreePaymentDetailsResult commandResult = command.perform(PAYMENT_METHOD_TOKEN);

        //then
        verify(command).makeGraphQlCall(eq(DEFINITION_FILE_NAME), any());
        assertNotNull(commandResult);
        assertEquals(paymentDetailsResult, commandResult);
    }

    @Test(expected = AdapterException.class)
    public void shouldThrowErrorWhenGraphQLResponseContainsError() throws IOException {
        //given
        ValidationError validationError =
                new ValidationError(ERROR_ATTRIBUTE, ValidationErrorCode.UNKNOWN_VALIDATION_ERROR, ERROR_MESSAGE);
        Map<String, Object> errorMap = Map.of(ERROR_KEY_MAP, validationError);
        List<Map<String, Object>> errorList = new ArrayList<>();
        errorList.add(errorMap);
        mapResult.put(RESULT_ERRORS, errorList);

        //when
        command.perform(PAYMENT_METHOD_TOKEN);

        //then
        verify(command).makeGraphQlCall(eq(DEFINITION_FILE_NAME), any());
    }

    @Test(expected = AdapterException.class)
    public void shouldThrowErrorWhenGraphQLCallFails() throws IOException {
        //given
        doThrow(IOException.class).when(command).makeGraphQlCall(any(), any());

        //when
        command.perform(PAYMENT_METHOD_TOKEN);

        //then
        verify(command).makeGraphQlCall(eq(DEFINITION_FILE_NAME), any());
    }

}
