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


##########
jbpm/jbpm-flow/src/test/java/org/jbpm/process/core/timer/CalendarBeanTest.java:
##########
@@ -0,0 +1,295 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.jbpm.process.core.timer;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.Executable;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+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.TIMEZONE;
+import static org.jbpm.process.core.timer.BusinessCalendarImpl.WEEKEND_DAYS;
+import static 
org.jbpm.process.core.timer.CalendarBean.DEFAULT_HOLIDAY_DATE_FORMAT;
+import static org.jbpm.process.core.timer.CalendarBean.DEFAULT_TIMEZONE;
+import static org.jbpm.process.core.timer.CalendarBean.DEFAULT_WEEKENDS;
+import static org.jbpm.process.core.timer.CalendarBean.DEFAULT_WEEKEND_DAYS;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class CalendarBeanTest {
+
+    // Static validation methods
+    @ParameterizedTest
+    @MethodSource("getMissingPropertiesCalendar")
+    void requiredPropertyValidation(Map<String, Object> propertyMap, 
List<String> errorMessages) {
+        Properties calendarConfiguration = new Properties();
+        calendarConfiguration.putAll(propertyMap);
+        commonStaticMethodValidation(errorMessage -> 
CalendarBean.requiredPropertyValidation(errorMessage, calendarConfiguration), 
errorMessages);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getWronglyFormatPropertiesCalendar")
+    void propertyFormatValidation(Map<String, Object> propertyMap, 
List<String> errorMessages) {
+        Properties calendarConfiguration = new Properties();
+        calendarConfiguration.putAll(propertyMap);
+        commonStaticMethodValidation(errorMessage -> 
CalendarBean.propertyFormatValidation(errorMessage, calendarConfiguration), 
errorMessages);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getBusinessInvalidPropertiesCalendar")
+    void businessValidation(Map<String, Object> propertyMap, List<String> 
errorMessages) {
+        Properties calendarConfiguration = new Properties();
+        calendarConfiguration.putAll(propertyMap);
+        commonStaticMethodValidation(errorMessage -> 
CalendarBean.businessValidation(errorMessage, calendarConfiguration), 
errorMessages);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getPartialPropertiesCalendar")
+    void missingDataPopulation(Map<String, Object> propertyMap, Map<String, 
Object> defaultValuesMap) {
+        Properties calendarConfiguration = new Properties();
+        calendarConfiguration.putAll(propertyMap);
+        defaultValuesMap.keySet().forEach(key -> 
assertThat(calendarConfiguration.containsKey(key)).isFalse());
+        CalendarBean.missingDataPopulation(calendarConfiguration);
+        defaultValuesMap.forEach((key, value) -> {
+            assertThat(calendarConfiguration.containsKey(key)).isTrue();
+            
assertThat(calendarConfiguration.getProperty(key)).isEqualTo(value);
+        });
+    }
+
+    @Test
+    void getPropertyAsInt() {
+        Properties calendarConfiguration = new Properties();
+        String propertyName = "propertyName";
+        int originalValue = 1;
+        String value = "" + originalValue;
+        calendarConfiguration.put(propertyName, value);
+        int retrieved = CalendarBean.getPropertyAsInt(propertyName, 
calendarConfiguration);
+        assertThat(retrieved).isEqualTo(originalValue);
+        value = "WRONG";
+        calendarConfiguration.put(propertyName, value);
+        NumberFormatException thrown = 
assertThrows(NumberFormatException.class, () -> 
CalendarBean.getPropertyAsInt(propertyName, calendarConfiguration));
+        String expectedMessage = "For input string: \"WRONG\"";
+        assertThat(thrown.getMessage()).isEqualTo(expectedMessage);
+    }
+
+    @Test
+    void validateRequiredProperty() {
+        Properties calendarConfiguration = new Properties();
+        String propertyName = "propertyName";
+        String value = "propertyValue";
+        calendarConfiguration.put(propertyName, value);
+        StringBuilder errorMessage = new StringBuilder();
+        CalendarBean.validateRequiredProperty(propertyName, errorMessage, 
calendarConfiguration);
+        assertThat(errorMessage).isEmpty();
+        CalendarBean.validateRequiredProperty("missingProperty", errorMessage, 
calendarConfiguration);
+        String[] retrievedErrors = errorMessage.toString().split("\n");
+        assertThat(retrievedErrors).hasSize(1);
+        assertThat(retrievedErrors).contains("Property missingProperty is 
required");
+    }
+
+    @Test
+    void getFormattedDate() throws ParseException {
+        Properties calendarConfiguration = new Properties();
+        String dateFormat = "dd-MM-yyyy";
+        String date = "27-11-2024";
+        calendarConfiguration.put(HOLIDAY_DATE_FORMAT, dateFormat);
+        Date retrieved = CalendarBean.getFormattedDate(date, 
calendarConfiguration);
+        Date expected = 
CalendarBean.getSimpleDateFormat(dateFormat).parse(date);
+        assertThat(retrieved).isEqualTo(expected);
+
+    }
+
+    @Test
+    void getSimpleDateFormat() {
+        SimpleDateFormat retrieved = 
CalendarBean.getSimpleDateFormat(DEFAULT_HOLIDAY_DATE_FORMAT);
+        assertThat(retrieved).isNotNull();
+        retrieved = CalendarBean.getSimpleDateFormat("dd-MM-yyyy");
+        assertThat(retrieved).isNotNull();
+        String wrong = "WRONG";
+        IllegalArgumentException thrown = 
assertThrows(IllegalArgumentException.class, () -> 
CalendarBean.getSimpleDateFormat(wrong));
+        String expectedMessage = "Illegal pattern character 'R'";
+        assertThat(thrown.getMessage()).isEqualTo(expectedMessage);
+    }
+
+    // Instance methods
+    @ParameterizedTest
+    @MethodSource("getMissingPropertiesCalendar")
+    void requiredPropertyMissing(Map<String, Object> propertyMap, List<String> 
errorMessages) {
+        Properties calendarConfiguration = new Properties();
+        calendarConfiguration.putAll(propertyMap);
+        commonIllegalArgumentAssertion(() -> new 
CalendarBean(calendarConfiguration), errorMessages);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getWronglyFormatPropertiesCalendar")
+    void propertyWrongFormat(Map<String, Object> propertyMap, List<String> 
errorMessages) {
+        Properties calendarConfiguration = new Properties();
+        calendarConfiguration.putAll(propertyMap);
+        commonIllegalArgumentAssertion(() -> new 
CalendarBean(calendarConfiguration), errorMessages);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getBusinessInvalidPropertiesCalendar")
+    void businessInvalid(Map<String, Object> propertyMap, List<String> 
errorMessages) {
+        Properties calendarConfiguration = new Properties();
+        calendarConfiguration.putAll(propertyMap);
+        commonIllegalArgumentAssertion(() -> new 
CalendarBean(calendarConfiguration), errorMessages);
+    }
+
+    @Test
+    void instantiationFull() throws ParseException {
+        Properties calendarConfiguration = new Properties();
+        int startHour = 10;
+        int endHour = 16;
+        List<Integer> weekendDays = Arrays.asList(3, 4);
+        String dateFormat = "dd-MM-yyyy";
+        String timezone = "ACT";
+        String holidays = "27-11-2024";
+        calendarConfiguration.put(START_HOUR, String.valueOf(startHour));
+        calendarConfiguration.put(END_HOUR, String.valueOf(endHour));
+        calendarConfiguration.put(WEEKEND_DAYS, 
weekendDays.stream().map(String::valueOf).collect(Collectors.joining(",")));
+        calendarConfiguration.put(HOLIDAY_DATE_FORMAT, dateFormat);
+        calendarConfiguration.put(TIMEZONE, timezone);
+        calendarConfiguration.put(HOLIDAYS, holidays);
+        CalendarBean retrieved = new CalendarBean(calendarConfiguration);
+
+        Date from = CalendarBean.getFormattedDate(holidays, 
calendarConfiguration);
+        Date to = CalendarBean.getFormattedDate("28-11-2024", 
calendarConfiguration);
+        assertThat(retrieved.getHolidays()).isEqualTo(List.of(new 
BusinessCalendarImpl.TimePeriod(from, to)));
+        assertThat(retrieved.getWeekendDays()).isEqualTo(weekendDays);
+        assertThat(retrieved.getDaysPerWeek()).isEqualTo(7 - 
weekendDays.size());
+        assertThat(retrieved.getTimezone()).isEqualTo(timezone);
+        assertThat(retrieved.getStartHour()).isEqualTo(startHour);
+        assertThat(retrieved.getEndHour()).isEqualTo(endHour);
+        assertThat(retrieved.getHoursInDay()).isEqualTo(endHour - startHour);
+    }
+
+    @Test
+    void instantiationPartial() {
+        Properties calendarConfiguration = new Properties();
+        int startHour = 10;
+        int endHour = 16;
+        calendarConfiguration.put(START_HOUR, String.valueOf(startHour));
+        calendarConfiguration.put(END_HOUR, String.valueOf(endHour));
+        CalendarBean retrieved = new CalendarBean(calendarConfiguration);
+        assertThat(retrieved.getHolidays()).isEqualTo(Collections.emptyList());
+        assertThat(retrieved.getWeekendDays()).isEqualTo(DEFAULT_WEEKEND_DAYS);
+        assertThat(retrieved.getDaysPerWeek()).isEqualTo(5);
+        assertThat(retrieved.getTimezone()).isEqualTo(DEFAULT_TIMEZONE);
+        assertThat(retrieved.getStartHour()).isEqualTo(startHour);
+        assertThat(retrieved.getEndHour()).isEqualTo(endHour);
+        assertThat(retrieved.getHoursInDay()).isEqualTo(endHour - startHour);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getMissingPropertiesCalendar")
+    void missingProperties(Map<String, Object> propertyMap, List<String> 
errorMessages) {
+        Properties calendarConfiguration = new Properties();
+        calendarConfiguration.putAll(propertyMap);
+        commonIllegalArgumentAssertion(() -> new 
CalendarBean(calendarConfiguration), errorMessages);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getInvalidPropertiesCalendar")
+    public void invalidProperties(Map<String, Object> propertyMap, 
List<String> errorMessages) {
+        Properties calendarConfiguration = new Properties();
+        calendarConfiguration.putAll(propertyMap);
+        commonIllegalArgumentAssertion(() -> new 
CalendarBean(calendarConfiguration), errorMessages);
+    }
+
+    // Let's avoid duplication
+    private void commonStaticMethodValidation(Consumer<StringBuilder> 
executedMethod,
+            List<String> errorMessages) {
+        StringBuilder errors = new StringBuilder();
+        assertThat(errors).isEmpty();
+        executedMethod.accept(errors);
+        assertThat(errors).isNotEmpty();
+        String[] retrievedErrors = errors.toString().split("\n");
+        assertThat(retrievedErrors).hasSize(errorMessages.size());
+        assertThat(retrievedErrors).contains(retrievedErrors);
+    }
+
+    private void commonIllegalArgumentAssertion(Executable executedMethod, 
List<String> errorMessages) {

Review Comment:
   @pibizza @yesamer https://github.com/apache/incubator-kie-issues/issues/1661



-- 
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