HibernateSimpleQueryUtils { static String replaceSubstring(Pair<Integer, Integer> startEndIndexPair, String stringToReplaceIn, String replacement) { var startIndex = startEndIndexPair.getFirst(); var endIndex = startEndIndexPair.getSecond(); var predReplaceSubstring = startIndex <= 1 ? "" : stringToReplaceIn.substring(0, startIndex); var succReplaceSubstring = endIndex >= (stringToReplaceIn.length() - 1) ? "" : stringToReplaceIn.substring(endIndex + 1); return String.format("%s%s%s", predReplaceSubstring, replacement, succReplaceSubstring); }  static String makeQueryPrefixAndFuzzy(@NonNull String simpleQuery); static String createLuceneQueryString(Collection<String> values, String operator,
                                                  Function<String, String> function); static String createLuceneQueryString(Collection<String> values, String operator);  }
@Test public void replaceSubstringReplacesWholeStringWhenStartIndexZeroAndEndIndexLastOfWord() { var originalString = "replace"; var replacement = "java"; var startIndex = 0; var endIndex = originalString.length() - 1; var startEndIndexPair = Pair.of(startIndex, endIndex); var actualString = HibernateSimpleQueryUtils.replaceSubstring(startEndIndexPair, originalString, replacement); assertThat(actualString).isEqualTo(replacement); }
@Test public void replaceSubstringReplacesSubstringAtTheEndOfWord() { var originalString = "Spring Security"; var replacement = "Data"; var expectedString = "Spring Data"; var startIndex = 7; var endIndex = 14; var startEndIndexPair = Pair.of(startIndex, endIndex); var actualString = HibernateSimpleQueryUtils.replaceSubstring(startEndIndexPair, originalString, replacement); assertThat(actualString).isEqualTo(expectedString); }
@Test public void replaceSubstringReplacesAtTheBeginningOfWord() { var originalString = "Hello World"; var replacement = "Hi"; var expectedString = "Hi World"; var startIndex = 0; var endIndex = 4; var startEndIndexPair = Pair.of(startIndex, endIndex); var actualString = HibernateSimpleQueryUtils.replaceSubstring(startEndIndexPair, originalString, replacement); assertThat(actualString).isEqualTo(expectedString); }
@Test public void replaceSubstringReplacesInTheMiddleOfTheWord() { var originalString = "Oh hello there"; var replacement = "hi"; var expectedString = "Oh hi there"; var startIndex = 3; var endIndex = 7; var startEndIndexPair = Pair.of(startIndex, endIndex); var actualString = HibernateSimpleQueryUtils.replaceSubstring(startEndIndexPair, originalString, replacement); assertThat(actualString).isEqualTo(expectedString); }