<commit_before>/*
 * Grakn - A Distributed Semantic Database
 * Copyright (C) 2016  Grakn Labs Limited
 *
 * Grakn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Grakn is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

package ai.grakn.client;

import ai.grakn.Grakn;
import ai.grakn.GraknGraph;
import ai.grakn.GraknSession;
import ai.grakn.GraknTxType;
import ai.grakn.concept.EntityType;
import ai.grakn.concept.ResourceType;
import ai.grakn.graql.Graql;
import ai.grakn.graql.InsertQuery;
import ai.grakn.test.EngineContext;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

import static ai.grakn.graql.Graql.var;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.argThat;
import static java.util.stream.Stream.generate;
import static org.mockito.Mockito.*;

public class LoaderClientTest {

    private GraknSession session;

    @ClassRule
    public static final EngineContext engine = EngineContext.startInMemoryServer();

    @Before
    public void setupSession(){
        this.session = engine.factoryWithNewKeyspace();

    }

    @Test
    public void whenSingleQueryLoadedAndTaskCompletionFunctionThrowsError_ErrorIsLogged(){
        // Mock the Logger
        Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(LoaderClient.class);
        Appender<ILoggingEvent> mockAppender = mock(Appender.class);
        root.addAppender(mockAppender);

        // Create a LoaderClient with a callback that will fail
        LoaderClient loader = loader();
        loader.setTaskCompletionConsumer((json) -> assertTrue("Testing Log failure",false));

        // Load some queries
        generate(this::query).limit(1).forEach(loader::add);

        // Wait for queries to finish
        loader.waitToFinish();

        // Verify that the logger received the failed log message
        verify(mockAppender).doAppend(argThat(argument -> argument.getFormattedMessage().contains("error in callback")));
    }

    @Test
    public void whenSingleQueryLoaded_TaskCompletionExecutesExactlyOnce(){
        AtomicInteger tasksCompleted = new AtomicInteger(0);

        // Create a LoaderClient with a callback that will fail
        LoaderClient loader = loader();
        loader.setTaskCompletionConsumer((json) -> tasksCompleted.incrementAndGet());

        // Load some queries
        generate(this::query).limit(1).forEach(loader::add);

        // Wait for queries to finish
        loader.waitToFinish();

        // Verify that the logger received the failed log message
        assertEquals(1, tasksCompleted.get());
    }
<commit_msg>Remove logger mock in LoaderClientTest (#1569)<commit_after>/*
 * Grakn - A Distributed Semantic Database
 * Copyright (C) 2016  Grakn Labs Limited
 *
 * Grakn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Grakn is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

package ai.grakn.client;

import ai.grakn.Grakn;
import ai.grakn.GraknGraph;
import ai.grakn.GraknSession;
import ai.grakn.GraknTxType;
import ai.grakn.concept.EntityType;
import ai.grakn.concept.ResourceType;
import ai.grakn.graql.Graql;
import ai.grakn.graql.InsertQuery;
import ai.grakn.test.EngineContext;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

import static ai.grakn.graql.Graql.var;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.argThat;
import static java.util.stream.Stream.generate;
import static org.mockito.Mockito.*;

public class LoaderClientTest {

    private GraknSession session;

    @Rule
    public final SystemOutRule systemOut = new SystemOutRule().enableLog();

    @ClassRule
    public static final EngineContext engine = EngineContext.startInMemoryServer();

    @Before
    public void setupSession(){
        this.session = engine.factoryWithNewKeyspace();
    }

    @Test
    public void whenSingleQueryLoadedAndTaskCompletionFunctionThrowsError_ErrorIsLogged(){
        // Create a LoaderClient with a callback that will fail
        LoaderClient loader = loader();
        loader.setTaskCompletionConsumer((json) -> assertTrue("Testing Log failure",false));

        // Load some queries
        generate(this::query).limit(1).forEach(loader::add);

        // Wait for queries to finish
        loader.waitToFinish();

        // Verify that the logger received the failed log message
        assertThat(systemOut.getLog(), containsString("error in callback"));
    }

    @Test
    public void whenSingleQueryLoaded_TaskCompletionExecutesExactlyOnce(){
        AtomicInteger tasksCompleted = new AtomicInteger(0);

        // Create a LoaderClient with a callback that will fail
        LoaderClient loader = loader();
        loader.setTaskCompletionConsumer((json) -> tasksCompleted.incrementAndGet());

        // Load some queries
        generate(this::query).limit(1).forEach(loader::add);

        // Wait for queries to finish
        loader.waitToFinish();

        // Verify that the logger received the failed log message
        assertEquals(1, tasksCompleted.get());
    }

{'fraction_non_alphanumeric': 0.089573732718894, 'fraction_numerical': 0.0033122119815668202, 'mean_word_length': 3.7797660013764625, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2495435', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1878, 'n_words': 578}