package com.braintree.daos.impl;

import com.braintree.model.PaypalAccessTokenModel;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.servicelayer.exceptions.ModelNotFoundException;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
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.runners.MockitoJUnitRunner;

import java.util.Map;
import java.util.Optional;

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

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

    private static final String GUID = "GUID";
    private static final String GUID_KEY = "guid";

    private static final String FIND_ACCESS_TOKEN_BY_GUID_QUERY = "SELECT {pk} FROM {PaypalAccessToken} WHERE {guid} = ?guid";

    @Mock
    private FlexibleSearchService flexibleSearchService;

    @InjectMocks
    private DefaultBrainTreeAccessTokenDao unit;

    @Captor
    private ArgumentCaptor<FlexibleSearchQuery> flexibleSearchQueryCaptor;

    @Mock
    private PaypalAccessTokenModel paypalAccessToken;

    @Test
    public void shouldReturnPayPalAccessToken() {
        when(flexibleSearchService.searchUnique(any(FlexibleSearchQuery.class))).thenReturn(paypalAccessToken);

        Optional<PaypalAccessTokenModel> result = unit.findAccessTokenByGuid(GUID);

        assertTrue(result.isPresent());
        assertEquals(paypalAccessToken, result.get());
        verify(flexibleSearchService).searchUnique(flexibleSearchQueryCaptor.capture());
        FlexibleSearchQuery flexibleSearchQuery = flexibleSearchQueryCaptor.getValue();
        assertEquals(FIND_ACCESS_TOKEN_BY_GUID_QUERY, flexibleSearchQuery.getQuery());
        Map<String, Object> queryParameters = flexibleSearchQuery.getQueryParameters();
        assertTrue(queryParameters.containsKey(GUID_KEY));
        assertEquals(GUID, queryParameters.get(GUID_KEY));
    }

    @Test
    public void shouldReturnEmptyIfPayPalAccessTokenNotPresentForGivenGuid() {
        when(flexibleSearchService.searchUnique(any(FlexibleSearchQuery.class))).thenThrow(new ModelNotFoundException("ModelNotFoundException"));

        Optional<PaypalAccessTokenModel> result = unit.findAccessTokenByGuid(GUID);

        assertFalse(result.isPresent());
        verify(flexibleSearchService).searchUnique(flexibleSearchQueryCaptor.capture());
        FlexibleSearchQuery flexibleSearchQuery = flexibleSearchQueryCaptor.getValue();
        assertEquals(FIND_ACCESS_TOKEN_BY_GUID_QUERY, flexibleSearchQuery.getQuery());
        Map<String, Object> queryParameters = flexibleSearchQuery.getQueryParameters();
        assertTrue(queryParameters.containsKey(GUID_KEY));
        assertEquals(GUID, queryParameters.get(GUID_KEY));
    }
}