Fix uniq cache (#17)

* Fix uniq caching
This commit is contained in:
Kostya 2019-10-09 12:55:58 +03:00 committed by GitHub
parent 6718b8e3d4
commit 9fa862fe43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 80 additions and 75 deletions

View File

@ -11,7 +11,7 @@
</parent>
<artifactId>fraudo</artifactId>
<version>0.0.7-SNAPSHOT</version>
<version>0.0.8-SNAPSHOT</version>
<properties>
<hamcrest.junit.version>2.0.0.0</hamcrest.junit.version>

View File

@ -25,13 +25,13 @@ public class CommonKeyGenerator {
.toString();
}
static String generateKeyGroupedErrorFunction(TerminalNode targetNode,
TerminalNode errorCodeNode,
ParseTree parseTree,
FraudoParser.Time_windowContext timeWindowContext,
FraudoParser.Group_byContext groupByContext) {
String target = TextUtil.safeGetText(targetNode);
String errorCode = TextUtil.safeGetText(errorCodeNode);
static String generateKeyGroupedTwoFieldFunction(TerminalNode firstField,
TerminalNode secondField,
ParseTree parseTree,
FraudoParser.Time_windowContext timeWindowContext,
FraudoParser.Group_byContext groupByContext) {
String target = TextUtil.safeGetText(firstField);
String errorCode = TextUtil.safeGetText(secondField);
return new StringBuilder()
.append(parseTree)
.append(target)

View File

@ -26,7 +26,7 @@ public class CountKeyGenerator {
public static String generateErrorKey(ParserRuleContext context) {
FraudoParser.Count_errorContext ctx = (FraudoParser.Count_errorContext) context;
return CommonKeyGenerator.generateKeyGroupedErrorFunction(ctx.STRING(0),
return CommonKeyGenerator.generateKeyGroupedTwoFieldFunction(ctx.STRING(0),
ctx.STRING(1),
ctx.children.get(0),
ctx.time_window(),

View File

@ -26,7 +26,7 @@ public class SumKeyGenerator {
public static String generateErrorKey(ParserRuleContext context) {
FraudoParser.Sum_errorContext ctx = (FraudoParser.Sum_errorContext) context;
return CommonKeyGenerator.generateKeyGroupedErrorFunction(ctx.STRING(0),
return CommonKeyGenerator.generateKeyGroupedTwoFieldFunction(ctx.STRING(0),
ctx.STRING(1),
ctx.children.get(0),
ctx.time_window(),

View File

@ -1,7 +1,6 @@
package com.rbkmoney.fraudo.utils.key.generator;
import com.rbkmoney.fraudo.FraudoParser;
import com.rbkmoney.fraudo.utils.TextUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.antlr.v4.runtime.ParserRuleContext;
@ -10,12 +9,11 @@ import org.antlr.v4.runtime.ParserRuleContext;
public class UniqueKeyGenerator {
public static String generate(ParserRuleContext context) {
FraudoParser.UniqueContext ctx = (FraudoParser.UniqueContext) context;
String field = TextUtil.safeGetText(ctx.STRING(0));
String fieldBy = TextUtil.safeGetText(ctx.STRING(1));
return new StringBuilder()
.append(ctx.children.get(0))
.append(field)
.append(fieldBy)
.toString();
return CommonKeyGenerator.generateKeyGroupedTwoFieldFunction(
ctx.STRING(0),
ctx.STRING(1),
ctx.children.get(0),
ctx.time_window(),
ctx.group_by());
}
}

View File

@ -10,7 +10,9 @@ import org.mockito.MockitoAnnotations;
import java.io.InputStream;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
public class CountTest extends AbstractFraudoTest {
@ -22,64 +24,66 @@ public class CountTest extends AbstractFraudoTest {
@Test
public void countTest() throws Exception {
InputStream resourceAsStream = CountTest.class.getResourceAsStream("/rules/count.frd");
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
Mockito.when(countAggregator.countError(anyObject(), any(), any(), anyString(), any())).thenReturn(6);
Mockito.when(countAggregator.countSuccess(anyObject(), any(), any(), any())).thenReturn(4);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
when(countAggregator.countError(anyObject(), any(), any(), anyString(), any())).thenReturn(6);
when(countAggregator.countSuccess(anyObject(), any(), any(), any())).thenReturn(4);
com.rbkmoney.fraudo.FraudoParser.ParseContext parseContext = getParseContext(resourceAsStream);
ResultModel result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
Assert.assertEquals("1", result.getRuleChecked());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals("1", result.getRuleChecked());
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(9);
Mockito.when(countAggregator.countError(anyObject(), any(), any(), anyString(), any())).thenReturn(6);
Mockito.when(countAggregator.countSuccess(anyObject(), any(), any(), any())).thenReturn(6);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(9);
when(countAggregator.countError(anyObject(), any(), any(), anyString(), any())).thenReturn(6);
when(countAggregator.countSuccess(anyObject(), any(), any(), any())).thenReturn(6);
result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
}
@Test
public void countGroupByTest() throws Exception {
InputStream resourceAsStream = CountTest.class.getResourceAsStream("/rules/countGroupBy.frd");
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
com.rbkmoney.fraudo.FraudoParser.ParseContext parseContext = getParseContext(resourceAsStream);
ResultModel result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
Assert.assertEquals("1", result.getRuleChecked());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals("1", result.getRuleChecked());
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(1);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(1);
result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus());
assertEquals(ResultStatus.NORMAL, result.getResultStatus());
}
@Test
public void countTimeWindowGroupByTest() throws Exception {
InputStream resourceAsStream = CountTest.class.getResourceAsStream("/rules/countTimeWindowGroupBy.frd");
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
com.rbkmoney.fraudo.FraudoParser.ParseContext parseContext = getParseContext(resourceAsStream);
ResultModel result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
Assert.assertEquals("1", result.getRuleChecked());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals("1", result.getRuleChecked());
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(1);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(1);
result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus());
assertEquals(ResultStatus.NORMAL, result.getResultStatus());
resourceAsStream = CountTest.class.getResourceAsStream("/rules/countTimeWindowGroupBy_2.frd");
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
parseContext = getParseContext(resourceAsStream);
result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
Assert.assertEquals("1", result.getRuleChecked());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals("1", result.getRuleChecked());
}
@Test
public void countCardTokenTest() throws Exception {
InputStream resourceAsStream = CountTest.class.getResourceAsStream("/rules/count_card_token.frd");
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
com.rbkmoney.fraudo.FraudoParser.ParseContext parseContext = getParseContext(resourceAsStream);
ResultModel result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
verify(countAggregator, times(1)).count(anyObject(), any(), any(), any());
}
}

View File

@ -4,15 +4,15 @@ import com.rbkmoney.fraudo.constant.ResultStatus;
import com.rbkmoney.fraudo.exception.UnknownResultException;
import com.rbkmoney.fraudo.model.FraudModel;
import com.rbkmoney.fraudo.model.ResultModel;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import java.io.InputStream;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
public class CustomTest extends AbstractFraudoTest {
@ -26,47 +26,47 @@ public class CustomTest extends AbstractFraudoTest {
@Test
public void threeDsTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/three_ds.frd");
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
ResultModel result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.THREE_DS, result.getResultStatus());
assertEquals(ResultStatus.THREE_DS, result.getResultStatus());
}
@Test
public void highRiskTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/highRisk.frd");
Mockito.when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10);
ResultModel result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.HIGH_RISK, result.getResultStatus());
assertEquals(ResultStatus.HIGH_RISK, result.getResultStatus());
}
@Test
public void notifyTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/notify.frd");
ResultModel result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus());
Assert.assertEquals(1, result.getNotificationsRule().size());
assertEquals(ResultStatus.NORMAL, result.getResultStatus());
assertEquals(1, result.getNotificationsRule().size());
}
@Test
public void declineTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/decline.frd");
ResultModel result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
Assert.assertEquals("test_11", result.getRuleChecked());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals("test_11", result.getRuleChecked());
}
@Test
public void acceptTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/accept.frd");
ResultModel result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
}
@Test
public void ruleIsNotFireTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/rule_is_not_fire.frd");
ResultModel result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus());
assertEquals(ResultStatus.NORMAL, result.getResultStatus());
}
@Test(expected = UnknownResultException.class)
@ -82,18 +82,18 @@ public class CustomTest extends AbstractFraudoTest {
FraudModel model = new FraudModel();
model.setEmail(TEST_GMAIL_RU);
ResultModel result = invoke(parseContext, model);
Assert.assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
}
@Test
public void inCountryTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/in_country.frd");
Mockito.when(countryResolver.resolveCountry(any(), anyString())).thenReturn("RU");
when(countryResolver.resolveCountry(any(), anyString())).thenReturn("RU");
com.rbkmoney.fraudo.FraudoParser.ParseContext parseContext = getParseContext(resourceAsStream);
FraudModel model = new FraudModel();
ResultModel result = invoke(parseContext, model);
Assert.assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
}
@Test
@ -103,16 +103,16 @@ public class CustomTest extends AbstractFraudoTest {
FraudModel model = new FraudModel();
model.setAmount(56L);
ResultModel result = invoke(parseContext, model);
Assert.assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
}
@Test
public void catchTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/catch.frd");
Mockito.when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenThrow(new UnknownResultException("as"));
when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenThrow(new UnknownResultException("as"));
com.rbkmoney.fraudo.FraudoParser.ParseContext parseContext = getParseContext(resourceAsStream);
ResultModel result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
}
@Test
@ -122,11 +122,11 @@ public class CustomTest extends AbstractFraudoTest {
FraudModel model = new FraudModel();
model.setEmail(TEST_GMAIL_RU);
ResultModel result = invoke(parseContext, model);
Assert.assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
model.setEmail("teeeee");
result = invoke(parseContext, model);
Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus());
assertEquals(ResultStatus.NORMAL, result.getResultStatus());
}
@Test
@ -136,45 +136,47 @@ public class CustomTest extends AbstractFraudoTest {
FraudModel model = new FraudModel();
model.setEmail(TEST_GMAIL_RU);
ResultModel result = invoke(parseContext, model);
Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus());
assertEquals(ResultStatus.NORMAL, result.getResultStatus());
}
@Test
public void uniqCountTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/count_uniq.frd");
Mockito.when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenReturn(2);
when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenReturn(2);
com.rbkmoney.fraudo.FraudoParser.ParseContext parseContext = getParseContext(resourceAsStream);
ResultModel result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
}
@Test
public void uniqCountGroupByTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/count_uniqGroupBy_window.frd");
Mockito.when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenReturn(2);
when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenReturn(2);
com.rbkmoney.fraudo.FraudoParser.ParseContext parseContext = getParseContext(resourceAsStream);
ResultModel result = invokeParse(parseContext);
Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus());
assertEquals(ResultStatus.DECLINE, result.getResultStatus());
verify(uniqueValueAggregator, times(1)).countUniqueValue(any(), any(), any(), any(), any());
}
@Test
public void eqCountryTest() throws Exception {
InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/eq_country.frd");
Mockito.when(countryResolver.resolveCountry(any(), anyString())).thenReturn("RU");
when(countryResolver.resolveCountry(any(), anyString())).thenReturn("RU");
ResultModel result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus());
Assert.assertEquals(1, result.getNotificationsRule().size());
assertEquals(ResultStatus.NORMAL, result.getResultStatus());
assertEquals(1, result.getNotificationsRule().size());
Mockito.when(countryResolver.resolveCountry(any(), anyString())).thenReturn("US");
when(countryResolver.resolveCountry(any(), anyString())).thenReturn("US");
resourceAsStream = CustomTest.class.getResourceAsStream("/rules/eq_country.frd");
result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus());
assertEquals(ResultStatus.NORMAL, result.getResultStatus());
resourceAsStream = CustomTest.class.getResourceAsStream("/rules/accept_with_notify.frd");
result = parseAndVisit(resourceAsStream);
Assert.assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
Assert.assertEquals(2, result.getNotificationsRule().size());
assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
assertEquals(2, result.getNotificationsRule().size());
}
}

View File

@ -1,2 +1,2 @@
rule: count("card_token", 1444) >= 10
rule: count("card_token", 1444) >= 10 AND count("card_token", 1444) >= 9
-> decline;

View File

@ -1,2 +1,3 @@
rule: unique("email", "ip", 1444, 123, "party_id", "shop_id") < 4
AND unique("email", "ip", 1444, 123, "party_id", "shop_id") < 4
-> decline;