Skip to content

Commit

Permalink
HSEARCH-3654 Upgrade Junit where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Sep 27, 2023
1 parent 8576865 commit 1da09f0
Show file tree
Hide file tree
Showing 960 changed files with 17,582 additions and 16,414 deletions.
4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ stage('Non-default environments') {
withMavenWorkspace {
// Tests may fail because of hourly AWS snapshots,
// which prevent deleting indexes while they are being executed.
// Unfortunately, this triggers test failure in @BeforeClass/@AfterClass,
// Unfortunately, this triggers test failure in @BeforeAll/@AfterAll,
// which cannot be handled by the maven-failsafe-plugin,
// which normally re-runs failing tests, but only if
// the failure occurs in the @Test method.
Expand Down Expand Up @@ -660,7 +660,7 @@ stage('Non-default environments') {

// Tests may fail because of hourly AWS snapshots,
// which prevent deleting indexes while they are being executed.
// Unfortunately, this triggers test failure in @BeforeClass/@AfterClass,
// Unfortunately, this triggers test failure in @BeforeAll/@AfterAll,
// which cannot be handled by the maven-failsafe-plugin,
// which normally re-runs failing tests, but only if
// the failure occurs in the @Test method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import org.hibernate.search.backend.elasticsearch.ElasticsearchVersion;
import org.hibernate.search.util.common.SearchException;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.assertj.core.api.AbstractBooleanAssert;
import org.assertj.core.api.ObjectArrayAssert;

public class ElasticsearchVersionTest {
class ElasticsearchVersionTest {

@Test
public void of_string() {
void of_string() {
assertComponents( ElasticsearchVersion.of( "7" ) )
.containsExactly( ElasticsearchDistributionName.ELASTIC, 7, null, null, null );
assertComponents( ElasticsearchVersion.of( "7.0" ) )
Expand Down Expand Up @@ -66,7 +66,7 @@ public void of_string() {
}

@Test
public void of_string_forceLowercase() {
void of_string_forceLowercase() {
assertComponents( ElasticsearchVersion.of( "7.0.0-Beta1" ) )
.containsExactly( ElasticsearchDistributionName.ELASTIC, 7, 0, 0, "beta1" );
assertComponents( ElasticsearchVersion.of( "ELASTIC:7.0.0-Beta1" ) )
Expand All @@ -78,7 +78,7 @@ public void of_string_forceLowercase() {
}

@Test
public void of_string_invalid() {
void of_string_invalid() {
assertThatThrownBy( () -> ElasticsearchVersion.of( "7_0" ) )
.isInstanceOf( SearchException.class )
.hasMessageContainingAll( "Invalid Elasticsearch version",
Expand Down Expand Up @@ -118,7 +118,7 @@ public void of_string_invalid() {
}

@Test
public void of_distributionNameAndString() {
void of_distributionNameAndString() {
assertComponents( ElasticsearchVersion.of( ElasticsearchDistributionName.ELASTIC, "7" ) )
.containsExactly( ElasticsearchDistributionName.ELASTIC, 7, null, null, null );
assertComponents( ElasticsearchVersion.of( ElasticsearchDistributionName.ELASTIC, "7.0" ) )
Expand Down Expand Up @@ -158,7 +158,7 @@ public void of_distributionNameAndString() {
}

@Test
public void of_distributionNameAndString_invalid() {
void of_distributionNameAndString_invalid() {
assertThatThrownBy( () -> ElasticsearchVersion.of( ElasticsearchDistributionName.ELASTIC, "7_0" ) )
.isInstanceOf( SearchException.class )
.hasMessageContainingAll( "Invalid Elasticsearch version",
Expand All @@ -177,7 +177,7 @@ public void of_distributionNameAndString_invalid() {
}

@Test
public void exactMatch_elasticsearch() {
void exactMatch_elasticsearch() {
assertMatch( "5.6.0", ElasticsearchDistributionName.ELASTIC, "5.6.0" ).isTrue();
assertMatch( "5.6.1", ElasticsearchDistributionName.ELASTIC, "5.6.1" ).isTrue();
assertMatch( "6.0.0", ElasticsearchDistributionName.ELASTIC, "6.0.0" ).isTrue();
Expand All @@ -192,7 +192,7 @@ public void exactMatch_elasticsearch() {
}

@Test
public void exactMatch_opensearch() {
void exactMatch_opensearch() {
assertMatch( "opensearch:1.0.0-beta1", ElasticsearchDistributionName.OPENSEARCH, "1.0.0-beta1" ).isTrue();
assertMatch( "opensearch:1.0.0-rc1", ElasticsearchDistributionName.OPENSEARCH, "1.0.0-rc1" ).isTrue();
assertMatch( "opensearch:1.0.0-rc2", ElasticsearchDistributionName.OPENSEARCH, "1.0.0-rc2" ).isTrue();
Expand All @@ -213,7 +213,7 @@ public void exactMatch_amazonOpensearchServerless() {
}

@Test
public void lenientMatch_elasticsearch() {
void lenientMatch_elasticsearch() {
assertMatch( "5", ElasticsearchDistributionName.ELASTIC, "5.6.0" ).isTrue();
assertMatch( "5", ElasticsearchDistributionName.ELASTIC, "5.6.1" ).isTrue();
assertMatch( "5", ElasticsearchDistributionName.ELASTIC, "5.7.1" ).isTrue();
Expand Down Expand Up @@ -247,7 +247,7 @@ public void lenientMatch_elasticsearch() {
}

@Test
public void lenientMatch_opensearch() {
void lenientMatch_opensearch() {
assertMatch( "opensearch:1", ElasticsearchDistributionName.OPENSEARCH, "1.0.0-rc1" ).isTrue();
assertMatch( "opensearch:1", ElasticsearchDistributionName.OPENSEARCH, "1.0.0" ).isTrue();
assertMatch( "opensearch:1", ElasticsearchDistributionName.OPENSEARCH, "1.0.1" ).isTrue();
Expand All @@ -270,7 +270,7 @@ public void lenientMatch_opensearch() {
}

@Test
public void nonMatching_elastic() {
void nonMatching_elastic() {
assertMatch( "5.6.0", ElasticsearchDistributionName.ELASTIC, "5.5.0" ).isFalse();
assertMatch( "5.6.0", ElasticsearchDistributionName.ELASTIC, "5.7.0" ).isFalse();
assertMatch( "5.6.0", ElasticsearchDistributionName.ELASTIC, "6.0.0" ).isFalse();
Expand Down Expand Up @@ -309,7 +309,7 @@ public void nonMatching_elastic() {
}

@Test
public void nonMatching_opensearch() {
void nonMatching_opensearch() {
assertMatch( "opensearch:1.0.0", ElasticsearchDistributionName.OPENSEARCH, "1.1.0" ).isFalse();
assertMatch( "opensearch:1.0", ElasticsearchDistributionName.OPENSEARCH, "1.1.0" ).isFalse();
assertMatch( "opensearch:1.1.0", ElasticsearchDistributionName.OPENSEARCH, "1.0.0" ).isFalse();
Expand All @@ -320,7 +320,7 @@ public void nonMatching_opensearch() {
}

@Test
public void nonMatching_elasticAndOpensearch() {
void nonMatching_elasticAndOpensearch() {
assertMatch( "7.0.0", ElasticsearchDistributionName.OPENSEARCH, "7.0.0" ).isFalse();
assertMatch( "7.0", ElasticsearchDistributionName.OPENSEARCH, "7.0.0" ).isFalse();
assertMatch( "7", ElasticsearchDistributionName.OPENSEARCH, "7.0.0" ).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;

Expand All @@ -20,69 +22,49 @@
import org.hibernate.search.backend.elasticsearch.client.spi.ElasticsearchResponse;
import org.hibernate.search.util.common.SearchException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import com.google.gson.JsonObject;

import org.apache.http.HttpHost;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

@RunWith(Parameterized.class)
public class ElasticsearchClientUtilsTryGetElasticsearchVersionTest {

@Parameterized.Parameters(name = "{0} - {1}")
public static Object[][] data() {
return new Object[][] {
{ null, "1.0.0", ElasticsearchDistributionName.ELASTIC, 1, 0, 0, null },
{ null, "2.0.0", ElasticsearchDistributionName.ELASTIC, 2, 0, 0, null },
{ null, "2.4.4", ElasticsearchDistributionName.ELASTIC, 2, 4, 4, null },
{ null, "5.0.0", ElasticsearchDistributionName.ELASTIC, 5, 0, 0, null },
{ null, "5.6.6", ElasticsearchDistributionName.ELASTIC, 5, 6, 6, null },
{ null, "6.0.0", ElasticsearchDistributionName.ELASTIC, 6, 0, 0, null },
{ null, "6.7.0", ElasticsearchDistributionName.ELASTIC, 6, 7, 0, null },
{ null, "7.0.0-beta1", ElasticsearchDistributionName.ELASTIC, 7, 0, 0, "beta1" },
{ null, "7.0.0", ElasticsearchDistributionName.ELASTIC, 7, 0, 0, null },
{ "opensearch", "1.0.0-rc1", ElasticsearchDistributionName.OPENSEARCH, 1, 0, 0, "rc1" },
{ "opensearch", "1.0.0", ElasticsearchDistributionName.OPENSEARCH, 1, 0, 0, null },
{ "opensearch", "1.0.1", ElasticsearchDistributionName.OPENSEARCH, 1, 0, 1, null }
};
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
@ExtendWith(MockitoExtension.class)
class ElasticsearchClientUtilsTryGetElasticsearchVersionTest {

public static List<? extends Arguments> params() {
return Arrays.asList(
Arguments.of( null, "1.0.0", ElasticsearchDistributionName.ELASTIC, 1, 0, 0, null ),
Arguments.of( null, "2.0.0", ElasticsearchDistributionName.ELASTIC, 2, 0, 0, null ),
Arguments.of( null, "2.4.4", ElasticsearchDistributionName.ELASTIC, 2, 4, 4, null ),
Arguments.of( null, "5.0.0", ElasticsearchDistributionName.ELASTIC, 5, 0, 0, null ),
Arguments.of( null, "5.6.6", ElasticsearchDistributionName.ELASTIC, 5, 6, 6, null ),
Arguments.of( null, "6.0.0", ElasticsearchDistributionName.ELASTIC, 6, 0, 0, null ),
Arguments.of( null, "6.7.0", ElasticsearchDistributionName.ELASTIC, 6, 7, 0, null ),
Arguments.of( null, "7.0.0-beta1", ElasticsearchDistributionName.ELASTIC, 7, 0, 0, "beta1" ),
Arguments.of( null, "7.0.0", ElasticsearchDistributionName.ELASTIC, 7, 0, 0, null ),
Arguments.of( "opensearch", "1.0.0-rc1", ElasticsearchDistributionName.OPENSEARCH, 1, 0, 0, "rc1" ),
Arguments.of( "opensearch", "1.0.0", ElasticsearchDistributionName.OPENSEARCH, 1, 0, 0, null ),
Arguments.of( "opensearch", "1.0.1", ElasticsearchDistributionName.OPENSEARCH, 1, 0, 1, null )
);
}

@Rule
public final MockitoRule mockito = MockitoJUnit.rule().strictness( Strictness.STRICT_STUBS );

private final String distributionString;
private final String versionString;

private final ElasticsearchDistributionName expectedDistribution;
private final int expectedMajor;
private final int expectedMinor;
private final int expectedMicro;
private final String expectedQualifier;

@Mock
private ElasticsearchClient clientMock;

public ElasticsearchClientUtilsTryGetElasticsearchVersionTest(String distributionString, String versionString,

@ParameterizedTest(name = "{0} - {1}")
@MethodSource("params")
void testValid(String distributionString, String versionString,
ElasticsearchDistributionName expectedDistribution,
int expectedMajor, int expectedMinor, int expectedMicro, String expectedQualifier) {
this.distributionString = distributionString;
this.versionString = versionString;
this.expectedDistribution = expectedDistribution;
this.expectedMajor = expectedMajor;
this.expectedMinor = expectedMinor;
this.expectedMicro = expectedMicro;
this.expectedQualifier = expectedQualifier;
}

@Test
public void testValid() {
doMock( distributionString, versionString );
ElasticsearchVersion version = ElasticsearchClientUtils.tryGetElasticsearchVersion( clientMock );
assertThat( version ).isNotNull();
Expand All @@ -98,32 +80,43 @@ public void testValid() {
}
}

@Test
public void testInvalid_distribution() {
@ParameterizedTest(name = "{0} - {1}")
@MethodSource("params")
void testInvalid_distribution(String distributionString, String versionString,
ElasticsearchDistributionName expectedDistribution,
int expectedMajor, int expectedMinor, int expectedMicro, String expectedQualifier) {
String invalidDistributionName = "QWDWQD" + distributionString;
doMock( invalidDistributionName, versionString );
assertThatThrownBy( () -> ElasticsearchClientUtils.tryGetElasticsearchVersion( clientMock ) )
.isInstanceOf( SearchException.class )
.hasMessageContainingAll(
"Invalid Elasticsearch distribution name",
"'" + invalidDistributionName.toLowerCase( Locale.ROOT ) + "'",
"Valid names are: [elastic, opensearch]" );
"Valid names are: [elastic, opensearch]"
);
}

@Test
public void testInvalid_version() {
@ParameterizedTest(name = "{0} - {1}")
@MethodSource("params")
void testInvalid_version(String distributionString, String versionString,
ElasticsearchDistributionName expectedDistribution,
int expectedMajor, int expectedMinor, int expectedMicro, String expectedQualifier) {
String invalidVersionString = versionString.substring( 0, versionString.length() - 1 ) + "-A-B";
doMock( distributionString, invalidVersionString );
assertThatThrownBy( () -> ElasticsearchClientUtils.tryGetElasticsearchVersion( clientMock ) )
.isInstanceOf( SearchException.class )
.hasMessageContainingAll(
"Invalid Elasticsearch version",
"'" + invalidVersionString.toLowerCase( Locale.ROOT ) + "'",
"Expected format is 'x.y.z-qualifier'" );
"Expected format is 'x.y.z-qualifier'"
);
}

@Test
public void testHttpStatus404() {
@ParameterizedTest(name = "{0} - {1}")
@MethodSource("params")
public void testHttpStatus404(String distributionString, String versionString,
ElasticsearchDistributionName expectedDistribution,
int expectedMajor, int expectedMinor, int expectedMicro, String expectedQualifier) {
// This should only happen on Amazon OpenSearch Service,
// which doesn't allow retrieving the version.
when( clientMock.submit( any() ) )
Expand Down
Loading

0 comments on commit 1da09f0

Please sign in to comment.