1+ /*
2+ *
3+ * Copyright (c) 2025 Contributors to the Eclipse Foundation
4+ * All rights reserved. This program and the accompanying materials
5+ * are made available under the terms of the Eclipse Public License v1.0
6+ * and Apache License v2.0 which accompanies this distribution.
7+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
8+ * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
9+ *
10+ * You may elect to redistribute this code under either of these licenses.
11+ *
12+ * Contributors:
13+ *
14+ * Otavio Santana
15+ *
16+ */
17+ package org .eclipse .jnosql .databases .neo4j .communication ;
18+
19+
20+ import org .junit .jupiter .api .DisplayName ;
21+ import org .junit .jupiter .api .Test ;
22+ import org .junit .jupiter .params .ParameterizedTest ;
23+ import org .junit .jupiter .params .provider .CsvSource ;
24+
25+ import static org .assertj .core .api .Assertions .*;
26+
27+ class LikeToCypherRegexTest {
28+
29+
30+ @ ParameterizedTest (name = "LIKE \" {0}\" -> regex \" {1}\" " )
31+ @ CsvSource ({
32+ // contains / starts / ends
33+ "'%Ota%', '^.*\\ QOta\\ E.*$'" ,
34+ "'Ota%', '^\\ QOta\\ E.*$'" ,
35+ "'%Ota', '^.*\\ QOta\\ E$'" ,
36+ // exact (no wildcards)
37+ "'Ota', '^\\ QOta\\ E$'" ,
38+ // single-char wildcard
39+ "'Ot_', '^\\ QOt\\ E.$'" ,
40+ // mixed case with both _ and %
41+ "'_%ta%', '^..*\\ Qta\\ E.*$'"
42+ })
43+ @ DisplayName ("Converts SQL LIKE to anchored Cypher regex" )
44+ void shouldConvertSqlLikeToAnchoredRegex (String like , String expectedRegex ) {
45+ String actual = LikeToCypherRegex .INSTANCE .toCypherRegex (like );
46+ assertThat (actual ).isEqualTo (expectedRegex );
47+ }
48+
49+ @ Test
50+ @ DisplayName ("Escapes regex metacharacters in literals" )
51+ void shouldEscapeRegexMetacharacters () {
52+ // Input contains regex metas: . ^ $ ( ) [ ] { } + ? * | \
53+ String like = "%a.^$()[]{}+?*|\\ b%" ;
54+ String regex = LikeToCypherRegex .INSTANCE .toCypherRegex (like );
55+
56+ assertThat (regex )
57+ .startsWith ("^.*" )
58+ .endsWith (".*$" )
59+ // The literal run should be quoted as one block
60+ .contains ("\\ Qa.^$()[]{}+?*|\\ b\\ E" );
61+ }
62+
63+ @ Test
64+ @ DisplayName ("Returns never-matching regex for null" )
65+ void shouldReturnNeverMatchingForNull () {
66+ String regex = LikeToCypherRegex .INSTANCE .toCypherRegex (null );
67+ assertThat (regex ).isEqualTo ("(?!)" );
68+ }
69+
70+ @ Test
71+ @ DisplayName ("Handles empty string as exact empty match" )
72+ void shouldHandleEmptyString () {
73+ String regex = LikeToCypherRegex .INSTANCE .toCypherRegex ("" );
74+ assertThat (regex ).isEqualTo ("^$" ); // not "^\\Q\\E$"
75+ }
76+
77+ @ Test
78+ @ DisplayName ("Handles only wildcards" )
79+ void shouldHandleOnlyWildcards () {
80+ assertThat (LikeToCypherRegex .INSTANCE .toCypherRegex ("%" )).isEqualTo ("^.*$" );
81+ assertThat (LikeToCypherRegex .INSTANCE .toCypherRegex ("%%" )).isEqualTo ("^.*.*$" );
82+ assertThat (LikeToCypherRegex .INSTANCE .toCypherRegex ("_" )).isEqualTo ("^.$" );
83+ assertThat (LikeToCypherRegex .INSTANCE .toCypherRegex ("__" )).isEqualTo ("^..$" );
84+ }
85+ }
0 commit comments