<commit_before>package org.ovirt.engine.core.searchbackend;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Arrays;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.ovirt.engine.core.common.businessentities.Tags;
import org.ovirt.engine.core.common.config.Config;
import org.ovirt.engine.core.common.config.ConfigCommon;
import org.ovirt.engine.core.common.config.ConfigValues;
import org.ovirt.engine.core.common.config.IConfigUtilsInterface;
import org.ovirt.engine.core.common.interfaces.ITagsHandler;
import org.ovirt.engine.core.utils.MockConfigRule;

public class SyntaxCheckerTest {

    private final static String TAG_NAME = "'tag1'";
    private final static String TAG_NAME_WITH_CHILDREN = "'tag1','all'";

    @Rule
    public MockConfigRule mcr = new MockConfigRule();

    public boolean contains(SyntaxContainer res, String item) {
        return Arrays.asList(res.getCompletionArray()).contains(item);
    }

    @Before
    public void setup() {
        final IConfigUtilsInterface configUtils = mock(IConfigUtilsInterface.class);
        when(configUtils.getValue(ConfigValues.SearchResultsLimit, ConfigCommon.defaultConfigurationVersion))
                .thenReturn(100);
        when(configUtils.getValue(ConfigValues.DBPagingType, ConfigCommon.defaultConfigurationVersion))
                .thenReturn("Range");
        when(configUtils.getValue(ConfigValues.DBSearchTemplate, ConfigCommon.defaultConfigurationVersion))
                .thenReturn("SELECT * FROM (%2$s) %1$s) as T1 %3$s");
        when(configUtils.getValue(ConfigValues.DBPagingSyntax, ConfigCommon.defaultConfigurationVersion))
                .thenReturn("OFFSET (%1$s -1) LIMIT %2$s");
        when(configUtils.getValue(ConfigValues.PgMajorRelease, ConfigCommon.defaultConfigurationVersion))
                .thenReturn(9);
        when(configUtils.getValue(ConfigValues.DBI18NPrefix, ConfigCommon.defaultConfigurationVersion))
        .thenReturn("");
        BaseConditionFieldAutoCompleter.tagsHandler = mock(ITagsHandler.class);
        Tags tags = new Tags();
        tags.setTagName(TAG_NAME);
        when(BaseConditionFieldAutoCompleter.tagsHandler.getTagByTagName(anyString())).thenReturn(tags);
        when(BaseConditionFieldAutoCompleter.tagsHandler.getTagNamesAndChildrenNamesByRegExp(anyString()))
                .thenReturn(TAG_NAME_WITH_CHILDREN);

        Config.setConfigUtils(configUtils);
    }

    /**
     * Test the following where each word should be the completion for the earlier portion Vms : Events =
     */
    @Test
    public void testVMCompletion() {
        SyntaxChecker chkr = new SyntaxChecker(20);
        SyntaxContainer res = chkr.getCompletion("");
        assertTrue("Vms", contains(res, "Vms"));
        res = chkr.getCompletion("V");
        assertTrue("Vms2", contains(res, "Vms"));
        res = chkr.getCompletion("Vms");
        assertTrue(":", contains(res, ":"));
        res = chkr.getCompletion("Vms : ");
        assertTrue("Events", contains(res, "Events"));
        res = chkr.getCompletion("Vms : Events");
        assertTrue("=", contains(res, "="));
    }

    /**<commit_msg>core: SyntaxCheckerTest MockConfigRule<commit_after>package org.ovirt.engine.core.searchbackend;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.ovirt.engine.core.utils.MockConfigRule.mockConfig;

import java.util.Arrays;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.ovirt.engine.core.common.businessentities.Tags;
import org.ovirt.engine.core.common.config.ConfigValues;
import org.ovirt.engine.core.common.interfaces.ITagsHandler;
import org.ovirt.engine.core.utils.MockConfigRule;

public class SyntaxCheckerTest {

    private final static String TAG_NAME = "'tag1'";
    private final static String TAG_NAME_WITH_CHILDREN = "'tag1','all'";

    @Rule
    public MockConfigRule mcr = new MockConfigRule(
            mockConfig(ConfigValues.SearchResultsLimit, 100),
            mockConfig(ConfigValues.DBPagingType, "Range"),
            mockConfig(ConfigValues.DBSearchTemplate, "SELECT * FROM (%2$s) %1$s) as T1 %3$s"),
            mockConfig(ConfigValues.DBPagingSyntax, "OFFSET (%1$s -1) LIMIT %2$s"),
            mockConfig(ConfigValues.PgMajorRelease, 9),
            mockConfig(ConfigValues.DBI18NPrefix, "")
    );

    public boolean contains(SyntaxContainer res, String item) {
        return Arrays.asList(res.getCompletionArray()).contains(item);
    }

    @Before
    public void setup() {
        BaseConditionFieldAutoCompleter.tagsHandler = mock(ITagsHandler.class);
        Tags tags = new Tags();
        tags.setTagName(TAG_NAME);
        when(BaseConditionFieldAutoCompleter.tagsHandler.getTagByTagName(anyString())).thenReturn(tags);
        when(BaseConditionFieldAutoCompleter.tagsHandler.getTagNamesAndChildrenNamesByRegExp(anyString()))
                .thenReturn(TAG_NAME_WITH_CHILDREN);
    }

    /**
     * Test the following where each word should be the completion for the earlier portion Vms : Events =
     */
    @Test
    public void testVMCompletion() {
        SyntaxChecker chkr = new SyntaxChecker(20);
        SyntaxContainer res = chkr.getCompletion("");
        assertTrue("Vms", contains(res, "Vms"));
        res = chkr.getCompletion("V");
        assertTrue("Vms2", contains(res, "Vms"));
        res = chkr.getCompletion("Vms");
        assertTrue(":", contains(res, ":"));
        res = chkr.getCompletion("Vms : ");
        assertTrue("Events", contains(res, "Events"));
        res = chkr.getCompletion("Vms : Events");
        assertTrue("=", contains(res, "="));
    }

    /**
{'fraction_non_alphanumeric': 0.1181967213114754, 'fraction_numerical': 0.005901639344262295, 'mean_word_length': 3.7963836477987423, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6546989', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1757, 'n_words': 321}