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'
|
: 'minutes'
|
||||||
| 'hours'
|
| 'hours'
|
||||||
| 'days'
|
| 'days'
|
||||||
|
| 'calendar_months'
|
||||||
;
|
;
|
||||||
|
|
||||||
group_by
|
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
|
@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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
, 4, 2, calendar_months
|
Loading…
Reference in New Issue
Block a user