Skip to content

Commit 96fcd10

Browse files
committed
chore: fixed the negation query conversion
Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 54d23cc commit 96fcd10

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/DocumentQueryConversor.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.eclipse.jnosql.communication.semistructured.Element;
2626

2727
import java.util.List;
28+
import java.util.regex.Pattern;
2829

2930
final class DocumentQueryConversor {
3031

@@ -48,14 +49,13 @@ public static Bson convert(CriteriaCondition condition) {
4849
var criteriaCondition = document.get(CriteriaCondition.class);
4950
if (Condition.EQUALS.equals(criteriaCondition.condition())) {
5051
Element element = criteriaCondition.element();
51-
if(element.get() == null) {
52+
if (element.get() == null) {
5253
yield Filters.exists(element.name(), true);
5354
}
54-
yield Filters.ne(element.name(), element.get());
5555
}
56-
yield Filters.not(convert(criteriaCondition));
56+
yield Filters.nor(convert(criteriaCondition));
5757
}
58-
case LIKE -> Filters.regex(document.name(), value.toString());
58+
case LIKE -> Filters.regex(document.name(), Pattern.compile(prepareRegexValue(value.toString())));
5959
case AND -> {
6060
List<CriteriaCondition> andList = condition.element().value().get(new TypeReference<>() {
6161
});
@@ -67,7 +67,8 @@ public static Bson convert(CriteriaCondition condition) {
6767
});
6868
yield Filters.or(orList.stream()
6969
.map(DocumentQueryConversor::convert).toList());
70-
}case BETWEEN -> {
70+
}
71+
case BETWEEN -> {
7172
List<Object> betweenList = ValueUtil.convertToList(document.value());
7273
yield Filters.and(Filters.gte(document.name(), betweenList.get(0)),
7374
Filters.lte(document.name(), betweenList.get(1)));
@@ -78,5 +79,12 @@ public static Bson convert(CriteriaCondition condition) {
7879
};
7980
}
8081

82+
public static String prepareRegexValue(String rawData) {
83+
if (rawData == null)
84+
return "^$";
85+
return "^" + rawData
86+
.replaceAll("_", ".{1}")
87+
.replaceAll("%", ".{1,}");
88+
}
8189

8290
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2022 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Maximillian Arruda
14+
*/
15+
package org.eclipse.jnosql.databases.mongodb.communication;
16+
17+
import org.junit.jupiter.api.Test;
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
import org.junit.jupiter.params.provider.CsvSource;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
class DocumentQueryConversorTest {
24+
25+
@ParameterizedTest
26+
@CsvSource(textBlock = """
27+
Max_;^Max.{1}
28+
Max%;^Max.{1,}
29+
M_x;^M.{1}x
30+
M%x;^M.{1,}x
31+
_ax;^.{1}ax
32+
%ax;^.{1,}ax
33+
;^$
34+
""", delimiterString = ";")
35+
void shouldPrepareRegexValueSupportedByMongoDB(String rawValue, String expectedValue) {
36+
assertThat(DocumentQueryConversor.prepareRegexValue(rawValue))
37+
.as("The value should be prepared to be used in a MongoDB regex query: " +
38+
"the '_' character should matches any single character, and " +
39+
"the '%' character should matches any sequence of characters.")
40+
.isEqualTo(expectedValue);
41+
}
42+
43+
@Test
44+
void shouldReturnEmptyRegexWhenRawValueIsNull() {
45+
assertThat(DocumentQueryConversor.prepareRegexValue(null))
46+
.as("should return an empty regex when the raw value is null")
47+
.isEqualTo("^$");
48+
}
49+
}

0 commit comments

Comments
 (0)