gitgabrio commented on code in PR #3788:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/3788#discussion_r1866317335


##########
jbpm/jbpm-flow/src/test/java/org/jbpm/process/core/timer/BusinessCalendarImplTest.java:
##########
@@ -18,419 +18,325 @@
  */
 package org.jbpm.process.core.timer;
 
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.util.Arrays;
 import java.util.Calendar;
+import java.util.Collections;
 import java.util.Date;
+import java.util.List;
 import java.util.Properties;
-import java.util.concurrent.TimeUnit;
+import java.util.function.BiFunction;
+import java.util.stream.IntStream;
 
 import org.jbpm.test.util.AbstractBaseTest;
+import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
-import org.kie.kogito.timer.SessionPseudoClock;
 import org.slf4j.LoggerFactory;
 
+import static java.time.temporal.ChronoUnit.DAYS;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.jbpm.process.core.timer.BusinessCalendarImpl.END_HOUR;
+import static org.jbpm.process.core.timer.BusinessCalendarImpl.HOLIDAYS;
+import static 
org.jbpm.process.core.timer.BusinessCalendarImpl.HOLIDAY_DATE_FORMAT;
+import static org.jbpm.process.core.timer.BusinessCalendarImpl.START_HOUR;
+import static org.jbpm.process.core.timer.BusinessCalendarImpl.WEEKEND_DAYS;
 
