Skip to content

Commit

Permalink
fix: number regex
Browse files Browse the repository at this point in the history
- add new unit test
  • Loading branch information
ajclopez committed Nov 14, 2022
1 parent af0e92a commit 4cee333
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
<goals>
<goal>sign</goal>
</goals>
<configuration>
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
33 changes: 14 additions & 19 deletions src/main/java/io/github/ajclopez/mss/MongoSpringSearch.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package io.github.ajclopez.mss;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

import io.github.ajclopez.mss.criteria.CriteriaImpl;
import io.github.ajclopez.mss.criteria.CriteriaQueryVisitor;
import io.github.ajclopez.mss.exception.ArgumentNotValidException;
import io.github.ajclopez.mss.model.*;
import io.github.ajclopez.mss.parser.QueryParser;
import io.github.ajclopez.mss.pattern.SearchPatterns;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import io.github.ajclopez.mss.criteria.CriteriaImpl;
import io.github.ajclopez.mss.criteria.CriteriaQueryVisitor;
import io.github.ajclopez.mss.exception.ArgumentNotValidException;
import io.github.ajclopez.mss.model.CastType;
import io.github.ajclopez.mss.model.Configuration;
import io.github.ajclopez.mss.model.KeySearchOperation;
import io.github.ajclopez.mss.model.SearchCriteria;
import io.github.ajclopez.mss.model.SortSearchOperation;
import io.github.ajclopez.mss.parser.QueryParser;
import io.github.ajclopez.mss.pattern.SearchPatterns;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

