From 8a4130e27ca7b690f6397d1eb987e41c363d8dc8 Mon Sep 17 00:00:00 2001 From: Gregory <32060161+ggmaleva@users.noreply.github.com> Date: Thu, 23 Jun 2022 13:44:16 +0300 Subject: [PATCH] add calendar months (#17) * add calendar months * add calendar months Co-authored-by: ggmaleva --- src/main/antlr4/imports/Fraudo.g4 | 1 + .../dev/vality/fraudo/constant/TimeUnit.java | 12 +++++++ .../dev/vality/fraudo/model/TimeWindow.java | 4 +-- .../resolver/PaymentTimeWindowResolver.java | 26 +++++++++++---- .../PaymentTimeWindowResolverTest.java | 32 +++++++++++++------ .../time_window/withCalMonthsTimeUnit.frd | 1 + 6 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 src/main/java/dev/vality/fraudo/constant/TimeUnit.java create mode 100644 src/test/resources/rules/time_window/withCalMonthsTimeUnit.frd diff --git a/src/main/antlr4/imports/Fraudo.g4 b/src/main/antlr4/imports/Fraudo.g4 index 9fa5cff..9d0ca1f 100644 --- a/src/main/antlr4/imports/Fraudo.g4 +++ b/src/main/antlr4/imports/Fraudo.g4 @@ -120,6 +120,7 @@ time_unit : 'minutes' | 'hours' | 'days' + | 'calendar_months' ; group_by diff --git a/src/main/java/dev/vality/fraudo/constant/TimeUnit.java b/src/main/java/dev/vality/fraudo/constant/TimeUnit.java new file mode 100644 index 0000000..71fce1a --- /dev/null +++ b/src/main/java/dev/vality/fraudo/constant/TimeUnit.java @@ -0,0 +1,12 @@ +package dev.vality.fraudo.constant; + +import lombok.experimental.UtilityClass; + +@UtilityClass +public class TimeUnit { + + public static final String MINUTES = "minutes"; + public static final String DAYS = "days"; + public static final String CALENDAR_MONTHS = "calendar_months"; + +} diff --git a/src/main/java/dev/vality/fraudo/model/TimeWindow.java b/src/main/java/dev/vality/fraudo/model/TimeWindow.java index 088559a..afae6ee 100644 --- a/src/main/java/dev/vality/fraudo/model/TimeWindow.java +++ b/src/main/java/dev/vality/fraudo/model/TimeWindow.java @@ -9,8 +9,8 @@ import java.time.temporal.ChronoUnit; @Builder public class TimeWindow { - private Long startWindowTime; - private Long endWindowTime; + private int start; + private int end; private ChronoUnit timeUnit; } diff --git a/src/main/java/dev/vality/fraudo/payment/resolver/PaymentTimeWindowResolver.java b/src/main/java/dev/vality/fraudo/payment/resolver/PaymentTimeWindowResolver.java index f1b8004..0b2facf 100644 --- a/src/main/java/dev/vality/fraudo/payment/resolver/PaymentTimeWindowResolver.java +++ b/src/main/java/dev/vality/fraudo/payment/resolver/PaymentTimeWindowResolver.java @@ -10,24 +10,38 @@ import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Objects; +import static dev.vality.fraudo.constant.TimeUnit.*; + + public class PaymentTimeWindowResolver implements TimeWindowResolver { @Override public TimeWindow resolve(FraudoPaymentParser.Time_windowContext ctx) { TimeWindow.TimeWindowBuilder builder = TimeWindow.builder(); List times = ctx.INTEGER(); - String startWindow = TextUtil.safeGetText(times.get(0)); + String start = TextUtil.safeGetText(times.get(0)); builder - .startWindowTime(Long.valueOf(startWindow)) + .start(Integer.parseInt(start)) .timeUnit(ChronoUnit.HOURS); if (times.size() == 2) { - String endWindow = TextUtil.safeGetText(times.get(1)); - builder.endWindowTime(Long.valueOf(endWindow)); + String end = TextUtil.safeGetText(times.get(1)); + builder + .end(Integer.parseInt(end)); } if (Objects.nonNull(ctx.time_unit())) { - String timeUnit = ctx.time_unit().getText().toUpperCase(); - builder.timeUnit(ChronoUnit.valueOf(timeUnit)); + String timeUnit = ctx.time_unit().getText(); + builder + .timeUnit(resolveTimeUnit(timeUnit)); } return builder.build(); } + + private ChronoUnit resolveTimeUnit(String timeUnit) { + return switch (timeUnit) { + case MINUTES -> ChronoUnit.MINUTES; + case DAYS, CALENDAR_MONTHS -> ChronoUnit.DAYS; + default -> ChronoUnit.HOURS; + }; + } + } diff --git a/src/test/java/dev/vality/fraudo/payment/resolver/PaymentTimeWindowResolverTest.java b/src/test/java/dev/vality/fraudo/payment/resolver/PaymentTimeWindowResolverTest.java index 7ab925c..9846f64 100644 --- a/src/test/java/dev/vality/fraudo/payment/resolver/PaymentTimeWindowResolverTest.java +++ b/src/test/java/dev/vality/fraudo/payment/resolver/PaymentTimeWindowResolverTest.java @@ -12,7 +12,8 @@ import java.io.IOException; import java.io.InputStream; import java.time.temporal.ChronoUnit; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class PaymentTimeWindowResolverTest { @@ -28,8 +29,8 @@ public class PaymentTimeWindowResolverTest { TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext); assertNotNull(timeWindow); - assertEquals(24L, timeWindow.getStartWindowTime()); - assertNull(timeWindow.getEndWindowTime()); + assertEquals(24, timeWindow.getStart()); + assertEquals(0, timeWindow.getEnd()); assertEquals(ChronoUnit.HOURS, timeWindow.getTimeUnit()); } @@ -41,8 +42,8 @@ public class PaymentTimeWindowResolverTest { TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext); assertNotNull(timeWindow); - assertEquals(24L, timeWindow.getStartWindowTime()); - assertEquals(2L, timeWindow.getEndWindowTime()); + assertEquals(24, timeWindow.getStart()); + assertEquals(2, timeWindow.getEnd()); assertEquals(ChronoUnit.HOURS, timeWindow.getTimeUnit()); } @@ -54,8 +55,8 @@ public class PaymentTimeWindowResolverTest { TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext); assertNotNull(timeWindow); - assertEquals(24L, timeWindow.getStartWindowTime()); - assertNull(timeWindow.getEndWindowTime()); + assertEquals(24, timeWindow.getStart()); + assertEquals(0, timeWindow.getEnd()); assertEquals(ChronoUnit.DAYS, timeWindow.getTimeUnit()); } @@ -67,11 +68,24 @@ public class PaymentTimeWindowResolverTest { TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext); assertNotNull(timeWindow); - assertEquals(24L, timeWindow.getStartWindowTime()); - assertEquals(1L, timeWindow.getEndWindowTime()); + assertEquals(24, timeWindow.getStart()); + assertEquals(1, timeWindow.getEnd()); assertEquals(ChronoUnit.HOURS, timeWindow.getTimeUnit()); } + @Test + void withCalMonthsTimeUnitTest() throws Exception { + FraudoPaymentParser.Time_windowContext timeWindowContext = + getTimeWindowContext("/rules/time_window/withCalMonthsTimeUnit.frd"); + + TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext); + + assertNotNull(timeWindow); + assertEquals(4, timeWindow.getStart()); + assertEquals(2, timeWindow.getEnd()); + assertEquals(ChronoUnit.DAYS, timeWindow.getTimeUnit()); + } + private FraudoPaymentParser.Time_windowContext getTimeWindowContext(String path) throws IOException { InputStream resourceAsStream = PaymentTimeWindowResolverTest.class.getResourceAsStream(path); FraudoPaymentLexer lexer = new FraudoPaymentLexer(CharStreams.fromStream(resourceAsStream)); diff --git a/src/test/resources/rules/time_window/withCalMonthsTimeUnit.frd b/src/test/resources/rules/time_window/withCalMonthsTimeUnit.frd new file mode 100644 index 0000000..528c919 --- /dev/null +++ b/src/test/resources/rules/time_window/withCalMonthsTimeUnit.frd @@ -0,0 +1 @@ +, 4, 2, calendar_months \ No newline at end of file