package com.braintree.customersupportbackoffice.handler;

import com.hybris.cockpitng.dataaccess.facades.object.exceptions.ObjectSavingException;
import de.hybris.bootstrap.annotations.UnitTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

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

    private static final String BRAIN_TREE_GENERAL_CONFIGURATION_VALIDATION_INTERCEPTOR =
            "BrainTreeGeneralConfigurationValidationInterceptor";
    private static final String TEST_OBJECT_ID = "id";
    private static final String TEST_STRING = "string:substring";
    private static final String TEST_SUBSTRING = "substring";
    private static final String NOT_CORRECT_STRING = "not correct string";

    @InjectMocks
    private BrainTreeGeneralConfigurationValidationTranslationHandler unit;

    @Test
    public void shouldReturnTrueWhenArgumentIsInstanceOfObjectContainsSpecifiedString() {
        Throwable throwable = new Throwable(BRAIN_TREE_GENERAL_CONFIGURATION_VALIDATION_INTERCEPTOR);
        ObjectSavingException objectSavingException = new ObjectSavingException(TEST_OBJECT_ID, throwable);

        Assert.assertTrue(unit.canHandle(objectSavingException));
    }

    @Test
    public void shouldReturnFalseWhenArgumentIsInstanceOfObjectButContainsNotCorrectStringString() {
        Throwable throwable = new Throwable(NOT_CORRECT_STRING);
        ObjectSavingException objectSavingException = new ObjectSavingException(TEST_OBJECT_ID, throwable);

        Assert.assertFalse(unit.canHandle(objectSavingException));
    }

    @Test
    public void shouldReturnFalseWhenArgumentIsNoInstanceOfObjectContainsSpecifiedString() {
        Assert.assertFalse(unit.canHandle(null));
    }

    @Test
    public void shouldReturnSubstringStartedFromColonSymbol() {
        Throwable throwable = new Throwable(TEST_STRING);
        ObjectSavingException objectSavingException = new ObjectSavingException(TEST_OBJECT_ID, throwable);

        Assert.assertEquals(TEST_SUBSTRING, unit.toString(objectSavingException));
    }
}
