-
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.
Merge pull request #210 from yrodiere/reference
Add endpoints listing reference data
- Loading branch information
Showing
14 changed files
with
224 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,33 @@ | ||
package io.quarkus.search.app; | ||
|
||
import java.util.Comparator; | ||
|
||
public final class QuarkusVersions { | ||
private QuarkusVersions() { | ||
} | ||
|
||
public static final String LATEST = "latest"; | ||
public static final String MAIN = "main"; | ||
public static final String V3_2 = "3.2"; | ||
|
||
public static final Comparator<String> COMPARATOR = new Comparator<String>() { | ||
@Override | ||
public int compare(String left, String right) { | ||
if (left.equals(right)) { | ||
return 0; | ||
} else if (left.equals(MAIN)) { | ||
return 1; | ||
} else if (right.equals(MAIN)) { | ||
return -1; | ||
} else if (left.equals(LATEST)) { | ||
// "latest" actually means "latest non-snapshot", so it's older than main. | ||
return 1; | ||
} else if (right.equals(LATEST)) { | ||
return -1; | ||
} else { | ||
return left.compareTo(right); | ||
} | ||
} | ||
}; | ||
|
||
} |
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,82 @@ | ||
package io.quarkus.search.app; | ||
|
||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.transaction.Transactional; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
|
||
import io.quarkus.search.app.cache.MethodNameCacheKeyGenerator; | ||
import io.quarkus.search.app.entity.Guide; | ||
import io.quarkus.search.app.entity.Language; | ||
|
||
import io.quarkus.cache.Cache; | ||
import io.quarkus.cache.CacheName; | ||
import io.quarkus.cache.CacheResult; | ||
|
||
import org.hibernate.search.engine.search.aggregation.AggregationKey; | ||
import org.hibernate.search.mapper.orm.session.SearchSession; | ||
|
||
import org.eclipse.microprofile.openapi.annotations.Operation; | ||
|
||
@ApplicationScoped | ||
@Path("/") | ||
@Transactional | ||
@org.jboss.resteasy.reactive.Cache(maxAge = 120) | ||
public class ReferenceService { | ||
|
||
private static final String REFERENCE_CACHE = "reference-cache"; | ||
|
||
@CacheName(REFERENCE_CACHE) | ||
Cache cache; | ||
|
||
@Inject | ||
SearchSession session; | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Operation(summary = "List available versions") | ||
@Path("/versions") | ||
@CacheResult(cacheName = REFERENCE_CACHE, keyGenerator = MethodNameCacheKeyGenerator.class) | ||
public List<String> versions() { | ||
return listAllValues("quarkusVersion", QuarkusVersions.COMPARATOR.reversed()); | ||
} | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Operation(summary = "List available languages") | ||
@Path("/languages") | ||
@CacheResult(cacheName = REFERENCE_CACHE, keyGenerator = MethodNameCacheKeyGenerator.class) | ||
public List<String> languages() { | ||
return Arrays.stream(Language.values()).map(lang -> lang.code).toList(); | ||
} | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Operation(summary = "List available categories") | ||
@Path("/categories") | ||
@CacheResult(cacheName = REFERENCE_CACHE, keyGenerator = MethodNameCacheKeyGenerator.class) | ||
public List<String> categories() { | ||
return listAllValues("categories", Comparator.naturalOrder()); | ||
} | ||
|
||
public void invalidateCaches() { | ||
cache.invalidateAll().subscribe().asCompletionStage().join(); | ||
} | ||
|
||
private List<String> listAllValues(String fieldName, Comparator<String> comparator) { | ||
var aggKey = AggregationKey.<Map<String, Long>> of("versions"); | ||
var result = session.search(Guide.class) | ||
.where(f -> f.matchAll()) | ||
.aggregation(aggKey, f -> f.terms().field(fieldName, String.class)) | ||
.fetch(0); | ||
return result.aggregation(aggKey).keySet().stream().sorted(comparator).toList(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/quarkus/search/app/cache/MethodNameCacheKeyGenerator.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,12 @@ | ||
package io.quarkus.search.app.cache; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import io.quarkus.cache.CacheKeyGenerator; | ||
|
||
public class MethodNameCacheKeyGenerator implements CacheKeyGenerator { | ||
@Override | ||
public Object generate(Method method, Object... methodParams) { | ||
return method.getName(); | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
src/test/java/io/quarkus/search/app/ReferenceServiceTest.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,67 @@ | ||
package io.quarkus.search.app; | ||
|
||
import static io.restassured.RestAssured.when; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import io.quarkus.search.app.testsupport.QuarkusIOSample; | ||
import io.quarkus.search.app.testsupport.SetupUtil; | ||
|
||
import io.quarkus.test.common.http.TestHTTPEndpoint; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.common.mapper.TypeRef; | ||
import io.restassured.filter.log.LogDetail; | ||
|
||
@QuarkusTest | ||
@TestHTTPEndpoint(SearchService.class) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
@QuarkusIOSample.Setup | ||
class ReferenceServiceTest { | ||
private static final TypeRef<List<String>> LIST_OF_STRINGS = new TypeRef<>() { | ||
}; | ||
|
||
private List<String> get(String referenceName) { | ||
return when().get("/" + referenceName) | ||
.then() | ||
.statusCode(200) | ||
.extract().body().as(LIST_OF_STRINGS); | ||
} | ||
|
||
@BeforeAll | ||
void setup() { | ||
SetupUtil.waitForIndexing(getClass()); | ||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.BODY); | ||
} | ||
|
||
@Test | ||
void versions() { | ||
assertThat(get("versions")).containsExactly("main", "latest", "3.2"); | ||
} | ||
|
||
@Test | ||
void languages() { | ||
assertThat(get("languages")).containsExactly("en", "es", "pt", "cn", "ja"); | ||
} | ||
|
||
@Test | ||
void categories() { | ||
assertThat(get("categories")).containsExactly( | ||
"alt-languages", | ||
"architecture", | ||
"cloud", | ||
"compatibility", | ||
"core", | ||
"data", | ||
"miscellaneous", | ||
"security", | ||
"web", | ||
"writing-extensions"); | ||
} | ||
} |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.