package com.braintree.service;

import com.braintree.commands.impl.DefaultBrainTreeConnectWithPayPalExchangeCodeToAccessTokenCommand;
import com.braintree.commands.impl.DefaultBrainTreeConnectWithPayPalGetUserInformationCommand;
import com.braintree.commands.impl.DefaultBrainTreeGetPayPalAccessTokenCommand;
import com.braintree.exceptions.BrainTreeConnectWithPayPalException;
import com.braintree.hybris.data.BrainTreeConnectWithPayPalAccessTokenData;
import com.braintree.hybris.data.BrainTreeConnectWithPayPalUserData;
import com.braintree.hybris.data.BrainTreeGetPayPalAccessTokenData;
import com.braintree.service.impl.DefaultBrainTreeConnectWithPayPalService;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.payment.AdapterException;
import de.hybris.platform.payment.commands.factory.CommandFactory;
import de.hybris.platform.payment.commands.factory.CommandFactoryRegistry;
import de.hybris.platform.payment.commands.factory.CommandNotSupportedException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static com.braintree.constants.BraintreeConstants.BRAINTREE_PROVIDER_NAME;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;


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

    private static final String CODE = "CODE";
    private static final String TOKEN = "TOKEN";

    @Mock
    private CommandFactoryRegistry commandFactoryRegistry;
    @Mock
    private CommandFactory commandFactory;

    @InjectMocks
    private DefaultBrainTreeConnectWithPayPalService unit;

    @Test
    public void shouldGetAccessToken() throws CommandNotSupportedException {
        final DefaultBrainTreeGetPayPalAccessTokenCommand command =
                mock(DefaultBrainTreeGetPayPalAccessTokenCommand.class);
        final BrainTreeGetPayPalAccessTokenData data = mock(BrainTreeGetPayPalAccessTokenData.class);

        when(commandFactoryRegistry.getFactory(BRAINTREE_PROVIDER_NAME)).thenReturn(commandFactory);
        when(commandFactory.createCommand(DefaultBrainTreeGetPayPalAccessTokenCommand.class)).thenReturn(command);
        when(command.perform(null)).thenReturn(data);
        when(data.getAccessToken()).thenReturn(TOKEN);

        final String result = unit.getAccessToken();

        assertEquals(TOKEN, result);
        verify(commandFactoryRegistry).getFactory(BRAINTREE_PROVIDER_NAME);
        verify(commandFactory).createCommand(DefaultBrainTreeGetPayPalAccessTokenCommand.class);
        verify(command).perform(null);
        verify(data).getAccessToken();
    }

    @Test(expected = BrainTreeConnectWithPayPalException.class)
    public void shouldThrowErrorForGetAccessToken() throws CommandNotSupportedException {
        final DefaultBrainTreeGetPayPalAccessTokenCommand command =
                mock(DefaultBrainTreeGetPayPalAccessTokenCommand.class);

        when(commandFactoryRegistry.getFactory(BRAINTREE_PROVIDER_NAME)).thenReturn(commandFactory);
        when(commandFactory.createCommand(DefaultBrainTreeGetPayPalAccessTokenCommand.class))
                .thenThrow(new CommandNotSupportedException());

        unit.getAccessToken();

        verify(commandFactoryRegistry).getFactory(BRAINTREE_PROVIDER_NAME);
        verify(commandFactory).createCommand(DefaultBrainTreeGetPayPalAccessTokenCommand.class);
        verify(command, never()).perform(null);
    }

    @Test
    public void shouldExchangeAuthorizationCodeToAccessToken() throws CommandNotSupportedException {
        final DefaultBrainTreeConnectWithPayPalExchangeCodeToAccessTokenCommand command =
                mock(DefaultBrainTreeConnectWithPayPalExchangeCodeToAccessTokenCommand.class);
        final BrainTreeConnectWithPayPalAccessTokenData data = mock(BrainTreeConnectWithPayPalAccessTokenData.class);

        when(commandFactoryRegistry.getFactory(BRAINTREE_PROVIDER_NAME)).thenReturn(commandFactory);
        when(commandFactory.createCommand(DefaultBrainTreeConnectWithPayPalExchangeCodeToAccessTokenCommand.class)).thenReturn(command);
        when(command.perform(CODE)).thenReturn(data);
        when(data.getAccessToken()).thenReturn(TOKEN);

        final String result = unit.exchangeAuthorizationCodeToAccessToken(CODE);

        assertEquals(TOKEN, result);
        verify(commandFactoryRegistry).getFactory(BRAINTREE_PROVIDER_NAME);
        verify(commandFactory).createCommand(DefaultBrainTreeConnectWithPayPalExchangeCodeToAccessTokenCommand.class);
        verify(command).perform(CODE);
        verify(data).getAccessToken();
    }

    @Test(expected = BrainTreeConnectWithPayPalException.class)
    public void shouldThrowErrorForExchangeAuthorizationCodeToAccessToken() throws CommandNotSupportedException {
        final DefaultBrainTreeConnectWithPayPalExchangeCodeToAccessTokenCommand command =
                mock(DefaultBrainTreeConnectWithPayPalExchangeCodeToAccessTokenCommand.class);

        when(commandFactoryRegistry.getFactory(BRAINTREE_PROVIDER_NAME)).thenReturn(commandFactory);
        when(commandFactory.createCommand(DefaultBrainTreeConnectWithPayPalExchangeCodeToAccessTokenCommand.class))
                .thenThrow(new CommandNotSupportedException());

        unit.exchangeAuthorizationCodeToAccessToken(CODE);

        verify(commandFactoryRegistry).getFactory(BRAINTREE_PROVIDER_NAME);
        verify(commandFactory).createCommand(DefaultBrainTreeConnectWithPayPalExchangeCodeToAccessTokenCommand.class);
        verify(command, never()).perform(CODE);
    }

    @Test
    public void shouldGetUserDataByAccessToken() throws CommandNotSupportedException {
        final DefaultBrainTreeConnectWithPayPalGetUserInformationCommand command =
                mock(DefaultBrainTreeConnectWithPayPalGetUserInformationCommand.class);
        final BrainTreeConnectWithPayPalUserData data = mock(BrainTreeConnectWithPayPalUserData.class);

        when(commandFactoryRegistry.getFactory(BRAINTREE_PROVIDER_NAME)).thenReturn(commandFactory);
        when(commandFactory.createCommand(DefaultBrainTreeConnectWithPayPalGetUserInformationCommand.class)).thenReturn(command);
        when(command.perform(TOKEN)).thenReturn(data);

        final BrainTreeConnectWithPayPalUserData result = unit.getUserDataByAccessToken(TOKEN);

        assertEquals(data, result);
        verify(commandFactoryRegistry).getFactory(BRAINTREE_PROVIDER_NAME);
        verify(commandFactory).createCommand(DefaultBrainTreeConnectWithPayPalGetUserInformationCommand.class);
        verify(command).perform(TOKEN);
    }

    @Test(expected = AdapterException.class)
    public void shouldThrowErrorForGetUserDataByAccessToken() throws CommandNotSupportedException {
        final DefaultBrainTreeConnectWithPayPalGetUserInformationCommand command =
                mock(DefaultBrainTreeConnectWithPayPalGetUserInformationCommand.class);

        when(commandFactoryRegistry.getFactory(BRAINTREE_PROVIDER_NAME)).thenReturn(commandFactory);
        when(commandFactory.createCommand(DefaultBrainTreeConnectWithPayPalGetUserInformationCommand.class))
                .thenThrow(new CommandNotSupportedException());

        unit.getUserDataByAccessToken(TOKEN);

        verify(commandFactoryRegistry).getFactory(BRAINTREE_PROVIDER_NAME);
        verify(commandFactory).createCommand(DefaultBrainTreeConnectWithPayPalGetUserInformationCommand.class);
        verify(command, never()).perform(TOKEN);
    }

}