-public class BusinessCalendarImplTest extends AbstractBaseTest {
+class BusinessCalendarImplTest extends AbstractBaseTest {
 
     public void addLogger() {
         logger = LoggerFactory.getLogger(this.getClass());
     }
 
     @Test
-    public void testCalculateHours() {
-        Properties config = new Properties();
-        String expectedDate = "2012-05-04 16:45";
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDateWithTime("2012-05-04 13:45").getTime());
-
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("3h");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
+    void instantiate() {
+        BusinessCalendarImpl retrieved = 
BusinessCalendarImpl.builder().build();
+        assertThat(retrieved).isNotNull();
+        retrieved = BusinessCalendarImpl.builder()
+                .withCalendarBean(CalendarBeanFactory.createCalendarBean())
+                .build();
+        assertThat(retrieved).isNotNull();
+
+        Properties calendarConfiguration = new Properties();
+        int startHour = 10;
+        int endHour = 16;
+        calendarConfiguration.put(START_HOUR, String.valueOf(startHour));
+        calendarConfiguration.put(END_HOUR, String.valueOf(endHour));
+        retrieved = BusinessCalendarImpl.builder()
+                .withCalendarBean(new CalendarBean(calendarConfiguration))
+                .build();
+        assertThat(retrieved).isNotNull();
     }
 
     @Test
-    public void testCalculateHoursCustomWorkingHours() {
-        Properties config = new Properties();
-        config.setProperty(BusinessCalendarImpl.HOURS_PER_DAY, "6");
-        String expectedDate = "2012-05-04 15:45";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDateWithTime("2012-05-03 13:45").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("8h");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
+    void calculateBusinessTimeAsDateInsideDailyWorkingHourWithDelay() {
+        int daysToSkip = 0; // since executionHourDelay falls before endHOurGap
+        commonCalculateBusinessTimeAsDateAssertBetweenHours(-4, 4, 0, 3, 
daysToSkip, null, null);
     }
 
     @Test
-    public void testCalculateHoursPassingOverWeekend() {
-        Properties config = new Properties();
-        String expectedDate = "2012-05-07 12:45";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDateWithTime("2012-05-04 13:45").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("7h");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
-    }
+    void calculateBusinessTimeAsDateInsideDailyWorkingHourWithoutDelay() {
+        int daysToSkip = 0; // since executionHourDelay falls before endHOurGap
+        commonCalculateBusinessTimeAsDateAssertBetweenHours(-4, 4, 0, 
0,daysToSkip, null, null);
+   }
 
+    @Disabled("TO FIX")
     @Test
-    public void testCalculateHoursPassingOverCustomDefinedWeekend() {
-        Properties config = new Properties();
-        config.setProperty(BusinessCalendarImpl.WEEKEND_DAYS, Calendar.FRIDAY 
+ "," + Calendar.SATURDAY);
-        String expectedDate = "2012-05-06 12:45";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDateWithTime("2012-05-03 13:45").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("7h");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
-    }
+    void calculateBusinessTimeAsDateInsideNightlyWorkingHour() {
+        int daysToSkip = 0; // since executionHourDelay falls before endHOurGap
+        commonCalculateBusinessTimeAsDateAssertBetweenHours(4, -4, 0,3, 
daysToSkip, null, null);
+      }
 
     @Test
-    public void testCalculateMinutesPassingOverWeekend() {
-        Properties config = new Properties();
-        String expectedDate = "2012-05-07 09:15";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDateWithTime("2012-05-04 16:45").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("30m");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
+    void calculateBusinessTimeAsDateBeforeWorkingHourWithDelay() {
+        int daysToSkip = 0; // since executionHourDelay falls before endHOurGap
+        commonCalculateBusinessTimeAsDateAssertBetweenHours(2, 4, -1,1, 
daysToSkip, null, null);
     }
 
     @Test
-    public void testCalculateMinutesPassingOverHoliday() {
-        Properties config = new Properties();
-        config.setProperty(BusinessCalendarImpl.HOLIDAYS, 
"2012-05-12:2012-05-19");
-        String expectedDate = "2012-05-21 09:15";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDateWithTime("2012-05-11 16:45").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("30m");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
+    void calculateBusinessTimeAsDateBeforeWorkingHourWithoutDelay() {
+        int daysToSkip = 0; // since executionHourDelay falls before endHOurGap
+        commonCalculateBusinessTimeAsDateAssertBetweenHours(-1, 4, -2, 1, 
daysToSkip, null, null);
     }
 
     @Test
-    public void testCalculateDays() {
-        Properties config = new Properties();
-        String expectedDate = "2012-05-14 09:00";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDate("2012-05-04").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("6d");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
+    void calculateBusinessTimeAsDateAfterWorkingHour() {
+        int daysToSkip = 1; // because the executionHourDelay is bigger to 
endHOurGap, so it goes to next day;
+        commonCalculateBusinessTimeAsDateAssertAtStartHour(-1, 2, 3,3, 
daysToSkip, null, null);
     }
 
     @Test
-    public void testCalculateDaysStartingInWeekend() {
-        Properties config = new Properties();
-        String expectedDate = "2012-05-09 09:00";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDate("2012-05-05").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("2d");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
+    void calculateBusinessTimeAsDateWhenTodayAndTomorrowAreHolidays() {
+        String holidayDateFormat = "yyyy-MM-dd";
+        DateTimeFormatter sdf = DateTimeFormatter.ofPattern(holidayDateFormat);
+        LocalDate today = LocalDate.now();
+        LocalDate tomorrow = today.plusDays(1);
+        String holidays = sdf.format(today) + "," + sdf.format(tomorrow);
+        int daysToSkip = 2; // because both today and tomorrow are holiday
+        // endHOurGap and executionHourDelay are ininfluent in this context
+        commonCalculateBusinessTimeAsDateAssertBetweenHours(-4, 4, 0,3, 
daysToSkip, holidayDateFormat, holidays);
+        commonCalculateBusinessTimeAsDateAssertBetweenHours(-4, 4, 5,3, 
daysToSkip, holidayDateFormat, holidays);
     }
 
     @Test
-    public void testCalculateDaysCustomWorkingDays() {
-        Properties config = new Properties();
-        config.setProperty(BusinessCalendarImpl.DAYS_PER_WEEK, "4");
-        config.setProperty(BusinessCalendarImpl.WEEKEND_DAYS, Calendar.FRIDAY 
+ "," + Calendar.SATURDAY + "," + Calendar.SUNDAY);
-        String expectedDate = "2012-05-15 14:30";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDateWithTime("2012-05-03 14:30").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("6d");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
+    void calculateBusinessTimeAsDateWhenNextDayIsHoliday() {
+        String holidayDateFormat = "yyyy-MM-dd";
+        DateTimeFormatter sdf = DateTimeFormatter.ofPattern(holidayDateFormat);
+        LocalDate tomorrow = LocalDate.now().plusDays(1);
+        String holidays = sdf.format(tomorrow);
+        // 1 because the executionHourDelay is equal to endHOurGap, so it goes 
to next day;
+        // 1 because next day is holiday
+        int daysToSkip = 2;
+
+        commonCalculateBusinessTimeAsDateAssertBetweenHours(-4, 4, 0,4, 
daysToSkip, holidayDateFormat, holidays);
+        daysToSkip = 0; // since executionHourDelay falls before endHOurGap
+        commonCalculateBusinessTimeAsDateAssertBetweenHours(-4, 4, 0, 3, 
daysToSkip, holidayDateFormat, holidays);
     }
 
     @Test
-    public void testCalculateDaysMiddleDay() {
-        Properties config = new Properties();
-        String expectedDate = "2012-05-11 12:27";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDateWithTime("2012-05-03 12:27").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("6d");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
+    void rollCalendarToDailyWorkingHour() {
+        int startHour = 14;
+        int endHour = 16;
+        Calendar toRoll = Calendar.getInstance();
+        int currentHour = 8;
+        toRoll.set(Calendar.HOUR_OF_DAY, currentHour);
+        int dayOfYear = toRoll.get(Calendar.DAY_OF_YEAR);
+        BusinessCalendarImpl.rollCalendarToDailyWorkingHour(toRoll, startHour, 
endHour);
+        assertThat(toRoll.get(Calendar.HOUR_OF_DAY)).isEqualTo(startHour);
+        assertThat(toRoll.get(Calendar.DAY_OF_YEAR)).isEqualTo(dayOfYear);
+
+        toRoll = Calendar.getInstance();
+        currentHour = 19;
+        toRoll.set(Calendar.HOUR_OF_DAY, currentHour);
+        dayOfYear = toRoll.get(Calendar.DAY_OF_YEAR);
+        BusinessCalendarImpl.rollCalendarToDailyWorkingHour(toRoll, startHour, 
endHour);
+        assertThat(toRoll.get(Calendar.HOUR_OF_DAY)).isEqualTo(startHour);
+        assertThat(toRoll.get(Calendar.DAY_OF_YEAR)).isEqualTo(dayOfYear + 1);
     }
 
+    @Disabled("TO FIX 
https://github.com/apache/incubator-kie-issues/issues/1651";)
     @Test
-    public void testCalculateDaysHoursMinutes() {
-        Properties config = new Properties();
-        String expectedDate = "2012-05-14 14:20";
+    void rollCalendarToNightlyWorkingHour() {
+        int startHour = 20;
+        int endHour = 4;
+        Calendar toRoll = Calendar.getInstance();
+        int currentHour = 21;
+        toRoll.set(Calendar.HOUR_OF_DAY, currentHour);
+        int dayOfYear = toRoll.get(Calendar.DAY_OF_YEAR);
+        BusinessCalendarImpl.rollCalendarToNightlyWorkingHour(toRoll, 
startHour, endHour);
+        assertThat(toRoll.get(Calendar.HOUR_OF_DAY)).isEqualTo(startHour);
+        assertThat(toRoll.get(Calendar.DAY_OF_YEAR)).isEqualTo(dayOfYear);
+
+        toRoll = Calendar.getInstance();
+        currentHour = 3;
+        toRoll.set(Calendar.HOUR_OF_DAY, currentHour);
+        dayOfYear = toRoll.get(Calendar.DAY_OF_YEAR);
+        BusinessCalendarImpl.rollCalendarToNightlyWorkingHour(toRoll, 
startHour, endHour);
+        assertThat(toRoll.get(Calendar.HOUR_OF_DAY)).isEqualTo(startHour);
+        assertThat(toRoll.get(Calendar.DAY_OF_YEAR)).isEqualTo(dayOfYear + 1);
 
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDate("2012-05-04").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("6d4h80m");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
     }
 
+    @Disabled("TO FIX 
https://github.com/apache/incubator-kie-issues/issues/1651";)
     @Test
-    public void testCalculateTimeDaysHoursMinutesHolidays() {
-        Properties config = new Properties();
-        config.setProperty(BusinessCalendarImpl.HOLIDAYS, 
"2012-05-10:2012-05-19");
-        String expectedDate = "2012-05-21 14:20";
-
-        SessionPseudoClock clock = new 
StaticPseudoClock(parseToDate("2012-05-04").getTime());
-        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, 
clock);
-
-        Date result = businessCal.calculateBusinessTimeAsDate("6d4h80m");
-
-        assertThat(formatDate("yyyy-MM-dd HH:mm", 
result)).isEqualTo(expectedDate);
+    void rollCalendarAfterHolidays() {

Review Comment:
   🤔 
   @Abhitocode 
   The test is related to `BusinessCalendarImpl.rollCalendarAfterHolidays`: why 
it has been disabled, and bound to nightly hour issue ?
   If it fails, it could be:
   1. wrong test setup
   2. wrong implementation (bad)
   
   Either case, IMO it has to be fixed....



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to