我正在尝试制作适用于任何给定数据和日期集的动态逻辑。
目标是在包含
discount_start_month
和discount_end_month
的数据库中将给定日期作为输入和查询。日期应在这些月份范围内。
这是记录在数据库中的样子的快照。它会像这样存储。对于这部分,我将跳过查询并通过代码显示所有内容。
输入:开始日期和结束日期
算法实现
seasonId
LinkedList<Map<String, Object>> seasonIdMap = DiscountUtils.getRangeByClientId(seasonId);
迭代开始和结束日期
seasonIdMap
的每个结果
一个。创建
YearMonth
组合discount_start_month
和
discount_end_month
b.找到这个范围内的日期,
(yearMonthDiscountDate.isAfter(fromMonthDiscount)|| yearMonthDiscountDate.equals(fromMonthDiscount))&& (yearMonthDiscountDate.isBefore(toMonthDiscount)|| yearMonthDiscountDate.equals(toMonthDiscount));
yearMonthDiscountDate
- 是输入日期(开始和结束日期)
startDateGeneration
(本地日期)这将被视为我们应该开始生成日期的第一个日期
Day -> 从
LocalDate
开始的一个月中的第几天
月份 ->
advance_start_month
Year -> 如果
advance_start_month
是前一年或等于 12 月减去年份 - 1
例如:对于 discount_code
JAN_MAR
如果 advance_start_month
小于 12,即在 12 月之前或小于 12 月,那么如果 startDateGeneration
为 01,则 fromDate
的年份将小于 fromDate
的年份-01-2023(日-月-年)
起始年将是 2022 年。
endDateGeneration
Day -> 从
LocalDate
开始的一个月中的第几天
月份 ->
advance_end_month
年 -> 对于
discount_code
DEC_JAN
如果 toDate
在 2022 年 12 月那么 endYear
将是 2023 因为 advance_end_month
我的问题:
我已经通过硬编码手动处理了 12 月、10 月的案例?我该如何解决这个问题,以便无论
discount_start_month
和 discount_end_month
出现的月份如何动态处理它们?
基本上就是这个逻辑,isDateInDiscountRange
对于计算年份的日期生成部分,请告诉我如何动态处理它,
if (advanceStartMonth >= Constants.OCT_MONTH && 提前开始月份<= Constants.DEC_MONTH && startYear == endYear) { startYear = startYear - 1; }
int discountResultStartMonth = discountStartFunction.apply(toDateSeasonMap);
int discountResultEndMonth = discountEndFunction.apply(toDateSeasonMap);
if (discountResultStartMonth > discountResultEndMonth) {
if (toDate.getMonthValue() == Constants.DEC_MONTH) {
endYear = endYear + 1;
}
}
代码部分从这里开始 春季启动项目 构建工具:Gradle
@Component
public class DiscountServiceImpl {
static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static List<DiscountRanges> getDiscountDates(Map<String, Object> data) {
List<DiscountRanges> discountRangesList = new ArrayList<>();
if (ObjectUtils.anyNull(data.get(Constants.FROM_DATE), data.get(Constants.TO_DATE))) {
return discountRangesList;
}
LocalDate fromDate =
LocalDate.parse(
dateFormatter.format(dateFormatter.parse((String) data.get(Constants.FROM_DATE))));
LocalDate toDate =
LocalDate.parse(
dateFormatter.format(dateFormatter.parse((String) data.get(Constants.TO_DATE))));
if (fromDate.isAfter(toDate)) {
return discountRangesList;
}
Function<Map<String, Object>, Integer> discountStartFunction =
(map) -> (Integer) map.get(Constants.DISCOUNT_START_MONTH);
Function<Map<String, Object>, Integer> discountEndFunction =
(map) -> (Integer) map.get(Constants.DISCOUNT_END_MONTH);
String seasonId = (String) data.get("seasonId");
LinkedList<Map<String, Object>> seasonIdMap = DiscountUtils.getRangeByClientId(seasonId);
Map<String, Object> fromDateSeasonMap;
fromDateSeasonMap =
seasonIdMap.stream()
.filter(
stringObjectMap ->
isDateInDiscountRange(
fromDate,
discountStartFunction.apply(stringObjectMap),
discountEndFunction.apply(stringObjectMap)))
.findFirst()
.orElseGet(HashMap::new);
Map<String, Object> toDateSeasonMap;
toDateSeasonMap =
seasonIdMap.stream()
.filter(
stringObjectMap ->
isDateInDiscountRange(
toDate,
discountStartFunction.apply(stringObjectMap),
discountEndFunction.apply(stringObjectMap)))
.findFirst()
.orElseGet(HashMap::new);
if (!CollectionUtils.isEmpty(fromDateSeasonMap) && !CollectionUtils.isEmpty(toDateSeasonMap)) {
int startYear = fromDate.getYear();
int endYear = toDate.getYear();
int advanceStartMonth = (int) fromDateSeasonMap.get(Constants.ADVANCE_START_MONTH);
if (advanceStartMonth >= Constants.OCT_MONTH
&& advanceStartMonth <= Constants.DEC_MONTH
&& startYear == endYear) {
startYear = startYear - 1;
}
int discountResultStartMonth = discountStartFunction.apply(toDateSeasonMap);
int discountResultEndMonth = discountEndFunction.apply(toDateSeasonMap);
if (discountResultStartMonth > discountResultEndMonth) {
if (toDate.getMonthValue() == Constants.DEC_MONTH) {
endYear = endYear + 1;
}
}
LocalDate startDateGeneration =
fromDate
.withMonth((Integer) fromDateSeasonMap.get(Constants.ADVANCE_START_MONTH))
.withYear(startYear);
LocalDate endDateGeneration =
toDate
.withMonth((Integer) toDateSeasonMap.get(Constants.ADVANCE_END_MONTH))
.withYear(endYear)
.withDayOfMonth(
YearMonth.of(endYear, (Integer) toDateSeasonMap.get(Constants.ADVANCE_END_MONTH))
.atEndOfMonth()
.getDayOfMonth());
discountRangesList = DateUtils.generateDates(startDateGeneration, endDateGeneration);
}
return discountRangesList;
}
private static boolean isDateInDiscountRange(
LocalDate discountDate, Integer fromMonth, Integer toMonth) {
YearMonth fromMonthDiscount = YearMonth.of(discountDate.getYear(), fromMonth);
YearMonth toMonthDiscount = YearMonth.of(discountDate.getYear(), toMonth);
YearMonth yearMonthDiscountDate =
YearMonth.of(discountDate.getYear(), discountDate.getMonthValue());
if (fromMonth > toMonth) {
if (discountDate.getMonthValue() == Constants.DEC_MONTH) {
toMonthDiscount = YearMonth.of(discountDate.getYear() + 1, toMonth);
} else if (discountDate.getMonthValue() == Constants.JAN_MONTH) {
fromMonthDiscount = YearMonth.of(discountDate.getYear() - 1, fromMonth);
}
}
return (yearMonthDiscountDate.isAfter(fromMonthDiscount)
|| yearMonthDiscountDate.equals(fromMonthDiscount))
&& (yearMonthDiscountDate.isBefore(toMonthDiscount)
|| yearMonthDiscountDate.equals(toMonthDiscount));
}
}
模仿数据库的类,
public class DiscountUtils {
public static LinkedList<Map<String, Object>> getRangeByClientId(String seasonId) {
final String DISCOUNT_CODE = "discount_code";
Map<String, Map<String, Object>> discountMonthMap = new HashMap<>();
LinkedList<Map<String, Object>> q1DiscountSeason = new LinkedList<>();
LinkedList<Map<String, Object>> q2DiscountSeason = new LinkedList<>();
Map<String, LinkedList<Map<String, Object>>> seasonIdMap = new HashMap<>();
Map<String, Object> janMarDsCode = new HashMap<>();
janMarDsCode.put(Constants.DISCOUNT_START_MONTH, 1);
janMarDsCode.put(Constants.DISCOUNT_END_MONTH, 3);
janMarDsCode.put(Constants.ADVANCE_START_MONTH, 11);
janMarDsCode.put(Constants.ADVANCE_END_MONTH, 3);
janMarDsCode.put(DISCOUNT_CODE, "JAN_MAR");
discountMonthMap.put("JAN_MAR", janMarDsCode);
discountMonthMap.put("JAN_MAR_102", janMarDsCode);
discountMonthMap.put("JAN_MAR_103", janMarDsCode);
q1DiscountSeason.add(janMarDsCode);
Map<String, Object> aprilMayDsCode = new HashMap<>();
aprilMayDsCode.put(Constants.DISCOUNT_START_MONTH, 4);
aprilMayDsCode.put(Constants.DISCOUNT_END_MONTH, 5);
aprilMayDsCode.put(Constants.ADVANCE_START_MONTH, 2);
aprilMayDsCode.put(Constants.ADVANCE_END_MONTH, 5);
aprilMayDsCode.put(DISCOUNT_CODE, "APR_MAY");
discountMonthMap.put("APR_MAY", aprilMayDsCode);
discountMonthMap.put("APR_MAY_102", aprilMayDsCode);
discountMonthMap.put("APR_MAY_103", aprilMayDsCode);
q1DiscountSeason.add(aprilMayDsCode);
Map<String, Object> juneJulyDsCode = new HashMap<>();
juneJulyDsCode.put(Constants.DISCOUNT_START_MONTH, 6);
juneJulyDsCode.put(Constants.DISCOUNT_END_MONTH, 7);
juneJulyDsCode.put(Constants.ADVANCE_START_MONTH, 4);
juneJulyDsCode.put(Constants.ADVANCE_END_MONTH, 7);
juneJulyDsCode.put(DISCOUNT_CODE, "JUN_JUL");
discountMonthMap.put("JUN_JUL", juneJulyDsCode);
discountMonthMap.put("JUN_JUL_102", juneJulyDsCode);
discountMonthMap.put("JUN_JUL_103", juneJulyDsCode);
q1DiscountSeason.add(juneJulyDsCode);
Map<String, Object> augSeptDsCode = new HashMap<>();
augSeptDsCode.put(Constants.DISCOUNT_START_MONTH, 8);
augSeptDsCode.put(Constants.DISCOUNT_END_MONTH, 9);
augSeptDsCode.put(Constants.ADVANCE_START_MONTH, 6);
augSeptDsCode.put(Constants.ADVANCE_END_MONTH, 9);
augSeptDsCode.put(DISCOUNT_CODE, "AUG_SEPT");
discountMonthMap.put("AUG_SEPT", augSeptDsCode);
discountMonthMap.put("AUG_SEPT_102", augSeptDsCode);
discountMonthMap.put("AUG_SEPT_103", augSeptDsCode);
q1DiscountSeason.add(augSeptDsCode);
Map<String, Object> octDecDsCode = new HashMap<>();
octDecDsCode.put(Constants.DISCOUNT_START_MONTH, 10);
octDecDsCode.put(Constants.DISCOUNT_END_MONTH, 12);
octDecDsCode.put(Constants.ADVANCE_START_MONTH, 8);
octDecDsCode.put(Constants.ADVANCE_END_MONTH, 12);
octDecDsCode.put(DISCOUNT_CODE, "OCT_DEC");
discountMonthMap.put("OCT_DEC", octDecDsCode);
discountMonthMap.put("OCT_DEC_102", octDecDsCode);
discountMonthMap.put("OCT_DEC_103", octDecDsCode);
q1DiscountSeason.add(octDecDsCode);
Map<String, Object> decJanDsCode = new HashMap<>();
decJanDsCode.put(Constants.DISCOUNT_START_MONTH, 12);
decJanDsCode.put(Constants.DISCOUNT_END_MONTH, 1);
decJanDsCode.put(Constants.ADVANCE_START_MONTH, 10);
decJanDsCode.put(Constants.ADVANCE_END_MONTH, 1);
decJanDsCode.put(DISCOUNT_CODE, "DEC_JAN");
discountMonthMap.put("DEC_JAN", decJanDsCode);
q2DiscountSeason.add(decJanDsCode);
Map<String, Object> febMarchDsCode = new HashMap<>();
febMarchDsCode.put(Constants.DISCOUNT_START_MONTH, 2);
febMarchDsCode.put(Constants.DISCOUNT_END_MONTH, 3);
febMarchDsCode.put(Constants.ADVANCE_START_MONTH, 12);
febMarchDsCode.put(Constants.ADVANCE_END_MONTH, 3);
febMarchDsCode.put(DISCOUNT_CODE, "FEB_MAR");
discountMonthMap.put("FEB_MAR", febMarchDsCode);
q2DiscountSeason.add(febMarchDsCode);
Map<String, Object> aprMayDsCode = new HashMap<>();
aprMayDsCode.put(Constants.DISCOUNT_START_MONTH, 4);
aprMayDsCode.put(Constants.DISCOUNT_END_MONTH, 5);
aprMayDsCode.put(Constants.ADVANCE_START_MONTH, 2);
aprMayDsCode.put(Constants.ADVANCE_END_MONTH, 5);
aprMayDsCode.put(DISCOUNT_CODE, "APR_MAY");
discountMonthMap.put("APR_MAY", aprMayDsCode);
q2DiscountSeason.add(aprMayDsCode);
Map<String, Object> junJulyDsCode = new HashMap<>();
junJulyDsCode.put(Constants.DISCOUNT_START_MONTH, 6);
junJulyDsCode.put(Constants.DISCOUNT_END_MONTH, 7);
junJulyDsCode.put(Constants.ADVANCE_START_MONTH, 4);
junJulyDsCode.put(Constants.ADVANCE_END_MONTH, 7);
junJulyDsCode.put(DISCOUNT_CODE, "JUN_JUL_2");
discountMonthMap.put("JUN_JUL_2", junJulyDsCode);
q2DiscountSeason.add(junJulyDsCode);
Map<String, Object> augDsCode = new HashMap<>();
augDsCode.put(Constants.DISCOUNT_START_MONTH, 8);
augDsCode.put(Constants.DISCOUNT_END_MONTH, 8);
augDsCode.put(Constants.ADVANCE_START_MONTH, 6);
augDsCode.put(Constants.ADVANCE_END_MONTH, 8);
augDsCode.put(DISCOUNT_CODE, "AUG_ONLY");
discountMonthMap.put("AUG_ONLY", augDsCode);
q2DiscountSeason.add(augDsCode);
Map<String, Object> septNovDsCode = new HashMap<>();
septNovDsCode.put(Constants.DISCOUNT_START_MONTH, 9);
septNovDsCode.put(Constants.DISCOUNT_END_MONTH, 11);
septNovDsCode.put(Constants.ADVANCE_START_MONTH, 7);
septNovDsCode.put(Constants.ADVANCE_END_MONTH, 11);
septNovDsCode.put(DISCOUNT_CODE, "SEPT_NOV");
discountMonthMap.put("SEPT_NOV", septNovDsCode);
q2DiscountSeason.add(septNovDsCode);
seasonIdMap.put("100", q1DiscountSeason);
seasonIdMap.put("101", q2DiscountSeason);
return seasonIdMap.get(seasonId);
}
常数,
public class Constants {
public static final String FROM_DATE = "from_dates";
public static final String TO_DATE = "to_dates";
public static final String DISCOUNT_START_MONTH = "discount_start_month_by_x";
public static final String DISCOUNT_END_MONTH = "discount_start_month_by_y";
public static final int DEC_MONTH = 12;
public static final int OCT_MONTH = 10;
public static final int JAN_MONTH = 1;
public static final String ADVANCE_START_MONTH = "advance_start_month" ;
public static final String ADVANCE_END_MONTH = "advance_end_month" ;
}
public static List<DiscountRanges> generateDates(LocalDate startDate, LocalDate endDate) {
int monthsBetween = (int) ChronoUnit.MONTHS.between(startDate, endDate);
List<DiscountRanges> dateRangeList = new ArrayList<>(monthsBetween);
for(LocalDate date = startDate; !date.isAfter(endDate); date = date.plusMonths(1)){
YearMonth month = YearMonth.from(date);
DiscountRanges DiscountRanges = new DiscountRanges(String.valueOf(month.atDay(1)), String.valueOf(month.atEndOfMonth()));
dateRangeList.add(DiscountRanges);
}
return dateRangeList;
}
public class DiscountRanges {
String startDate;
String endDate;
public DiscountRanges(String startDate, String endDate) {
this.startDate = startDate;
this.endDate = endDate;
}
}
测试用例,
class MockitoDemoApplicationTests {
Function<List<DiscountRanges>,Integer> endIndexOfListFunction=(dateList)-> dateList.size()-1;
@Test
public void testDiscountMonthCase1() {
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put("seasonId", "100");
seasonIdMap.put(Constants.FROM_DATE, "2023-01-30");
seasonIdMap.put(Constants.TO_DATE, "2023-02-01");
List<DiscountRanges> discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-11-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-03-31");
discountDatesList.clear();
//EDGE CASE - 1
seasonIdMap.put(Constants.FROM_DATE, "2023-01-30");
seasonIdMap.put(Constants.TO_DATE, "2023-02-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);;
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-11-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-03-31");
discountDatesList.clear();
// EDGE CASE - 2
seasonIdMap.put(Constants.FROM_DATE, "2022-10-30");
seasonIdMap.put(Constants.TO_DATE, "2023-02-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);;
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-08-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-03-31");
discountDatesList.clear();
// EDGE CASE - 3
seasonIdMap.put(Constants.FROM_DATE, "2022-08-30");
seasonIdMap.put(Constants.TO_DATE, "2022-10-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);;
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-06-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2022-12-31");
discountDatesList.clear();
seasonIdMap.put(Constants.FROM_DATE, "2022-08-30");
seasonIdMap.put(Constants.TO_DATE, "2023-02-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);;
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-06-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-03-31");
discountDatesList.clear();
}
@Test
public void testDiscountMonthCase2() {
List<DiscountRanges> discountDatesList = new ArrayList<>();
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put(Constants.FROM_DATE, "2023-04-30");
seasonIdMap.put(Constants.TO_DATE, "2023-07-01");
seasonIdMap.put("seasonId", "100");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2023-02-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-07-31");
}
@Test
public void testDiscountMonthCase3() {
List<DiscountRanges> discountDatesList = new ArrayList<>();
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put(Constants.FROM_DATE, "2023-06-30");
seasonIdMap.put(Constants.TO_DATE, "2023-07-01");
seasonIdMap.put("seasonId", "100");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2023-04-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-07-31");
}
@Test
public void testDiscountMonthCase4() {
List<DiscountRanges> discountDatesList = new ArrayList<>();
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put(Constants.FROM_DATE, "2023-08-30");
seasonIdMap.put(Constants.TO_DATE, "2023-10-01");
seasonIdMap.put("seasonId", "100");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2023-06-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-12-31");
}
@Test
public void testDiscountMonthCase5() {
List<DiscountRanges> discountDatesList = new ArrayList<>();
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put(Constants.FROM_DATE, "2023-10-30");
seasonIdMap.put(Constants.TO_DATE, "2024-01-01");
seasonIdMap.put("seasonId", "100");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2023-08-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2024-03-31");
}
@Test
public void testDiscountMonthCase6() {
List<DiscountRanges> discountDatesList = new ArrayList<>();
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put("seasonId", "101");
// EDGE CASE 1
seasonIdMap.put(Constants.FROM_DATE, "2022-12-30");
seasonIdMap.put(Constants.TO_DATE, "2023-02-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-10-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-03-31");
discountDatesList.clear();
// EDGE CASE 2
seasonIdMap.put(Constants.FROM_DATE, "2023-01-30");
seasonIdMap.put(Constants.TO_DATE, "2023-02-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-10-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-03-31");
discountDatesList.clear();
// EDGE CASE 3
seasonIdMap.put(Constants.FROM_DATE, "2022-10-01");
seasonIdMap.put(Constants.TO_DATE, "2023-02-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-07-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-03-31");
discountDatesList.clear();
// EDGE CASE 4
seasonIdMap.put(Constants.FROM_DATE, "2022-12-01");
seasonIdMap.put(Constants.TO_DATE, "2023-02-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-10-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-03-31");
discountDatesList.clear();
}
@Test
public void testDiscountMonthCase7() {
List<DiscountRanges> discountDatesList = new ArrayList<>();
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put("seasonId", "101");
// edge case - 1
seasonIdMap.put(Constants.FROM_DATE, "2023-02-01");
seasonIdMap.put(Constants.TO_DATE, "2023-04-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-12-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-05-31");
discountDatesList.clear();
// edge case - 2
seasonIdMap.put(Constants.FROM_DATE, "2023-02-01");
seasonIdMap.put(Constants.TO_DATE, "2023-03-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2022-12-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-03-31");
discountDatesList.clear();
}
@Test
public void testDiscountMonthCase8() {
List<DiscountRanges> discountDatesList = new ArrayList<>();
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put("seasonId", "101");
seasonIdMap.put(Constants.FROM_DATE, "2023-04-30");
seasonIdMap.put(Constants.TO_DATE, "2023-06-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2023-02-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-07-31");
discountDatesList.clear();
}
@Test
public void testDiscountMonthCase9() {
List<DiscountRanges> discountDatesList = new ArrayList<>();
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put("seasonId", "101");
seasonIdMap.put(Constants.FROM_DATE, "2023-06-30");
seasonIdMap.put(Constants.TO_DATE, "2023-08-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2023-04-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-08-31");
discountDatesList.clear();
}
@Test
public void testDiscountMonthCase10() {
List<DiscountRanges> discountDatesList = new ArrayList<>();
Map<String, Object> seasonIdMap = new HashMap<>();
seasonIdMap.put("seasonId", "101");
seasonIdMap.put(Constants.FROM_DATE, "2023-08-01");
seasonIdMap.put(Constants.TO_DATE, "2023-10-01");
discountDatesList = DiscountServiceImpl.getDiscountDates(seasonIdMap);
assertNotNull(discountDatesList);
assertThat(discountDatesList).isNotEmpty();
assertEquals(discountDatesList.get(0).getStartDate(), "2023-06-01");
assertEquals(discountDatesList.get(endIndexOfListFunction.apply(discountDatesList)).getEndDate(), "2023-11-30");
discountDatesList.clear();
}
}