Add type flags (#37)

This commit is contained in:
Kostya 2020-08-04 16:38:55 +03:00 committed by GitHub
parent 992b0a8586
commit 3952c3ea07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 105 additions and 8 deletions

View File

@ -11,7 +11,7 @@
</parent>
<artifactId>fraudo</artifactId>
<version>0.0.19</version>
<version>0.0.20</version>
<name>Fraudo</name>
<description>Language for describing antifraud patterns</description>

View File

@ -36,6 +36,8 @@ relationalExpression
| in_grey_list
| in_list
| like
| is_mobile
| is_recurrent
;
unaryExpression
@ -97,4 +99,12 @@ sum_refund
in
: 'in' LPAREN (stringExpression) DELIMETER string_list RPAREN
;
is_mobile
: 'isMobile' LPAREN RPAREN
;
is_recurrent
: 'isRecurrent' LPAREN RPAREN
;

View File

@ -32,6 +32,14 @@ currency
: 'currency' LPAREN RPAREN
;
payer_type
: 'payerType' LPAREN RPAREN
;
token_provider
: 'tokenProvider' LPAREN RPAREN
;
count
: 'count' LPAREN STRING time_window (group_by)? RPAREN
;

View File

@ -7,6 +7,7 @@ import com.rbkmoney.fraudo.payment.aggregator.CountPaymentAggregator;
import com.rbkmoney.fraudo.payment.aggregator.SumPaymentAggregator;
import com.rbkmoney.fraudo.payment.resolver.PaymentGroupResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTimeWindowResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTypeResolver;
import com.rbkmoney.fraudo.payment.visitor.impl.FirstFindVisitorImpl;
import com.rbkmoney.fraudo.resolver.CountryResolver;
import com.rbkmoney.fraudo.resolver.FieldResolver;
@ -20,6 +21,7 @@ public interface FraudVisitorFactory {
InListFinder<T, U> listFinder,
FieldResolver<T, U> fieldPairResolver,
PaymentGroupResolver<T, U> paymentGroupResolver,
PaymentTimeWindowResolver timeWindowResolver);
PaymentTimeWindowResolver timeWindowResolver,
PaymentTypeResolver<T> paymentTypeResolver);
}

View File

@ -7,6 +7,7 @@ import com.rbkmoney.fraudo.payment.aggregator.CountPaymentAggregator;
import com.rbkmoney.fraudo.payment.aggregator.SumPaymentAggregator;
import com.rbkmoney.fraudo.payment.resolver.PaymentGroupResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTimeWindowResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTypeResolver;
import com.rbkmoney.fraudo.payment.visitor.CountVisitor;
import com.rbkmoney.fraudo.payment.visitor.CustomFuncVisitor;
import com.rbkmoney.fraudo.payment.visitor.ListVisitor;
@ -26,12 +27,18 @@ public class FraudVisitorFactoryImpl implements FraudVisitorFactory {
InListFinder<T, U> listFinder,
FieldResolver<T, U> fieldResolver,
PaymentGroupResolver<T, U> paymentGroupResolver,
PaymentTimeWindowResolver timeWindowResolver) {
PaymentTimeWindowResolver timeWindowResolver,
PaymentTypeResolver<T> paymentTypeResolver) {
CountVisitor<T> countVisitor = new CountVisitorImpl<>(countPaymentAggregator, fieldResolver, paymentGroupResolver, timeWindowResolver);
SumVisitor<T> sumVisitor = new SumVisitorImpl<>(sumPaymentAggregator, fieldResolver, paymentGroupResolver, timeWindowResolver);
ListVisitor<T> listVisitor = new ListVisitorImpl<>(listFinder, fieldResolver);
CustomFuncVisitor<T> customFuncVisitor = new CustomFuncVisitorImpl<>(
uniqueValueAggregator, countryResolver, fieldResolver, paymentGroupResolver, timeWindowResolver);
uniqueValueAggregator,
countryResolver,
fieldResolver,
paymentGroupResolver,
timeWindowResolver,
paymentTypeResolver);
return new FirstFindVisitorImpl<>(countVisitor, sumVisitor, listVisitor, customFuncVisitor, fieldResolver);
}

View File

@ -7,6 +7,7 @@ import com.rbkmoney.fraudo.payment.aggregator.CountPaymentAggregator;
import com.rbkmoney.fraudo.payment.aggregator.SumPaymentAggregator;
import com.rbkmoney.fraudo.payment.resolver.PaymentGroupResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTimeWindowResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTypeResolver;
import com.rbkmoney.fraudo.payment.visitor.CountVisitor;
import com.rbkmoney.fraudo.payment.visitor.CustomFuncVisitor;
import com.rbkmoney.fraudo.payment.visitor.ListVisitor;
@ -26,12 +27,18 @@ public class FullVisitorFactoryImpl implements FraudVisitorFactory {
InListFinder<T, U> listFinder,
FieldResolver<T, U> fieldResolver,
PaymentGroupResolver<T, U> paymentGroupResolver,
PaymentTimeWindowResolver timeWindowResolver) {
PaymentTimeWindowResolver timeWindowResolver,
PaymentTypeResolver<T> paymentTypeResolver) {
CountVisitor<T> countVisitor = new CountVisitorImpl<>(countPaymentAggregator, fieldResolver, paymentGroupResolver, timeWindowResolver);
SumVisitor<T> sumVisitor = new SumVisitorImpl<>(sumPaymentAggregator, fieldResolver, paymentGroupResolver, timeWindowResolver);
ListVisitor<T> listVisitor = new ListVisitorImpl<>(listFinder, fieldResolver);
CustomFuncVisitor<T> customFuncVisitor = new CustomFuncVisitorImpl<>(
uniqueValueAggregator, countryResolver, fieldResolver, paymentGroupResolver, timeWindowResolver);
uniqueValueAggregator,
countryResolver,
fieldResolver,
paymentGroupResolver,
timeWindowResolver,
paymentTypeResolver);
return new FullVisitorImpl<>(countVisitor, sumVisitor, listVisitor, customFuncVisitor, fieldResolver);
}

