Skip to content

Commit

Permalink
HSEARCH-4214 Refresh ElasticsearchBootstrapIT
Browse files Browse the repository at this point in the history
* Better test method names
* More assertions
  • Loading branch information
yrodiere committed Apr 20, 2021
1 parent b59f88a commit 74ef63c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery;

import org.hibernate.search.backend.elasticsearch.ElasticsearchVersion;
import org.hibernate.search.backend.elasticsearch.cfg.ElasticsearchBackendSettings;
Expand All @@ -34,6 +35,8 @@ public class ElasticsearchBootstrapIT {
@Rule
public ElasticsearchClientSpy elasticsearchClientSpy = new ElasticsearchClientSpy();

private final StubMappedIndex index = StubMappedIndex.withoutFields();

/**
* Check that we boot successfully when the Elasticsearch model dialect is determined
* from an explicitly configured Elasticsearch version,
Expand All @@ -50,27 +53,29 @@ public void explicitModelDialect() {
elasticsearchClientSpy.factoryReference()
)
.withSchemaManagement( StubMappingSchemaManagementStrategy.DROP_ON_SHUTDOWN_ONLY )
.withIndex( StubMappedIndex.withoutFields() )
.withIndex( index )
.setupFirstPhaseOnly();

// We do not expect the client to be created in the first phase
assertThat( elasticsearchClientSpy.getCreatedClientCount() ).isEqualTo( 0 );
elasticsearchClientSpy.verifyExpectationsMet();
assertThat( elasticsearchClientSpy.getCreatedClientCount() ).isZero();

// In the *second* phase, however, we expect the client to be created and used to check the version
elasticsearchClientSpy.expectNext(
ElasticsearchRequest.get().build(), ElasticsearchRequestAssertionMode.EXTENSIBLE
);
partialSetup.doSecondPhase();
elasticsearchClientSpy.verifyExpectationsMet();

checkBackendWorks();
}

/**
* Check that an exception is thrown when version check is at false without explicit cluster version specified
* Check that an exception is thrown when version_check.enabled is false
* without specifying the Elasticsearch version.
*/
@Test
@TestForIssue(jiraKey = "HSEARCH-3841")
public void explicitProtocolDialect_noVersionCheck() {
public void noVersionCheck_missingVersion() {
assertThatThrownBy(
() -> setupHelper.start()
.withBackendProperty(
Expand All @@ -97,11 +102,12 @@ public void explicitProtocolDialect_noVersionCheck() {
}

/**
* Check that an exception is thrown when version check is at false with a partial version is specified
* Check that an exception is thrown when version_check.enabled is false
* while specifying only the major number of the Elasticsearch version.
*/
@Test
@TestForIssue(jiraKey = "HSEARCH-3841")
public void explicitProtocolDialect_noVersionCheck_incompleteVersion() {
public void noVersionCheck_incompleteVersion() {
ElasticsearchVersion clusterVersion = ElasticsearchVersion.of( ElasticsearchTestDialect.getClusterVersion() );
String versionWithMajorOnly = String.valueOf( clusterVersion.major() );

Expand Down Expand Up @@ -134,14 +140,14 @@ public void explicitProtocolDialect_noVersionCheck_incompleteVersion() {
}

/**
* Check that everything is fine when version check is at false with a version is specified with major & minor
* Check everything works fine when version_check.enabled is false
* and specifying the major and minor number of the Elasticsearch version.
*/
@Test
@TestForIssue(jiraKey = "HSEARCH-3841")
public void explicitProtocolDialect_noVersionCheck_completeVersion() {
@TestForIssue(jiraKey = {"HSEARCH-3841", "HSEARCH-4214"})
public void noVersionCheck_completeVersion() {
ElasticsearchVersion clusterVersion = ElasticsearchVersion.of( ElasticsearchTestDialect.getClusterVersion() );
String versionWithMajorAndMinorOnly = String.valueOf( clusterVersion.major() )
+ "." + String.valueOf( clusterVersion.minor().getAsInt() );
String versionWithMajorAndMinorOnly = clusterVersion.major() + "." + clusterVersion.minor().getAsInt();

SearchSetupHelper.PartialSetup partialSetup = setupHelper.start()
.withBackendProperty(
Expand All @@ -155,13 +161,24 @@ public void explicitProtocolDialect_noVersionCheck_completeVersion() {
elasticsearchClientSpy.factoryReference()
)
.withSchemaManagement( StubMappingSchemaManagementStrategy.DROP_ON_SHUTDOWN_ONLY )
.withIndex( StubMappedIndex.withoutFields() )
.withIndex( index )
.setupFirstPhaseOnly();
// We do not expect the client to be created in the first phase
assertThat( elasticsearchClientSpy.getCreatedClientCount() ).isEqualTo( 0 );
elasticsearchClientSpy.verifyExpectationsMet();
assertThat( elasticsearchClientSpy.getCreatedClientCount() ).isZero();

partialSetup.doSecondPhase();
elasticsearchClientSpy.verifyExpectationsMet();
// We do not expect any request, since the version check is disabled
assertThat( elasticsearchClientSpy.getRequestCount() ).isZero();

checkBackendWorks();

assertThat( elasticsearchClientSpy.getRequestCount() ).isNotZero();
}

private void checkBackendWorks() {
index.schemaManager().createIfMissing().join();
assertThatQuery( index.query().where( f -> f.matchAll() ) ).hasNoHits();
index.index( "1", document -> { } );
assertThatQuery( index.query().where( f -> f.matchAll() ) ).hasDocRefHitsAnyOrder( index.typeName(), "1" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

public class ElasticsearchClientSpy implements TestRule {
private final AtomicInteger createdClientCount = new AtomicInteger();
private final AtomicInteger requestCount = new AtomicInteger();
private final CallQueue<ElasticsearchClientSubmitCall> expectations = new CallQueue<>();

@Override
Expand Down Expand Up @@ -67,6 +68,10 @@ public int getCreatedClientCount() {
return createdClientCount.get();
}

public int getRequestCount() {
return requestCount.get();
}

public BeanReference<ElasticsearchClientFactory> factoryReference() {
return beanResolver -> {
BeanHolder<ElasticsearchClientFactory> delegateHolder =
Expand Down Expand Up @@ -120,6 +125,7 @@ public void close() {

@Override
public CompletableFuture<ElasticsearchResponse> submit(ElasticsearchRequest request) {
requestCount.incrementAndGet();
return expectations.verify(
new ElasticsearchClientSubmitCall( request ),
// If there was an expectation, check it is met and forward the request to the actual client
Expand Down

0 comments on commit 74ef63c

Please sign in to comment.