Aliya
Aliya

Reputation: 1178

How to write Elastic unit tests to test query building

I want to write unit tests that test the Elastic query building. I want to test that certain param values produce certain queries.

I started looking into ESTestCase. I see that you can mock a client using ESTestCase. I don't really need to mock the ES node, I just need to reproduce the query building part, but that requires the client.

Has anybody dealt with such issue?

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESTestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.google.common.collect.Lists;

public class SearchRequestBuilderTests extends ESTestCase {
    private static Client client;

    @BeforeClass
    public static void initClient() {
        //this client will not be hit by any request, but it needs to be a non null proper client
        //that is why we create it but we don't add any transport address to it
        Settings settings = Settings.builder()
            .put("", createTempDir().toString())
            .build();

        client = TransportClient.builder().settings(settings).build();
    }

    @AfterClass
    public static void closeClient() {
        client.close();
        client = null;
    }

    public static Map<String, String> createSampleSearchParams() {
        Map<String, String> searchParams = new HashMap<>();
        searchParams.put(SenseneConstants.ADC_PARAM, "US");
        searchParams.put(SenseneConstants.FETCH_SIZE_QUERY_PARAM, "10");
        searchParams.put(SenseneConstants.QUERY_PARAM, "some query");
        searchParams.put(SenseneConstants.LOCATION_QUERY_PARAM, "");
        searchParams.put(SenseneConstants.RADIUS_QUERY_PARAM, "20");
        searchParams.put(SenseneConstants.DISTANCE_UNIT_PARAM, DistanceUnit.MILES.name());
        searchParams.put(SenseneConstants.GEO_DISTANCE_PARAM, "true");

        return searchParams;
     }

    @Test
    public void test() {
        BasicSearcher searcher = new BasicSearcher(client); // this is my application's searcher
        Map<String, String> searchParams = createSampleSearchParams();

        ArrayList<String> filterQueries = Lists.newArrayList();
        SearchRequest searchRequest = SearchRequest.create(searchParams, filterQueries);

        MySearchRequestBuilder medleyReqBuilder = new MySearchRequestBuilder.Builder(client, "my_index", searchRequest).build();
        SearchRequestBuilder searchRequestBuilder = medleyReqBuilder.constructSearchRequestBuilder();
        System.out.print(searchRequestBuilder.toString());
    // Here I want to assert that the search request builder output is what it should be for the above client params    
    }
}

I get this, and nothing in the code runs:

 Assertions mismatch: -ea was not specified but -Dtests.asserts=true
 REPRODUCE WITH: mvn test -Pdev -Dtests.seed=5F09BEDD71BBD14E -      Dtests.class=SearchRequestBuilderTests -Dtests.locale=en_US -Dtests.timezone=America/Los_Angeles
 NOTE: test params are: codec=null, sim=null, locale=null, timezone=(null)
 NOTE: Mac OS X 10.10.5 x86_64/Oracle Corporation 1.7.0_80 (64-bit)/cpus=4,threads=1,free=122894936,total=128974848
 NOTE: All tests run in this JVM: [SearchRequestBuilderTests]

Upvotes: 2

Views: 7432

Answers (1)

Andrew Frieden
Andrew Frieden

Reputation: 31

Obviously a bit late but...

So this actually has nothing to do with the ES Testing framework but rather your run settings. Assuming you are running this in eclipse, this is actually a duplicate of Assertions mismatch: -ea was not specified but -Dtests.asserts=true.

  1. eclipse preference -> junit -> Add -ea checkbox enable.
  2. right click on the eclipse project -> run as -> run configure -> arguments tab -> add the -ea option in vm arguments

Upvotes: 3

Related Questions