mirror of
https://github.com/valitydev/fraudo.git
synced 2024-11-06 01:45:16 +00:00
add calendar months (#17)
* add calendar months * add calendar months Co-authored-by: ggmaleva <ggmaleva@yandex.ru>
This commit is contained in:
parent
b61877fd25
commit
8a4130e27c
@ -120,6 +120,7 @@ time_unit
|
||||
: 'minutes'
|
||||
| 'hours'
|
||||
| 'days'
|
||||
| 'calendar_months'
|
||||
;
|
||||
|
||||
group_by
|
||||
|
12
src/main/java/dev/vality/fraudo/constant/TimeUnit.java
Normal file
12
src/main/java/dev/vality/fraudo/constant/TimeUnit.java
Normal 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";
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -0,0 +1 @@
|
||||
, 4, 2, calendar_months
|
Loading…
Reference in New Issue
Block a user