Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
Fixed issue with invalid cron expression (#1472)
Browse files Browse the repository at this point in the history
  • Loading branch information
SAtanasovv authored Mar 25, 2022
1 parent 77ee7eb commit 36b8a2e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.sap.xsk.utils.XSKCommonsUtils;
import com.sap.xsk.xsjob.ds.api.XSKCronExpressionException;
import java.text.ParseException;
import java.util.List;

public class XSKCronToQuartzCronTransformer {

Expand All @@ -39,7 +40,7 @@ public String transform(String xskCronExpression) throws ParseException {
try {
quartzCronExpression.setYear(parseRange(xskCronExpressionArr[XSK_CRON_YEAR]));
quartzCronExpression.setMonth(parseRange(xskCronExpressionArr[XSK_CRON_MONTH]));
quartzCronExpression.setDayOfMonth(parseRange(xskCronExpressionArr[XSK_CRON_DAY]));
quartzCronExpression.setDayOfMonth(checkDayOfWeekAndDayOfMonth());

String quartzDayOfWeek = parseRange(xskCronExpressionArr[XSK_CRON_DAY_OF_WEEK]);
quartzDayOfWeek = parseDayOfWeekElement(quartzDayOfWeek);
Expand Down Expand Up @@ -88,4 +89,14 @@ private String parseDayOfWeekElement(String dayOfWeekElement) throws ParseExcept

throw new XSKCronExpressionException(String.join(" ", xskCronExpressionArr), XSK_CRON_DAY_OF_WEEK);
}

private String checkDayOfWeekAndDayOfMonth(){
final List<String> xskCronExpressionDayOfWeekArr = List.of("mon", "tue", "wed", "thu", "fri", "sat", "sun", "1", "2", "3", "4", "5", "6", "7");
final List<String> dayOfWeekList = List.of(xskCronExpressionArr[XSK_CRON_DAY_OF_WEEK].split(","));

boolean isEveryDay = xskCronExpressionArr[XSK_CRON_DAY].equalsIgnoreCase("*");
boolean hasDayOfWeek = dayOfWeekList.stream().map(String::toLowerCase).anyMatch(xskCronExpressionDayOfWeekArr::contains);

return isEveryDay && hasDayOfWeek ? "?" : parseRange(xskCronExpressionArr[XSK_CRON_DAY]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,37 @@

import org.apache.commons.io.IOUtils;
import org.eclipse.dirigible.api.v3.problems.ProblemsFacade;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import com.sap.xsk.xsjob.ds.model.XSKJobArtifact;
import com.sap.xsk.xsjob.ds.model.XSKJobDefinition;
import com.sap.xsk.xsjob.ds.service.XSKJobCoreService;
import com.sap.xsk.xsjob.ds.transformer.XSKCronToQuartzCronTransformer;
import com.sap.xsk.xsjob.ds.transformer.XSKJobToXSKJobDefinitionTransformer;

@RunWith(MockitoJUnitRunner.class)
public class XSKJobTransformerTest {

@InjectMocks
private XSKJobToXSKJobDefinitionTransformer xskJobToXSKJobDefinitionTransformer = new XSKJobToXSKJobDefinitionTransformer();
@Mock
private XSKCronToQuartzCronTransformer xskCronToQuartzCronTransformer;

@Before
public void openMocks() {
MockitoAnnotations.openMocks(this);
}
private final XSKJobCoreService xskJobCoreService = new XSKJobCoreService();
private final XSKJobToXSKJobDefinitionTransformer xskJobToXSKJobDefinitionTransformer = new XSKJobToXSKJobDefinitionTransformer();

@Test
public void executeTransformSuccessfully() throws Exception {
XSKJobCoreService xskJobCoreService = new XSKJobCoreService();
String xsjobSample = IOUtils.toString(XSKJobTransformerTest.class.getResourceAsStream("/TestXSKJobTransformSuccess.xsjob"), StandardCharsets.UTF_8);
String expectedCronExpressionEveryFiveSeconds = "*/5 * * * * ? *";
String xsjobSample = IOUtils.toString(XSKJobTransformerTest.class.getResourceAsStream("/TestXSKJobTransformSuccess.xsjob"), StandardCharsets.UTF_8);
XSKJobArtifact xskJobArtifact = xskJobCoreService.parseJob(xsjobSample);
String cronExpression = xskCronToQuartzCronTransformer.transform(xskJobArtifact.getSchedules().get(0).getXscron());
Map<String, String> parametersAsMap = xskJobArtifact.getSchedules().get(0).getParameter();
ArrayList<XSKJobDefinition> jobDefinitions = xskJobToXSKJobDefinitionTransformer.transform(xskJobArtifact);

assertEquals("XSJOB:bugXsjob.xsjs::logFunc-0", jobDefinitions.get(0).getName());
assertEquals("XSJOB/bugXsjob.xsjs", jobDefinitions.get(0).getModule());
assertEquals("logFunc", jobDefinitions.get(0).getFunction());
assertEquals("My Job configuration My Schedule configuration for execution every second", jobDefinitions.get(0).getDescription());
assertEquals(cronExpression, jobDefinitions.get(0).getCronExpression());
assertEquals(expectedCronExpressionEveryFiveSeconds, jobDefinitions.get(0).getCronExpression());
assertEquals(parametersAsMap, jobDefinitions.get(0).getParametersAsMap());
assertEquals(parametersAsMap.size(), 1);
assertEquals(jobDefinitions.size(), 1);
Expand All @@ -72,7 +59,6 @@ public void executeTransformSuccessfully() throws Exception {
public void executeTransformFailed() throws Exception {
try (MockedStatic<ProblemsFacade> problemsFacade = Mockito.mockStatic(ProblemsFacade.class)) {
problemsFacade.when(() -> ProblemsFacade.save(any(), any(), any(), any(), any(), any(), any(), any(), any(), any())).thenAnswer((Answer<Void>) invocation -> null);
XSKJobCoreService xskJobCoreService = new XSKJobCoreService();
String xsjobSample = IOUtils.toString(XSKJobTransformerTest.class.getResourceAsStream("/TestXSKJobTransformFailure.xsjob"), StandardCharsets.UTF_8);
XSKJobArtifact xskJobArtifact = xskJobCoreService.parseJob(xsjobSample);
xskJobToXSKJobDefinitionTransformer.transform(xskJobArtifact);
Expand All @@ -81,11 +67,19 @@ public void executeTransformFailed() throws Exception {

@Test
public void executeTransformWithEmptySchedule() throws Exception {
XSKJobCoreService xskJobCoreService = new XSKJobCoreService();
String xsjobSample = IOUtils.toString(XSKJobTransformerTest.class.getResourceAsStream("/TestXSKJobTransformWithEmptySchedule.xsjob"), StandardCharsets.UTF_8);
XSKJobArtifact xskJobArtifact = xskJobCoreService.parseJob(xsjobSample);
ArrayList<XSKJobDefinition> jobDefinitions = xskJobToXSKJobDefinitionTransformer.transform(xskJobArtifact);
assertEquals(jobDefinitions.size(), 0);
}

@Test
public void testTransformEachDayOfWeekAndMonth() throws Exception {
String expectedCronExpressionEveryWeekDay = "0 30 22 ? * MON,TUE,WED,THU,FRI *";
String xsjobSample = IOUtils.toString(XSKJobTransformerTest.class.getResourceAsStream("/TestXSKJobTransformerCronExpression.xsjob"), StandardCharsets.UTF_8);
XSKJobArtifact xskJobArtifact = xskJobCoreService.parseJob(xsjobSample);
ArrayList<XSKJobDefinition> jobDefinitions = xskJobToXSKJobDefinitionTransformer.transform(xskJobArtifact);
assertEquals(jobDefinitions.size(), 1);
assertEquals(jobDefinitions.get(0).getCronExpression(), expectedCronExpressionEveryWeekDay);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"description": "My Job configuration",
"action": "xsjob_cron_demo:handler.xsjslib::consolePrintSuccess",
"schedules": [
{
"description": "Execute at 10:30:00pm, on every Monday, Tuesday, Wednesday, Thursday and Friday, every month",
"xscron": "* * * MON,TUE,WED,THU,FRI 22 30 0",
"parameter": {}
}
]
}

0 comments on commit 36b8a2e

Please sign in to comment.