Skip to content

Commit

Permalink
Merge pull request #144 from malpalma/other-offers-search-fix
Browse files Browse the repository at this point in the history
other offers search fix - search text case insensitive
  • Loading branch information
mndzielski authored Jun 7, 2022
2 parents 1c03d5e + f3ee946 commit 16d36bd
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,32 @@ protected List<Specification<OtherOffer>> fromOfferSpecific(OtherOfferSearchCrit
specifications.add(fromLocationNullable("location", criteria.getLocation()));
}
if (StringUtils.hasText(criteria.getSearchText())) {
String searchText = criteria.getSearchText().toLowerCase();
switch (criteria.getLang()) {
case UA -> specifications.add(searchTextUa(criteria.getSearchText()));
case EN -> specifications.add(searchTextEn(criteria.getSearchText()));
case RU -> specifications.add(searchTextRu(criteria.getSearchText()));
default -> specifications.add(searchText(criteria.getSearchText()));
case UA -> specifications.add(searchTextUa(searchText));
case EN -> specifications.add(searchTextEn(searchText));
case RU -> specifications.add(searchTextRu(searchText));
default -> specifications.add(searchText(searchText));
}
}
return specifications;
}

private Specification<OtherOffer> searchText(String searchText) {
return (root, cq, cb) -> cb.like(cb.concat(root.get("title"), root.get("description")), prepareForQuery(searchText));
return (root, cq, cb) ->
cb.like(cb.lower(cb.concat(root.get("title"), root.get("description"))), prepareForQuery(searchText));
}

private Specification<OtherOffer> searchTextUa(String searchText) {
return (root, cq, cb) -> cb.or(
cb.and(
cb.isNull(root.get("detectedLanguage")),
cb.like(cb.concat(root.get("title"), root.get("description")), prepareForQuery(searchText))
cb.like(cb.lower(cb.concat(root.get("title"), root.get("description"))),
prepareForQuery(searchText))
),
cb.and(
cb.like(cb.concat(root.get("titleUa"), root.get("descriptionUa")), prepareForQuery(searchText))
cb.like(cb.lower(cb.concat(root.get("titleUa"), root.get("descriptionUa"))),
prepareForQuery(searchText))
)
);
}
Expand All @@ -48,10 +52,12 @@ private Specification<OtherOffer> searchTextEn(String searchText) {
return (root, cq, cb) -> cb.or(
cb.and(
cb.isNull(root.get("detectedLanguage")),
cb.like(cb.concat(root.get("title"), root.get("description")), prepareForQuery(searchText))
cb.like(cb.lower(cb.concat(root.get("title"), root.get("description"))),
prepareForQuery(searchText))
),
cb.and(
cb.like(cb.concat(root.get("titleEn"), root.get("descriptionEn")), prepareForQuery(searchText))
cb.like(cb.lower(cb.concat(root.get("titleEn"), root.get("descriptionEn"))),
prepareForQuery(searchText))
)
);
}
Expand All @@ -60,10 +66,12 @@ private Specification<OtherOffer> searchTextRu(String searchText) {
return (root, cq, cb) -> cb.or(
cb.and(
cb.isNull(root.get("detectedLanguage")),
cb.like(cb.concat(root.get("title"), root.get("description")), prepareForQuery(searchText))
cb.like(cb.lower(cb.concat(root.get("title"), root.get("description"))),
prepareForQuery(searchText))
),
cb.and(
cb.like(cb.concat(root.get("titleRu"), root.get("descriptionRu")), prepareForQuery(searchText))
cb.like(cb.lower(cb.concat(root.get("titleRu"), root.get("descriptionRu"))),
prepareForQuery(searchText))
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
import org.springframework.data.repository.CrudRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
import pl.gov.coi.pomocua.ads.BaseResourceTest;
import pl.gov.coi.pomocua.ads.OffersVM;
import pl.gov.coi.pomocua.ads.translation.TranslationOfferVM;

import java.net.URI;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static pl.gov.coi.pomocua.ads.other.OtherTestDataGenerator.aOtherOffer;
import static pl.gov.coi.pomocua.ads.translation.TranslationTestDataGenerator.aTranslationOffer;

public class OtherOfferResourceTest extends BaseResourceTest<OtherOffer, OtherOfferVM> {

Expand All @@ -41,9 +46,40 @@ protected CrudRepository<OtherOffer, Long> getRepository() {
return repository;
}

//TODO search with criteria
@Nested
class Searching {
@Test
void shouldSearchBySearchTextWithIgnoreCaseTitle() {
OtherOfferVM offer1 = postOffer(aOtherOffer()
.title("Opieka nad dziećmi")
.description("Udzielę pomocy w opiece nad dziećmi")
.build());

postOffer(aOtherOffer().title("jakiś tytuł").description("jakiś opis").build());

OtherOfferSearchCriteria searchCriteria = new OtherOfferSearchCriteria();
searchCriteria.setSearchText("opieka");
var results = searchOffers(searchCriteria);

assertThat(results).hasSize(1).contains(offer1);
}

@Test
void shouldSearchBySearchTextWithIgnoreCaseDescription() {
OtherOfferVM offer1 = postOffer(aOtherOffer()
.title("Opieka nad dziećmi")
.description("Udzielę pomocy w opiece nad dziećmi")
.build());

postOffer(aOtherOffer().title("jakiś tytuł").description("jakiś opis").build());

OtherOfferSearchCriteria searchCriteria = new OtherOfferSearchCriteria();
searchCriteria.setSearchText("udzielę");
var results = searchOffers(searchCriteria);

assertThat(results).hasSize(1).contains(offer1);
}

@Test
void shouldReturnPageOfData() {
postOffer(aOtherOffer().title("a").build());
Expand All @@ -59,6 +95,18 @@ void shouldReturnPageOfData() {
assertThat(results.totalElements).isEqualTo(6);
assertThat(results.content).hasSize(2).extracting(r -> r.getTitle()).containsExactly("c", "d");
}

@Test
void shouldIgnoreDeactivatedOffer() {
OtherOfferVM offer = postOffer(aOtherOffer().build());
deleteOffer(offer.getId());

PageRequest page = PageRequest.of(0, 10);
var results = searchOffers(page);

assertThat(results.totalElements).isEqualTo(0);
assertThat(results.content).isEmpty();
}
}

@Nested
Expand Down Expand Up @@ -89,6 +137,21 @@ void shouldUpdateOffer() {
}
}

private List<OtherOfferVM> searchOffers(OtherOfferSearchCriteria searchCriteria) {
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/api/" + getOfferSuffix());
if (StringUtils.hasText(searchCriteria.getSearchText())) {
builder
.queryParam("searchText", searchCriteria.getSearchText());
}
if (searchCriteria.getLocation() != null) {
builder
.queryParam("location.city", searchCriteria.getLocation().getCity())
.queryParam("location.region", searchCriteria.getLocation().getRegion());
}
String url = builder.encode().toUriString();
return listOffers(URI.create(url)).content;
}

private OffersVM<OtherOfferVM> searchOffers(PageRequest pageRequest) {
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/api/" + getOfferSuffix());
builder.queryParam("page", pageRequest.getPageNumber());
Expand Down

0 comments on commit 16d36bd

Please sign in to comment.