Skip to content

Commit

Permalink
HSEARCH-2593 Add a unit test for Elasticsearch node discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere authored and Sanne committed Feb 21, 2017
1 parent 28a5306 commit c0e5180
Showing 1 changed file with 75 additions and 0 deletions.
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.search.elasticsearch.test;

import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
Expand All @@ -15,6 +16,7 @@
import org.eclipse.jetty.http.HttpHeader;
import org.hibernate.search.elasticsearch.cfg.ElasticsearchEnvironment;
import org.hibernate.search.elasticsearch.client.impl.JestClient;
import org.hibernate.search.elasticsearch.impl.JsonBuilder;
import org.hibernate.search.test.util.impl.ExpectedLog4jLog;
import org.hibernate.search.testsupport.TestForIssue;
import org.hibernate.search.testsupport.setup.BuildContextForTest;
Expand All @@ -24,7 +26,9 @@
import org.junit.rules.ExpectedException;

import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.gson.JsonObject;

import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;
Expand Down Expand Up @@ -142,6 +146,51 @@ public void multipleHosts() {
}
}

@Test
@TestForIssue(jiraKey = "HSEARCH-2449")
public void discovery() throws Exception {
SearchConfigurationForTest configuration = new SearchConfigurationForTest()
.addProperty( CLIENT_PROPERTY_PREFIX + ElasticsearchEnvironment.SERVER_URI, URI_1 )
.addProperty( CLIENT_PROPERTY_PREFIX + ElasticsearchEnvironment.DISCOVERY_ENABLED, "true" )
.addProperty( CLIENT_PROPERTY_PREFIX + ElasticsearchEnvironment.DISCOVERY_REFRESH_INTERVAL, "1" );

String nodesInfoResult = dummyNodeInfoResponse( PORT_1, PORT_2 );

wireMockRule1.stubFor( get( WireMock.urlMatching( "/_nodes.*" ) )
.willReturn( elasticsearchResponse().withStatus( 200 ).withBody( nodesInfoResult ) ) );
wireMockRule2.stubFor( get( WireMock.urlMatching( "/_nodes.*" ) )
.willReturn( elasticsearchResponse().withStatus( 200 ).withBody( nodesInfoResult ) ) );

String payload = "{ \"foo\": \"bar\" }";
wireMockRule1.stubFor( post( urlPathEqualTo( "/myIndex/myType" ) )
.withRequestBody( equalToJson( payload ) )
.willReturn( elasticsearchResponse().withStatus( 200 ) ) );
wireMockRule2.stubFor( post( urlPathEqualTo( "/myIndex/myType" ) )
.withRequestBody( equalToJson( payload ) )
.willReturn( elasticsearchResponse().withStatus( 200 ) ) );

try {
jestClient.start( configuration.getProperties(), new BuildContextForTest( configuration ) );

Index request = new Index.Builder( payload ).index( "myIndex" ).type( "myType" ).build();
DocumentResult result = jestClient.executeRequest( request );
assertThat( result.isSucceeded() ).as( "isSucceeded" ).isTrue();

Thread.sleep( 2000 ); // Wait for the refresh to occur

result = jestClient.executeRequest( request );
assertThat( result.isSucceeded() ).as( "isSucceeded" ).isTrue();
result = jestClient.executeRequest( request );
assertThat( result.isSucceeded() ).as( "isSucceeded" ).isTrue();

wireMockRule1.verify( postRequestedFor( urlPathEqualTo( "/myIndex/myType" ) ) );
wireMockRule2.verify( postRequestedFor( urlPathEqualTo( "/myIndex/myType" ) ) );
}
finally {
jestClient.stop();
}
}

@Test
@TestForIssue(jiraKey = "HSEARCH-2453")
public void authentication() {
Expand Down Expand Up @@ -239,4 +288,30 @@ private static ResponseDefinitionBuilder elasticsearchResponse() {
return ResponseDefinitionBuilder.okForEmptyJson();
}

private String dummyNodeInfoResponse(int... ports) {
JsonBuilder.Object nodesBuilder = JsonBuilder.object();
int index = 1;
for ( int port : ports ) {
nodesBuilder.add( "hJLXmY_NTrCytiIMbX4_" + index + "g", dummyNodeInfo( port ) );
++index;
}

return JsonBuilder.object()
.addProperty( "cluster_name", "foo-cluster.local" )
.add( "nodes", nodesBuilder.build() )
.build()
.toString();
}

private JsonObject dummyNodeInfo(int port) {
return JsonBuilder.object()
.addProperty( "name", "nodeForPort" + port )
.addProperty( "transport_address", "inet[/localhost:" + (port + 100) + "]" )
.addProperty( "hostname", "localhost" )
.addProperty( "version", "2.4.4" )
.addProperty( "http_address", "inet[/localhost:" + port + "]" )
.add( "plugins", JsonBuilder.array().build() )
.build();
}

}

0 comments on commit c0e5180

Please sign in to comment.