add calendar months (#17)

* add calendar months

* add calendar months

Co-authored-by: ggmaleva <ggmaleva@yandex.ru>
This commit is contained in:
Gregory 2022-06-23 13:44:16 +03:00 committed by GitHub
parent b61877fd25
commit 8a4130e27c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 17 deletions

View File

@ -120,6 +120,7 @@ time_unit
: 'minutes' : 'minutes'
| 'hours' | 'hours'
| 'days' | 'days'
| 'calendar_months'
; ;
group_by group_by

View File

@ -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";
}

View File

@ -9,8 +9,8 @@ import java.time.temporal.ChronoUnit;
@Builder @Builder
public class TimeWindow { public class TimeWindow {
private Long startWindowTime; private int start;
private Long endWindowTime; private int end;
private ChronoUnit timeUnit; private ChronoUnit timeUnit;
} }

View File

@ -10,24 +10,38 @@ import java.time.temporal.ChronoUnit;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import static dev.vality.fraudo.constant.TimeUnit.*;
public class PaymentTimeWindowResolver implements TimeWindowResolver<FraudoPaymentParser.Time_windowContext> { public class PaymentTimeWindowResolver implements TimeWindowResolver<FraudoPaymentParser.Time_windowContext> {
@Override @Override
public TimeWindow resolve(FraudoPaymentParser.Time_windowContext ctx) { public TimeWindow resolve(FraudoPaymentParser.Time_windowContext ctx) {
TimeWindow.TimeWindowBuilder builder = TimeWindow.builder(); TimeWindow.TimeWindowBuilder builder = TimeWindow.builder();
List<TerminalNode> times = ctx.INTEGER(); List<TerminalNode> times = ctx.INTEGER();
String startWindow = TextUtil.safeGetText(times.get(0)); String start = TextUtil.safeGetText(times.get(0));
builder builder
.startWindowTime(Long.valueOf(startWindow)) .start(Integer.parseInt(start))
.timeUnit(ChronoUnit.HOURS); .timeUnit(ChronoUnit.HOURS);
if (times.size() == 2) { if (times.size() == 2) {
String endWindow = TextUtil.safeGetText(times.get(1)); String end = TextUtil.safeGetText(times.get(1));
builder.endWindowTime(Long.valueOf(endWindow)); builder
.end(Integer.parseInt(end));
} }
if (Objects.nonNull(ctx.time_unit())) { if (Objects.nonNull(ctx.time_unit())) {
String timeUnit = ctx.time_unit().getText().toUpperCase(); String timeUnit = ctx.time_unit().getText();
builder.timeUnit(ChronoUnit.valueOf(timeUnit)); builder
.timeUnit(resolveTimeUnit(timeUnit));
} }
return builder.build(); return builder.build();
} }
private ChronoUnit resolveTimeUnit(String timeUnit) {
return switch (timeUnit) {
case MINUTES -> ChronoUnit.MINUTES;
case DAYS, CALENDAR_MONTHS -> ChronoUnit.DAYS;
default -> ChronoUnit.HOURS;
};
}
} }

View File

@ -12,7 +12,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.time.temporal.ChronoUnit; 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 { public class PaymentTimeWindowResolverTest {
@ -28,8 +29,8 @@ public class PaymentTimeWindowResolverTest {
TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext); TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext);
assertNotNull(timeWindow); assertNotNull(timeWindow);
assertEquals(24L, timeWindow.getStartWindowTime()); assertEquals(24, timeWindow.getStart());
assertNull(timeWindow.getEndWindowTime()); assertEquals(0, timeWindow.getEnd());
assertEquals(ChronoUnit.HOURS, timeWindow.getTimeUnit()); assertEquals(ChronoUnit.HOURS, timeWindow.getTimeUnit());
} }
@ -41,8 +42,8 @@ public class PaymentTimeWindowResolverTest {
TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext); TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext);
assertNotNull(timeWindow); assertNotNull(timeWindow);
assertEquals(24L, timeWindow.getStartWindowTime()); assertEquals(24, timeWindow.getStart());
assertEquals(2L, timeWindow.getEndWindowTime()); assertEquals(2, timeWindow.getEnd());
assertEquals(ChronoUnit.HOURS, timeWindow.getTimeUnit()); assertEquals(ChronoUnit.HOURS, timeWindow.getTimeUnit());
} }
@ -54,8 +55,8 @@ public class PaymentTimeWindowResolverTest {
TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext); TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext);
assertNotNull(timeWindow); assertNotNull(timeWindow);
assertEquals(24L, timeWindow.getStartWindowTime()); assertEquals(24, timeWindow.getStart());
assertNull(timeWindow.getEndWindowTime()); assertEquals(0, timeWindow.getEnd());
assertEquals(ChronoUnit.DAYS, timeWindow.getTimeUnit()); assertEquals(ChronoUnit.DAYS, timeWindow.getTimeUnit());
} }
@ -67,11 +68,24 @@ public class PaymentTimeWindowResolverTest {
TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext); TimeWindow timeWindow = timeWindowResolver.resolve(timeWindowContext);
assertNotNull(timeWindow); assertNotNull(timeWindow);
assertEquals(24L, timeWindow.getStartWindowTime()); assertEquals(24, timeWindow.getStart());
assertEquals(1L, timeWindow.getEndWindowTime()); assertEquals(1, timeWindow.getEnd());
assertEquals(ChronoUnit.HOURS, timeWindow.getTimeUnit()); 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 { private FraudoPaymentParser.Time_windowContext getTimeWindowContext(String path) throws IOException {
InputStream resourceAsStream = PaymentTimeWindowResolverTest.class.getResourceAsStream(path); InputStream resourceAsStream = PaymentTimeWindowResolverTest.class.getResourceAsStream(path);
FraudoPaymentLexer lexer = new FraudoPaymentLexer(CharStreams.fromStream(resourceAsStream)); FraudoPaymentLexer lexer = new FraudoPaymentLexer(CharStreams.fromStream(resourceAsStream));

View File

@ -0,0 +1 @@
, 4, 2, calendar_months