Change eqCountry to get country by field

This commit is contained in:
k.struzhkin 2018-12-07 10:48:37 +03:00
parent 72b8ddc739
commit 8a0afd9b44
7 changed files with 38 additions and 33 deletions

View File

@ -22,12 +22,13 @@ expression
| sum_error #sumErrorExpression
| unique #uniqueExpression
| in #inFunctionExpression
| inWhiteList #inWhiteListExpression
| inBlackList #inBlackListExpression
| in_white_list #inWhiteListExpression
| in_black_list #inBlackListExpression
| like #likeFunctionExpression
| equals_country #equalsCountryFunctionExpression
| country_by #countryByFunctionExpression
| IDENTIFIER #identifierExpression
| DECIMAL #decimalExpression
| STRING #stringExpression
;
comparator
@ -74,11 +75,11 @@ in
: 'in' LPAREN STRING DELIMETER string_list RPAREN
;
inWhiteList
in_white_list
: 'inWhiteList' LPAREN STRING RPAREN
;
inBlackList
in_black_list
: 'inBlackList' LPAREN STRING RPAREN
;
@ -86,23 +87,23 @@ like
: 'like' LPAREN STRING DELIMETER STRING RPAREN
;
equals_country
: 'equalsCountry' LPAREN RPAREN
;
DELIMETER : ',' ;
country_by
: 'countryBy' LPAREN STRING DELIMETER STRING RPAREN
;
result
: 'accept' | '3ds' | 'decline' | 'notify'
;
string_list
: STRING (',' STRING | WS)+
;
STRING
: '"' (~["\r\n] | '""')* '"'
;
string_list
: STRING (',' STRING | WS)+
;
DELIMETER : ',' ;
COMMENT
: '#' ~[\r\n]* -> skip

View File

@ -1,9 +1,9 @@
package com.rbkmoney.fraudo.resolver;
import com.rbkmoney.fraudo.constant.CheckedField;
public interface CountryResolver {
String resolveCountryBank(String bank);
String resolveCountryIp(String ip);
String resolveCountry(CheckedField checkedField, String value);
}

View File

@ -19,10 +19,10 @@ public class CustomFuncVisitorImpl extends FraudoBaseVisitor<Object> {
private final CountryResolver countryResolver;
@Override
public Object visitEquals_country(FraudoParser.Equals_countryContext ctx) {
String countryIp = countryResolver.resolveCountryIp(fraudModel.getIp());
String countryBank = countryResolver.resolveCountryBank(fraudModel.getBin());
return countryBank.equals(countryIp);
public Object visitCountry_by(FraudoParser.Country_byContext ctx) {
String fieldName = TextUtil.safeGetText(ctx.STRING(0));
String value = TextUtil.safeGetText(ctx.STRING(1));
return countryResolver.resolveCountry(CheckedField.getByValue(fieldName), value);
}
@Override

View File

@ -5,6 +5,7 @@ import com.rbkmoney.fraudo.FraudoParser;
import com.rbkmoney.fraudo.constant.ResultStatus;
import com.rbkmoney.fraudo.exception.NotImplementedOperatorException;
import com.rbkmoney.fraudo.exception.UnknownResultException;
import com.rbkmoney.fraudo.utils.TextUtil;
import lombok.RequiredArgsConstructor;
import java.util.Optional;
@ -47,6 +48,11 @@ public class FastFraudVisitorImpl extends FraudoBaseVisitor<Object> {
return Double.valueOf(ctx.DECIMAL().getText());
}
@Override
public Object visitStringExpression(FraudoParser.StringExpressionContext ctx) {
return TextUtil.safeGetText(ctx.STRING());
}
@Override
public Object visitNotExpression(com.rbkmoney.fraudo.FraudoParser.NotExpressionContext ctx) {
return !((Boolean) this.visit(ctx.expression()));
@ -119,8 +125,8 @@ public class FastFraudVisitorImpl extends FraudoBaseVisitor<Object> {
}
@Override
public Object visitEquals_country(FraudoParser.Equals_countryContext ctx) {
return customFuncVisitor.visitEquals_country(ctx);
public Object visitCountry_by(FraudoParser.Country_byContext ctx) {
return customFuncVisitor.visitCountry_by(ctx);
}
@Override
@ -139,13 +145,13 @@ public class FastFraudVisitorImpl extends FraudoBaseVisitor<Object> {
}
@Override
public Object visitInWhiteList(FraudoParser.InWhiteListContext ctx) {
return listVisitor.visitInWhiteList(ctx);
public Object visitIn_white_list(FraudoParser.In_white_listContext ctx) {
return listVisitor.visitIn_white_list(ctx);
}
@Override
public Object visitInBlackList(FraudoParser.InBlackListContext ctx) {
return listVisitor.visitInBlackList(ctx);
public Object visitIn_black_list(FraudoParser.In_black_listContext ctx) {
return listVisitor.visitIn_black_list(ctx);
}
private boolean asBoolean(com.rbkmoney.fraudo.FraudoParser.ExpressionContext ctx) {

View File

@ -17,17 +17,16 @@ public class ListVisitorImpl extends FraudoBaseVisitor<Object> {
private final InListFinder whiteListFinder;
@Override
public Object visitInWhiteList(FraudoParser.InWhiteListContext ctx) {
public Object visitIn_white_list(FraudoParser.In_white_listContext ctx) {
String fieldName = TextUtil.safeGetText(ctx.STRING());
String fieldValue = FieldResolver.resolveString(fieldName, fraudModel);
return whiteListFinder.findInList(CheckedField.getByValue(fieldName), fieldValue);
}
@Override
public Object visitInBlackList(FraudoParser.InBlackListContext ctx) {
public Object visitIn_black_list(FraudoParser.In_black_listContext ctx) {
String fieldName = TextUtil.safeGetText(ctx.STRING());
String fieldValue = FieldResolver.resolveString(fieldName, fraudModel);
return blackListFinder.findInList(CheckedField.getByValue(fieldName), fieldValue);
}
}

View File

@ -189,13 +189,12 @@ public class FraudoTest {
public void eqCountryTest() throws Exception {
InputStream resourceAsStream = FraudoTest.class.getResourceAsStream("/rules/eq_country.frd");
Mockito.when(countryResolver.resolveCountryBank(anyString())).thenReturn("RU");
Mockito.when(countryResolver.resolveCountryIp(anyString())).thenReturn("RU");
Mockito.when(countryResolver.resolveCountry(any(), anyString())).thenReturn("RU");
Object result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.NOTIFY, result);
Mockito.when(countryResolver.resolveCountryBank(anyString())).thenReturn("US");
Mockito.when(countryResolver.resolveCountry(any(), anyString())).thenReturn("US");
resourceAsStream = FraudoTest.class.getResourceAsStream("/rules/eq_country.frd");
result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.ACCEPT, result);

View File

@ -1,2 +1,2 @@
rule: equalsCountry()
rule: countryBy("ip", "192.168.1.1") = "RU"
-> notify;