/**
*
Expand Down Expand Up @@ -78,7 +73,7 @@ public static Query mss(String query, Optional<Configuration> configuration) thr
filters.add(criteria);
break;
case FILTER:
filterCriteria = parseFilterAdvanced(mongoQuery, criteria.getValue(), casters);
filterCriteria = parseFilterAdvanced(criteria.getValue(), casters);
break;
case SKIP:
parseSkip(mongoQuery, criteria.getValue());
Expand Down Expand Up @@ -189,7 +184,7 @@ private static void parseSort(Query query, String value) {
}
}

private static Criteria parseFilterAdvanced(Query query, String input, Map<String, CastType> casters) {
private static Criteria parseFilterAdvanced(String input, Map<String, CastType> casters) {

QueryLexer lexer = new QueryLexer(CharStreams.fromString(input));
CommonTokenStream tokens = new CommonTokenStream(lexer);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/io/github/ajclopez/mss/parser/QueryParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.github.ajclopez.mss.parser;

import io.github.ajclopez.mss.exception.ArgumentNotValidException;
import io.github.ajclopez.mss.model.CastType;
import io.github.ajclopez.mss.model.SearchCriteria;
import io.github.ajclopez.mss.model.SearchOperation;
import io.github.ajclopez.mss.pattern.SearchPatterns;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
Expand All @@ -15,12 +21,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.github.ajclopez.mss.exception.ArgumentNotValidException;
import io.github.ajclopez.mss.model.CastType;
import io.github.ajclopez.mss.model.SearchCriteria;
import io.github.ajclopez.mss.model.SearchOperation;
import io.github.ajclopez.mss.pattern.SearchPatterns;

/**
*
* Class used to parse a search query {@code String} and create a list of {@link SearchCriteria}.
Expand Down Expand Up @@ -81,11 +81,11 @@ public static SearchCriteria criteriaParser(String condition, Map<String, CastTy

/**
*
* The value is automatically casted: {@code Number, Date, Boolean, RegExp, String, List}.
* The value is automatically cast: {@code Number, Date, Boolean, RegExp, String, List}.
*
* @param value The value will be cast.
* @param caster Specify casting per value.
* @return Object Casted to {@code Number, Date, Boolean, RegExp, String, List}.
* @return Object Cast to {@code Number, Date, Boolean, RegExp, String, List}.
*/
public static Object parseValue(String value, CastType caster) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SearchPatterns {
private static final String OPERATOR_PATTERN = "(!?)([^><!=]+)([><]=?|!?=|)(.*)";
private static final String REGEX_PATTERN = "^/(.*)/([igmsx]*)$";
private static final String SORT_PATTERN = "^(\\+|-)?(.*)";
private static final String NUMBER_PATTERN = "^(-?)(\\d+)([,.0-9]*)$";
private static final String NUMBER_PATTERN = "^(-?)(\\d+)([.0-9]*)$";

private static Pattern operatorPattern;
private static Pattern regExpPattern;
Expand Down
31 changes: 19 additions & 12 deletions src/test/java/io/github/ajclopez/mss/MongoSpringSearchTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package io.github.ajclopez.mss;

import com.mongodb.BasicDBList;
import io.github.ajclopez.mss.exception.ArgumentNotValidException;
import io.github.ajclopez.mss.model.CastType;
import io.github.ajclopez.mss.model.Configuration;
import io.github.ajclopez.mss.model.LogicalOperation;
import org.bson.Document;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.data.mongodb.core.query.Query;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
Expand All @@ -9,17 +19,6 @@
import java.util.Optional;
import java.util.regex.Pattern;

import org.bson.Document;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.data.mongodb.core.query.Query;

import com.mongodb.BasicDBList;

import io.github.ajclopez.mss.exception.ArgumentNotValidException;
import io.github.ajclopez.mss.model.CastType;
import io.github.ajclopez.mss.model.Configuration;

public class MongoSpringSearchTest {


Expand Down Expand Up @@ -322,7 +321,7 @@ public void canUseCastingPattern() {
@Test
public void complexQuery() {

String query = "filter=(city=Madrid or city=Spanin) and gender=female"
String query = "filter=(city=Madrid or city=Barcelona) and gender=female"
+ "&skip=50"
+ "&limit=10"
+ "&sort=-birthday"
Expand All @@ -341,5 +340,13 @@ public void complexQuery() {
Assert.assertEquals(Integer.valueOf(1), mongoQuery.getFieldsObject().getInteger("age"));
Assert.assertEquals(Integer.valueOf(-1), mongoQuery.getSortObject().getInteger("birthday"));
}

@Test
public void whenUseInvalidLogicalOperator() {

LogicalOperation result = LogicalOperation.getLogicalOperation("y");
Assert.assertEquals(null, result);

}

}
43 changes: 43 additions & 0 deletions src/test/java/io/github/ajclopez/mss/QueryParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.ajclopez.mss;

import io.github.ajclopez.mss.model.CastType;
import io.github.ajclopez.mss.model.SearchCriteria;
import io.github.ajclopez.mss.parser.QueryParser;
import org.junit.Assert;
import org.junit.Test;

import java.util.regex.Pattern;

public class QueryParserTest {


@Test
public void whenQueryNotMatchThenReturnNull() {

SearchCriteria result = QueryParser.criteriaParser("", null);
Assert.assertEquals(null, result);
}

@Test
public void whenRegexNotMatchWithPatternThenReturnPatternWithValue() {

String regex = "^58";
Object result = QueryParser.parseValue(regex, CastType.PATTERN);
Assert.assertEquals(Pattern.compile(regex).toString(), result.toString());
}

@Test
public void whenFalseValueThenReturnBooleanWithFalseValue() {

Object result = QueryParser.parseValue("false", null);
Assert.assertEquals(Boolean.parseBoolean("false"), result);
}

@Test
public void whenValueIsNullThenReturnNullValue() {

Object result = QueryParser.parseValue(null, null);
Assert.assertEquals(null, result);
}

}
21 changes: 21 additions & 0 deletions src/test/java/io/github/ajclopez/mss/SearchPatternsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.ajclopez.mss;

import io.github.ajclopez.mss.pattern.SearchPatterns;
import org.junit.Assert;
import org.junit.Test;

public class SearchPatternsTest {

@Test
public void canUseDefaultFlagWithNullOptions() {
int flags = SearchPatterns.getFlags(null);
Assert.assertEquals(0x00, flags);
}

@Test
public void canUseDefaultFlagWithUnknownRegexFlag() {
int flags = SearchPatterns.getFlags("t");
Assert.assertEquals(0x00, flags);
}

}

0 comments on commit 4cee333

Please sign in to comment.