View File

@ -0,0 +1,9 @@
package com.rbkmoney.fraudo.payment.resolver;
public interface PaymentTypeResolver<T> {
Boolean isMobile(T model);
Boolean isRecurrent(T model);
}

View File

@ -10,4 +10,8 @@ public interface CustomFuncVisitor<T> {
Integer visitUnique(UniqueContext ctx, T model);
boolean visitCheckMobile(Is_mobileContext ctx, T model);
boolean visitCheckRecurrent(Is_recurrentContext ctx, T model);
}

View File

@ -4,6 +4,7 @@ import com.rbkmoney.fraudo.aggregator.UniqueValueAggregator;
import com.rbkmoney.fraudo.model.Pair;
import com.rbkmoney.fraudo.payment.resolver.PaymentGroupResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTimeWindowResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTypeResolver;
import com.rbkmoney.fraudo.payment.visitor.CustomFuncVisitor;
import com.rbkmoney.fraudo.resolver.CountryResolver;
import com.rbkmoney.fraudo.resolver.FieldResolver;
@ -20,6 +21,7 @@ public class CustomFuncVisitorImpl<T, U> implements CustomFuncVisitor<T> {
private final FieldResolver<T, U> fieldResolver;
private final PaymentGroupResolver<T, U> groupResolver;
private final PaymentTimeWindowResolver timeWindowResolver;
private final PaymentTypeResolver<T> paymentTypeResolver;
@Override
public String visitCountryBy(Country_byContext ctx, T model) {
@ -49,4 +51,14 @@ public class CustomFuncVisitorImpl<T, U> implements CustomFuncVisitor<T> {
);
}
@Override
public boolean visitCheckMobile(Is_mobileContext ctx, T model) {
return paymentTypeResolver.isMobile(model);
}
@Override
public boolean visitCheckRecurrent(Is_recurrentContext ctx, T model) {
return paymentTypeResolver.isRecurrent(model);
}
}

View File

@ -365,4 +365,13 @@ public class FirstFindVisitorImpl<T extends BaseModel, U> extends FraudoPaymentB
);
}
@Override
public Boolean visitIs_mobile(Is_mobileContext ctx) {
return customFuncVisitor.visitCheckMobile(ctx, threadLocalModel.get());
}
@Override
public Boolean visitIs_recurrent(Is_recurrentContext ctx) {
return customFuncVisitor.visitCheckRecurrent(ctx, threadLocalModel.get());
}
}

View File

@ -10,6 +10,7 @@ import com.rbkmoney.fraudo.payment.factory.FraudVisitorFactoryImpl;
import com.rbkmoney.fraudo.payment.factory.FullVisitorFactoryImpl;
import com.rbkmoney.fraudo.payment.resolver.PaymentGroupResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTimeWindowResolver;
import com.rbkmoney.fraudo.payment.resolver.PaymentTypeResolver;
import com.rbkmoney.fraudo.resolver.CountryResolver;
import com.rbkmoney.fraudo.resolver.FieldResolver;
import com.rbkmoney.fraudo.test.constant.PaymentCheckedField;
@ -36,6 +37,8 @@ public class AbstractPaymentTest {
InListFinder<PaymentModel, PaymentCheckedField> inListFinder;
@Mock
PaymentTimeWindowResolver timeWindowResolver;
@Mock
PaymentTypeResolver<PaymentModel> paymentModelPaymentTypeResolver;
private FieldResolver<PaymentModel, PaymentCheckedField> fieldResolver = new PaymentModelFieldResolver();
private PaymentGroupResolver<PaymentModel, PaymentCheckedField> paymentGroupResolver = new PaymentGroupResolver<>(fieldResolver);
@ -60,7 +63,8 @@ public class AbstractPaymentTest {
inListFinder,
fieldResolver,
paymentGroupResolver,
timeWindowResolver)
timeWindowResolver,
paymentModelPaymentTypeResolver)
.visit(parse, model);
}
@ -74,7 +78,8 @@ public class AbstractPaymentTest {
inListFinder,
fieldResolver,
paymentGroupResolver,
timeWindowResolver)
timeWindowResolver,
paymentModelPaymentTypeResolver)
.visit(parse, model);
}

View File

@ -123,6 +123,28 @@ public class CustomTest extends AbstractPaymentTest {
assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus());
}
@Test
public void tokenProviderTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/is_mobile.frd");
when(paymentModelPaymentTypeResolver.isMobile(any())).thenReturn(true);
ParseContext parseContext = getParseContext(resourceAsStream);
PaymentModel model = new PaymentModel();
ResultModel result = invoke(parseContext, model);
assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus());
}
@Test
public void payerTypeTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/is_recurrent.frd");
when(paymentModelPaymentTypeResolver.isRecurrent(any())).thenReturn(true);
ParseContext parseContext = getParseContext(resourceAsStream);
PaymentModel model = new PaymentModel();
ResultModel result = invoke(parseContext, model);
assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus());
}
@Test
public void amountTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/amount.frd");

View File

@ -0,0 +1 @@
rule: isMobile() -> accept;

View File

@ -0,0 +1 @@
rule: isRecurrent() -> decline;