From 992b0a85863a250b13b80aea17cf950f80eaf3a2 Mon Sep 17 00:00:00 2001 From: Kostya Date: Tue, 21 Jul 2020 16:33:39 +0300 Subject: [PATCH] Add full result visitor (#36) --- build_utils | 2 +- pom.xml | 4 +- .../rbkmoney/fraudo/model/ResultModel.java | 9 +-- .../com/rbkmoney/fraudo/model/RuleResult.java | 16 +++++ .../visitor/impl/FirstFindP2PVisitorImpl.java | 11 ++-- .../factory/FullVisitorFactoryImpl.java | 38 ++++++++++++ .../visitor/impl/FirstFindVisitorImpl.java | 13 ++-- .../payment/visitor/impl/FullVisitorImpl.java | 45 ++++++++++++++ .../rbkmoney/fraudo/utils/ResultUtils.java | 28 +++++++++ .../rbkmoney/fraudo/AbstractPaymentTest.java | 15 +++++ .../java/com/rbkmoney/fraudo/CountTest.java | 30 +++++----- .../java/com/rbkmoney/fraudo/CustomTest.java | 59 ++++++++++--------- .../java/com/rbkmoney/fraudo/ListTest.java | 13 ++-- .../com/rbkmoney/fraudo/RealTimerTest.java | 13 ++-- .../java/com/rbkmoney/fraudo/SumTest.java | 20 ++++--- .../com/rbkmoney/fraudo/p2p/CountP2PTest.java | 28 +++++---- .../rbkmoney/fraudo/p2p/CustomP2PTest.java | 53 +++++++++-------- .../java/com/rbkmoney/fraudo/p2p/P2PTest.java | 6 +- .../com/rbkmoney/fraudo/p2p/ParenP2PTest.java | 3 +- .../com/rbkmoney/fraudo/p2p/SumP2PTest.java | 18 +++--- src/test/resources/rules/payment_template.frd | 2 +- 21 files changed, 291 insertions(+), 135 deletions(-) create mode 100644 src/main/java/com/rbkmoney/fraudo/model/RuleResult.java create mode 100644 src/main/java/com/rbkmoney/fraudo/payment/factory/FullVisitorFactoryImpl.java create mode 100644 src/main/java/com/rbkmoney/fraudo/payment/visitor/impl/FullVisitorImpl.java create mode 100644 src/main/java/com/rbkmoney/fraudo/utils/ResultUtils.java diff --git a/build_utils b/build_utils index 07039c2..5401838 160000 --- a/build_utils +++ b/build_utils @@ -1 +1 @@ -Subproject commit 07039c262ceb8fe944e4dae3840278494049ca0f +Subproject commit 540183862bc9fd04682e226de2056a320fd44be9 diff --git a/pom.xml b/pom.xml index c024c69..848a990 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ fraudo - 0.0.18 + 0.0.19 Fraudo Language for describing antifraud patterns @@ -110,7 +110,7 @@ org.jacoco jacoco-maven-plugin - 0.8.2 + 0.8.4 ${sonar.jacoco.reportPath} true diff --git a/src/main/java/com/rbkmoney/fraudo/model/ResultModel.java b/src/main/java/com/rbkmoney/fraudo/model/ResultModel.java index 8408706..c031e5c 100644 --- a/src/main/java/com/rbkmoney/fraudo/model/ResultModel.java +++ b/src/main/java/com/rbkmoney/fraudo/model/ResultModel.java @@ -1,6 +1,5 @@ package com.rbkmoney.fraudo.model; -import com.rbkmoney.fraudo.constant.ResultStatus; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -12,12 +11,6 @@ import java.util.List; @AllArgsConstructor public class ResultModel { - private ResultStatus resultStatus; - private String ruleChecked; - private List notificationsRule; + private List ruleResults; - public ResultModel(ResultStatus resultStatus, List notificationsRule) { - this.resultStatus = resultStatus; - this.notificationsRule = notificationsRule; - } } diff --git a/src/main/java/com/rbkmoney/fraudo/model/RuleResult.java b/src/main/java/com/rbkmoney/fraudo/model/RuleResult.java new file mode 100644 index 0000000..d9f1d39 --- /dev/null +++ b/src/main/java/com/rbkmoney/fraudo/model/RuleResult.java @@ -0,0 +1,16 @@ +package com.rbkmoney.fraudo.model; + +import com.rbkmoney.fraudo.constant.ResultStatus; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class RuleResult { + + private ResultStatus resultStatus; + private String ruleChecked; + +} diff --git a/src/main/java/com/rbkmoney/fraudo/p2p/visitor/impl/FirstFindP2PVisitorImpl.java b/src/main/java/com/rbkmoney/fraudo/p2p/visitor/impl/FirstFindP2PVisitorImpl.java index bd4d96e..86b59f3 100644 --- a/src/main/java/com/rbkmoney/fraudo/p2p/visitor/impl/FirstFindP2PVisitorImpl.java +++ b/src/main/java/com/rbkmoney/fraudo/p2p/visitor/impl/FirstFindP2PVisitorImpl.java @@ -2,13 +2,13 @@ package com.rbkmoney.fraudo.p2p.visitor.impl; import com.rbkmoney.fraudo.FraudoP2PBaseVisitor; import com.rbkmoney.fraudo.FraudoP2PParser.*; -import com.rbkmoney.fraudo.FraudoPaymentParser; import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.exception.NotImplementedOperatorException; import com.rbkmoney.fraudo.exception.NotValidContextException; import com.rbkmoney.fraudo.exception.UnknownResultException; import com.rbkmoney.fraudo.model.BaseModel; import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.model.RuleResult; import com.rbkmoney.fraudo.p2p.generator.*; import com.rbkmoney.fraudo.p2p.visitor.CountP2PVisitor; import com.rbkmoney.fraudo.p2p.visitor.CustomP2PFuncVisitor; @@ -63,7 +63,7 @@ public class FirstFindP2PVisitorImpl extends FraudoP2PBa @Override public ResultModel visitParse(ParseContext ctx) { - List notifications = new ArrayList<>(); + List results = new ArrayList<>(); for (int i = 0; i < ctx.fraud_rule().size(); i++) { Fraud_ruleContext fraudRuleContext = ctx.fraud_rule().get(i); ResultStatus result = (ResultStatus) visit(fraudRuleContext); @@ -71,12 +71,13 @@ public class FirstFindP2PVisitorImpl extends FraudoP2PBa if (result == null) { throw new UnknownResultException(fraudRuleContext.getText()); } else if (result == ResultStatus.NOTIFY) { - notifications.add(key); + results.add(new RuleResult(result, key)); } else if (result != ResultStatus.NORMAL) { - return new ResultModel(result, key, notifications); + results.add(new RuleResult(result, key)); + return new ResultModel(results); } } - return new ResultModel(ResultStatus.NORMAL, notifications); + return new ResultModel(results); } @Override diff --git a/src/main/java/com/rbkmoney/fraudo/payment/factory/FullVisitorFactoryImpl.java b/src/main/java/com/rbkmoney/fraudo/payment/factory/FullVisitorFactoryImpl.java new file mode 100644 index 0000000..0531610 --- /dev/null +++ b/src/main/java/com/rbkmoney/fraudo/payment/factory/FullVisitorFactoryImpl.java @@ -0,0 +1,38 @@ +package com.rbkmoney.fraudo.payment.factory; + +import com.rbkmoney.fraudo.aggregator.UniqueValueAggregator; +import com.rbkmoney.fraudo.finder.InListFinder; +import com.rbkmoney.fraudo.model.BaseModel; +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.visitor.CountVisitor; +import com.rbkmoney.fraudo.payment.visitor.CustomFuncVisitor; +import com.rbkmoney.fraudo.payment.visitor.ListVisitor; +import com.rbkmoney.fraudo.payment.visitor.SumVisitor; +import com.rbkmoney.fraudo.payment.visitor.impl.*; +import com.rbkmoney.fraudo.resolver.CountryResolver; +import com.rbkmoney.fraudo.resolver.FieldResolver; + +public class FullVisitorFactoryImpl implements FraudVisitorFactory { + + @Override + public FirstFindVisitorImpl createVisitor( + CountPaymentAggregator countPaymentAggregator, + SumPaymentAggregator sumPaymentAggregator, + UniqueValueAggregator uniqueValueAggregator, + CountryResolver countryResolver, + InListFinder listFinder, + FieldResolver fieldResolver, + PaymentGroupResolver paymentGroupResolver, + PaymentTimeWindowResolver timeWindowResolver) { + CountVisitor countVisitor = new CountVisitorImpl<>(countPaymentAggregator, fieldResolver, paymentGroupResolver, timeWindowResolver); + SumVisitor sumVisitor = new SumVisitorImpl<>(sumPaymentAggregator, fieldResolver, paymentGroupResolver, timeWindowResolver); + ListVisitor listVisitor = new ListVisitorImpl<>(listFinder, fieldResolver); + CustomFuncVisitor customFuncVisitor = new CustomFuncVisitorImpl<>( + uniqueValueAggregator, countryResolver, fieldResolver, paymentGroupResolver, timeWindowResolver); + return new FullVisitorImpl<>(countVisitor, sumVisitor, listVisitor, customFuncVisitor, fieldResolver); + } + +} diff --git a/src/main/java/com/rbkmoney/fraudo/payment/visitor/impl/FirstFindVisitorImpl.java b/src/main/java/com/rbkmoney/fraudo/payment/visitor/impl/FirstFindVisitorImpl.java index 0bb81a8..a642955 100644 --- a/src/main/java/com/rbkmoney/fraudo/payment/visitor/impl/FirstFindVisitorImpl.java +++ b/src/main/java/com/rbkmoney/fraudo/payment/visitor/impl/FirstFindVisitorImpl.java @@ -7,6 +7,7 @@ import com.rbkmoney.fraudo.exception.NotValidContextException; import com.rbkmoney.fraudo.exception.UnknownResultException; import com.rbkmoney.fraudo.model.BaseModel; import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.model.RuleResult; import com.rbkmoney.fraudo.payment.generator.RuleKeyGenerator; import com.rbkmoney.fraudo.payment.visitor.CountVisitor; import com.rbkmoney.fraudo.payment.visitor.CustomFuncVisitor; @@ -67,7 +68,7 @@ public class FirstFindVisitorImpl extends FraudoPaymentB @Override public ResultModel visitParse(ParseContext ctx) { - List notifications = new ArrayList<>(); + List results = new ArrayList<>(); for (int i = 0; i < ctx.fraud_rule().size(); i++) { Fraud_ruleContext fraudRuleContext = ctx.fraud_rule().get(i); ResultStatus result = (ResultStatus) visit(fraudRuleContext); @@ -75,15 +76,13 @@ public class FirstFindVisitorImpl extends FraudoPaymentB if (result == null) { throw new UnknownResultException(fraudRuleContext.getText()); } else if (result == ResultStatus.NOTIFY) { - notifications.add(key); + results.add(new RuleResult(result, key)); } else if (result != ResultStatus.NORMAL) { - if (result == ResultStatus.DECLINE_AND_NOTIFY || result == ResultStatus.ACCEPT_AND_NOTIFY) { - notifications.add(key); - } - return new ResultModel(result, key, notifications); + results.add(new RuleResult(result, key)); + return new ResultModel(results); } } - return new ResultModel(ResultStatus.NORMAL, notifications); + return new ResultModel(results); } @Override diff --git a/src/main/java/com/rbkmoney/fraudo/payment/visitor/impl/FullVisitorImpl.java b/src/main/java/com/rbkmoney/fraudo/payment/visitor/impl/FullVisitorImpl.java new file mode 100644 index 0000000..f029906 --- /dev/null +++ b/src/main/java/com/rbkmoney/fraudo/payment/visitor/impl/FullVisitorImpl.java @@ -0,0 +1,45 @@ +package com.rbkmoney.fraudo.payment.visitor.impl; + +import com.rbkmoney.fraudo.constant.ResultStatus; +import com.rbkmoney.fraudo.exception.UnknownResultException; +import com.rbkmoney.fraudo.model.BaseModel; +import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.model.RuleResult; +import com.rbkmoney.fraudo.payment.generator.RuleKeyGenerator; +import com.rbkmoney.fraudo.payment.visitor.CountVisitor; +import com.rbkmoney.fraudo.payment.visitor.CustomFuncVisitor; +import com.rbkmoney.fraudo.payment.visitor.ListVisitor; +import com.rbkmoney.fraudo.payment.visitor.SumVisitor; +import com.rbkmoney.fraudo.resolver.FieldResolver; +import lombok.extern.slf4j.Slf4j; + +import java.util.ArrayList; +import java.util.List; + +import static com.rbkmoney.fraudo.FraudoPaymentParser.Fraud_ruleContext; +import static com.rbkmoney.fraudo.FraudoPaymentParser.ParseContext; + +@Slf4j +public class FullVisitorImpl extends FirstFindVisitorImpl { + + public FullVisitorImpl(CountVisitor countVisitor, SumVisitor sumVisitor, ListVisitor listVisitor, CustomFuncVisitor customFuncVisitor, FieldResolver fieldResolver) { + super(countVisitor, sumVisitor, listVisitor, customFuncVisitor, fieldResolver); + } + + @Override + public ResultModel visitParse(ParseContext ctx) { + List results = new ArrayList<>(); + for (int i = 0; i < ctx.fraud_rule().size(); i++) { + Fraud_ruleContext fraudRuleContext = ctx.fraud_rule().get(i); + ResultStatus result = (ResultStatus) visit(fraudRuleContext); + String key = RuleKeyGenerator.generateRuleKey(fraudRuleContext, i); + if (result == null) { + throw new UnknownResultException(fraudRuleContext.getText()); + } else if (result != ResultStatus.NORMAL) { + results.add(new RuleResult(result, key)); + } + } + return new ResultModel(results); + } + +} diff --git a/src/main/java/com/rbkmoney/fraudo/utils/ResultUtils.java b/src/main/java/com/rbkmoney/fraudo/utils/ResultUtils.java new file mode 100644 index 0000000..fb24840 --- /dev/null +++ b/src/main/java/com/rbkmoney/fraudo/utils/ResultUtils.java @@ -0,0 +1,28 @@ +package com.rbkmoney.fraudo.utils; + +import com.rbkmoney.fraudo.constant.ResultStatus; +import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.model.RuleResult; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +public class ResultUtils { + + public static List getNotifications(ResultModel result) { + return result.getRuleResults().stream() + .filter(ruleResult -> ResultStatus.NOTIFY.equals(ruleResult.getResultStatus()) + || ResultStatus.DECLINE_AND_NOTIFY.equals(ruleResult.getResultStatus()) + || ResultStatus.ACCEPT_AND_NOTIFY.equals(ruleResult.getResultStatus())) + .map(RuleResult::getRuleChecked) + .collect(Collectors.toList()); + } + + public static Optional findFirstNotNotifyStatus(ResultModel result) { + return result.getRuleResults().stream() + .filter(ruleResult -> !ruleResult.getResultStatus().equals(ResultStatus.NOTIFY)) + .findFirst(); + } + +} diff --git a/src/test/java/com/rbkmoney/fraudo/AbstractPaymentTest.java b/src/test/java/com/rbkmoney/fraudo/AbstractPaymentTest.java index 435074b..c387a5b 100644 --- a/src/test/java/com/rbkmoney/fraudo/AbstractPaymentTest.java +++ b/src/test/java/com/rbkmoney/fraudo/AbstractPaymentTest.java @@ -7,6 +7,7 @@ import com.rbkmoney.fraudo.model.ResultModel; import com.rbkmoney.fraudo.payment.aggregator.CountPaymentAggregator; import com.rbkmoney.fraudo.payment.aggregator.SumPaymentAggregator; 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.resolver.CountryResolver; @@ -63,6 +64,20 @@ public class AbstractPaymentTest { .visit(parse, model); } + ResultModel invokeFullVisitor(ParseContext parse, PaymentModel model) { + return new FullVisitorFactoryImpl() + .createVisitor( + countPaymentAggregator, + sumPaymentAggregator, + uniqueValueAggregator, + countryResolver, + inListFinder, + fieldResolver, + paymentGroupResolver, + timeWindowResolver) + .visit(parse, model); + } + ParseContext getParseContext(InputStream resourceAsStream) throws IOException { com.rbkmoney.fraudo.FraudoPaymentLexer lexer = new com.rbkmoney.fraudo.FraudoPaymentLexer(new ANTLRInputStream(resourceAsStream)); com.rbkmoney.fraudo.FraudoPaymentParser parser = new com.rbkmoney.fraudo.FraudoPaymentParser(new CommonTokenStream(lexer)); diff --git a/src/test/java/com/rbkmoney/fraudo/CountTest.java b/src/test/java/com/rbkmoney/fraudo/CountTest.java index 34088b0..c562bcf 100644 --- a/src/test/java/com/rbkmoney/fraudo/CountTest.java +++ b/src/test/java/com/rbkmoney/fraudo/CountTest.java @@ -3,6 +3,7 @@ package com.rbkmoney.fraudo; import com.rbkmoney.fraudo.FraudoPaymentParser.ParseContext; import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.utils.ResultUtils; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -10,6 +11,7 @@ import org.mockito.MockitoAnnotations; import java.io.InputStream; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; @@ -30,15 +32,15 @@ public class CountTest extends AbstractPaymentTest { when(countPaymentAggregator.countRefund(anyObject(), any(), any(), any())).thenReturn(1); ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals("0", result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals("0", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); when(countPaymentAggregator.count(anyObject(), any(), any(), any())).thenReturn(9); when(countPaymentAggregator.countError(anyObject(), any(), any(), anyString(), any())).thenReturn(6); when(countPaymentAggregator.countSuccess(anyObject(), any(), any(), any())).thenReturn(6); result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -48,8 +50,8 @@ public class CountTest extends AbstractPaymentTest { when(countPaymentAggregator.countRefund(anyObject(), any(), any(), any())).thenReturn(5); ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals("0", result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals("0", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); } @Test @@ -58,12 +60,12 @@ public class CountTest extends AbstractPaymentTest { when(countPaymentAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals("0", result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals("0", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); when(countPaymentAggregator.count(anyObject(), any(), any(), any())).thenReturn(1); result = invokeParse(parseContext); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test @@ -72,19 +74,19 @@ public class CountTest extends AbstractPaymentTest { when(countPaymentAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals("0", result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals("0", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); when(countPaymentAggregator.count(anyObject(), any(), any(), any())).thenReturn(1); result = invokeParse(parseContext); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); resourceAsStream = CountTest.class.getResourceAsStream("/rules/countTimeWindowGroupBy_2.frd"); when(countPaymentAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); parseContext = getParseContext(resourceAsStream); result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals("0", result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals("0", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); } @Test @@ -94,7 +96,7 @@ public class CountTest extends AbstractPaymentTest { ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); verify(countPaymentAggregator, 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 9ed1706..fe7f41b 100644 --- a/src/test/java/com/rbkmoney/fraudo/CustomTest.java +++ b/src/test/java/com/rbkmoney/fraudo/CustomTest.java @@ -5,7 +5,9 @@ import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.exception.UnknownResultException; import com.rbkmoney.fraudo.model.ResultModel; import com.rbkmoney.fraudo.test.model.PaymentModel; +import com.rbkmoney.fraudo.utils.ResultUtils; import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -13,6 +15,7 @@ import org.mockito.MockitoAnnotations; import java.io.InputStream; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; @@ -31,7 +34,7 @@ public class CustomTest extends AbstractPaymentTest { InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/three_ds.frd"); when(countPaymentAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.THREE_DS, result.getResultStatus()); + assertEquals(ResultStatus.THREE_DS, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -39,48 +42,46 @@ public class CustomTest extends AbstractPaymentTest { InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/highRisk.frd"); when(countPaymentAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.HIGH_RISK, result.getResultStatus()); + assertEquals(ResultStatus.HIGH_RISK, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test public void notifyTest() throws Exception { InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/notify.frd"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - assertEquals(1, result.getNotificationsRule().size()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + assertEquals(1, ResultUtils.getNotifications(result).size()); } @Test public void declineTest() throws Exception { InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/decline.frd"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals("test_11", result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals("test_11", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); } @Test public void declineAndNotifyTest() throws Exception { InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/declineAndNotify.frd"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.DECLINE_AND_NOTIFY, result.getResultStatus()); - assertEquals("test_11", result.getRuleChecked()); - assertEquals(1, result.getNotificationsRule().size()); - - System.out.println(result.getNotificationsRule()); + assertEquals(ResultStatus.DECLINE_AND_NOTIFY, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals("test_11", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); + Assert.assertEquals(1, ResultUtils.getNotifications(result).size()); } @Test public void acceptTest() throws Exception { InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/accept.frd"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test public void ruleIsNotFireTest() throws Exception { InputStream resourceAsStream = CustomTest.class.getResourceAsStream("/rules/rule_is_not_fire.frd"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test(expected = UnknownResultException.class) @@ -96,7 +97,7 @@ public class CustomTest extends AbstractPaymentTest { PaymentModel model = new PaymentModel(); model.setEmail(TEST_GMAIL_RU); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -108,7 +109,7 @@ public class CustomTest extends AbstractPaymentTest { PaymentModel model = new PaymentModel(); model.setAmount(500L); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -119,7 +120,7 @@ public class CustomTest extends AbstractPaymentTest { PaymentModel model = new PaymentModel(); model.setCurrency("EUR"); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -130,11 +131,11 @@ public class CustomTest extends AbstractPaymentTest { model.setAmount(56L); model.setCurrency("RUB"); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); model.setCurrency("USD"); result = invoke(parseContext, model); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test @@ -143,7 +144,7 @@ public class CustomTest extends AbstractPaymentTest { when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenThrow(new UnknownResultException("as")); ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -155,11 +156,11 @@ public class CustomTest extends AbstractPaymentTest { model.setBin("553619"); model.setPan("9137"); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); model.setPan("9111"); result = invoke(parseContext, model); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test @@ -169,7 +170,7 @@ public class CustomTest extends AbstractPaymentTest { PaymentModel model = new PaymentModel(); model.setEmail(TEST_GMAIL_RU); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test @@ -178,7 +179,7 @@ public class CustomTest extends AbstractPaymentTest { when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenReturn(2); ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -188,7 +189,7 @@ public class CustomTest extends AbstractPaymentTest { ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); verify(uniqueValueAggregator, times(1)).countUniqueValue(any(), any(), any(), any(), any()); } @@ -199,17 +200,17 @@ public class CustomTest extends AbstractPaymentTest { when(countryResolver.resolveCountry(any(), anyString())).thenReturn("RU"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - assertEquals(1, result.getNotificationsRule().size()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + assertEquals(1, ResultUtils.getNotifications(result).size()); when(countryResolver.resolveCountry(any(), anyString())).thenReturn("US"); resourceAsStream = CustomTest.class.getResourceAsStream("/rules/eq_country.frd"); result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); resourceAsStream = CustomTest.class.getResourceAsStream("/rules/accept_with_notify.frd"); result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); - assertEquals(2, result.getNotificationsRule().size()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals(2, ResultUtils.getNotifications(result).size()); } } diff --git a/src/test/java/com/rbkmoney/fraudo/ListTest.java b/src/test/java/com/rbkmoney/fraudo/ListTest.java index 7618954..02112d2 100644 --- a/src/test/java/com/rbkmoney/fraudo/ListTest.java +++ b/src/test/java/com/rbkmoney/fraudo/ListTest.java @@ -3,6 +3,7 @@ package com.rbkmoney.fraudo; import com.rbkmoney.fraudo.FraudoPaymentParser.ParseContext; import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.utils.ResultUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -11,6 +12,7 @@ import org.mockito.MockitoAnnotations; import java.io.InputStream; +import static org.junit.Assert.assertFalse; import static org.mockito.Matchers.*; public class ListTest extends AbstractPaymentTest { @@ -26,14 +28,15 @@ public class ListTest extends AbstractPaymentTest { ParseContext parseContext = getParseContext(resourceAsStream); Mockito.when(inListFinder.findInWhiteList(anyList(), anyObject())).thenReturn(true); ResultModel result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + Assert.assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); resourceAsStream = ListTest.class.getResourceAsStream("/rules/blacklist.frd"); parseContext = getParseContext(resourceAsStream); Mockito.when(inListFinder.findInBlackList(anyList(), anyObject())).thenReturn(true); result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - Assert.assertEquals(1, result.getNotificationsRule().size()); + + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(1, ResultUtils.getNotifications(result).size()); } @Test @@ -42,7 +45,7 @@ public class ListTest extends AbstractPaymentTest { ParseContext parseContext = getParseContext(resourceAsStream); Mockito.when(inListFinder.findInGreyList(anyList(), anyObject())).thenReturn(true); ResultModel result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + Assert.assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -51,6 +54,6 @@ public class ListTest extends AbstractPaymentTest { ParseContext parseContext = getParseContext(resourceAsStream); Mockito.when(inListFinder.findInList(anyString(), anyList(), anyObject())).thenReturn(true); ResultModel result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + Assert.assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } } diff --git a/src/test/java/com/rbkmoney/fraudo/RealTimerTest.java b/src/test/java/com/rbkmoney/fraudo/RealTimerTest.java index bd8f26a..85de71a 100644 --- a/src/test/java/com/rbkmoney/fraudo/RealTimerTest.java +++ b/src/test/java/com/rbkmoney/fraudo/RealTimerTest.java @@ -4,6 +4,8 @@ import com.rbkmoney.fraudo.FraudoPaymentParser.ParseContext; import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.model.ResultModel; import com.rbkmoney.fraudo.test.model.PaymentModel; +import com.rbkmoney.fraudo.utils.ResultUtils; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -43,11 +45,15 @@ public class RealTimerTest extends AbstractPaymentTest { long start = System.currentTimeMillis(); ResultModel result = invoke(parseContext, model); long executionTime = System.currentTimeMillis() - start; - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + + Assert.assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); assertEquals(0, countDownLatch.getCount()); assertTrue(executionTime < TIME_CALL_AGGR_FUNC + 1 + TIME_CALLING); System.out.println("executionTime=" + executionTime); + + result = invokeFullVisitor(parseContext, model); + assertEquals(2, result.getRuleResults().size()); } @Test @@ -66,10 +72,7 @@ public class RealTimerTest extends AbstractPaymentTest { ResultModel result = invoke(parseContext, model); long executionTime = System.currentTimeMillis() - start; - System.out.println("executionTime=" + executionTime); - System.out.println("result=" + result.getRuleChecked()); - - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + Assert.assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); assertTrue(executionTime < TIME_CALL_AGGR_FUNC * 4 + TIME_CALLING); } diff --git a/src/test/java/com/rbkmoney/fraudo/SumTest.java b/src/test/java/com/rbkmoney/fraudo/SumTest.java index 233abdb..c3eafa5 100644 --- a/src/test/java/com/rbkmoney/fraudo/SumTest.java +++ b/src/test/java/com/rbkmoney/fraudo/SumTest.java @@ -3,6 +3,7 @@ package com.rbkmoney.fraudo; import com.rbkmoney.fraudo.FraudoPaymentParser.ParseContext; import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.utils.ResultUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -28,16 +29,16 @@ public class SumTest extends AbstractPaymentTest { Mockito.when(sumPaymentAggregator.sumSuccess(anyObject(), any(), any(), any())).thenReturn(4.0); ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - Assert.assertEquals(1, result.getNotificationsRule().size()); + Assert.assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(1, ResultUtils.getNotifications(result).size()); Mockito.when(sumPaymentAggregator.sum(anyObject(), any(), any(), any())).thenReturn(90.0); Mockito.when(sumPaymentAggregator.sumError(anyObject(), any(), any(), anyString(), any())).thenReturn(504.0); Mockito.when(sumPaymentAggregator.sumSuccess(anyObject(), any(), any(), any())).thenReturn(501.0); result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - Assert.assertEquals(0, result.getNotificationsRule().size()); + Assert.assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(0, ResultUtils.getNotifications(result).size()); } @Test @@ -47,7 +48,7 @@ public class SumTest extends AbstractPaymentTest { Mockito.when(sumPaymentAggregator.sumRefund(anyObject(), any(), any(), any())).thenReturn(10000.60); ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + Assert.assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -58,16 +59,17 @@ public class SumTest extends AbstractPaymentTest { Mockito.when(sumPaymentAggregator.sumSuccess(anyObject(), any(), any(), any())).thenReturn(4.0); ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - Assert.assertEquals(1, result.getNotificationsRule().size()); + + Assert.assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(1, ResultUtils.getNotifications(result).size()); Mockito.when(sumPaymentAggregator.sum(anyObject(), any(), any(), any())).thenReturn(90.0); Mockito.when(sumPaymentAggregator.sumError(anyObject(), any(), any(), anyString(), any())).thenReturn(504.0); Mockito.when(sumPaymentAggregator.sumSuccess(anyObject(), any(), any(), any())).thenReturn(501.0); result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - Assert.assertEquals(0, result.getNotificationsRule().size()); + Assert.assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(0, ResultUtils.getNotifications(result).size()); } } diff --git a/src/test/java/com/rbkmoney/fraudo/p2p/CountP2PTest.java b/src/test/java/com/rbkmoney/fraudo/p2p/CountP2PTest.java index ee1b751..9d741a6 100644 --- a/src/test/java/com/rbkmoney/fraudo/p2p/CountP2PTest.java +++ b/src/test/java/com/rbkmoney/fraudo/p2p/CountP2PTest.java @@ -3,6 +3,8 @@ package com.rbkmoney.fraudo.p2p; import com.rbkmoney.fraudo.FraudoP2PParser; import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.utils.ResultUtils; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -11,6 +13,7 @@ import org.mockito.MockitoAnnotations; import java.io.InputStream; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.*; @@ -33,13 +36,13 @@ public class CountP2PTest extends AbstractP2PTest { FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals(FIRST_RULE_INDEX, result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals(FIRST_RULE_INDEX, ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(14); result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -48,12 +51,13 @@ public class CountP2PTest extends AbstractP2PTest { when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals(FIRST_RULE_INDEX, result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals(FIRST_RULE_INDEX, ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(1); result = invokeParse(parseContext); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + + Assert.assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test @@ -62,19 +66,19 @@ public class CountP2PTest extends AbstractP2PTest { when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals(FIRST_RULE_INDEX, result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals(FIRST_RULE_INDEX, ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(1); result = invokeParse(parseContext); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); resourceAsStream = CountP2PTest.class.getResourceAsStream("/rules/p2p/countTimeWindowGroupBy_2.frd"); when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); parseContext = getParseContext(resourceAsStream); result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals(FIRST_RULE_INDEX, result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals(FIRST_RULE_INDEX, ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); } @Test @@ -84,7 +88,7 @@ public class CountP2PTest extends AbstractP2PTest { FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); verify(countAggregator, times(1)).count(anyObject(), any(), any(), any()); } diff --git a/src/test/java/com/rbkmoney/fraudo/p2p/CustomP2PTest.java b/src/test/java/com/rbkmoney/fraudo/p2p/CustomP2PTest.java index 5ead232..85bd10e 100644 --- a/src/test/java/com/rbkmoney/fraudo/p2p/CustomP2PTest.java +++ b/src/test/java/com/rbkmoney/fraudo/p2p/CustomP2PTest.java @@ -8,6 +8,8 @@ import com.rbkmoney.fraudo.exception.UnknownResultException; import com.rbkmoney.fraudo.model.ResultModel; import com.rbkmoney.fraudo.test.model.P2PModel; import com.rbkmoney.fraudo.test.model.PaymentModel; +import com.rbkmoney.fraudo.utils.ResultUtils; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -15,6 +17,7 @@ import org.mockito.MockitoAnnotations; import java.io.InputStream; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; @@ -32,7 +35,7 @@ public class CustomP2PTest extends AbstractP2PTest { InputStream resourceAsStream = CustomP2PTest.class.getResourceAsStream("/rules/p2p/three_ds.frd"); when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.THREE_DS, result.getResultStatus()); + assertEquals(ResultStatus.THREE_DS, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -40,37 +43,37 @@ public class CustomP2PTest extends AbstractP2PTest { InputStream resourceAsStream = CustomP2PTest.class.getResourceAsStream("/rules/p2p/highRisk.frd"); when(countAggregator.count(anyObject(), any(), any(), any())).thenReturn(10); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.HIGH_RISK, result.getResultStatus()); + assertEquals(ResultStatus.HIGH_RISK, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test public void notifyTest() throws Exception { InputStream resourceAsStream = CustomP2PTest.class.getResourceAsStream("/rules/p2p/notify.frd"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - assertEquals(1, result.getNotificationsRule().size()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + assertEquals(1, ResultUtils.getNotifications(result).size()); } @Test public void declineTest() throws Exception { InputStream resourceAsStream = CustomP2PTest.class.getResourceAsStream("/rules/p2p/decline.frd"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); - assertEquals("test_11", result.getRuleChecked()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + assertEquals("test_11", ResultUtils.findFirstNotNotifyStatus(result).get().getRuleChecked()); } @Test public void acceptTest() throws Exception { InputStream resourceAsStream = CustomP2PTest.class.getResourceAsStream("/rules/p2p/accept.frd"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test public void ruleIsNotFireTest() throws Exception { InputStream resourceAsStream = CustomP2PTest.class.getResourceAsStream("/rules/p2p/rule_is_not_fire.frd"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test(expected = UnknownResultException.class) @@ -89,7 +92,7 @@ public class CustomP2PTest extends AbstractP2PTest { model.setEmail(TEST_GMAIL_RU); model.setBin("123213"); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -100,7 +103,7 @@ public class CustomP2PTest extends AbstractP2PTest { P2PModel model = new P2PModel(); model.setCurrency("EUR"); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -112,7 +115,7 @@ public class CustomP2PTest extends AbstractP2PTest { P2PModel model = new P2PModel(); model.setAmount(500L); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -123,11 +126,11 @@ public class CustomP2PTest extends AbstractP2PTest { model.setAmount(56L); model.setCurrency("RUB"); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); model.setCurrency("USD"); result = invoke(parseContext, model); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test @@ -136,7 +139,7 @@ public class CustomP2PTest extends AbstractP2PTest { when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenThrow(new UnknownResultException("as")); FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -148,11 +151,11 @@ public class CustomP2PTest extends AbstractP2PTest { model.setBin("553619"); model.setPan("9137"); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); model.setPan("9111"); result = invoke(parseContext, model); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test @@ -162,7 +165,7 @@ public class CustomP2PTest extends AbstractP2PTest { P2PModel model = new P2PModel(); model.setEmail(TEST_GMAIL_RU); ResultModel result = invoke(parseContext, model); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); } @Test @@ -171,7 +174,7 @@ public class CustomP2PTest extends AbstractP2PTest { when(uniqueValueAggregator.countUniqueValue(any(), any(), any(), any(), any())).thenReturn(2); FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); } @Test @@ -181,7 +184,7 @@ public class CustomP2PTest extends AbstractP2PTest { FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); verify(uniqueValueAggregator, times(1)).countUniqueValue(any(), any(), any(), any(), any()); } @@ -192,18 +195,18 @@ public class CustomP2PTest extends AbstractP2PTest { when(countryResolver.resolveCountry(any(), anyString())).thenReturn("RU"); ResultModel result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - assertEquals(1, result.getNotificationsRule().size()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(1, ResultUtils.getNotifications(result).size()); when(countryResolver.resolveCountry(any(), anyString())).thenReturn("US"); resourceAsStream = CustomP2PTest.class.getResourceAsStream("/rules/p2p/eq_country.frd"); result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - assertEquals(0, result.getNotificationsRule().size()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(0, ResultUtils.getNotifications(result).size()); resourceAsStream = CustomP2PTest.class.getResourceAsStream("/rules/p2p/accept_with_notify.frd"); result = parseAndVisit(resourceAsStream); - assertEquals(ResultStatus.ACCEPT, result.getResultStatus()); - assertEquals(2, result.getNotificationsRule().size()); + assertEquals(ResultStatus.ACCEPT, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); + Assert.assertEquals(2, ResultUtils.getNotifications(result).size()); } } diff --git a/src/test/java/com/rbkmoney/fraudo/p2p/P2PTest.java b/src/test/java/com/rbkmoney/fraudo/p2p/P2PTest.java index 1aa5a47..6f12475 100644 --- a/src/test/java/com/rbkmoney/fraudo/p2p/P2PTest.java +++ b/src/test/java/com/rbkmoney/fraudo/p2p/P2PTest.java @@ -3,6 +3,7 @@ package com.rbkmoney.fraudo.p2p; import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.model.ResultModel; import com.rbkmoney.fraudo.test.model.P2PModel; +import com.rbkmoney.fraudo.utils.ResultUtils; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -11,8 +12,7 @@ import org.mockito.stubbing.Answer; import java.io.InputStream; import java.util.concurrent.CountDownLatch; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.mockito.Matchers.*; import static org.mockito.Mockito.when; @@ -43,7 +43,7 @@ public class P2PTest extends AbstractP2PTest { long start = System.currentTimeMillis(); ResultModel result = invoke(parseContext, model); long executionTime = System.currentTimeMillis() - start; - assertEquals(ResultStatus.NORMAL, result.getResultStatus()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); assertEquals(0, countDownLatch.getCount()); assertTrue(executionTime < TIME_CALL_AGGR_FUNC + 1 + TIME_CALLING); diff --git a/src/test/java/com/rbkmoney/fraudo/p2p/ParenP2PTest.java b/src/test/java/com/rbkmoney/fraudo/p2p/ParenP2PTest.java index 742cdd3..77dec77 100644 --- a/src/test/java/com/rbkmoney/fraudo/p2p/ParenP2PTest.java +++ b/src/test/java/com/rbkmoney/fraudo/p2p/ParenP2PTest.java @@ -3,6 +3,7 @@ package com.rbkmoney.fraudo.p2p; import com.rbkmoney.fraudo.FraudoP2PParser; import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.utils.ResultUtils; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -31,7 +32,7 @@ public class ParenP2PTest extends AbstractP2PTest { FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - assertEquals(ResultStatus.DECLINE, result.getResultStatus()); + assertEquals(ResultStatus.DECLINE, ResultUtils.findFirstNotNotifyStatus(result).get().getResultStatus()); verify(countAggregator, times(2)).count(anyObject(), any(), any(), any()); } diff --git a/src/test/java/com/rbkmoney/fraudo/p2p/SumP2PTest.java b/src/test/java/com/rbkmoney/fraudo/p2p/SumP2PTest.java index d3cb7d3..df03a36 100644 --- a/src/test/java/com/rbkmoney/fraudo/p2p/SumP2PTest.java +++ b/src/test/java/com/rbkmoney/fraudo/p2p/SumP2PTest.java @@ -3,6 +3,7 @@ package com.rbkmoney.fraudo.p2p; import com.rbkmoney.fraudo.FraudoP2PParser; import com.rbkmoney.fraudo.constant.ResultStatus; import com.rbkmoney.fraudo.model.ResultModel; +import com.rbkmoney.fraudo.utils.ResultUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -11,6 +12,7 @@ import org.mockito.MockitoAnnotations; import java.io.InputStream; +import static org.junit.Assert.assertFalse; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyObject; @@ -27,14 +29,14 @@ public class SumP2PTest extends AbstractP2PTest { Mockito.when(sumAggregator.sum(anyObject(), any(), any(), any())).thenReturn(10500.60); FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - Assert.assertEquals(1, result.getNotificationsRule().size()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(1, ResultUtils.getNotifications(result).size()); Mockito.when(sumAggregator.sum(anyObject(), any(), any(), any())).thenReturn(90.0); result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - Assert.assertEquals(0, result.getNotificationsRule().size()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(0, ResultUtils.getNotifications(result).size()); } @Test @@ -43,14 +45,14 @@ public class SumP2PTest extends AbstractP2PTest { Mockito.when(sumAggregator.sum(anyObject(), any(), any(), any())).thenReturn(10500.60); FraudoP2PParser.ParseContext parseContext = getParseContext(resourceAsStream); ResultModel result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - Assert.assertEquals(1, result.getNotificationsRule().size()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(1, ResultUtils.getNotifications(result).size()); Mockito.when(sumAggregator.sum(anyObject(), any(), any(), any())).thenReturn(90.0); result = invokeParse(parseContext); - Assert.assertEquals(ResultStatus.NORMAL, result.getResultStatus()); - Assert.assertEquals(0, result.getNotificationsRule().size()); + assertFalse(ResultUtils.findFirstNotNotifyStatus(result).isPresent()); + Assert.assertEquals(0, ResultUtils.getNotifications(result).size()); } } diff --git a/src/test/resources/rules/payment_template.frd b/src/test/resources/rules/payment_template.frd index a7bac41..4714563 100644 --- a/src/test/resources/rules/payment_template.frd +++ b/src/test/resources/rules/payment_template.frd @@ -13,4 +13,4 @@ rule:cards_test_count_3:unique("fingerprint","card_token",1440)>3 AND not in(cou rule:USSRcount:count("card_token",1440,"party_id")<11 AND in(countryBy("country_bank"),"UZB")->accept; rule:count11tur:count("card_token",1440,"party_id")<12 AND in(countryBy("country_bank"),"TUR")->accept; rule:count10:count("card_token",1440,"party_id")<11 AND not in(countryBy("country_bank"),"ARG")->accept; -rule:count5bra:count("card_token",1440,"party_id")>5->decline; \ No newline at end of file +rule:count5bra:count("card_token",1440,"party_id")>0->decline; \ No newline at end of file