Skip to content

Commit

Permalink
Simplify JUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Oct 20, 2023
1 parent cca84fd commit 20fa4b0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/test/java/io/quarkus/search/app/SearchServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class SearchServiceIT extends SearchServiceTest {
class SearchServiceIT extends SearchServiceTest {
}
73 changes: 26 additions & 47 deletions src/test/java/io/quarkus/search/app/SearchServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@QuarkusTest
@TestHTTPEndpoint(SearchService.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class SearchServiceTest {
class SearchServiceTest {
private static final TypeRef<SearchResult<SearchHit>> SEARCH_RESULT_SEARCH_HITS = new TypeRef<>() {
};

Expand All @@ -37,8 +37,17 @@ protected int managementPort() {
}
}

private SearchResult<SearchHit> search(String term) {
return given()
.queryParam("q", term)
.when().get()
.then()
.statusCode(200)
.extract().body().as(SEARCH_RESULT_SEARCH_HITS);
}

@BeforeAll
public void waitForIndexing() {
void waitForIndexing() {
Awaitility.await().untilAsserted(() -> {
when().get("http://localhost:" + managementPort() + "/q/health/ready")
.then()
Expand All @@ -47,25 +56,15 @@ public void waitForIndexing() {
}

@Test
public void queryNotMatching() {
var result = given()
.queryParam("q", "termnotmatching")
.when().get()
.then()
.statusCode(200)
.extract().body().as(SEARCH_RESULT_SEARCH_HITS);
void queryNotMatching() {
var result = search("termnotmatching");
assertThat(result.hits()).isEmpty();
assertThat(result.total()).isEqualTo(0);
}

@Test
public void queryMatchingFullTerm() {
var result = given()
.queryParam("q", "orm")
.when().get()
.then()
.statusCode(200)
.extract().body().as(SEARCH_RESULT_SEARCH_HITS);
void queryMatchingFullTerm() {
var result = search("orm");
// We check order in another test
assertThat(result.hits()).extracting(SearchHit::id).containsExactlyInAnyOrder(
DatasetConstants.GuideIds.HIBERNATE_ORM,
Expand All @@ -79,13 +78,8 @@ public void queryMatchingFullTerm() {
}

@Test
public void queryMatchingPrefixTerm() {
var result = given()
.queryParam("q", "hiber")
.when().get()
.then()
.statusCode(200)
.extract().body().as(SEARCH_RESULT_SEARCH_HITS);
void queryMatchingPrefixTerm() {
var result = search("hiber");
// We check order in another test
assertThat(result.hits()).extracting(SearchHit::id).containsExactlyInAnyOrder(
GuideIds.HIBERNATE_ORM,
Expand All @@ -100,20 +94,15 @@ public void queryMatchingPrefixTerm() {
}

@Test
public void queryEmptyString() {
var result = given()
.queryParam("q", "")
.when().get()
.then()
.statusCode(200)
.extract().body().as(SEARCH_RESULT_SEARCH_HITS);
void queryEmptyString() {
var result = search("");
assertThat(result.hits()).extracting(SearchHit::id)
.containsExactlyInAnyOrder(DatasetConstants.GuideIds.ALL);
assertThat(result.total()).isEqualTo(10);
}

@Test
public void queryNotProvided() {
void queryNotProvided() {
var result = when().get()
.then()
.statusCode(200)
Expand All @@ -124,21 +113,16 @@ public void queryNotProvided() {
}

@ParameterizedTest
@MethodSource("relevance_params")
public void relevance(String query, List<String> expectedGuideIds) {
var result = given()
.queryParam("q", query)
.when().get()
.then()
.statusCode(200)
.extract().body().as(SEARCH_RESULT_SEARCH_HITS);
@MethodSource
void relevance(String query, List<String> expectedGuideIds) {
var result = search(query);
// Using "startsWith" here, because what we want is to have the most relevant hits first.
// We don't mind that much if there's a trail of not-so-relevant hits.
assertThat(result.hits()).extracting(SearchHit::id).startsWith(
expectedGuideIds.toArray(String[]::new));
}

private static List<Arguments> relevance_params() {
private static List<Arguments> relevance() {
return List.of(
// I wonder if we could use something similar to https://stackoverflow.com/a/74737474/5043585
// to have some sort of weight in the documents and prioritize some of them
Expand Down Expand Up @@ -189,13 +173,8 @@ private static List<Arguments> relevance_params() {
}

@Test
public void projections() {
var result = given()
.queryParam("q", "hibernate + elasticsearch")
.when().get()
.then()
.statusCode(200)
.extract().body().as(SEARCH_RESULT_SEARCH_HITS);
void projections() {
var result = search("hibernate + elasticsearch");
assertThat(result.hits()).containsExactlyInAnyOrder(
new SearchHit(GuideIds.HIBERNATE_SEARCH_ORM_ELASTICSEARCH,
"Hibernate Search guide"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import io.quarkus.search.app.entity.Guide;
import io.quarkus.test.component.QuarkusComponentTestExtension;

public class FetchingServiceTest {
class FetchingServiceTest {

private static final Logger LOG = Logger.getLogger(FetchingServiceTest.class);

Expand All @@ -49,6 +49,7 @@ static void deleteTmpDir() throws IOException {
PathUtils.deleteDirectory(tmpDir);
}

@SuppressWarnings("JUnitMalformedDeclaration")
static abstract class AbstractTest {
protected static QuarkusComponentTestExtension extension(FetchingConfig.Source.Method quarkusIOMethod,
URI quarkusIOURI) {
Expand Down Expand Up @@ -77,7 +78,7 @@ public URI uri() {
FetchingService service;

@Test
public void fetchQuarkusIo() throws Exception {
void fetchQuarkusIo() throws Exception {
try (QuarkusIO quarkusIO = service.fetchQuarkusIo()) {
try (var guides = quarkusIO.guides()) {
assertThat(guides)
Expand Down

0 comments on commit 20fa4b0

Please sign in to comment.