forked from Onlineberatung/onlineBeratung-userService
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #12 from virtualidentityag/DIAKONIE-286-fix-search…
…-alternative fix: fix search alternative
- Loading branch information
Showing
3 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoder.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,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); | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
...t/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoderTest.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,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); | ||
} | ||
} |