diff --git a/pom.xml b/pom.xml index 26f37ed..a5ae018 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ fraudo - 0.0.7-SNAPSHOT + 0.0.8-SNAPSHOT 2.0.0.0 diff --git a/src/main/java/com/rbkmoney/fraudo/utils/key/generator/CommonKeyGenerator.java b/src/main/java/com/rbkmoney/fraudo/utils/key/generator/CommonKeyGenerator.java index b4d24e8..f3eabd0 100644 --- a/src/main/java/com/rbkmoney/fraudo/utils/key/generator/CommonKeyGenerator.java +++ b/src/main/java/com/rbkmoney/fraudo/utils/key/generator/CommonKeyGenerator.java @@ -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) diff --git a/src/main/java/com/rbkmoney/fraudo/utils/key/generator/CountKeyGenerator.java b/src/main/java/com/rbkmoney/fraudo/utils/key/generator/CountKeyGenerator.java index 9abedf0..ab70e1b 100644 --- a/src/main/java/com/rbkmoney/fraudo/utils/key/generator/CountKeyGenerator.java +++ b/src/main/java/com/rbkmoney/fraudo/utils/key/generator/CountKeyGenerator.java @@ -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(), diff --git a/src/main/java/com/rbkmoney/fraudo/utils/key/generator/SumKeyGenerator.java b/src/main/java/com/rbkmoney/fraudo/utils/key/generator/SumKeyGenerator.java index ac6cb32..1748eec 100644 --- a/src/main/java/com/rbkmoney/fraudo/utils/key/generator/SumKeyGenerator.java +++ b/src/main/java/com/rbkmoney/fraudo/utils/key/generator/SumKeyGenerator.java @@ -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(), diff --git a/src/main/java/com/rbkmoney/fraudo/utils/key/generator/UniqueKeyGenerator.java b/src/main/java/com/rbkmoney/fraudo/utils/key/generator/UniqueKeyGenerator.java index 13c3f7a..4eea4d3 100644 --- a/src/main/java/com/rbkmoney/fraudo/utils/key/generator/UniqueKeyGenerator.java +++ b/src/main/java/com/rbkmoney/fraudo/utils/key/generator/UniqueKeyGenerator.java @@ -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()); } } diff --git a/src/test/java/com/rbkmoney/fraudo/CountTest.java b/src/test/java/com/rbkmoney/fraudo/CountTest.java index 67c79a7..8b3baed 100644 --- a/src/test/java/com/rbkmoney/fraudo/CountTest.java +++ b/src/test/java/com/rbkmoney/fraudo/CountTest.java @@ -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()); } } diff --git a/src/test/java/com/rbkmoney/fraudo/CustomTest.java b/src/test/java/com/rbkmoney/fraudo/CustomTest.java index d29be6a..bc5c0f1 100644 --- a/src/test/java/com/rbkmoney/fraudo/CustomTest.java +++ b/src/test/java/com/rbkmoney/fraudo/CustomTest.java @@ -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()); } } diff --git a/src/test/resources/rules/count_card_token.frd b/src/test/resources/rules/count_card_token.frd index 274f5e3..a583e88 100644 --- a/src/test/resources/rules/count_card_token.frd +++ b/src/test/resources/rules/count_card_token.frd @@ -1,2 +1,2 @@ -rule: count("card_token", 1444) >= 10 +rule: count("card_token", 1444) >= 10 AND count("card_token", 1444) >= 9 -> decline; \ No newline at end of file diff --git a/src/test/resources/rules/count_uniqGroupBy_window.frd b/src/test/resources/rules/count_uniqGroupBy_window.frd index 824d15d..39cebb6 100644 --- a/src/test/resources/rules/count_uniqGroupBy_window.frd +++ b/src/test/resources/rules/count_uniqGroupBy_window.frd @@ -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; \ No newline at end of file