-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add synonyms filter to the analysis configuration
- Loading branch information
1 parent
ac86060
commit 1a3f256
Showing
4 changed files
with
263 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
src/test/java/io/quarkus/search/app/SynonymSearchServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
package io.quarkus.search.app; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static io.restassured.RestAssured.when; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import io.quarkus.search.app.dto.GuideSearchHit; | ||
import io.quarkus.search.app.dto.SearchResult; | ||
import io.quarkus.search.app.quarkusio.QuarkusIO; | ||
import io.quarkus.search.app.testsupport.GitTestUtils; | ||
import io.quarkus.search.app.util.CloseableDirectory; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
import io.quarkus.test.junit.TestProfile; | ||
import io.restassured.RestAssured; | ||
import io.restassured.common.mapper.TypeRef; | ||
import io.restassured.filter.log.LogDetail; | ||
import org.apache.commons.io.file.PathUtils; | ||
import org.awaitility.Awaitility; | ||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.revwalk.RevCommit; | ||
|
||
@QuarkusTest | ||
@TestProfile(SynonymSearchServiceTest.Profile.class) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class SynonymSearchServiceTest { | ||
private static final TypeRef<SearchResult<GuideSearchHit>> SEARCH_RESULT_SEARCH_HITS = new TypeRef<>() { | ||
}; | ||
private static final String GUIDES_SEARCH = "api/guides/search"; | ||
|
||
protected int managementPort() { | ||
if (getClass().getName().endsWith("IT")) { | ||
return 9000; | ||
} else { | ||
return 9001; | ||
} | ||
} | ||
|
||
// Unfortunately we can't use @TempDir here, | ||
// because we need the path initialized before we create the extension below. | ||
static CloseableDirectory tmpDir; | ||
|
||
static { | ||
try { | ||
tmpDir = CloseableDirectory.temp("synonym-service-test"); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Could not init temp directory: " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
public static class Profile implements QuarkusTestProfile { | ||
@Override | ||
public Map<String, String> getConfigOverrides() { | ||
try { | ||
initOrigin(); | ||
} catch (IOException | GitAPIException e) { | ||
throw new IllegalStateException("Unable to initialized sample git repository: " + e.getMessage(), e); | ||
} | ||
Map<String, String> config = new HashMap<>(); | ||
config.put("quarkusio.git-uri", tmpDir.path().toString()); | ||
return config; | ||
} | ||
|
||
@Override | ||
public String getConfigProfile() { | ||
return "synonyms-profile"; | ||
} | ||
} | ||
|
||
static void initOrigin() throws IOException, GitAPIException { | ||
Path sourceRepoPath = tmpDir.path(); | ||
Path metadata1ToFetch = sourceRepoPath.resolve("_data/versioned/latest/index/quarkus.yaml"); | ||
Path guide1HtmlToFetch = sourceRepoPath.resolve("guides/" + FETCHED_GUIDE_1_NAME + ".html"); | ||
try (Git git = Git.init().setDirectory(sourceRepoPath.toFile()) | ||
.setInitialBranch(QuarkusIO.PAGES_BRANCH).call()) { | ||
GitTestUtils.cleanGitUserConfig(); | ||
|
||
RevCommit initialCommit = git.commit().setMessage("Initial commit") | ||
.setAllowEmpty(true) | ||
.call(); | ||
|
||
PathUtils.createParentDirectories(guide1HtmlToFetch); | ||
Files.writeString(guide1HtmlToFetch, "initial"); | ||
git.add().addFilepattern(".").call(); | ||
git.commit().setMessage("Pages first commit").call(); | ||
|
||
Files.writeString(guide1HtmlToFetch, FETCHED_GUIDE_1_CONTENT_HTML); | ||
git.add().addFilepattern(".").call(); | ||
git.commit().setMessage("Pages second commit").call(); | ||
|
||
git.checkout() | ||
.setName(QuarkusIO.SOURCE_BRANCH) | ||
.setCreateBranch(true) | ||
.setStartPoint(initialCommit) | ||
.call(); | ||
|
||
PathUtils.createParentDirectories(metadata1ToFetch); | ||
Files.writeString(metadata1ToFetch, METADATA_YAML); | ||
git.add().addFilepattern(".").call(); | ||
git.commit().setMessage("Source first commit").call(); | ||
} | ||
} | ||
|
||
@BeforeAll | ||
void waitForIndexing() { | ||
Awaitility.await().timeout(Duration.ofMinutes(1)) | ||
.untilAsserted(() -> when().get("http://localhost:" + managementPort() + "/q/health/ready") | ||
.then() | ||
.statusCode(200)); | ||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.BODY); | ||
} | ||
|
||
@AfterAll | ||
void deleteTmpDir() throws IOException { | ||
if (tmpDir != null) { | ||
tmpDir.close(); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
void synonymsTitle(String query, String result) { | ||
assertThat(searchHitSearchResult(query).hits()).extracting(GuideSearchHit::title) | ||
.contains(result); | ||
} | ||
|
||
private List<? extends Arguments> synonymsTitle() { | ||
return List.of( | ||
Arguments.of("REST Development Service", | ||
"A title with <span class=\"highlighted\">DevServices</span> in it as well as Vert.x and <span class=\"highlighted\">RESTEasy</span>"), | ||
Arguments.of("rest easy", | ||
"A title with DevServices in it as well as Vert.x and <span class=\"highlighted\">RESTEasy</span>"), | ||
Arguments.of("vertx", | ||
"A title with DevServices in it as well as <span class=\"highlighted\">Vert.x</span> and RESTEasy"), | ||
Arguments.of("rest api", | ||
"A title with DevServices in it as well as Vert.x and <span class=\"highlighted\">RESTEasy</span>")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
void synonymsContent(String query, Set<String> result) { | ||
assertThat(searchHitSearchResult(query).hits()).extracting(GuideSearchHit::content) | ||
.contains(result); | ||
} | ||
|
||
private List<? extends Arguments> synonymsContent() { | ||
return List.of( | ||
Arguments.of("Development Service", | ||
Set.of("Quarkus supports a feature called <span class=\"highlighted\">DevServices</span> that allows you to start various containers", | ||
"This page lists all the <span class=\"highlighted\">Dev</span> <span class=\"highlighted\">Services</span> that Quarkus supports.")), | ||
Arguments.of("dev Service", | ||
Set.of("Quarkus supports a feature called <span class=\"highlighted\">DevServices</span> that allows you to start various containers", | ||
"This page lists all the <span class=\"highlighted\">Dev</span> <span class=\"highlighted\">Services</span> that Quarkus supports.")), | ||
Arguments.of("rest easy", | ||
Set.of("<span class=\"highlighted\">RESTEasy</span> Classic Writing <span class=\"highlighted\">REST</span> Services with <span class=\"highlighted\">RESTEasy</span> Reactive.", | ||
"reactive <span class=\"highlighted\">REST</span> client.")), | ||
Arguments.of("vertx", | ||
Set.of("the quarkus-<span class=\"highlighted\">vertx</span> extension to your project.", | ||
"Migrating to RESTEasy Reactive Access the <span class=\"highlighted\">Vert.x</span> instance To access the managed <span class=\"highlighted\">Vert.x</span> instance, add")), | ||
Arguments.of("rest api", | ||
Set.of("<span class=\"highlighted\">RESTEasy</span> Classic Writing <span class=\"highlighted\">REST</span> Services with <span class=\"highlighted\">RESTEasy</span> Reactive.", | ||
"reactive <span class=\"highlighted\">REST</span> client."))); | ||
} | ||
|
||
private static SearchResult<GuideSearchHit> searchHitSearchResult(String q) { | ||
return given() | ||
.queryParam("q", q) | ||
.queryParam("contentSnippets", 2) | ||
.when().get(GUIDES_SEARCH) | ||
.then() | ||
.statusCode(200) | ||
.extract().body().as(SEARCH_RESULT_SEARCH_HITS); | ||
} | ||
|
||
private static final String METADATA_YAML = """ | ||
# Generated file. Do not edit | ||
--- | ||
types: | ||
reference: | ||
- title: A title with DevServices in it as well as Vert.x and RESTEasy | ||
filename: foo.adoc | ||
summary: This is a summary without words that are in synonyms list | ||
categories: "category1, category2" | ||
keywords: keyword1 keyword2 | ||
topics: | ||
- topic1 | ||
- topic2 | ||
extensions: | ||
- io.quarkus:extension1 | ||
- io.quarkus:extension2 | ||
id: foo | ||
type: reference | ||
url: /guides/foo | ||
"""; | ||
|
||
private static final String FETCHED_GUIDE_1_NAME = "foo"; | ||
private static final String FETCHED_GUIDE_1_CONTENT_HTML = """ | ||
<html> | ||
<head></head> | ||
<body> | ||
<h1></h1> | ||
<p>This is the guide body | ||
This guide shows how you can use virtual threads with RESTEasy Reactive and the reactive REST client. Learn more about virtual threads support on. | ||
<h2>RESTEasy Classic</h2> | ||
Writing REST Services with RESTEasy Reactive. | ||
Migrating to RESTEasy Reactive | ||
<h2>Access the Vert.x instance</h2> | ||
To access the managed Vert.x instance, add the quarkus-vertx extension to your project. | ||
<p> | ||
This page lists all the Dev Services that Quarkus supports. | ||
If you need multiple (shared) servers, you can configure the quarkus.elasticsearch.devservices.service-name attribute and indicate the server name. | ||
Quarkus supports a feature called DevServices that allows you to start various containers | ||
"""; | ||
} |