Skip to content

Commit

Permalink
Return empty immediately for the empty query.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmalloy committed Aug 30, 2024
1 parent b4fcceb commit db59295
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/jasper/web/rest/RefController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -42,6 +44,7 @@
import javax.validation.constraints.Size;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static jasper.domain.Ref.URL_LEN;
import static jasper.domain.proj.HasOrigin.ORIGIN_LEN;
Expand Down Expand Up @@ -126,6 +129,11 @@ HttpEntity<Page<RefDto>> getRefPage(
@RequestParam(required = false) @Size(max = 100) List<@Length(max = TAG_LEN) @Pattern(regexp = Plugin.REGEX) String> noPluginResponse,
@RequestParam(required = false) @Length(max = SEARCH_LEN) String search
) {
if ("!@*".equals(query)) {
ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(100, TimeUnit.DAYS).cachePublic())
.body(Page.empty(pageable));
}
var rankedSort = false;
if (pageable.getSort().isUnsorted() || pageable.getSort().getOrderFor("rank") != null) {
if (isNotBlank(search)) {
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/jasper/service/RefServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,40 @@ void testGetPageRefWithQuery() {
assertThat(page.getTotalElements())
.isEqualTo(1);
}

@Test
void testGetEmptyPageRefWithEmptyQuery() {
refWithTags("public");
refWithTags("public", "custom", "extra");

var page = refService.page(
RefFilter
.builder()
.query("!@*")
.build(),
PageRequest.of(0, 10));

assertThat(page.getTotalElements())
.isEqualTo(0);
}

@Test
void testGetEmptyPageRefWithEmptyQueryNoTags() {
refWithTags();
refWithTags("public");
refWithTags("public", "custom", "extra");

var page = refService.page(
RefFilter
.builder()
.query("!@*")
.build(),
PageRequest.of(0, 10));

assertThat(page.getTotalElements())
.isEqualTo(0);
}

@Test
void testGetPageRefWithQueryPrivateTagFailed() {
refWithTags("public");
Expand Down Expand Up @@ -462,6 +496,40 @@ void testGetPageRefWithOriginQuery() {
.isEqualTo(1);
}

@Test
void testGetEmptyPageRefWithOriginEmptyQuery() {
refWithOriginTags("@a", "public");
refWithOriginTags("@a", "public", "custom", "extra");

var page = refService.page(
RefFilter
.builder()
.query("!@*")
.build(),
PageRequest.of(0, 10));

assertThat(page.getTotalElements())
.isEqualTo(0);
}

@Test
void testGetEmptyPageRefWithOriginEmptyQueryNoTags() {
refWithOriginTags("@a");
refWithOriginTags("@b");
refWithOriginTags("@a", "public");
refWithOriginTags("@a", "public", "custom", "extra");

var page = refService.page(
RefFilter
.builder()
.query("!@*")
.build(),
PageRequest.of(0, 10));

assertThat(page.getTotalElements())
.isEqualTo(0);
}

@Test
void testGetPageRefWithOriginOrQuery() {
refWithOriginTags("@a", "public", "custom");
Expand Down

0 comments on commit db59295

Please sign in to comment.