package com.braintree.customfield.service.impl;

import com.braintree.constants.BraintreeConstants.PropertyConfiguration;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.servicelayer.config.ConfigurationService;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;

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

    public static final String KEY_1 = "key_1";
    public static final String KEY_2 = "key_2";

    @Mock
    private Configuration configuration;

    @Mock
    private ConfigurationService configurationService;

    @InjectMocks
    private DefaultBraintreeCustomFieldsService unit;

    @Test
    public void shouldGetDefaultCustomFieldsMap() {
        final List<String> list = Arrays.asList(KEY_1, KEY_2);

        when(configuration.getKeys(PropertyConfiguration.BRAINTRE_CUSTOM_FIELD_GENERAL_KEY)).thenReturn(list.iterator());
        when(configurationService.getConfiguration()).thenReturn(configuration);

        unit.getDefaultCustomFieldsMap();
        verify(configuration).getString(KEY_1);
        verify(configuration).getString(KEY_2);
    }

    @Test
    public void shouldGetEmptyDefaultCustomFieldsMap() {
        final List<String> list = Arrays.asList(KEY_1);

        when(configuration.getKeys(PropertyConfiguration.BRAINTRE_CUSTOM_FIELD_GENERAL_KEY)).thenReturn(list.iterator());
        when(configurationService.getConfiguration()).thenReturn(configuration);
        when(configuration.getString(KEY_1)).thenReturn("");

        final Map<String, String> result = unit.getDefaultCustomFieldsMap();

        assertTrue(result.isEmpty());
        verify(configuration).getKeys(PropertyConfiguration.BRAINTRE_CUSTOM_FIELD_GENERAL_KEY);
        verify(configurationService, times(2)).getConfiguration();
        verify(configuration).getString(KEY_1);
    }

}
