Skip to content

Commit

Permalink
Merge pull request #12 from virtualidentityag/DIAKONIE-286-fix-search…
Browse files Browse the repository at this point in the history
…-alternative

fix: fix search alternative
  • Loading branch information
tkuzynow authored Apr 25, 2024
2 parents 0405c60 + 4a8f326 commit e9f72b7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.caritas.cob.userservice.api.adapters.web.controller;

import static com.google.common.collect.Lists.newArrayList;

import com.github.jknack.handlebars.internal.lang3.StringUtils;
import com.google.common.base.Splitter;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SearchQueryDecoder {

private SearchQueryDecoder() {
throw new IllegalStateException("Utility class");
}

private static final String SEPARATOR = "+";

/* Allows the query string to contain plus sign, but not change it to space.
See https://bugs.openjdk.org/browse/JDK-8179507 */
public static String decode(String query) {
if (StringUtils.isBlank(query)) {
return StringUtils.EMPTY;
}
var parts = Splitter.on(SEPARATOR).split(query);
return newArrayList(parts).stream()
.map(SearchQueryDecoder::decodePart)
.collect(Collectors.joining(SEPARATOR))
.trim();
}

private static String decodePart(String s) {
return URLDecoder.decode(s, StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@
import de.caritas.cob.userservice.api.tenant.TenantContext;
import de.caritas.cob.userservice.generated.api.adapters.web.controller.UsersApi;
import io.swagger.annotations.Api;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -777,7 +775,7 @@ public ResponseEntity<List<ConsultantResponseDTO>> getConsultants(@RequestParam
@Override
public ResponseEntity<ConsultantSearchResultDTO> searchConsultants(
String query, Integer page, Integer perPage, String field, String order) {
var decodedInfix = URLDecoder.decode(query, StandardCharsets.UTF_8).trim();
var decodedInfix = SearchQueryDecoder.decode(query);
var isAscending = order.equalsIgnoreCase("asc");
var mappedField = consultantDtoMapper.mappedFieldOf(field);
var resultMap =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.caritas.cob.userservice.api.adapters.web.controller;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class SearchQueryDecoderTest {

@ParameterizedTest
@CsvSource({
"[email protected], [email protected]",
"*, *",
"[email protected], [email protected]",
"A Test, A Test",
",''",
"\"\",\"\"",
"stringWithoutPlusSign,stringWithoutPlusSign",
"firstname.lastname%2Bconsultant%40virtual-identity.com, [email protected]"
})
void decode_Should_Decode_String_Not_ChangingPlusIntoSpace(String input, String expectedOutput) {
assertThat(SearchQueryDecoder.decode(input).trim()).isEqualTo(expectedOutput);
}
}

0 comments on commit e9f72b7

Please sign in to comment.