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'
| 'hours'
| 'days'
| 'calendar_months'
;
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
public class TimeWindow {
private Long startWindowTime;
private Long endWindowTime;
private int start;
private int end;
private ChronoUnit timeUnit;
}

View File

@ -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<FraudoPaymentParser.Time_windowContext> {
@Override
public TimeWindow resolve(FraudoPaymentParser.Time_windowContext ctx) {
TimeWindow.TimeWindowBuilder builder = TimeWindow.builder();
List<TerminalNode> 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;
};
}
}

View File

@ -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));

View File

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