<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 */
package org.searchisko.api.testtools;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.mockito.Mockito;
import org.searchisko.api.service.SearchClientService;

/**
 * Base class for unit tests which need to run some test against ElasticSearch cluster. You can use next pattern in your
 * unit test method to obtain testing client connected to in-memory ES cluster without any data.
 * 
 * <pre>
 * try{
 *   Client client = prepareESClientForUnitTest();
 * 
 *   ... your unit test code here
 * 
 * } finally {
 *   finalizeESClientForUnitTest();
 * }
 * </pre>
 * 
 * Or you can use
 * 
 * <pre>
 * try{
 *   SearchClientService scService = prepareSearchClientServiceMock();
 * 
 *   ... your unit test code here
 * 
 * } finally {
 *   finalizeESClientForUnitTest();
 * }
 * </pre>
 * 
 * @author <NAME> (velias at redhat dot com)
 */
public abstract class ESRealClientTestBase {

	private Client client;

	private Node node;

	private File tempFolder;

	/**
	 * Prepare SearchClientService against in-memory EC cluster for unit test. Do not forgot to call
	 * {@link #finalizeESClientForUnitTest()} at the end of test!
	 * 
	 * @return
	 */
	public final SearchClientService prepareSearchClientServiceMock() {
		SearchClientService ret = Mockito.mock(SearchClientService.class);
		if (client == null)
			prepareESClientForUnitTest();
		Mockito.when(ret.getClient()).thenReturn(client);
		return ret;
	}

	/**
	 * Prepare ES in-memory client for unit test. Do not forgot to call {@link #finalizeESClientForUnitTest()} at the end
	 * of test!
	 * 
	 * @return
	 * @throws Exception
	 */
	public final Client prepareESClientForUnitTest() {
		if (client != null)
			return client;
		try {
			// For unit tests it is recommended to use local node.
			// This is to ensure that your node will never join existing cluster on the network.

			// path.data location
			tempFolder = new File("tmp");
			String tempFolderName = tempFolder.getCanonicalPath();

			if (tempFolder.exists()) {
				FileUtils.deleteDirectory(tempFolder);
			}<commit_msg>Create ES Client for JUnit tests in temporary directory not in actual dir. It avoid problems with deleting this directory on NFS mounted drives e.g. Jenkin's workspace can be mounted to NFS and causes problems with deleting such dir.<commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 */
package org.searchisko.api.testtools;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.FileUtils;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.mockito.Mockito;
import org.searchisko.api.service.SearchClientService;

/**
 * Base class for unit tests which need to run some test against ElasticSearch cluster. You can use next pattern in your
 * unit test method to obtain testing client connected to in-memory ES cluster without any data.
 * 
 * <pre>
 * try{
 *   Client client = prepareESClientForUnitTest();
 * 
 *   ... your unit test code here
 * 
 * } finally {
 *   finalizeESClientForUnitTest();
 * }
 * </pre>
 * 
 * Or you can use
 * 
 * <pre>
 * try{
 *   SearchClientService scService = prepareSearchClientServiceMock();
 * 
 *   ... your unit test code here
 * 
 * } finally {
 *   finalizeESClientForUnitTest();
 * }
 * </pre>
 * 
 * @author <NAME> (velias at redhat dot com)
 */
public abstract class ESRealClientTestBase {

	private Client client;

	private Node node;

	private File tempFolder;

	protected Logger log = Logger.getLogger(ESRealClientTestBase.class.getName());

	/**
	 * Prepare SearchClientService against in-memory EC cluster for unit test. Do not forgot to call
	 * {@link #finalizeESClientForUnitTest()} at the end of test!
	 * 
	 * @return
	 */
	public final SearchClientService prepareSearchClientServiceMock() {
		SearchClientService ret = Mockito.mock(SearchClientService.class);
		if (client == null)
			prepareESClientForUnitTest();
		Mockito.when(ret.getClient()).thenReturn(client);
		return ret;
	}

	/**
	 * Prepare ES in-memory client for unit test. Do not forgot to call {@link #finalizeESClientForUnitTest()} at the end
	 * of test!
	 * 
	 * @return
	 * @throws Exception
	 */
	public final Client prepareESClientForUnitTest() {
		if (client != null)
			return client;
		try {
			// For unit tests it is recommended to use local node.
			// This is to ensure that your node will never join existing cluster on the network.

			// path.data location
			// parent folder is system's defined tmp dir instead of actual directory where test runs
			String parent = System.getProperty("java.io.tmpdir");
			tempFolder = new File(parent, "tmp");
			String tempFolderName = tempFolder.getCanonicalPath();
			log.log(Level.INFO, "Temporary folder for ES JUnit Client: {0}", tempFolderName);

			if (tempFolder.exists()) {
				FileUtils.deleteDirectory(tempFolder);
			}
{'fraction_non_alphanumeric': 0.09811495673671199, 'fraction_numerical': 0.0013906056860321385, 'mean_word_length': 4.145468998410175, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '6616298', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1806, 'n_words': 579}