<commit_before>package rres.knetminer.datasource.server.multispecie;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.BeforeClass;
import org.junit.Test;

import rres.knetminer.api.ApiIT;

public class MultiSpecieApiIT 
{		
	private Logger log = LogManager.getLogger ( this.getClass () );

	/**
	 * Keeps probing the API server until it seems initialized. A wrapper of {@link ApiIT#synchToServer()}. 
	 */
	@BeforeClass
	public static void synchToServer () throws Exception
	{
		ApiIT.synchToServer ();
	}
	
	
	/**
	 * Testing GenomeKeyword.
	 */
	@Test
	public void testKeyword () throws JSONException, IOException, URISyntaxException {
		testGenome ( "keyword->seed" );
	}
	
	/**
	 * Testing GenomeKeyword taxId.
	 */
	@Test
	public void testKeywordTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome ( "keyword->seed","taxId->3702" );
	}
	
	/**
	 * Testing Genes only.
	 */
	@Test
	public void testGenes () throws JSONException, IOException, URISyntaxException {
		testGenome ( "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230" );
	}
	
	/**
	 * Testing Genes and taxId.
	 */
	@Test
	public void testGenesTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230", "taxId->4577" );
	}
	
	/**
	 * Testing Keyword, genes & regions.
	 */
	@Test
	public void testRegion () throws JSONException, IOException, URISyntaxException {
		testGenome( "qtl->qtl1=5A:580000000:590000000" );
	}
	
	/**
	 * Testing Keyword, genes & regions.
	 */
	@Test
	public void testRegionTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "qtl->qtl1=5A:580000000:590000000", "taxId->4565" );
	}
	
	/**
	 * Testing Keyword & regions.
	 */
	@Test
	public void testKeywordRegion () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed","qtl->qtl1=5A:580000000:590000000" );
	}
	
	/**
	 * Testing Keyword, regions & taxId.
	 */
	@Test
	public void testKeywordRegionTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed", "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230",
				"qtl->qtl1=5A:580000000:590000000", "taxId->4577" );
	}
	
	/**
	 * Testing Keyword, genes & taxId.
	 */
	@Test
	public void testKeywordGenesTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed", "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230",
				"taxId->4577" );
	}
	
	/**
	 * Testing Keyword, genes & regions.
	 */
	@Test
	public void testKeywordGenesRegion () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed", "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230",
				"qtl->qtl1=5A:580000000:590000000" );
	}
	
	/**
	 * Testing Keyword, genes, regions & taxId.
	 */
	@Test
	public void testKeywordGenesRegionTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed", "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230",
				"qtl->qtl1=5A:580000000:590000000", "taxId->4577" );
	}
	
	/**
	 * Invokes /genome with the given params and verifies the result.
	 * 
	 */
	private void testGenome ( String... params  ) throws JSONException, IOException, URISyntaxException
	{
		Pattern arrayPattern  = Pattern.compile ( ",|:" );
		List<Serializable> objects = Arrays.stream ( params ).map ( a -> a.split ( "->" ) )
				.flatMap ( list -> Stream.of ( list[0], arrayPattern.matcher(list[1]).find() ? list[1].split ( "," ) : list[1] ) )
				.collect ( Collectors.toList () );
		
		JSONObject js = ApiIT.invokeApi ( "genome" , objects.toArray () );
		
		log.info ( "JSON from GraphInfo API:\n{}", js.toString () );
		assertTrue ( "geneCount from /genome + " + params[0] + " is wrong!", js.getInt ( "geneCount" ) > 0 );
		assertTrue ( "evidenceTable from /genome is not exists !", js.getString ( "evidenceTable" ).length () > 0  );
		assertTrue ( "geneTable from /genome is not exists !", js.getString ( "geneTable" ).length () > 0  );
		assertTrue ( "gviewer from /genome is not exists !", js.getString ( "gviewer" ).length () > 0  );
		assertTrue ( "totalDocSize from /genome + " + params[0] + " is wrong!", js.getInt ( "totalDocSize" ) > 0 );
	}	 
}
<commit_msg>CountHits and countLoci test case changes (#626)<commit_after>package rres.knetminer.datasource.server.multispecie;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.BeforeClass;
import org.junit.Test;

import rres.knetminer.api.ApiIT;

public class MultiSpecieApiIT 
{		
	private Logger log = LogManager.getLogger ( this.getClass () );

	/**
	 * Keeps probing the API server until it seems initialized. A wrapper of {@link ApiIT#synchToServer()}. 
	 */
	@BeforeClass
	public static void synchToServer () throws Exception
	{
		ApiIT.synchToServer ();
	}
	
	
	/**
	 * Testing GenomeKeyword.
	 */
	@Test
	public void testKeyword () throws JSONException, IOException, URISyntaxException {
		testGenome ( "keyword->seed" );
	}
	
	/**
	 * Testing GenomeKeyword taxId.
	 */
	@Test
	public void testKeywordTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome ( "keyword->seed","taxId->3702" );
	}
	
	/**
	 * Testing Genes only.
	 */
	@Test
	public void testGenes () throws JSONException, IOException, URISyntaxException {
		testGenome ( "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230" );
	}
	
	/**
	 * Testing Genes and taxId.
	 */
	@Test
	public void testGenesTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230", "taxId->4577" );
	}
	
	/**
	 * Testing Keyword, genes & regions.
	 */
	@Test
	public void testRegion () throws JSONException, IOException, URISyntaxException {
		testGenome( "qtl->qtl1=5A:580000000:590000000" );
	}
	
	/**
	 * Testing Keyword, genes & regions.
	 */
	@Test
	public void testRegionTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "qtl->qtl1=5A:580000000:590000000", "taxId->4565" );
	}
	
	/**
	 * Testing Keyword & regions.
	 */
	@Test
	public void testKeywordRegion () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed","qtl->qtl1=5A:580000000:590000000" );
	}
	
	/**
	 * Testing Keyword, regions & taxId.
	 */
	@Test
	public void testKeywordRegionTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed", "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230",
				"qtl->qtl1=5A:580000000:590000000", "taxId->4577" );
	}
	
	/**
	 * Testing Keyword, genes & taxId.
	 */
	@Test
	public void testKeywordGenesTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed", "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230",
				"taxId->4577" );
	}
	
	/**
	 * Testing Keyword, genes & regions.
	 */
	@Test
	public void testKeywordGenesRegion () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed", "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230",
				"qtl->qtl1=5A:580000000:590000000" );
	}
	
	/**
	 * Testing Keyword, genes, regions & taxId.
	 */
	@Test
	public void testKeywordGenesRegionTaxId () throws JSONException, IOException, URISyntaxException {
		testGenome( "keyword->seed", "list->AT1G21970,AT1G80840,AT4G14110,TRAESCS5B02G381900,ZM00001EB307230",
				"qtl->qtl1=5A:580000000:590000000", "taxId->4577" );
	}
	
	/**
	 * Invokes /genome with the given params and verifies the result.
	 * 
	 */
	private void testGenome ( String... params  ) throws JSONException, IOException, URISyntaxException
	{
		Pattern arrayPattern  = Pattern.compile ( ",|:" );
		List<Serializable> objects = Arrays.stream ( params ).map ( a -> a.split ( "->" ) )
				.flatMap ( list -> Stream.of ( list[0], arrayPattern.matcher(list[1]).find() ? list[1].split ( "," ) : list[1] ) )
				.collect ( Collectors.toList () );
		
		JSONObject js = ApiIT.invokeApi ( "genome" , objects.toArray () );
		
		log.info ( "JSON from GraphInfo API:\n{}", js.toString () );
		assertTrue ( "geneCount from /genome + " + params[0] + " is wrong!", js.getInt ( "geneCount" ) > 0 );
		assertTrue ( "evidenceTable from /genome is not exists !", js.getString ( "evidenceTable" ).length () > 0  );
		assertTrue ( "geneTable from /genome is not exists !", js.getString ( "geneTable" ).length () > 0  );
		assertTrue ( "gviewer from /genome is not exists !", js.getString ( "gviewer" ).length () > 0  );
		assertTrue ( "totalDocSize from /genome + " + params[0] + " is wrong!", js.getInt ( "totalDocSize" ) > 0 );
	}
	
	@Test
	public void testCountHits () throws JSONException, IOException, URISyntaxException {
		testCountHits (  "keyword->seed"  );
	}
	
	@Test
	public void testCountHitsWithTaxId () throws JSONException, IOException, URISyntaxException {
		testCountHits (  "keyword->seed", "taxId->3702"  );
	}
	
	/**
	 * Invokes /countHits with the given keyword & taxId and verifies that the result is >0.
	 * 
	 */
	private void testCountHits ( String... params ) throws JSONException, IOException, URISyntaxException
	{
		List<Serializable> objects = Arrays.stream ( params ).map ( a -> a.split ( "->" ) )
				.flatMap ( list -> Stream.of ( list[0], list[1] ) )
				.collect ( Collectors.toList () );
		
		JSONObject js = ApiIT.invokeApi ( "countHits" , objects.toArray () );

		Stream.of ( "luceneCount", "luceneLinkedCount", "geneCount" )
		.forEach ( key -> 
		  assertTrue ( 
		  	"countHits for '" +  params[0] + "' returned a wrong result (" + key + ")!", 
		  	js.getInt ( key ) > 0 )
		);
	}
	
	@Test
	public void testCountLoci () throws JSONException, IOException, URISyntaxException {
		testCountLoci (  "keyword->4-9920000-10180000"  );
	}
	
	@Test
	public void testCountLociWithTaxId () throws JSONException, IOException, URISyntaxException {
		testCountLoci (  "keyword->4-9920000-10180000", "taxId->3702"  );
	}
	
	/**
	 * Invokes /countLoci with the given keyword & taxId and verifies that the result is >0.
	 * 
	 */
	private void testCountLoci ( String... params ) throws JSONException, IOException, URISyntaxException
	{
		List<Serializable> objects = Arrays.stream ( params ).map ( a -> a.split ( "->" ) )
				.flatMap ( list -> Stream.of ( list[0], list[1] ) )
				.collect ( Collectors.toList () );
		
		JSONObject js = ApiIT.invokeApi ( "countLoci" , objects.toArray () );

		assertTrue ( 
		  	"geneCount for '" +  params[0] + "' returned a wrong result (" + params[0] + ")!", 
		  	js.getInt ( "geneCount" ) > 0 );
		
	}	
}

{'fraction_non_alphanumeric': 0.13905965884492164, 'fraction_numerical': 0.07134816867261234, 'mean_word_length': 4.160857908847185, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 24, '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': '1379151', 'n_tokens_mistral': 4763, 'n_tokens_neox': 3967, 'n_words': 826}